Skip to content

Commit 5dd9291

Browse files
authored
[Fix #863] Adding WorkflowContext and TaskContext as additional params (#864)
User can now specify java filter functions that accepts WorkflowContext and TaskContext in their signature. Signed-off-by: fjtirado <ftirados@redhat.com>
1 parent 45c73c8 commit 5dd9291

File tree

11 files changed

+230
-0
lines changed

11 files changed

+230
-0
lines changed

experimental/lambda/src/main/java/io/serverlessworkflow/impl/expressions/func/JavaExpressionFactory.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@
1818
import io.cloudevents.CloudEventData;
1919
import io.serverlessworkflow.api.types.TaskBase;
2020
import io.serverlessworkflow.api.types.TaskMetadata;
21+
import io.serverlessworkflow.api.types.func.JavaContextFunction;
22+
import io.serverlessworkflow.api.types.func.JavaFilterFunction;
2123
import io.serverlessworkflow.api.types.func.TaskMetadataKeys;
2224
import io.serverlessworkflow.api.types.func.TypedFunction;
25+
import io.serverlessworkflow.api.types.func.TypedJavaContextFunction;
26+
import io.serverlessworkflow.api.types.func.TypedJavaFilterFunction;
2327
import io.serverlessworkflow.api.types.func.TypedPredicate;
2428
import io.serverlessworkflow.impl.WorkflowModelFactory;
2529
import io.serverlessworkflow.impl.WorkflowPredicate;
@@ -44,6 +48,14 @@ public ObjectExpression buildExpression(ExpressionDescriptor descriptor) {
4448
return (w, t, n) -> func.apply(n.asJavaObject());
4549
} else if (value instanceof TypedFunction func) {
4650
return (w, t, n) -> func.function().apply(n.as(func.argClass()).orElseThrow());
51+
} else if (value instanceof JavaFilterFunction func) {
52+
return (w, t, n) -> func.apply(n.asJavaObject(), w, t);
53+
} else if (value instanceof TypedJavaFilterFunction func) {
54+
return (w, t, n) -> func.function().apply(n.as(func.argClass()).orElseThrow(), w, t);
55+
} else if (value instanceof JavaContextFunction func) {
56+
return (w, t, n) -> func.apply(n.asJavaObject(), w);
57+
} else if (value instanceof TypedJavaContextFunction func) {
58+
return (w, t, n) -> func.function().apply(n.as(func.argClass()).orElseThrow(), w);
4759
} else {
4860
return (w, t, n) -> value;
4961
}

experimental/lambda/src/test/java/io/serverless/workflow/impl/JavaFunctions.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package io.serverless.workflow.impl;
1717

18+
import io.serverlessworkflow.impl.TaskContextData;
19+
import io.serverlessworkflow.impl.WorkflowContextData;
1820
import java.util.Map;
1921

2022
public class JavaFunctions {
@@ -27,6 +29,15 @@ static String getName(Person person) {
2729
return person.name() + " Javierito";
2830
}
2931

32+
static String getFilterName(
33+
Person person, WorkflowContextData workflowContext, TaskContextData taskContext) {
34+
return person.name() + "_" + workflowContext.instanceData().id() + "_" + taskContext.taskName();
35+
}
36+
37+
static String getContextName(Person person, WorkflowContextData workflowContext) {
38+
return person.name() + "_" + workflowContext.instanceData().id();
39+
}
40+
3041
static Map<String, Object> addJavierito(Map<String, Object> map) {
3142
return Map.of("name", map.get("name") + " Javierito");
3243
}

experimental/lambda/src/test/java/io/serverless/workflow/impl/ModelTest.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import io.serverlessworkflow.api.types.Workflow;
3131
import io.serverlessworkflow.api.types.func.OutputAsFunction;
3232
import io.serverlessworkflow.impl.WorkflowApplication;
33+
import io.serverlessworkflow.impl.WorkflowInstance;
3334
import java.util.List;
3435
import java.util.Map;
3536
import java.util.concurrent.ExecutionException;
@@ -173,4 +174,62 @@ void testPOJOStringExpression() throws InterruptedException, ExecutionException
173174
.isEqualTo("Francisco Javierito");
174175
}
175176
}
177+
178+
@Test
179+
void testPOJOStringExpressionWithContext() throws InterruptedException, ExecutionException {
180+
try (WorkflowApplication app = WorkflowApplication.builder().build()) {
181+
Workflow workflow =
182+
new Workflow()
183+
.withDocument(
184+
new Document().withNamespace("test").withName("testPojo").withVersion("1.0"))
185+
.withDo(
186+
List.of(
187+
new TaskItem(
188+
"doNothing",
189+
new Task()
190+
.withWaitTask(
191+
new WaitTask()
192+
.withWait(
193+
new TimeoutAfter()
194+
.withDurationInline(
195+
new DurationInline().withMilliseconds(10)))))))
196+
.withOutput(
197+
new Output()
198+
.withAs(new OutputAsFunction().withFunction(JavaFunctions::getContextName)));
199+
WorkflowInstance instance =
200+
app.workflowDefinition(workflow).instance(new Person("Francisco", 33));
201+
assertThat(instance.start().get().asText().orElseThrow())
202+
.isEqualTo("Francisco_" + instance.id());
203+
}
204+
}
205+
206+
@Test
207+
void testPOJOStringExpressionWithFilter() throws InterruptedException, ExecutionException {
208+
try (WorkflowApplication app = WorkflowApplication.builder().build()) {
209+
Workflow workflow =
210+
new Workflow()
211+
.withDocument(
212+
new Document().withNamespace("test").withName("testPojo").withVersion("1.0"))
213+
.withDo(
214+
List.of(
215+
new TaskItem(
216+
"doNothing",
217+
new Task()
218+
.withWaitTask(
219+
new WaitTask()
220+
.withOutput(
221+
new Output()
222+
.withAs(
223+
new OutputAsFunction()
224+
.withFunction(JavaFunctions::getFilterName)))
225+
.withWait(
226+
new TimeoutAfter()
227+
.withDurationInline(
228+
new DurationInline().withMilliseconds(10)))))));
229+
WorkflowInstance instance =
230+
app.workflowDefinition(workflow).instance(new Person("Francisco", 33));
231+
assertThat(instance.start().get().asText().orElseThrow())
232+
.isEqualTo("Francisco_" + instance.id() + "_doNothing");
233+
}
234+
}
176235
}

