Skip to content

Commit 7f8fe37

Browse files
committed
[Fix #466] Implement switch
Signed-off-by: Francisco Javier Tirado Sarti <ftirados@redhat.com>
1 parent 11d0050 commit 7f8fe37

21 files changed

+672
-250
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package io.serverlessworkflow.impl;
1717

1818
import com.fasterxml.jackson.databind.JsonNode;
19+
import io.serverlessworkflow.api.types.FlowDirective;
20+
import io.serverlessworkflow.api.types.FlowDirectiveEnum;
1921
import io.serverlessworkflow.api.types.TaskBase;
2022

2123
public class TaskContext<T extends TaskBase> {
@@ -26,11 +28,15 @@ public class TaskContext<T extends TaskBase> {
2628
private JsonNode input;
2729
private JsonNode output;
2830
private JsonNode rawOutput;
31+
private FlowDirective flowDirective;
2932

3033
public TaskContext(JsonNode rawInput, T task) {
3134
this.rawInput = rawInput;
3235
this.input = rawInput;
36+
this.rawOutput = rawInput;
37+
this.output = rawInput;
3338
this.task = task;
39+
this.flowDirective = task.getThen();
3440
}
3541

3642
public void input(JsonNode input) {
@@ -54,6 +60,10 @@ public void rawOutput(JsonNode output) {
5460
this.output = output;
5561
}
5662

63+
public JsonNode rawOutput() {
64+
return rawOutput;
65+
}
66+
5767
public void output(JsonNode output) {
5868
this.output = output;
5969
}
@@ -62,7 +72,13 @@ public JsonNode output() {
6272
return output;
6373
}
6474

65-
public JsonNode rawOutput() {
66-
return rawOutput;
75+
public void flowDirective(FlowDirective flowDirective) {
76+
this.flowDirective = flowDirective;
77+
}
78+
79+
public FlowDirective flowDirective() {
80+
return flowDirective == null
81+
? new FlowDirective().withFlowDirectiveEnum(FlowDirectiveEnum.CONTINUE)
82+
: flowDirective;
6783
}
6884
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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 io.serverlessworkflow.api.types.Document;
19+
import io.serverlessworkflow.api.types.Workflow;
20+
import io.serverlessworkflow.impl.executors.DefaultTaskExecutorFactory;
21+
import io.serverlessworkflow.impl.executors.TaskExecutorFactory;
22+
import io.serverlessworkflow.impl.expressions.ExpressionFactory;
23+
import io.serverlessworkflow.impl.expressions.JQExpressionFactory;
24+
import io.serverlessworkflow.impl.jsonschema.DefaultSchemaValidatorFactory;
25+
import io.serverlessworkflow.impl.jsonschema.SchemaValidatorFactory;
26+
import io.serverlessworkflow.resources.DefaultResourceLoaderFactory;
27+
import io.serverlessworkflow.resources.ResourceLoaderFactory;
28+
import java.util.Collection;
29+
import java.util.Collections;
30+
import java.util.HashSet;
31+
import java.util.Map;
32+
import java.util.concurrent.ConcurrentHashMap;
33+
34+
public class WorkflowApplication implements AutoCloseable {
35+
36+
private final TaskExecutorFactory taskFactory;
37+
private final ExpressionFactory exprFactory;
38+
private final ResourceLoaderFactory resourceLoaderFactory;
39+
private final SchemaValidatorFactory schemaValidatorFactory;
40+
private final Collection<WorkflowExecutionListener> listeners;
41+
private final Map<WorkflowId, WorkflowDefinition> definitions;
42+
43+
public WorkflowApplication(
44+
TaskExecutorFactory taskFactory,
45+
ExpressionFactory exprFactory,
46+
ResourceLoaderFactory resourceLoaderFactory,
47+
SchemaValidatorFactory schemaValidatorFactory,
48+
Collection<WorkflowExecutionListener> listeners) {
49+
this.taskFactory = taskFactory;
50+
this.exprFactory = exprFactory;
51+
this.resourceLoaderFactory = resourceLoaderFactory;
52+
this.schemaValidatorFactory = schemaValidatorFactory;
53+
this.listeners = listeners;
54+
this.definitions = new ConcurrentHashMap<>();
55+
}
56+
57+
public TaskExecutorFactory taskFactory() {
58+
return taskFactory;
59+
}
60+
61+
public static Builder builder() {
62+
return new Builder();
63+
}
64+
65+
public ExpressionFactory expressionFactory() {
66+
return exprFactory;
67+
}
68+
69+
public SchemaValidatorFactory validatorFactory() {
70+
return schemaValidatorFactory;
71+
}
72+
73+
public ResourceLoaderFactory resourceLoaderFactory() {
74+
return resourceLoaderFactory;
75+
}
76+
77+
public Collection<WorkflowExecutionListener> listeners() {
78+
return listeners;
79+
}
80+
81+
public static class Builder {
82+
private TaskExecutorFactory taskFactory = DefaultTaskExecutorFactory.get();
83+
private ExpressionFactory exprFactory = JQExpressionFactory.get();
84+
private Collection<WorkflowExecutionListener> listeners;
85+
private ResourceLoaderFactory resourceLoaderFactory = DefaultResourceLoaderFactory.get();
86+
private SchemaValidatorFactory schemaValidatorFactory = DefaultSchemaValidatorFactory.get();
87+
88+
private Builder() {}
89+
90+
public Builder withListener(WorkflowExecutionListener listener) {
91+
if (listeners == null) {
92+
listeners = new HashSet<>();
93+
}
94+
listeners.add(listener);
95+
return this;
96+
}
97+
98+
public Builder withTaskExecutorFactory(TaskExecutorFactory factory) {
99+
this.taskFactory = factory;
100+
return this;
101+
}
102+
103+
public Builder withExpressionFactory(ExpressionFactory factory) {
104+
this.exprFactory = factory;
105+
return this;
106+
}
107+
108+
public Builder withResourceLoaderFactory(ResourceLoaderFactory resourceLoader) {
109+
this.resourceLoaderFactory = resourceLoader;
110+
return this;
111+
}
112+
113+
public Builder withSchemaValidatorFactory(SchemaValidatorFactory factory) {
114+
this.schemaValidatorFactory = factory;
115+
return this;
116+
}
117+
118+
public WorkflowApplication build() {
119+
return new WorkflowApplication(
120+
taskFactory,
121+
exprFactory,
122+
resourceLoaderFactory,
123+
schemaValidatorFactory,
124+
listeners == null
125+
? Collections.emptySet()
126+
: Collections.unmodifiableCollection(listeners));
127+
}
128+
}
129+
130+
private static record WorkflowId(String namespace, String name, String version) {
131+
static WorkflowId of(Document document) {
132+
return new WorkflowId(document.getNamespace(), document.getName(), document.getVersion());
133+
}
134+
}
135+
136+
public WorkflowDefinition workflowDefinition(Workflow workflow) {
137+
return definitions.computeIfAbsent(
138+
WorkflowId.of(workflow.getDocument()), k -> WorkflowDefinition.of(this, workflow));
139+
}
140+
141+
@Override
142+
public void close() throws Exception {
143+
for (WorkflowDefinition definition : definitions.values()) {
144+
definition.close();
145+
}
146+
definitions.clear();
147+
}
148+
}

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,31 @@
2020

2121
public class WorkflowContext {
2222
private final WorkflowPosition position;
23-
private JsonNode context;
23+
private final WorkflowDefinition definition;
2424
private final JsonNode input;
25+
private JsonNode current;
26+
private JsonNode context;
2527

26-
private WorkflowContext(WorkflowPosition position, JsonNode input) {
28+
private WorkflowContext(
29+
WorkflowPosition position, WorkflowDefinition definition, JsonNode input) {
2730
this.position = position;
31+
this.definition = definition;
2832
this.input = input;
33+
this.current = input.deepCopy();
2934
this.context = JsonUtils.mapper().createObjectNode();
3035
}
3136

32-
public static Builder builder(JsonNode input) {
33-
return new Builder(input);
37+
public static Builder builder(WorkflowDefinition definition, JsonNode input) {
38+
return new Builder(definition, input);
3439
}
3540

3641
public static class Builder {
3742
private WorkflowPosition position = new DefaultWorkflowPosition();
43+
private WorkflowDefinition definition;
3844
private JsonNode input;
3945

40-
private Builder(JsonNode input) {
46+
private Builder(WorkflowDefinition definition, JsonNode input) {
47+
this.definition = definition;
4148
this.input = input;
4249
}
4350

@@ -47,7 +54,7 @@ public Builder position(WorkflowPosition position) {
4754
}
4855

4956
public WorkflowContext build() {
50-
return new WorkflowContext(position, input);
57+
return new WorkflowContext(position, definition, input);
5158
}
5259
}
5360

@@ -66,4 +73,16 @@ public void context(JsonNode context) {
6673
public JsonNode rawInput() {
6774
return input;
6875
}
76+
77+
public void current(JsonNode output) {
78+
this.current = output;
79+
}
80+
81+
public JsonNode current() {
82+
return current;
83+
}
84+
85+
public WorkflowDefinition definition() {
86+
return definition;
87+
}
6988
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy