Skip to content

Java experimental #644

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 1 commit into from
Jul 18, 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: 45 additions & 0 deletions experimental/lambda/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<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>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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.impl.executors;

import io.serverlessworkflow.api.types.CallJava;
import io.serverlessworkflow.api.types.TaskBase;
import io.serverlessworkflow.impl.TaskContext;
import io.serverlessworkflow.impl.WorkflowApplication;
import io.serverlessworkflow.impl.WorkflowContext;
import io.serverlessworkflow.impl.WorkflowModel;
import io.serverlessworkflow.impl.WorkflowModelFactory;
import io.serverlessworkflow.impl.resources.ResourceLoader;
import java.util.concurrent.CompletableFuture;

public class JavaCallExecutor implements CallableTask<CallJava> {

@Override
public void init(CallJava task, WorkflowApplication application, ResourceLoader loader) {}

@Override
public CompletableFuture<WorkflowModel> apply(
WorkflowContext workflowContext, TaskContext taskContext, WorkflowModel input) {
WorkflowModelFactory modelFactory = workflowContext.definition().application().modelFactory();
if (taskContext.task() instanceof CallJava.CallJavaFunction function) {
return CompletableFuture.completedFuture(
modelFactory.fromAny(function.function().apply(input.asJavaObject())));
} else if (taskContext.task() instanceof CallJava.CallJavaLoopFunction function) {
return CompletableFuture.completedFuture(
modelFactory.fromAny(
function
.function()
.apply(
input.asJavaObject(),
safeObject(taskContext.variables().get(function.varName())))));
} else if (taskContext.task() instanceof CallJava.CallJavaLoopFunctionIndex function) {
return CompletableFuture.completedFuture(
modelFactory.fromAny(
function
.function()
.apply(
input.asJavaObject(),
safeObject(taskContext.variables().get(function.varName())),
(Integer) safeObject(taskContext.variables().get(function.indexName())))));
} else if (taskContext.task() instanceof CallJava.CallJavaConsumer consumer) {
consumer.consumer().accept(input.asJavaObject());
}
return CompletableFuture.completedFuture(input);
}

@Override
public boolean accept(Class<? extends TaskBase> clazz) {
return CallJava.class.isAssignableFrom(clazz);
}

static Object safeObject(Object obj) {
return obj instanceof WorkflowModel model ? model.asJavaObject() : obj;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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.impl.executors;

import static io.serverlessworkflow.impl.executors.JavaCallExecutor.safeObject;

import io.serverlessworkflow.api.types.ForTask;
import io.serverlessworkflow.api.types.ForTaskFunction;
import io.serverlessworkflow.api.types.Workflow;
import io.serverlessworkflow.impl.WorkflowApplication;
import io.serverlessworkflow.impl.WorkflowFilter;
import io.serverlessworkflow.impl.WorkflowPosition;
import io.serverlessworkflow.impl.WorkflowUtils;
import io.serverlessworkflow.impl.executors.ForExecutor.ForExecutorBuilder;
import io.serverlessworkflow.impl.expressions.LoopPredicateIndex;
import io.serverlessworkflow.impl.resources.ResourceLoader;
import java.util.Optional;

public class JavaForExecutorBuilder extends ForExecutorBuilder {

protected JavaForExecutorBuilder(
WorkflowPosition position,
ForTask task,
Workflow workflow,
WorkflowApplication application,
ResourceLoader resourceLoader) {
super(position, task, workflow, application, resourceLoader);
if (task instanceof ForTaskFunction taskFunctions) {}
}

protected Optional<WorkflowFilter> buildWhileFilter() {
if (task instanceof ForTaskFunction taskFunctions) {
LoopPredicateIndex whilePred = taskFunctions.getWhilePredicate();
String varName = task.getFor().getEach();
String indexName = task.getFor().getAt();
if (whilePred != null) {
return Optional.of(
(w, t, n) -> {
Object item = safeObject(t.variables().get(varName));
return application
.modelFactory()
.from(
item == null
|| whilePred.test(
n.asJavaObject(),
item,
(Integer) safeObject(t.variables().get(indexName))));
});
}
}
return super.buildWhileFilter();
}

protected WorkflowFilter buildCollectionFilter() {
return task instanceof ForTaskFunction taskFunctions
? WorkflowUtils.buildWorkflowFilter(application, null, taskFunctions.getCollection())
: super.buildCollectionFilter();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.impl.executors;

import io.serverlessworkflow.api.types.SwitchCase;
import io.serverlessworkflow.api.types.SwitchCaseFunction;
import io.serverlessworkflow.api.types.SwitchTask;
import io.serverlessworkflow.api.types.Workflow;
import io.serverlessworkflow.impl.WorkflowApplication;
import io.serverlessworkflow.impl.WorkflowFilter;
import io.serverlessworkflow.impl.WorkflowPosition;
import io.serverlessworkflow.impl.WorkflowUtils;
import io.serverlessworkflow.impl.executors.SwitchExecutor.SwitchExecutorBuilder;
import io.serverlessworkflow.impl.resources.ResourceLoader;
import java.util.Optional;

public class JavaSwitchExecutorBuilder extends SwitchExecutorBuilder {

protected JavaSwitchExecutorBuilder(
WorkflowPosition position,
SwitchTask task,
Workflow workflow,
WorkflowApplication application,
ResourceLoader resourceLoader) {
super(position, task, workflow, application, resourceLoader);
}

@Override
protected Optional<WorkflowFilter> buildFilter(SwitchCase switchCase) {
return switchCase instanceof SwitchCaseFunction function
? Optional.of(WorkflowUtils.buildWorkflowFilter(application, null, function.predicate()))
: super.buildFilter(switchCase);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.impl.executors;

import io.serverlessworkflow.api.types.Task;
import io.serverlessworkflow.api.types.TaskBase;
import io.serverlessworkflow.api.types.Workflow;
import io.serverlessworkflow.impl.WorkflowApplication;
import io.serverlessworkflow.impl.WorkflowPosition;
import io.serverlessworkflow.impl.resources.ResourceLoader;

public class JavaTaskExecutorFactory extends DefaultTaskExecutorFactory {

public TaskExecutorBuilder<? extends TaskBase> getTaskExecutor(
WorkflowPosition position,
Task task,
Workflow workflow,
WorkflowApplication application,
ResourceLoader resourceLoader) {
if (task.getForTask() != null) {
return new JavaForExecutorBuilder(
position, task.getForTask(), workflow, application, resourceLoader);
} else if (task.getSwitchTask() != null) {
return new JavaSwitchExecutorBuilder(
position, task.getSwitchTask(), workflow, application, resourceLoader);
} else {
return super.getTaskExecutor(position, task, workflow, application, resourceLoader);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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.impl.expressions;

import io.serverlessworkflow.impl.TaskContext;
import io.serverlessworkflow.impl.WorkflowContext;
import io.serverlessworkflow.impl.WorkflowFilter;
import io.serverlessworkflow.impl.WorkflowModel;
import io.serverlessworkflow.impl.WorkflowModelFactory;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.Predicate;

public class JavaExpressionFactory implements ExpressionFactory {

private final WorkflowModelFactory modelFactory = new JavaModelFactory();
private final Expression dummyExpression =
new Expression() {
@Override
public WorkflowModel eval(
WorkflowContext workflowContext, TaskContext context, WorkflowModel model) {
return model;
}
};

@Override
public Expression buildExpression(String expression) {
return dummyExpression;
}

@Override
public WorkflowFilter buildFilter(String expr, Object value) {
if (value instanceof Function func) {
return (w, t, n) -> modelFactory.fromAny(func.apply(n.asJavaObject()));
} else if (value instanceof Predicate pred) {
return (w, t, n) -> modelFactory.from(pred.test(n.asJavaObject()));
} else if (value instanceof BiPredicate pred) {
return (w, t, n) -> modelFactory.from(pred.test(w, t));
} else if (value instanceof BiFunction func) {
return (w, t, n) -> modelFactory.fromAny(func.apply(w, t));
} else if (value instanceof WorkflowFilter filter) {
return filter;
} else {
return (w, t, n) -> modelFactory.fromAny(value);
}
}

@Override
public WorkflowModelFactory modelFactory() {
return modelFactory;
}
}
Loading