Skip to content

Commit 9db247c

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

16 files changed

+495
-172
lines changed

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

Lines changed: 16 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,13 @@ 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;
3336
this.task = task;
37+
this.flowDirective = task.getThen();
3438
}
3539

3640
public void input(JsonNode input) {
@@ -54,6 +58,10 @@ public void rawOutput(JsonNode output) {
5458
this.output = output;
5559
}
5660

61+
public JsonNode rawOutput() {
62+
return rawOutput;
63+
}
64+
5765
public void output(JsonNode output) {
5866
this.output = output;
5967
}
@@ -62,7 +70,13 @@ public JsonNode output() {
6270
return output;
6371
}
6472

65-
public JsonNode rawOutput() {
66-
return rawOutput;
73+
public void flowDirective(FlowDirective flowDirective) {
74+
this.flowDirective = flowDirective;
75+
}
76+
77+
public FlowDirective flowDirective() {
78+
return flowDirective == null
79+
? new FlowDirective().withFlowDirectiveEnum(FlowDirectiveEnum.CONTINUE)
80+
: flowDirective;
6781
}
6882
}

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
}

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

Lines changed: 75 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@
1616
package io.serverlessworkflow.impl;
1717

1818
import static io.serverlessworkflow.impl.WorkflowUtils.*;
19-
import static io.serverlessworkflow.impl.json.JsonUtils.*;
2019

21-
import com.fasterxml.jackson.databind.JsonNode;
2220
import io.serverlessworkflow.api.types.Input;
2321
import io.serverlessworkflow.api.types.Output;
2422
import io.serverlessworkflow.api.types.TaskBase;
25-
import io.serverlessworkflow.api.types.TaskItem;
2623
import io.serverlessworkflow.api.types.Workflow;
2724
import io.serverlessworkflow.impl.executors.DefaultTaskExecutorFactory;
2825
import io.serverlessworkflow.impl.executors.TaskExecutor;
@@ -34,52 +31,60 @@
3431
import io.serverlessworkflow.impl.jsonschema.SchemaValidator;
3532
import io.serverlessworkflow.impl.jsonschema.SchemaValidatorFactory;
3633
import io.serverlessworkflow.resources.DefaultResourceLoaderFactory;
34+
import io.serverlessworkflow.resources.ResourceLoader;
3735
import io.serverlessworkflow.resources.ResourceLoaderFactory;
3836
import java.nio.file.Path;
3937
import java.util.Collection;
4038
import java.util.Collections;
4139
import java.util.HashSet;
42-
import java.util.List;
4340
import java.util.Map;
4441
import java.util.Optional;
4542
import java.util.concurrent.ConcurrentHashMap;
4643

4744
public class WorkflowDefinition {
4845

46+
private final Workflow workflow;
47+
private final Collection<WorkflowExecutionListener> listeners;
48+
private Optional<SchemaValidator> inputSchemaValidator = Optional.empty();
49+
private Optional<SchemaValidator> outputSchemaValidator = Optional.empty();
50+
private Optional<WorkflowFilter> inputFilter = Optional.empty();
51+
private Optional<WorkflowFilter> outputFilter = Optional.empty();
52+
private final TaskExecutorFactory taskFactory;
53+
private final ExpressionFactory exprFactory;
54+
private final ResourceLoader resourceLoader;
55+
private final SchemaValidatorFactory schemaValidatorFactory;
56+
private final Map<String, TaskExecutor<? extends TaskBase>> taskExecutors =
57+
new ConcurrentHashMap<>();
58+
4959
private WorkflowDefinition(
5060
Workflow workflow,
5161
Collection<WorkflowExecutionListener> listeners,
52-
WorkflowFactories factories) {
62+
TaskExecutorFactory taskFactory,
63+
ResourceLoader resourceLoader,
64+
ExpressionFactory exprFactory,
65+
SchemaValidatorFactory schemaValidatorFactory) {
5366
this.workflow = workflow;
5467
this.listeners = listeners;
55-
this.factories = factories;
68+
this.taskFactory = taskFactory;
69+
this.exprFactory = exprFactory;
70+
this.schemaValidatorFactory = schemaValidatorFactory;
71+
this.resourceLoader = resourceLoader;
5672
if (workflow.getInput() != null) {
5773
Input input = workflow.getInput();
5874
this.inputSchemaValidator =
5975
getSchemaValidator(
60-
factories.getValidatorFactory(), schemaToNode(factories, input.getSchema()));
61-
this.inputFilter = buildWorkflowFilter(factories.getExpressionFactory(), input.getFrom());
76+
schemaValidatorFactory, schemaToNode(resourceLoader, input.getSchema()));
77+
this.inputFilter = buildWorkflowFilter(exprFactory, input.getFrom());
6278
}
6379
if (workflow.getOutput() != null) {
6480
Output output = workflow.getOutput();
6581
this.outputSchemaValidator =
6682
getSchemaValidator(
67-
factories.getValidatorFactory(), schemaToNode(factories, output.getSchema()));
68-
this.outputFilter = buildWorkflowFilter(factories.getExpressionFactory(), output.getAs());
83+
schemaValidatorFactory, schemaToNode(resourceLoader, output.getSchema()));
84+
this.outputFilter = buildWorkflowFilter(exprFactory, output.getAs());
6985
}
7086
}
7187

