Skip to content

Commit d201c5f

Browse files
authored
Merge pull request #486 from fjtirado/Fix_#463
[Fix #463] Supporting runtime expressions
2 parents 4bfa0d9 + 997f423 commit d201c5f

19 files changed

+452
-132
lines changed

impl/core/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@
88
<artifactId>serverlessworkflow-impl-core</artifactId>
99
<properties>
1010
<version.net.thisptr>1.1.0</version.net.thisptr>
11+
<version.com.github.f4b6a3>5.2.3</version.com.github.f4b6a3>
1112
</properties>
1213
<dependencies>
1314
<dependency>
1415
<groupId>io.serverlessworkflow</groupId>
1516
<artifactId>serverlessworkflow-api</artifactId>
1617
<version>7.0.0-SNAPSHOT</version>
1718
</dependency>
19+
<dependency>
20+
<groupId>com.github.f4b6a3</groupId>
21+
<artifactId>ulid-creator</artifactId>
22+
<version>${version.com.github.f4b6a3}</version>
23+
</dependency>
1824
<dependency>
1925
<groupId>com.networknt</groupId>
2026
<artifactId>json-schema-validator</artifactId>
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 java.util.ArrayDeque;
19+
import java.util.Deque;
20+
import java.util.stream.Collectors;
21+
22+
public class QueueWorkflowPosition implements WorkflowPosition {
23+
24+
private Deque<Object> queue;
25+
26+
QueueWorkflowPosition() {
27+
this(new ArrayDeque<>());
28+
}
29+
30+
private QueueWorkflowPosition(Deque<Object> list) {
31+
this.queue = list;
32+
}
33+
34+
public QueueWorkflowPosition copy() {
35+
return new QueueWorkflowPosition(new ArrayDeque<>(this.queue));
36+
}
37+
38+
@Override
39+
public WorkflowPosition addIndex(int index) {
40+
queue.add(index);
41+
return this;
42+
}
43+
44+
@Override
45+
public WorkflowPosition addProperty(String prop) {
46+
queue.add(prop);
47+
return this;
48+
}
49+
50+
@Override
51+
public String jsonPointer() {
52+
return queue.stream().map(Object::toString).collect(Collectors.joining("/"));
53+
}
54+
55+
@Override
56+
public String toString() {
57+
return "ListWorkflowPosition [list=" + queue + "]";
58+
}
59+
60+
@Override
61+
public WorkflowPosition back() {
62+
queue.removeLast();
63+
return this;
64+
}
65+
66+
@Override
67+
public Object last() {
68+
return queue.pollLast();
69+
}
70+
}

impl/core/src/main/java/io/serverlessworkflow/impl/DefaultWorkflowPositionFactory.java renamed to impl/core/src/main/java/io/serverlessworkflow/impl/RuntimeDescriptorFactory.java

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

18-
class DefaultWorkflowPositionFactory implements WorkflowPositionFactory {
18+
import io.serverlessworkflow.impl.expressions.RuntimeDescriptor;
19+
import java.util.function.Supplier;
1920

20-
private static WorkflowPositionFactory instance = new DefaultWorkflowPositionFactory();
21-
22-
public static WorkflowPositionFactory get() {
23-
return instance;
24-
}
25-
26-
private DefaultWorkflowPositionFactory() {}
27-
28-
@Override
29-
public WorkflowPosition buildPosition() {
30-
return new DefaultWorkflowPosition();
31-
}
32-
}
21+
@FunctionalInterface
22+
public interface RuntimeDescriptorFactory extends Supplier<RuntimeDescriptor> {}

impl/core/src/main/java/io/serverlessworkflow/impl/DefaultWorkflowPosition.java renamed to impl/core/src/main/java/io/serverlessworkflow/impl/StringBufferWorkflowPosition.java

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

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

2020
private StringBuilder sb;
2121

22-
DefaultWorkflowPosition() {
23-
this.sb = new StringBuilder("");
22+
StringBufferWorkflowPosition() {
23+
this("");
2424
}
2525

26-
private DefaultWorkflowPosition(WorkflowPosition position) {
27-
this.sb = new StringBuilder(position.toString());
26+
private StringBufferWorkflowPosition(String str) {
27+
this.sb = new StringBuilder(str);
2828
}
2929

30-
public DefaultWorkflowPosition copy() {
31-
return new DefaultWorkflowPosition(this);
30+
public StringBufferWorkflowPosition copy() {
31+
return new StringBufferWorkflowPosition(this.jsonPointer());
3232
}
3333

3434
@Override
@@ -50,7 +50,7 @@ public String jsonPointer() {
5050

5151
@Override
5252
public String toString() {
53-
return "DefaultWorkflowPosition [sb=" + sb + "]";
53+
return "StringBufferWorkflowPosition [sb=" + sb + "]";
5454
}
5555

5656
@Override
@@ -61,4 +61,10 @@ public WorkflowPosition back() {
6161
}
6262
return this;
6363
}
64+
65+
@Override
66+
public Object last() {
67+
int indexOf = sb.lastIndexOf("/");
68+
return indexOf != -1 ? jsonPointer().substring(indexOf + 1) : "";
69+
}
6470
}

impl/core/src/main/java/io/serverlessworkflow/impl/TaskContext.java

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.serverlessworkflow.api.types.FlowDirective;
2020
import io.serverlessworkflow.api.types.FlowDirectiveEnum;
2121
import io.serverlessworkflow.api.types.TaskBase;
22+
import java.time.Instant;
2223
import java.util.HashMap;
2324
import java.util.Map;
2425

@@ -27,6 +28,7 @@ public class TaskContext<T extends TaskBase> {
2728
private final JsonNode rawInput;
2829
private final T task;
2930
private final WorkflowPosition position;
31+
private final Instant startedAt = Instant.now();
3032

3133
private JsonNode input;
3234
private JsonNode output;
@@ -109,4 +111,8 @@ public Map<String, Object> variables() {
109111
public WorkflowPosition position() {
110112
return position;
111113
}
114+
115+
public Instant startedAt() {
116+
return startedAt;
117+
}
112118
}

impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowApplication.java

+32-1
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
*/
1616
package io.serverlessworkflow.impl;
1717

18+
import com.github.f4b6a3.ulid.UlidCreator;
1819
import io.serverlessworkflow.api.types.Document;
1920
import io.serverlessworkflow.api.types.Workflow;
2021
import io.serverlessworkflow.impl.executors.DefaultTaskExecutorFactory;
2122
import io.serverlessworkflow.impl.executors.TaskExecutorFactory;
2223
import io.serverlessworkflow.impl.expressions.ExpressionFactory;
2324
import io.serverlessworkflow.impl.expressions.JQExpressionFactory;
25+
import io.serverlessworkflow.impl.expressions.RuntimeDescriptor;
2426
import io.serverlessworkflow.impl.jsonschema.DefaultSchemaValidatorFactory;
2527
import io.serverlessworkflow.impl.jsonschema.SchemaValidatorFactory;
2628
import io.serverlessworkflow.impl.resources.DefaultResourceLoaderFactory;
@@ -37,22 +39,28 @@ public class WorkflowApplication implements AutoCloseable {
3739
private final ExpressionFactory exprFactory;
3840
private final ResourceLoaderFactory resourceLoaderFactory;
3941
private final SchemaValidatorFactory schemaValidatorFactory;
42+
private final WorkflowIdFactory idFactory;
4043
private final Collection<WorkflowExecutionListener> listeners;
4144
private final Map<WorkflowId, WorkflowDefinition> definitions;
4245
private final WorkflowPositionFactory positionFactory;
46+
private final RuntimeDescriptorFactory runtimeDescriptorFactory;
4347

4448
public WorkflowApplication(
4549
TaskExecutorFactory taskFactory,
4650
ExpressionFactory exprFactory,
4751
ResourceLoaderFactory resourceLoaderFactory,
4852
SchemaValidatorFactory schemaValidatorFactory,
4953
WorkflowPositionFactory positionFactory,
54+
WorkflowIdFactory idFactory,
55+
RuntimeDescriptorFactory runtimeDescriptorFactory,
5056
Collection<WorkflowExecutionListener> listeners) {
5157
this.taskFactory = taskFactory;
5258
this.exprFactory = exprFactory;
5359
this.resourceLoaderFactory = resourceLoaderFactory;
5460
this.schemaValidatorFactory = schemaValidatorFactory;
5561
this.positionFactory = positionFactory;
62+
this.idFactory = idFactory;
63+
this.runtimeDescriptorFactory = runtimeDescriptorFactory;
5664
this.listeners = listeners;
5765
this.definitions = new ConcurrentHashMap<>();
5866
}
@@ -81,13 +89,20 @@ public Collection<WorkflowExecutionListener> listeners() {
8189
return listeners;
8290
}
8391

92+
public WorkflowIdFactory idFactory() {
93+
return idFactory;
94+
}
95+
8496
public static class Builder {
8597
private TaskExecutorFactory taskFactory = DefaultTaskExecutorFactory.get();
8698
private ExpressionFactory exprFactory = JQExpressionFactory.get();
8799
private Collection<WorkflowExecutionListener> listeners;
88100
private ResourceLoaderFactory resourceLoaderFactory = DefaultResourceLoaderFactory.get();
89101
private SchemaValidatorFactory schemaValidatorFactory = DefaultSchemaValidatorFactory.get();
90-
private WorkflowPositionFactory positionFactory = DefaultWorkflowPositionFactory.get();
102+
private WorkflowPositionFactory positionFactory = () -> new QueueWorkflowPosition();
103+
private WorkflowIdFactory idFactory = () -> UlidCreator.getMonotonicUlid().toString();
104+
private RuntimeDescriptorFactory descriptorFactory =
105+
() -> new RuntimeDescriptor("reference impl", "1.0.0_alpha", Collections.emptyMap());
91106

92107
private Builder() {}
93108

@@ -124,13 +139,25 @@ public Builder withSchemaValidatorFactory(SchemaValidatorFactory factory) {
124139
return this;
125140
}
126141

142+
public Builder withIdFactory(WorkflowIdFactory factory) {
143+
this.idFactory = factory;
144+
return this;
145+
}
146+
147+
public Builder withDescriptorFactory(RuntimeDescriptorFactory factory) {
148+
this.descriptorFactory = factory;
149+
return this;
150+
}
151+
127152
public WorkflowApplication build() {
128153
return new WorkflowApplication(
129154
taskFactory,
130155
exprFactory,
131156
resourceLoaderFactory,
132157
schemaValidatorFactory,
133158
positionFactory,
159+
idFactory,
160+
descriptorFactory,
134161
listeners == null
135162
? Collections.emptySet()
136163
: Collections.unmodifiableCollection(listeners));
@@ -159,4 +186,8 @@ public void close() throws Exception {
159186
public WorkflowPositionFactory positionFactory() {
160187
return positionFactory;
161188
}
189+
190+
public RuntimeDescriptorFactory runtimeDescriptorFactory() {
191+
return runtimeDescriptorFactory;
192+
}
162193
}

impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowContext.java

+9-12
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,26 @@
1616
package io.serverlessworkflow.impl;
1717

1818
import com.fasterxml.jackson.databind.JsonNode;
19-
import io.serverlessworkflow.impl.json.JsonUtils;
2019

2120
public class WorkflowContext {
2221
private final WorkflowDefinition definition;
23-
private final JsonNode input;
24-
private JsonNode context;
22+
private final WorkflowInstance instance;
2523

26-
WorkflowContext(WorkflowDefinition definition, JsonNode input) {
24+
WorkflowContext(WorkflowDefinition definition, WorkflowInstance instance) {
2725
this.definition = definition;
28-
this.input = input;
29-
this.context = JsonUtils.mapper().createObjectNode();
26+
this.instance = instance;
3027
}
3128

32-
public JsonNode context() {
33-
return context;
29+
public WorkflowInstance instance() {
30+
return instance;
3431
}
3532

36-
public void context(JsonNode context) {
37-
this.context = context;
33+
public JsonNode context() {
34+
return instance.context();
3835
}
3936

40-
public JsonNode rawInput() {
41-
return input;
37+
public void context(JsonNode context) {
38+
this.instance.context(context);
4239
}
4340

4441
public WorkflowDefinition definition() {

0 commit comments

Comments
 (0)