Skip to content

Introduce Java Fluent DSL #646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 21, 2025
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
45 changes: 25 additions & 20 deletions experimental/lambda/pom.xml
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-experimental</artifactId>
<version>8.0.0-SNAPSHOT</version>
</parent>
<artifactId>serverlessworkflow-experimental-lambda</artifactId>
<name>ServelessWorkflow:: Experimental:: lambda</name>
<dependencies>
<dependency>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-experimental-types</artifactId>
</dependency>
<dependency>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-impl-core</artifactId>
</dependency>
<dependency>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-experimental</artifactId>
<version>8.0.0-SNAPSHOT</version>
</parent>
<artifactId>serverlessworkflow-experimental-lambda</artifactId>
<name>ServelessWorkflow:: Experimental:: lambda</name>
<dependencies>
<dependency>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-experimental-types</artifactId>
</dependency>
<dependency>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-impl-core</artifactId>
</dependency>
<dependency>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-fluent-java</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
Expand All @@ -41,5 +46,5 @@
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void testForLoop() throws InterruptedException, ExecutionException {
new Task()
.withForTask(
new ForTaskFunction()
.withWhile(this::isEven)
.withWhile(CallTest::isEven)
.withCollection(v -> (Collection) v)
.withFor(forConfig)
.withDo(
Expand All @@ -91,7 +91,7 @@ void testForLoop() throws InterruptedException, ExecutionException {
.withCallTask(
new CallTaskJava(
CallJava.loopFunction(
this::sum,
CallTest::sum,
forConfig.getEach()))))))))));

assertThat(
Expand Down Expand Up @@ -124,35 +124,35 @@ void testSwitch() throws InterruptedException, ExecutionException {
new SwitchItem(
"odd",
new SwitchCaseFunction()
.withPredicate(this::isOdd)
.withPredicate(CallTest::isOdd)
.withThen(
new FlowDirective()
.withFlowDirectiveEnum(
FlowDirectiveEnum.END))))))),
new TaskItem(
"java",
new Task()
.withCallTask(new CallTaskJava(CallJava.function(this::zero))))));
.withCallTask(new CallTaskJava(CallJava.function(CallTest::zero))))));

WorkflowDefinition definition = app.workflowDefinition(workflow);
assertThat(definition.instance(3).start().get().asNumber().orElseThrow()).isEqualTo(3);
assertThat(definition.instance(4).start().get().asNumber().orElseThrow()).isEqualTo(0);
}
}

