Skip to content

Commit bab05eb

Browse files
fjtiradofjtirado
fjtirado
authored andcommitted
[Fix_#549] Implement if for all task
Signed-off-by: fjtirado <[email protected]>
1 parent bc15319 commit bab05eb

File tree

5 files changed

+83
-32
lines changed

5 files changed

+83
-32
lines changed

impl/core/src/main/java/io/serverlessworkflow/impl/executors/AbstractTaskExecutor.java

+45-32
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@ public abstract class AbstractTaskExecutor<T extends TaskBase> implements TaskEx
4949
private final Optional<SchemaValidator> inputSchemaValidator;
5050
private final Optional<SchemaValidator> outputSchemaValidator;
5151
private final Optional<SchemaValidator> contextSchemaValidator;
52+
private final Optional<WorkflowFilter> ifFilter;
5253

5354
public abstract static class AbstractTaskExecutorBuilder<T extends TaskBase>
5455
implements TaskExecutorBuilder<T> {
5556
private Optional<WorkflowFilter> inputProcessor = Optional.empty();
5657
private Optional<WorkflowFilter> outputProcessor = Optional.empty();
5758
private Optional<WorkflowFilter> contextProcessor = Optional.empty();
59+
private Optional<WorkflowFilter> ifFilter = Optional.empty();
5860
private Optional<SchemaValidator> inputSchemaValidator = Optional.empty();
5961
private Optional<SchemaValidator> outputSchemaValidator = Optional.empty();
6062
private Optional<SchemaValidator> contextSchemaValidator = Optional.empty();
@@ -100,6 +102,7 @@ protected AbstractTaskExecutorBuilder(
100102
this.contextSchemaValidator =
101103
getSchemaValidator(application.validatorFactory(), resourceLoader, export.getSchema());
102104
}
105+
this.ifFilter = optionalFilter(application.expressionFactory(), task.getIf());
103106
}
104107

105108
protected final TransitionInfoBuilder next(
@@ -153,6 +156,7 @@ protected AbstractTaskExecutor(AbstractTaskExecutorBuilder<T> builder) {
153156
this.inputSchemaValidator = builder.inputSchemaValidator;
154157
this.outputSchemaValidator = builder.outputSchemaValidator;
155158
this.contextSchemaValidator = builder.contextSchemaValidator;
159+
this.ifFilter = builder.ifFilter;
156160
}
157161

158162
protected final CompletableFuture<TaskContext> executeNext(
@@ -177,40 +181,49 @@ public CompletableFuture<TaskContext> apply(
177181
if (!TaskExecutorHelper.isActive(workflowContext)) {
178182
return completable;
179183
}
180-
return executeNext(
181-
completable
182-
.thenApply(
183-
t -> {
184-
workflowContext
185-
.definition()
186-
.listeners()
187-
.forEach(l -> l.onTaskStarted(position, task));
188-
inputSchemaValidator.ifPresent(s -> s.validate(t.rawInput()));
189-
inputProcessor.ifPresent(
190-
p -> taskContext.input(p.apply(workflowContext, t, t.rawInput())));
191-
return t;
192-
})
193-
.thenCompose(t -> execute(workflowContext, t))
194-
.thenApply(
195-
t -> {
196-
outputProcessor.ifPresent(
197-
p -> t.output(p.apply(workflowContext, t, t.rawOutput())));
198-
outputSchemaValidator.ifPresent(s -> s.validate(t.output()));
199-
contextProcessor.ifPresent(
200-
p ->
201-
workflowContext.context(
202-
p.apply(workflowContext, t, workflowContext.context())));
203-
contextSchemaValidator.ifPresent(s -> s.validate(workflowContext.context()));
204-
t.completedAt(Instant.now());
205-
workflowContext
206-
.definition()
207-
.listeners()
208-
.forEach(l -> l.onTaskEnded(position, task));
209-
return t;
210-
}),
211-
workflowContext);
184+
if (ifFilter
185+
.map(f -> f.apply(workflowContext, taskContext, input).asBoolean(true))
186+
.orElse(true)) {
187+
return executeNext(
188+
completable
189+
.thenApply(
190+
t -> {
191+
workflowContext
192+
.definition()
193+
.listeners()
194+
.forEach(l -> l.onTaskStarted(position, task));
195+
inputSchemaValidator.ifPresent(s -> s.validate(t.rawInput()));
196+
inputProcessor.ifPresent(
197+
p -> taskContext.input(p.apply(workflowContext, t, t.rawInput())));
198+
return t;
199+
})
200+
.thenCompose(t -> execute(workflowContext, t))
201+
.thenApply(
202+
t -> {
203+
outputProcessor.ifPresent(
204+
p -> t.output(p.apply(workflowContext, t, t.rawOutput())));
205+
outputSchemaValidator.ifPresent(s -> s.validate(t.output()));
206+
contextProcessor.ifPresent(
207+
p ->
208+
workflowContext.context(
209+
p.apply(workflowContext, t, workflowContext.context())));
210+
contextSchemaValidator.ifPresent(s -> s.validate(workflowContext.context()));
211+
t.completedAt(Instant.now());
212+
workflowContext
213+
.definition()
214+
.listeners()
215+
.forEach(l -> l.onTaskEnded(position, task));
216+
return t;
217+
}),
218+
workflowContext);
219+
} else {
220+
taskContext.transition(getSkipTransition());
221+
return executeNext(completable, workflowContext);
222+
}
212223
}
213224

225+
protected abstract TransitionInfo getSkipTransition();
226+
214227
protected abstract CompletableFuture<TaskContext> execute(
215228
WorkflowContext workflow, TaskContext taskContext);
216229
}

impl/core/src/main/java/io/serverlessworkflow/impl/executors/RegularTaskExecutor.java

+5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ public void connect(Map<String, TaskExecutorBuilder<?>> connections) {
5454
}
5555
}
5656

57+
@Override
58+
protected TransitionInfo getSkipTransition() {
59+
return transition;
60+
}
61+
5762
protected CompletableFuture<TaskContext> execute(
5863
WorkflowContext workflow, TaskContext taskContext) {
5964
CompletableFuture<TaskContext> future =

impl/core/src/main/java/io/serverlessworkflow/impl/executors/SwitchExecutor.java

+5
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ protected TaskExecutor<SwitchTask> buildInstance() {
7979
}
8080
}
8181

82+
@Override
83+
protected TransitionInfo getSkipTransition() {
84+
return defaultTask;
85+
}
86+
8287
private SwitchExecutor(SwitchExecutorBuilder builder) {
8388
super(builder);
8489
this.defaultTask = TransitionInfo.build(builder.defaultTask);

impl/core/src/test/java/io/serverlessworkflow/impl/WorkflowDefinitionTest.java

+18
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ private static Stream<Arguments> provideParameters() {
8181
"simple-expression.yaml",
8282
Map.of("input", Arrays.asList(1, 2, 3)),
8383
WorkflowDefinitionTest::checkSpecialKeywords),
84+
args(
85+
"conditional-set.yaml",
86+
Map.of("enabled", true),
87+
WorkflowDefinitionTest::checkEnableCondition),
88+
args(
89+
"conditional-set.yaml",
90+
Map.of("enabled", false),
91+
WorkflowDefinitionTest::checkDisableCondition),
8492
args(
8593
"raise-inline copy.yaml",
8694
WorkflowDefinitionTest::checkWorkflowException,
@@ -166,4 +174,14 @@ private static void checkSpecialKeywords(Object obj) {
166174
assertThat(result.get("id").toString()).hasSize(26);
167175
assertThat(result.get("version").toString()).contains("alpha");
168176
}
177+
178+
private static void checkEnableCondition(Object obj) {
179+
Map<String, Object> result = (Map<String, Object>) obj;
180+
assertThat(result.get("name")).isEqualTo("javierito");
181+
}
182+
183+
private static void checkDisableCondition(Object obj) {
184+
Map<String, Object> result = (Map<String, Object>) obj;
185+
assertThat(result.get("enabled")).isEqualTo(false);
186+
}
169187
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
document:
2+
dsl: '1.0.0-alpha5'
3+
namespace: test
4+
name: conditional-set
5+
version: '0.1.0'
6+
do:
7+
- conditionalExpression:
8+
if: .enabled
9+
set:
10+
name: javierito

0 commit comments

Comments
 (0)