Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ public abstract class AbstractTaskExecutor<T extends TaskBase> implements TaskEx
private final Optional<SchemaValidator> inputSchemaValidator;
private final Optional<SchemaValidator> outputSchemaValidator;
private final Optional<SchemaValidator> contextSchemaValidator;
private final Optional<WorkflowFilter> ifFilter;

public abstract static class AbstractTaskExecutorBuilder<T extends TaskBase>
implements TaskExecutorBuilder<T> {
private Optional<WorkflowFilter> inputProcessor = Optional.empty();
private Optional<WorkflowFilter> outputProcessor = Optional.empty();
private Optional<WorkflowFilter> contextProcessor = Optional.empty();
private Optional<WorkflowFilter> ifFilter = Optional.empty();
private Optional<SchemaValidator> inputSchemaValidator = Optional.empty();
private Optional<SchemaValidator> outputSchemaValidator = Optional.empty();
private Optional<SchemaValidator> contextSchemaValidator = Optional.empty();
Expand Down Expand Up @@ -100,6 +102,7 @@ protected AbstractTaskExecutorBuilder(
this.contextSchemaValidator =
getSchemaValidator(application.validatorFactory(), resourceLoader, export.getSchema());
}
this.ifFilter = optionalFilter(application.expressionFactory(), task.getIf());
}

protected final TransitionInfoBuilder next(
Expand Down Expand Up @@ -153,6 +156,7 @@ protected AbstractTaskExecutor(AbstractTaskExecutorBuilder<T> builder) {
this.inputSchemaValidator = builder.inputSchemaValidator;
this.outputSchemaValidator = builder.outputSchemaValidator;
this.contextSchemaValidator = builder.contextSchemaValidator;
this.ifFilter = builder.ifFilter;
}

protected final CompletableFuture<TaskContext> executeNext(
Expand All @@ -177,40 +181,49 @@ public CompletableFuture<TaskContext> apply(
if (!TaskExecutorHelper.isActive(workflowContext)) {
return completable;
}
return executeNext(
completable
.thenApply(
t -> {
workflowContext
.definition()
.listeners()
.forEach(l -> l.onTaskStarted(position, task));
inputSchemaValidator.ifPresent(s -> s.validate(t.rawInput()));
inputProcessor.ifPresent(
p -> taskContext.input(p.apply(workflowContext, t, t.rawInput())));
return t;
})
.thenCompose(t -> execute(workflowContext, t))
.thenApply(
t -> {
outputProcessor.ifPresent(
p -> t.output(p.apply(workflowContext, t, t.rawOutput())));
outputSchemaValidator.ifPresent(s -> s.validate(t.output()));
contextProcessor.ifPresent(
p ->
workflowContext.context(
p.apply(workflowContext, t, workflowContext.context())));
contextSchemaValidator.ifPresent(s -> s.validate(workflowContext.context()));
t.completedAt(Instant.now());
workflowContext
.definition()
.listeners()
.forEach(l -> l.onTaskEnded(position, task));
return t;
}),
workflowContext);
if (ifFilter
.map(f -> f.apply(workflowContext, taskContext, input).asBoolean(true))
.orElse(true)) {
return executeNext(
completable
.thenApply(
t -> {
workflowContext
.definition()
.listeners()
.forEach(l -> l.onTaskStarted(position, task));
inputSchemaValidator.ifPresent(s -> s.validate(t.rawInput()));
inputProcessor.ifPresent(
p -> taskContext.input(p.apply(workflowContext, t, t.rawInput())));
return t;
})
.thenCompose(t -> execute(workflowContext, t))
.thenApply(
t -> {
outputProcessor.ifPresent(
p -> t.output(p.apply(workflowContext, t, t.rawOutput())));
outputSchemaValidator.ifPresent(s -> s.validate(t.output()));
contextProcessor.ifPresent(
p ->
workflowContext.context(
p.apply(workflowContext, t, workflowContext.context())));
contextSchemaValidator.ifPresent(s -> s.validate(workflowContext.context()));
t.completedAt(Instant.now());
workflowContext
.definition()
.listeners()
.forEach(l -> l.onTaskEnded(position, task));
return t;
}),
workflowContext);
} else {
taskContext.transition(getSkipTransition());
return executeNext(completable, workflowContext);
}
}

protected abstract TransitionInfo getSkipTransition();

protected abstract CompletableFuture<TaskContext> execute(
WorkflowContext workflow, TaskContext taskContext);
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public void connect(Map<String, TaskExecutorBuilder<?>> connections) {
}
}

@Override
protected TransitionInfo getSkipTransition() {
return transition;
}

protected CompletableFuture<TaskContext> execute(
WorkflowContext workflow, TaskContext taskContext) {
CompletableFuture<TaskContext> future =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ protected TaskExecutor<SwitchTask> buildInstance() {
}
}

@Override
protected TransitionInfo getSkipTransition() {
return defaultTask;
}

private SwitchExecutor(SwitchExecutorBuilder builder) {
super(builder);
this.defaultTask = TransitionInfo.build(builder.defaultTask);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ private static Stream<Arguments> provideParameters() {
"simple-expression.yaml",
Map.of("input", Arrays.asList(1, 2, 3)),
WorkflowDefinitionTest::checkSpecialKeywords),
args(
"conditional-set.yaml",
Map.of("enabled", true),
WorkflowDefinitionTest::checkEnableCondition),
args(
"conditional-set.yaml",
Map.of("enabled", false),
WorkflowDefinitionTest::checkDisableCondition),
args(
"raise-inline copy.yaml",
WorkflowDefinitionTest::checkWorkflowException,
Expand Down Expand Up @@ -166,4 +174,14 @@ private static void checkSpecialKeywords(Object obj) {
assertThat(result.get("id").toString()).hasSize(26);
assertThat(result.get("version").toString()).contains("alpha");
}

private static void checkEnableCondition(Object obj) {
Map<String, Object> result = (Map<String, Object>) obj;
assertThat(result.get("name")).isEqualTo("javierito");
}

private static void checkDisableCondition(Object obj) {
Map<String, Object> result = (Map<String, Object>) obj;
assertThat(result.get("enabled")).isEqualTo(false);
}
}
10 changes: 10 additions & 0 deletions impl/core/src/test/resources/conditional-set.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
document:
dsl: '1.0.0-alpha5'
namespace: test
name: conditional-set
version: '0.1.0'
do:
- conditionalExpression:
if: .enabled
set:
name: javierito