72-
private final Workflow workflow;
73-
private final Collection<WorkflowExecutionListener> listeners;
74-
private final WorkflowFactories factories;
75-
private Optional<SchemaValidator> inputSchemaValidator = Optional.empty();
76-
private Optional<SchemaValidator> outputSchemaValidator = Optional.empty();
77-
private Optional<WorkflowFilter> inputFilter = Optional.empty();
78-
private Optional<WorkflowFilter> outputFilter = Optional.empty();
79-
80-
private final Map<String, TaskExecutor<? extends TaskBase>> taskExecutors =
81-
new ConcurrentHashMap<>();
82-
8388
public static class Builder {
8489
private final Workflow workflow;
8590
private TaskExecutorFactory taskFactory = DefaultTaskExecutorFactory.get();
@@ -127,18 +132,15 @@ public Builder withSchemaValidatorFactory(SchemaValidatorFactory factory) {
127132
}
128133

129134
public WorkflowDefinition build() {
130-
WorkflowDefinition def =
131-
new WorkflowDefinition(
132-
workflow,
133-
listeners == null
134-
? Collections.emptySet()
135-
: Collections.unmodifiableCollection(listeners),
136-
new WorkflowFactories(
137-
taskFactory,
138-
resourceLoaderFactory.getResourceLoader(path),
139-
exprFactory,
140-
schemaValidatorFactory));
141-
return def;
135+
return new WorkflowDefinition(
136+
workflow,
137+
listeners == null
138+
? Collections.emptySet()
139+
: Collections.unmodifiableCollection(listeners),
140+
taskFactory,
141+
resourceLoaderFactory.getResourceLoader(path),
142+
exprFactory,
143+
schemaValidatorFactory);
142144
}
143145
}
144146

@@ -147,7 +149,7 @@ public static Builder builder(Workflow workflow) {
147149
}
148150

149151
public WorkflowInstance execute(Object input) {
150-
return new WorkflowInstance(JsonUtils.fromValue(input));
152+
return new WorkflowInstance(this, JsonUtils.fromValue(input));
151153
}
152154

153155
enum State {
@@ -156,50 +158,48 @@ enum State {
156158
FINISHED
157159
};
158160

159-
public class WorkflowInstance {
160-
161-
private JsonNode output;
162-
private State state;
163-
private WorkflowContext context;
164-
165-
private WorkflowInstance(JsonNode input) {
166-
this.output = input;
167-
inputSchemaValidator.ifPresent(v -> v.validate(input));
168-
this.context = WorkflowContext.builder(input).build();
169-
inputFilter.ifPresent(f -> output = f.apply(context, Optional.empty(), output));
170-
this.state = State.STARTED;
171-
processDo(workflow.getDo());
172-
outputFilter.ifPresent(f -> output = f.apply(context, Optional.empty(), output));
173-
outputSchemaValidator.ifPresent(v -> v.validate(output));
174-
}
161+
public Optional<SchemaValidator> inputSchemaValidator() {
162+
return inputSchemaValidator;
163+
}
175164

176-
private void processDo(List<TaskItem> tasks) {
177-
context.position().addProperty("do");
178-
int index = 0;
179-
for (TaskItem task : tasks) {
180-
context.position().addIndex(++index).addProperty(task.getName());
181-
listeners.forEach(l -> l.onTaskStarted(context.position(), task.getTask()));
182-
this.output =
183-
taskExecutors
184-
.computeIfAbsent(
185-
context.position().jsonPointer(),
186-
k -> factories.getTaskFactory().getTaskExecutor(task.getTask(), factories))
187-
.apply(context, output);
188-
listeners.forEach(l -> l.onTaskEnded(context.position(), task.getTask()));
189-
context.position().back().back();
190-
}
191-
}
165+
public Optional<WorkflowFilter> inputFilter() {
166+
return inputFilter;
167+
}
192168

193-
public State state() {
194-
return state;
195-
}
169+
public Workflow workflow() {
170+
return workflow;
171+
}
196172

197-
public Object output() {
198-
return toJavaValue(output);
199-
}
173+
public Collection<WorkflowExecutionListener> listeners() {
174+
return listeners;
175+
}
200176

201-
public Object outputAsJsonNode() {
202-
return output;
203-
}
177+
public Map<String, TaskExecutor<? extends TaskBase>> taskExecutors() {
178+
return taskExecutors;
179+
}
180+
181+
public TaskExecutorFactory taskFactory() {
182+
return taskFactory;
183+
}
184+
185+
public Optional<WorkflowFilter> outputFilter() {
186+
return outputFilter;
187+
}
188+
189+
public Optional<SchemaValidator> outputSchemaValidator() {
190+
return outputSchemaValidator;
191+
}
192+
193+
public ExpressionFactory expressionFactory() {
194+
return exprFactory;
195+
}
196+
197+
public SchemaValidatorFactory validatorFactory() {
198+
return schemaValidatorFactory;
199+
}
200+
201+
public ResourceLoader resourceLoader() {
202+
203+
return resourceLoader;
204204
}
205205
}

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

Lines changed: 0 additions & 56 deletions
This file was deleted.

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