experimental/types/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,9 @@
1212
<groupId>io.serverlessworkflow</groupId>
1313
<artifactId>serverlessworkflow-types</artifactId>
1414
</dependency>
15+
<dependency>
16+
<groupId>io.serverlessworkflow</groupId>
17+
<artifactId>serverlessworkflow-impl-core</artifactId>
18+
</dependency>
1519
</dependencies>
1620
</project>

experimental/types/src/main/java/io/serverlessworkflow/api/types/func/ExportAsFunction.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,24 @@ public <T, V> ExportAs withFunction(Function<T, V> value, Class<T> argClass) {
3131
setObject(new TypedFunction<>(value, argClass));
3232
return this;
3333
}
34+
35+
public <T, V> ExportAs withFunction(JavaFilterFunction<T, V> value) {
36+
setObject(value);
37+
return this;
38+
}
39+
40+
public <T, V> ExportAs withFunction(JavaFilterFunction<T, V> value, Class<T> argClass) {
41+
setObject(new TypedJavaFilterFunction<>(value, argClass));
42+
return this;
43+
}
44+
45+
public <T, V> ExportAs withFunction(JavaContextFunction<T, V> value) {
46+
setObject(value);
47+
return this;
48+
}
49+
50+
public <T, V> ExportAs withFunction(JavaContextFunction<T, V> value, Class<T> argClass) {
51+
setObject(new TypedJavaContextFunction<>(value, argClass));
52+
return this;
53+
}
3454
}

experimental/types/src/main/java/io/serverlessworkflow/api/types/func/InputFromFunction.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,24 @@ public <T, V> InputFrom withFunction(Function<T, V> value, Class<T> argClass) {
3131
setObject(new TypedFunction<>(value, argClass));
3232
return this;
3333
}
34+
35+
public <T, V> InputFrom withFunction(JavaFilterFunction<T, V> value) {
36+
setObject(value);
37+
return this;
38+
}
39+
40+
public <T, V> InputFrom withFunction(JavaFilterFunction<T, V> value, Class<T> argClass) {
41+
setObject(new TypedJavaFilterFunction<>(value, argClass));
42+
return this;
43+
}
44+
45+
public <T, V> InputFrom withFunction(JavaContextFunction<T, V> value) {
46+
setObject(value);
47+
return this;
48+
}
49+
50+
public <T, V> InputFrom withFunction(JavaContextFunction<T, V> value, Class<T> argClass) {
51+
setObject(new TypedJavaContextFunction<>(value, argClass));
52+
return this;
53+
}
3454
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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.api.types.func;
17+
18+
import io.serverlessworkflow.impl.WorkflowContextData;
19+
20+
@FunctionalInterface
21+
public interface JavaContextFunction<T, R> {
22+
R apply(T object, WorkflowContextData workflowContext);
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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.api.types.func;
17+
18+
import io.serverlessworkflow.impl.TaskContextData;
19+
import io.serverlessworkflow.impl.WorkflowContextData;
20+
21+
@FunctionalInterface
22+
public interface JavaFilterFunction<T, R> {
23+
R apply(T object, WorkflowContextData workflowContext, TaskContextData taskContext);
24+
}

experimental/types/src/main/java/io/serverlessworkflow/api/types/func/OutputAsFunction.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,24 @@ public <T, V> OutputAs withFunction(Function<T, V> value, Class<T> argClass) {
3131
setObject(new TypedFunction<>(value, argClass));
3232
return this;
3333
}
34+
35+
public <T, V> OutputAs withFunction(JavaFilterFunction<T, V> value) {
36+
setObject(value);
37+
return this;
38+
}
39+
40+
public <T, V> OutputAs withFunction(JavaFilterFunction<T, V> value, Class<T> argClass) {
41+
setObject(new TypedJavaFilterFunction<>(value, argClass));
42+
return this;
43+
}
44+
45+
public <T, V> OutputAs withFunction(JavaContextFunction<T, V> value) {
46+
setObject(value);
47+
return this;
48+
}
49+
50+
public <T, V> OutputAs withFunction(JavaContextFunction<T, V> value, Class<T> argClass) {
51+
setObject(new TypedJavaContextFunction<>(value, argClass));
52+
return this;
53+
}
3454
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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.api.types.func;
17+
18+
public record TypedJavaContextFunction<T, V>(
19+
JavaContextFunction<T, V> function, Class<T> argClass) {}

0 commit comments

Comments
 (0)