private boolean isEven(Object model, Integer number) {
public static boolean isEven(Object model, Integer number) {
return !isOdd(number);
}

private boolean isOdd(Integer number) {
public static boolean isOdd(Integer number) {
return number % 2 != 0;
}

private int zero(Integer value) {
public static int zero(Integer value) {
return 0;
}

private Integer sum(Object model, Integer item) {
public static Integer sum(Object model, Integer item) {
return model instanceof Collection ? item : (Integer) model + item;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverless.workflow.impl;

import static org.assertj.core.api.Assertions.assertThat;

import io.serverlessworkflow.api.types.FlowDirectiveEnum;
import io.serverlessworkflow.api.types.Workflow;
import io.serverlessworkflow.fluent.java.JavaWorkflowBuilder;
import io.serverlessworkflow.impl.WorkflowApplication;
import io.serverlessworkflow.impl.WorkflowDefinition;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ExecutionException;
import org.junit.jupiter.api.Test;

public class FluentDSLCallTest {

@Test
void testJavaFunction() throws InterruptedException, ExecutionException {
try (WorkflowApplication app = WorkflowApplication.builder().build()) {
final Workflow workflow =
JavaWorkflowBuilder.workflow("testJavaCall")
.tasks(tasks -> tasks.callFn(f -> f.fn(JavaFunctions::getName)))
.build();
assertThat(
app.workflowDefinition(workflow)
.instance(new Person("Francisco", 33))
.start()
.get()
.asText()
.orElseThrow())
.isEqualTo("Francisco Javierito");
}
}

@Test
void testForLoop() throws InterruptedException, ExecutionException {
try (WorkflowApplication app = WorkflowApplication.builder().build()) {
Workflow workflow =
JavaWorkflowBuilder.workflow()
.tasks(
t ->
t.forFn(
f ->
f.whileC(CallTest::isEven)
.collection(v -> (Collection<?>) v)
.tasks(CallTest::sum)))
.build();

assertThat(
app.workflowDefinition(workflow)
.instance(List.of(2, 4, 6))
.start()
.get()
.asNumber()
.orElseThrow())
.isEqualTo(12);
}
}

@Test
void testSwitch() throws InterruptedException, ExecutionException {
try (WorkflowApplication app = WorkflowApplication.builder().build()) {
Workflow workflow =
JavaWorkflowBuilder.workflow()
.tasks(
tasks ->
tasks
.switchFn(
switchOdd ->
switchOdd.items(
item ->
item.when(CallTest::isOdd).then(FlowDirectiveEnum.END)))
.callFn(callJava -> callJava.fn(CallTest::zero)))
.build();

WorkflowDefinition definition = app.workflowDefinition(workflow);
assertThat(definition.instance(3).start().get().asNumber().orElseThrow()).isEqualTo(3);
assertThat(definition.instance(4).start().get().asNumber().orElseThrow()).isEqualTo(0);
}
}
}
5 changes: 5 additions & 0 deletions experimental/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<artifactId>serverlessworkflow-experimental-types</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-fluent-java</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<modules>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public <T> SwitchCaseFunction withPredicate(Predicate<T> predicate) {
return this;
}

public <T> void setPredicate(Predicate<T> predicate) {
this.predicate = predicate;
}

public Predicate<?> predicate() {
return predicate;
}
Expand Down
42 changes: 42 additions & 0 deletions fluent/java/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-fluent</artifactId>
<version>8.0.0-SNAPSHOT</version>
</parent>

<name>Serverless Workflow :: Fluent :: Java</name>
<artifactId>serverlessworkflow-fluent-java</artifactId>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-experimental-types</artifactId>
</dependency>
<dependency>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-types</artifactId>
</dependency>
<dependency>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-fluent-standard</artifactId>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.fluent.java;

import io.serverlessworkflow.api.types.CallJava;
import io.serverlessworkflow.api.types.CallTaskJava;
import io.serverlessworkflow.fluent.standard.TaskBaseBuilder;
import java.util.function.Function;

public class CallTaskJavaBuilder extends TaskBaseBuilder<CallTaskJavaBuilder>
implements JavaTransformationHandlers<CallTaskJavaBuilder> {

private CallTaskJava callTaskJava;

CallTaskJavaBuilder() {
callTaskJava = new CallTaskJava(new CallJava() {});
super.setTask(callTaskJava.getCallJava());
}

@Override
protected CallTaskJavaBuilder self() {
return this;
}

public <T, V> CallTaskJavaBuilder fn(Function<T, V> function) {
this.callTaskJava = new CallTaskJava(CallJava.function(function));
super.setTask(this.callTaskJava.getCallJava());
return this;
}

public CallTaskJava build() {
return this.callTaskJava;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.fluent.java;

import io.serverlessworkflow.fluent.standard.BaseDoTaskBuilder;
import java.util.function.Consumer;

public class DoTaskJavaBuilder extends BaseDoTaskBuilder<DoTaskJavaBuilder, TaskItemListJavaBuilder>
implements JavaTransformationHandlers<DoTaskJavaBuilder> {

DoTaskJavaBuilder() {
super(new TaskItemListJavaBuilder());
}

@Override
protected DoTaskJavaBuilder self() {
return this;
}

public DoTaskJavaBuilder callFn(String name, Consumer<CallTaskJavaBuilder> consumer) {
this.innerListBuilder().callJava(name, consumer);
return this;
}

public DoTaskJavaBuilder callFn(Consumer<CallTaskJavaBuilder> consumer) {
this.innerListBuilder().callJava(consumer);
return this;
}

public DoTaskJavaBuilder forFn(String name, Consumer<ForTaskJavaBuilder> consumer) {
this.innerListBuilder().forFn(name, consumer);
return this;
}

public DoTaskJavaBuilder forFn(Consumer<ForTaskJavaBuilder> consumer) {
this.innerListBuilder().forFn(consumer);
return this;
}

public DoTaskJavaBuilder switchFn(String name, Consumer<SwitchTaskJavaBuilder> consumer) {
this.innerListBuilder().switchFn(name, consumer);
return this;
}

public DoTaskJavaBuilder switchFn(Consumer<SwitchTaskJavaBuilder> consumer) {
this.innerListBuilder().switchFn(consumer);
return this;
}
}
Loading