diff --git a/experimental/fluent/func/pom.xml b/experimental/fluent/func/pom.xml
index 9b652a18..f14b4e49 100644
--- a/experimental/fluent/func/pom.xml
+++ b/experimental/fluent/func/pom.xml
@@ -39,6 +39,12 @@
junit-jupiter-apitest
+
+ org.mockito
+ mockito-core
+ ${version.org.mockito}
+ test
+
\ No newline at end of file
diff --git a/experimental/fluent/func/src/main/java/io/serverlessworkflow/fluent/func/FuncCallTaskBuilder.java b/experimental/fluent/func/src/main/java/io/serverlessworkflow/fluent/func/FuncCallTaskBuilder.java
index 180804d0..58115e4e 100644
--- a/experimental/fluent/func/src/main/java/io/serverlessworkflow/fluent/func/FuncCallTaskBuilder.java
+++ b/experimental/fluent/func/src/main/java/io/serverlessworkflow/fluent/func/FuncCallTaskBuilder.java
@@ -18,6 +18,7 @@
import io.serverlessworkflow.api.types.func.CallJava;
import io.serverlessworkflow.api.types.func.CallTaskJava;
import io.serverlessworkflow.api.types.func.JavaContextFunction;
+import io.serverlessworkflow.api.types.func.JavaFilterFunction;
import io.serverlessworkflow.fluent.func.spi.ConditionalTaskBuilder;
import io.serverlessworkflow.fluent.func.spi.FuncTaskTransformations;
import io.serverlessworkflow.fluent.spec.TaskBaseBuilder;
@@ -61,6 +62,16 @@ public FuncCallTaskBuilder function(
return this;
}
+ public FuncCallTaskBuilder function(JavaFilterFunction function) {
+ return function(function, null);
+ }
+
+ public FuncCallTaskBuilder function(JavaFilterFunction function, Class argClass) {
+ this.callTaskJava = new CallTaskJava(CallJava.function(function, argClass));
+ super.setTask(this.callTaskJava.getCallJava());
+ return this;
+ }
+
/** Accept a side-effect Consumer; engine should pass input through unchanged. */
public FuncCallTaskBuilder consumer(Consumer consumer) {
this.callTaskJava = new CallTaskJava(CallJava.consumer(consumer));
diff --git a/experimental/fluent/func/src/main/java/io/serverlessworkflow/fluent/func/dsl/FuncCallStep.java b/experimental/fluent/func/src/main/java/io/serverlessworkflow/fluent/func/dsl/FuncCallStep.java
index c784cebf..2e828e3b 100644
--- a/experimental/fluent/func/src/main/java/io/serverlessworkflow/fluent/func/dsl/FuncCallStep.java
+++ b/experimental/fluent/func/src/main/java/io/serverlessworkflow/fluent/func/dsl/FuncCallStep.java
@@ -16,6 +16,7 @@
package io.serverlessworkflow.fluent.func.dsl;
import io.serverlessworkflow.api.types.func.JavaContextFunction;
+import io.serverlessworkflow.api.types.func.JavaFilterFunction;
import io.serverlessworkflow.fluent.func.FuncCallTaskBuilder;
import io.serverlessworkflow.fluent.func.FuncTaskItemListBuilder;
import java.util.function.Consumer;
@@ -26,6 +27,7 @@ public final class FuncCallStep extends Step, FuncCallT
private final String name;
private final Function fn;
private final JavaContextFunction ctxFn;
+ private final JavaFilterFunction filterFn;
private final Class argClass;
/** Function variant (unnamed). */
@@ -38,6 +40,7 @@ public final class FuncCallStep extends Step, FuncCallT
this.name = name;
this.fn = fn;
this.ctxFn = null;
+ this.filterFn = null;
this.argClass = argClass;
}
@@ -51,6 +54,21 @@ public final class FuncCallStep extends Step, FuncCallT
this.name = name;
this.fn = null;
this.ctxFn = ctxFn;
+ this.filterFn = null;
+ this.argClass = argClass;
+ }
+
+ /** JavaFilterFunction variant (unnamed). */
+ FuncCallStep(JavaFilterFunction filterFn, Class argClass) {
+ this(null, filterFn, argClass);
+ }
+
+ /** JavaFilterFunction variant (named). */
+ FuncCallStep(String name, JavaFilterFunction filterFn, Class argClass) {
+ this.name = name;
+ this.fn = null;
+ this.ctxFn = null;
+ this.filterFn = filterFn;
this.argClass = argClass;
}
@@ -60,6 +78,8 @@ protected void configure(FuncTaskItemListBuilder list, Consumer {
if (ctxFn != null) {
cb.function(ctxFn, argClass);
+ } else if (filterFn != null) {
+ cb.function(filterFn, argClass);
} else {
cb.function(fn, argClass);
}
diff --git a/experimental/fluent/func/src/main/java/io/serverlessworkflow/fluent/func/dsl/FuncDSL.java b/experimental/fluent/func/src/main/java/io/serverlessworkflow/fluent/func/dsl/FuncDSL.java
index e6a0d917..9977c8f7 100644
--- a/experimental/fluent/func/src/main/java/io/serverlessworkflow/fluent/func/dsl/FuncDSL.java
+++ b/experimental/fluent/func/src/main/java/io/serverlessworkflow/fluent/func/dsl/FuncDSL.java
@@ -18,6 +18,7 @@
import io.cloudevents.CloudEventData;
import io.serverlessworkflow.api.types.FlowDirectiveEnum;
import io.serverlessworkflow.api.types.func.JavaContextFunction;
+import io.serverlessworkflow.api.types.func.JavaFilterFunction;
import io.serverlessworkflow.fluent.func.FuncCallTaskBuilder;
import io.serverlessworkflow.fluent.func.FuncEmitTaskBuilder;
import io.serverlessworkflow.fluent.func.FuncSwitchTaskBuilder;
@@ -26,6 +27,8 @@
import io.serverlessworkflow.fluent.func.configurers.FuncTaskConfigurer;
import io.serverlessworkflow.fluent.func.configurers.SwitchCaseConfigurer;
import io.serverlessworkflow.fluent.func.dsl.internal.CommonFuncOps;
+import io.serverlessworkflow.impl.TaskContextData;
+import io.serverlessworkflow.impl.WorkflowContextData;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@@ -286,7 +289,7 @@ public static FuncCallStep function(Function fn, Class cla
}
/**
- * Build a call step for functions that need {@code WorkflowContextData} as the first parameter.
+ * Build a call step for functions that need {@link WorkflowContextData} as the first parameter.
* The DSL wraps it as a {@link JavaContextFunction} and injects the runtime context.
*
*
Signature expected: {@code (ctx, payload) -> result}
@@ -297,7 +300,7 @@ public static FuncCallStep function(Function fn, Class cla
* @param result type
* @return a call step
*/
- public static FuncCallStep withContext(CtxBiFunction fn, Class in) {
+ public static FuncCallStep withContext(JavaContextFunction fn, Class in) {
return withContext(null, fn, in);
}
@@ -319,7 +322,7 @@ public static FuncCallStep withInstanceId(
}
/**
- * Named variant of {@link #withContext(CtxBiFunction, Class)}.
+ * Named variant of {@link #withContext(JavaContextFunction, Class)}.
*
* @param name task name
* @param fn context-aware bi-function
@@ -329,9 +332,40 @@ public static FuncCallStep withInstanceId(
* @return a named call step
*/
public static FuncCallStep withContext(
- String name, CtxBiFunction fn, Class in) {
- JavaContextFunction jcf = (payload, wctx) -> fn.apply(wctx, payload);
- return new FuncCallStep<>(name, jcf, in);
+ String name, JavaContextFunction fn, Class in) {
+ return new FuncCallStep<>(name, fn, in);
+ }
+
+ /**
+ * Build a call step for functions that need {@link WorkflowContextData} and {@link
+ * io.serverlessworkflow.impl.TaskContextData} as the first and second parameter. The DSL wraps it
+ * as a {@link JavaFilterFunction} and injects the runtime context.
+ *
+ *
Signature expected: {@code (wctx, tctx, payload) -> result}
+ *
+ * @param fn context-aware bi-function
+ * @param in payload input class
+ * @param input type
+ * @param result type
+ * @return a call step
+ */
+ public static FuncCallStep withFilter(JavaFilterFunction fn, Class in) {
+ return withFilter(null, fn, in);
+ }
+
+ /**
+ * Named variant of {@link #withFilter(JavaFilterFunction, Class)}.
+ *
+ * @param name task name
+ * @param fn context-aware bi-function
+ * @param in payload input class
+ * @param input type
+ * @param result type
+ * @return a named call step
+ */
+ public static FuncCallStep withFilter(
+ String name, JavaFilterFunction fn, Class in) {
+ return new FuncCallStep<>(name, fn, in);
}
/**
@@ -350,6 +384,38 @@ public static FuncCallStep withInstanceId(
return new FuncCallStep<>(name, jcf, in);
}
+ /**
+ * Builds a composition of the current workflow instance id and the definition of the task
+ * position as a JSON pointer.
+ */
+ static String defaultUniqueId(WorkflowContextData wctx, TaskContextData tctx) {
+ return String.format("%s-%s", wctx.instanceData().id(), tctx.position().jsonPointer());
+ }
+
+ /**
+ * Build a call step for functions that expect a composition with the workflow instance id and the
+ * task position as the first parameter. The instance ID is extracted from the runtime context,
+ * the task position from the definition.
+ *
+ *
Signature expected: {@code (uniqueId, payload) -> result}
+ *
+ * @param fn unique-id-aware bi-function
+ * @param in payload input class
+ * @param input type
+ * @param result type
+ * @return a call step
+ */
+ public static FuncCallStep withUniqueId(
+ String name, UniqueIdBiFunction fn, Class in) {
+ JavaFilterFunction jff =
+ (payload, wctx, tctx) -> fn.apply(defaultUniqueId(wctx, tctx), payload);
+ return new FuncCallStep<>(name, jff, in);
+ }
+
+ public static FuncCallStep withUniqueId(UniqueIdBiFunction fn, Class in) {
+ return withUniqueId(null, fn, in);
+ }
+
/**
* Create a fire-and-forget side-effect step (unnamed). The consumer receives the typed input.
*
@@ -387,12 +453,12 @@ public static ConsumeStep consume(String name, Consumer consumer, Clas
* @param result type
* @return a call step
*/
- public static FuncCallStep agent(InstanceIdBiFunction fn, Class in) {
- return withInstanceId(fn, in);
+ public static FuncCallStep agent(UniqueIdBiFunction fn, Class in) {
+ return withUniqueId(fn, in);
}
/**
- * Named agent-style sugar. See {@link #agent(InstanceIdBiFunction, Class)}.
+ * Named agent-style sugar. See {@link #agent(UniqueIdBiFunction, Class)}.
*
* @param name task name
* @param fn (instanceId, payload) -> result
@@ -402,8 +468,8 @@ public static FuncCallStep agent(InstanceIdBiFunction fn, Cla
* @return a named call step
*/
public static FuncCallStep agent(
- String name, InstanceIdBiFunction fn, Class in) {
- return withInstanceId(name, fn, in);
+ String name, UniqueIdBiFunction fn, Class in) {
+ return withUniqueId(name, fn, in);
}
/**
@@ -677,7 +743,7 @@ public static FuncTaskConfigurer switchWhenOrElse(
* switchWhenOrElse(".approved == true", "sendEmail", FlowDirectiveEnum.END)
*
*
- * The JQ expression is evaluated against the task input at runtime.
+ *
The JQ expression is evaluated against the task input at runtime.
*/
public static FuncTaskConfigurer switchWhenOrElse(
String jqExpression, String thenTask, FlowDirectiveEnum otherwise) {
@@ -698,7 +764,7 @@ public static FuncTaskConfigurer switchWhenOrElse(
* switchWhenOrElse(".score >= 80", "pass", "fail")
*
*
- * The JQ expression is evaluated against the task input at runtime.
+ *
The JQ expression is evaluated against the task input at runtime.
*/
public static FuncTaskConfigurer switchWhenOrElse(
String jqExpression, String thenTask, String otherwiseTask) {
diff --git a/experimental/fluent/func/src/main/java/io/serverlessworkflow/fluent/func/dsl/UniqueIdBiFunction.java b/experimental/fluent/func/src/main/java/io/serverlessworkflow/fluent/func/dsl/UniqueIdBiFunction.java
new file mode 100644
index 00000000..229ed716
--- /dev/null
+++ b/experimental/fluent/func/src/main/java/io/serverlessworkflow/fluent/func/dsl/UniqueIdBiFunction.java
@@ -0,0 +1,30 @@
+/*
+ * 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.func.dsl;
+
+import java.util.function.BiFunction;
+
+/**
+ * Functions that expect a unique ID injection in runtime, typically an idempotent generated unique
+ * id based on the workflow instance id and task name.
+ *
+ * @param The task payload input
+ * @param The task result output
+ */
+@FunctionalInterface
+public interface UniqueIdBiFunction extends BiFunction {
+ R apply(String uniqueId, T object);
+}
diff --git a/experimental/fluent/func/src/test/java/io/serverlessworkflow/fluent/func/FuncDSLUniqueIdTest.java b/experimental/fluent/func/src/test/java/io/serverlessworkflow/fluent/func/FuncDSLUniqueIdTest.java
new file mode 100644
index 00000000..fe7a9b07
--- /dev/null
+++ b/experimental/fluent/func/src/test/java/io/serverlessworkflow/fluent/func/FuncDSLUniqueIdTest.java
@@ -0,0 +1,149 @@
+/*
+ * 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.func;
+
+import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.agent;
+import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.withUniqueId;
+import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.Mockito.*;
+
+import io.serverlessworkflow.api.types.Task;
+import io.serverlessworkflow.api.types.TaskItem;
+import io.serverlessworkflow.api.types.Workflow;
+import io.serverlessworkflow.api.types.func.CallJava;
+import io.serverlessworkflow.api.types.func.JavaFilterFunction;
+import io.serverlessworkflow.fluent.func.dsl.UniqueIdBiFunction;
+import io.serverlessworkflow.impl.TaskContextData;
+import io.serverlessworkflow.impl.WorkflowContextData;
+import io.serverlessworkflow.impl.WorkflowInstanceData;
+import io.serverlessworkflow.impl.WorkflowPosition;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicReference;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Verifies that withUniqueId/agent wrap the user's function so that, at runtime, the first argument
+ * is a "unique id" composed as instanceId + "-" + jsonPointer (e.g., inst-123-/do/0/task).
+ */
+class FuncDSLUniqueIdTest {
+
+ @SuppressWarnings("unchecked")
+ private static JavaFilterFunction