Skip to content

Commit 46961c6

Browse files
authored
Merge pull request #462 from fjtirado/Fix_#460
[Fix #460] Implementing input, output and context
2 parents c77d535 + 9ae8498 commit 46961c6

24 files changed

+847
-124
lines changed

impl/src/main/java/io/serverlessworkflow/impl/ExpressionUtils.java renamed to impl/src/main/java/io/serverlessworkflow/impl/DefaultWorkflowPosition.java

+27-15
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,38 @@
1515
*/
1616
package io.serverlessworkflow.impl;
1717

18-
public class ExpressionUtils {
18+
public class DefaultWorkflowPosition implements WorkflowPosition {
1919

20-
private static final String EXPR_PREFIX = "${";
21-
private static final String EXPR_SUFFIX = "}";
20+
private StringBuilder sb = new StringBuilder("");
2221

23-
private ExpressionUtils() {}
22+
@Override
23+
public WorkflowPosition addIndex(int index) {
24+
sb.append('/').append(index);
25+
return this;
26+
}
2427

25-
public static String trimExpr(String expr) {
26-
expr = expr.trim();
27-
if (expr.startsWith(EXPR_PREFIX)) {
28-
expr = trimExpr(expr, EXPR_PREFIX, EXPR_SUFFIX);
29-
}
30-
return expr.trim();
28+
@Override
29+
public WorkflowPosition addProperty(String prop) {
30+
sb.append('/').append(prop);
31+
return this;
32+
}
33+
34+
@Override
35+
public String jsonPointer() {
36+
return sb.toString();
37+
}
38+
39+
@Override
40+
public String toString() {
41+
return "DefaultWorkflowPosition [sb=" + sb + "]";
3142
}
3243

33-
private static String trimExpr(String expr, String prefix, String suffix) {
34-
expr = expr.substring(prefix.length());
35-
if (expr.endsWith(suffix)) {
36-
expr = expr.substring(0, expr.length() - suffix.length());
44+
@Override
45+
public WorkflowPosition back() {
46+
int indexOf = sb.lastIndexOf("/");
47+
if (indexOf != -1) {
48+
sb.substring(0, indexOf);
3749
}
38-
return expr;
50+
return this;
3951
}
4052
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.impl;
17+
18+
import com.fasterxml.jackson.databind.JsonNode;
19+
import io.serverlessworkflow.api.types.TaskBase;
20+
21+
public class TaskContext<T extends TaskBase> {
22+
23+
private final JsonNode rawInput;
24+
private final T task;
25+
26+
private JsonNode input;
27+
private JsonNode output;
28+
private JsonNode rawOutput;
29+
30+
public TaskContext(JsonNode rawInput, T task) {
31+
this.rawInput = rawInput;
32+
this.input = rawInput;
33+
this.task = task;
34+
}
35+
36+
public void input(JsonNode input) {
37+
this.input = input;
38+
}
39+
40+
public JsonNode input() {
41+
return input;
42+
}
43+
44+
public JsonNode rawInput() {
45+
return rawInput;
46+
}
47+
48+
public T task() {
49+
return task;
50+
}
51+
52+
public void rawOutput(JsonNode output) {
53+
this.rawOutput = output;
54+
this.output = output;
55+
}
56+
57+
public void output(JsonNode output) {
58+
this.output = output;
59+
}
60+
61+
public JsonNode output() {
62+
return output;
63+
}
64+
65+
public JsonNode rawOutput() {
66+
return rawOutput;
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.impl;
17+
18+
import com.fasterxml.jackson.databind.JsonNode;
19+
import io.serverlessworkflow.impl.json.JsonUtils;
20+
21+
public class WorkflowContext {
22+
23+
private final WorkflowPosition position;
24+
private JsonNode context;
25+
private final JsonNode input;
26+
27+
private WorkflowContext(WorkflowPosition position, JsonNode input) {
28+
this.position = position;
29+
this.input = input;
30+
this.context = JsonUtils.mapper().createObjectNode();
31+
}
32+
33+
public static Builder builder(JsonNode input) {
34+
return new Builder(input);
35+
}
36+
37+
public static class Builder {
38+
private WorkflowPosition position = new DefaultWorkflowPosition();
39+
private JsonNode input;
40+
41+
private Builder(JsonNode input) {
42+
this.input = input;
43+
}
44+
45+
public Builder position(WorkflowPosition position) {
46+
this.position = position;
47+
return this;
48+
}
49+
50+
public WorkflowContext build() {
51+
return new WorkflowContext(position, input);
52+
}
53+
}
54+
55+
public WorkflowPosition position() {
56+
return position;
57+
}
58+
59+
public JsonNode context() {
60+
return context;
61+
}
62+
63+
public void context(JsonNode context) {
64+
this.context = context;
65+
}
66+
67+
public JsonNode rawInput() {
68+
return input;
69+
}
70+
}

impl/src/main/java/io/serverlessworkflow/impl/WorkflowDefinition.java

+19-24
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@
1515
*/
1616
package io.serverlessworkflow.impl;
1717

18-
import static io.serverlessworkflow.impl.JsonUtils.*;
18+
import static io.serverlessworkflow.impl.json.JsonUtils.*;
1919

20-
import com.fasterxml.jackson.core.JsonPointer;
2120
import com.fasterxml.jackson.databind.JsonNode;
2221
import io.serverlessworkflow.api.types.TaskBase;
2322
import io.serverlessworkflow.api.types.TaskItem;
2423
import io.serverlessworkflow.api.types.Workflow;
24+
import io.serverlessworkflow.impl.executors.DefaultTaskExecutorFactory;
25+
import io.serverlessworkflow.impl.executors.TaskExecutor;
26+
import io.serverlessworkflow.impl.executors.TaskExecutorFactory;
27+
import io.serverlessworkflow.impl.json.JsonUtils;
2528
import java.util.Collection;
2629
import java.util.Collections;
2730
import java.util.HashSet;
@@ -43,7 +46,7 @@ private WorkflowDefinition(
4346
private final Workflow workflow;
4447
private final Collection<WorkflowExecutionListener> listeners;
4548
private final TaskExecutorFactory taskFactory;
46-
private final Map<JsonPointer, TaskExecutor<? extends TaskBase>> taskExecutors =
49+
private final Map<String, TaskExecutor<? extends TaskBase>> taskExecutors =
4750
new ConcurrentHashMap<>();
4851

4952
public static class Builder {
@@ -94,40 +97,32 @@ enum State {
9497

9598
public class WorkflowInstance {
9699

97-
private final JsonNode input;
98100
private JsonNode output;
99101
private State state;
100-
101-
private JsonPointer currentPos;
102+
private WorkflowContext context;
102103

103104
private WorkflowInstance(TaskExecutorFactory factory, JsonNode input) {
104-
this.input = input;
105-
this.output = object();
105+
this.output = input;
106106
this.state = State.STARTED;
107-
this.currentPos = JsonPointer.compile("/");
107+
this.context = WorkflowContext.builder(input).build();
108108
processDo(workflow.getDo());
109109
}
110110

111111
private void processDo(List<TaskItem> tasks) {
112-
currentPos = currentPos.appendProperty("do");
112+
context.position().addProperty("do");
113113
int index = 0;
114114
for (TaskItem task : tasks) {
115-
currentPos = currentPos.appendIndex(index).appendProperty(task.getName());
116-
listeners.forEach(l -> l.onTaskStarted(currentPos, task.getTask()));
115+
context.position().addIndex(++index).addProperty(task.getName());
116+
listeners.forEach(l -> l.onTaskStarted(context.position(), task.getTask()));
117117
this.output =
118-
MergeUtils.merge(
119-
taskExecutors
120-
.computeIfAbsent(currentPos, k -> taskFactory.getTaskExecutor(task.getTask()))
121-
.apply(input),
122-
output);
123-
listeners.forEach(l -> l.onTaskEnded(currentPos, task.getTask()));
124-
currentPos = currentPos.head().head();
118+
taskExecutors
119+
.computeIfAbsent(
120+
context.position().jsonPointer(),
121+
k -> taskFactory.getTaskExecutor(task.getTask()))
122+
.apply(context, output);
123+
listeners.forEach(l -> l.onTaskEnded(context.position(), task.getTask()));
124+
context.position().back().back();
125125
}
126-
currentPos = currentPos.head();
127-
}
128-
129-
public String currentPos() {
130-
return currentPos.toString();
131126
}
132127

133128
public State state() {

impl/src/main/java/io/serverlessworkflow/impl/WorkflowExecutionListener.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@
1515
*/
1616
package io.serverlessworkflow.impl;
1717

18-
import com.fasterxml.jackson.core.JsonPointer;
1918
import io.serverlessworkflow.api.types.Task;
2019

2120
public interface WorkflowExecutionListener {
2221

23-
void onTaskStarted(JsonPointer currentPos, Task task);
22+
void onTaskStarted(WorkflowPosition currentPos, Task task);
2423

25-
void onTaskEnded(JsonPointer currentPos, Task task);
24+
void onTaskEnded(WorkflowPosition currentPos, Task task);
2625
}

impl/src/main/java/io/serverlessworkflow/impl/Expression.java renamed to impl/src/main/java/io/serverlessworkflow/impl/WorkflowPosition.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@
1515
*/
1616
package io.serverlessworkflow.impl;
1717

18-
import com.fasterxml.jackson.databind.JsonNode;
18+
public interface WorkflowPosition {
1919

20-
public interface Expression {
21-
JsonNode eval(JsonNode input);
20+
String jsonPointer();
21+
22+
WorkflowPosition addProperty(String prop);
23+
24+
WorkflowPosition addIndex(int index);
25+
26+
WorkflowPosition back();
2227
}

0 commit comments

Comments
 (0)