Skip to content

Commit 76be534

Browse files
authored
[Fix #1343] Add ForkTask shortcut to DSL (#1344)
Signed-off-by: fjtirado <ftirados@redhat.com>
1 parent f2afafa commit 76be534

2 files changed

Lines changed: 95 additions & 0 deletions

File tree

  • experimental

experimental/fluent/func/src/main/java/io/serverlessworkflow/fluent/func/dsl/FuncDSL.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,32 @@ public static <T> FuncTaskConfigurer forEach(
11161116
return list -> list.forEach(j -> j.collection(ctx -> collection).tasks(body));
11171117
}
11181118

1119+
/**
1120+
* This is helper to quickly build fork task where every branch is unnamed and consist of just one
1121+
* task. For more advanced use cases, please check FuncTaskItemListBuilder.fork
1122+
*
1123+
* @param name the name of the fork task
1124+
* @param branches every task in this input array will become a branch of the fork.
1125+
* @return FuncTaskConfigurer
1126+
*/
1127+
public static FuncTaskConfigurer fork(String name, FuncTaskConfigurer... branches) {
1128+
if (branches == null || branches.length == 0) {
1129+
throw new IllegalArgumentException("branches must not be null or empty");
1130+
}
1131+
return list ->
1132+
list.fork(
1133+
name,
1134+
forkBuilder -> {
1135+
for (FuncTaskConfigurer branch : branches) {
1136+
forkBuilder.branch((FuncTaskItemListBuilder v) -> branch.accept(v));
1137+
}
1138+
});
1139+
}
1140+
1141+
public static FuncTaskConfigurer fork(FuncTaskConfigurer... branches) {
1142+
return fork(null, branches);
1143+
}
1144+
11191145
/**
11201146
* Set a raw expression on the current list (advanced).
11211147
*
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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.fluent.test;
17+
18+
import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.*;
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
21+
import io.serverlessworkflow.api.types.Workflow;
22+
import io.serverlessworkflow.fluent.func.FuncWorkflowBuilder;
23+
import io.serverlessworkflow.impl.WorkflowApplication;
24+
import org.junit.jupiter.api.Test;
25+
26+
public class ForkFuncTest {
27+
28+
@Test
29+
void testForkVerbose() {
30+
testIt(
31+
FuncWorkflowBuilder.workflow("parallel-execution-workflow")
32+
.tasks(
33+
funcTaskItemListBuilder ->
34+
funcTaskItemListBuilder.fork(
35+
funcForkTaskBuilder ->
36+
funcForkTaskBuilder.branches(
37+
inner -> {
38+
inner.function(f -> f.function(this::doubleIt, int.class));
39+
inner.function(f -> f.function(this::halfIt, int.class));
40+
})))
41+
.build());
42+
}
43+
44+
@Test
45+
void testForkSyntaxSugar() {
46+
testIt(
47+
FuncWorkflowBuilder.workflow("parallel-execution-workflow")
48+
.tasks(fork(function(this::doubleIt), function(this::halfIt)))
49+
.build());
50+
}
51+
52+
private void testIt(Workflow workflow) {
53+
try (WorkflowApplication app = WorkflowApplication.builder().build()) {
54+
assertThat(
55+
app.workflowDefinition(workflow).instance(8).start().join().asCollection().stream()
56+
.flatMap(m -> m.asMap().orElseThrow().values().stream())
57+
.toList())
58+
.containsExactlyInAnyOrder(4, 16);
59+
}
60+
}
61+
62+
private int doubleIt(int number) {
63+
return number << 1;
64+
}
65+
66+
private int halfIt(int number) {
67+
return number >> 1;
68+
}
69+
}

0 commit comments

Comments
 (0)