Skip to content

Commit 40fbfcb

Browse files
committed
we can do better
Signed-off-by: Dmitrii Tikhomirov <[email protected]>
1 parent db5218c commit 40fbfcb

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

experimental/fluent/agentic/src/test/java/io/serverlessworkflow/fluent/agentic/LC4JEquivalenceIT.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static io.serverlessworkflow.fluent.agentic.AgentWorkflowBuilder.workflow;
1919
import static io.serverlessworkflow.fluent.agentic.dsl.AgenticDSL.conditional;
2020
import static io.serverlessworkflow.fluent.agentic.dsl.AgenticDSL.doTasks;
21+
import static io.serverlessworkflow.fluent.agentic.dsl.AgenticDSL.fn;
2122
import static io.serverlessworkflow.fluent.agentic.dsl.AgenticDSL.loop;
2223
import static org.assertj.core.api.Assertions.assertThat;
2324
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -30,6 +31,7 @@
3031
import io.serverlessworkflow.api.types.func.CallTaskJava;
3132
import io.serverlessworkflow.api.types.func.ForTaskFunction;
3233
import io.serverlessworkflow.impl.WorkflowApplication;
34+
import java.util.ArrayList;
3335
import java.util.List;
3436
import java.util.Map;
3537
import java.util.concurrent.atomic.AtomicReference;
@@ -147,13 +149,49 @@ public void loopWorkflowWithMaxIterations() {
147149
assertThat(result).containsKey("story");
148150
}
149151

152+
public record EveningPlan(String movie, String meal) {}
153+
150154
@Test
151155
@DisplayName("Parallel agents via DSL.parallel(...)")
152156
public void parallelWorkflow() {
153157
var foodExpert = AgentsUtils.newFoodExpert();
154158
var movieExpert = AgentsUtils.newMovieExpert();
155159

156-
Workflow wf = workflow("forkFlow").parallel("fanout", foodExpert, movieExpert).build();
160+
workflow("forkFlow")
161+
.tasks(
162+
d ->
163+
d.parallel(foodExpert, movieExpert)
164+
.callFn(
165+
fn(
166+
f -> {
167+
Map<String, List<String>> asMap = (Map<String, List<String>>) f;
168+
List<EveningPlan> result = new ArrayList<>();
169+
int max =
170+
asMap.values().stream()
171+
.map(List::size)
172+
.min(Integer::compareTo)
173+
.orElse(0);
174+
for (int i = 0; i < max; i++) {
175+
result.add(
176+
new EveningPlan(
177+
asMap.get("movies").get(i), asMap.get("meals").get(i)));
178+
}
179+
return result;
180+
})))
181+
.build();
182+
183+
Workflow wf = workflow("forkFlow")
184+
.tasks(d -> d
185+
.parallel("fanout", foodExpert, movieExpert)
186+
.callFn(fn((Map<String, List<String>> m) -> {
187+
var movies = m.getOrDefault("movies", List.of());
188+
var meals = m.getOrDefault("meals", List.of());
189+
return java.util.stream.IntStream
190+
.range(0, Math.min(movies.size(), meals.size()))
191+
.mapToObj(i -> new EveningPlan(movies.get(i), meals.get(i)))
192+
.toList();
193+
}))
194+
).build();
157195

158196
List<TaskItem> items = wf.getDo();
159197
assertThat(items).hasSize(1);

experimental/fluent/agentic/src/test/java/io/serverlessworkflow/fluent/agentic/README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -288,17 +288,17 @@ List<EveningPlan> plans = eveningPlannerAgent.plan("romantic");
288288
<td style="vertical-align:top;">
289289
<pre style="background:none; margin:0; padding:0; font-family:monospace; line-height:1.4;">
290290
<code class="language-java" style="background:none;white-space:pre;">
291-
Workflow wf = workflow("forkFlow")
292-
.parallel(foodExpert, movieExpert)
293-
.build();
294-
&nbsp;
295-
&nbsp;
296-
&nbsp;
297-
&nbsp;
298-
&nbsp;
299-
&nbsp;
300-
&nbsp;
301-
&nbsp;
291+
Workflow wf = workflow("forkFlow")
292+
.tasks(d -> d.parallel(foodExpert, movieExpert)
293+
.callFn(fn((Map&lt;String, List<String>> m) -> {
294+
var movies = m.getOrDefault("movies", List.of());
295+
var meals = m.getOrDefault("meals", List.of());
296+
return java.util.stream.IntStream
297+
.range(0, Math.min(movies.size(), meals.size()))
298+
.mapToObj(i -> new EveningPlan(movies.get(i), meals.get(i)))
299+
.toList();
300+
}))
301+
).build();
302302
&nbsp;
303303
&nbsp;
304304
&nbsp;
@@ -307,7 +307,7 @@ Workflow wf = workflow("forkFlow")
307307
&nbsp;
308308
Map&lt;String, Object> input = Map.of("mood", "I am hungry and bored");
309309

310-
Map<String, Object> result = app.workflowDefinition(wf).instance(input).start().get().asMap().orElseThrow();
310+
List<EveningPlan> result = app.workflowDefinition(wf).instance(input).start().get().asMap().orElseThrow();
311311

312312
</code>
313313
</pre>

0 commit comments

Comments
 (0)