Skip to content

[Fix #466] Implement switch, set and do #472

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
Nov 25, 2024
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
4 changes: 2 additions & 2 deletions .github/workflows/maven-verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ jobs:
steps:
- uses: actions/checkout@v2

- name: Set up JDK 11
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
distribution: temurin
java-version: 11
java-version: 17
cache: 'maven'

- name: Verify with Maven
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ jobs:
gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
passphrase: ${{ secrets.GPG_PASSPHRASE }}

- name: Set up JDK 11
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
distribution: temurin
java-version: 11
java-version: 17
cache: 'maven'
server-id: ossrh
server-username: MAVEN_USERNAME
Expand All @@ -57,4 +57,4 @@ jobs:
MAVEN_PASSWORD: ${{ secrets.OSSRH_PASSWORD }}

- name: Push tags
run: git push && git push --tags
run: git push && git push --tags
20 changes: 18 additions & 2 deletions impl/src/main/java/io/serverlessworkflow/impl/TaskContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package io.serverlessworkflow.impl;

import com.fasterxml.jackson.databind.JsonNode;
import io.serverlessworkflow.api.types.FlowDirective;
import io.serverlessworkflow.api.types.FlowDirectiveEnum;
import io.serverlessworkflow.api.types.TaskBase;

public class TaskContext<T extends TaskBase> {
Expand All @@ -26,11 +28,15 @@ public class TaskContext<T extends TaskBase> {
private JsonNode input;
private JsonNode output;
private JsonNode rawOutput;
private FlowDirective flowDirective;

public TaskContext(JsonNode rawInput, T task) {
this.rawInput = rawInput;
this.input = rawInput;
this.rawOutput = rawInput;
this.output = rawInput;
this.task = task;
this.flowDirective = task.getThen();
}

public void input(JsonNode input) {
Expand All @@ -54,6 +60,10 @@ public void rawOutput(JsonNode output) {
this.output = output;
}

public JsonNode rawOutput() {
return rawOutput;
}

public void output(JsonNode output) {
this.output = output;
}
Expand All @@ -62,7 +72,13 @@ public JsonNode output() {
return output;
}

public JsonNode rawOutput() {
return rawOutput;
public void flowDirective(FlowDirective flowDirective) {
this.flowDirective = flowDirective;
}

public FlowDirective flowDirective() {
return flowDirective == null
? new FlowDirective().withFlowDirectiveEnum(FlowDirectiveEnum.CONTINUE)
: flowDirective;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* 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;

import io.serverlessworkflow.api.types.Document;
import io.serverlessworkflow.api.types.Workflow;
import io.serverlessworkflow.impl.executors.DefaultTaskExecutorFactory;
import io.serverlessworkflow.impl.executors.TaskExecutorFactory;
import io.serverlessworkflow.impl.expressions.ExpressionFactory;
import io.serverlessworkflow.impl.expressions.JQExpressionFactory;
import io.serverlessworkflow.impl.jsonschema.DefaultSchemaValidatorFactory;
import io.serverlessworkflow.impl.jsonschema.SchemaValidatorFactory;
import io.serverlessworkflow.resources.DefaultResourceLoaderFactory;
import io.serverlessworkflow.resources.ResourceLoaderFactory;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class WorkflowApplication implements AutoCloseable {

private final TaskExecutorFactory taskFactory;
private final ExpressionFactory exprFactory;
private final ResourceLoaderFactory resourceLoaderFactory;
private final SchemaValidatorFactory schemaValidatorFactory;
private final Collection<WorkflowExecutionListener> listeners;
private final Map<WorkflowId, WorkflowDefinition> definitions;

public WorkflowApplication(
TaskExecutorFactory taskFactory,
ExpressionFactory exprFactory,
ResourceLoaderFactory resourceLoaderFactory,
SchemaValidatorFactory schemaValidatorFactory,
Collection<WorkflowExecutionListener> listeners) {
this.taskFactory = taskFactory;
this.exprFactory = exprFactory;
this.resourceLoaderFactory = resourceLoaderFactory;
this.schemaValidatorFactory = schemaValidatorFactory;
this.listeners = listeners;
this.definitions = new ConcurrentHashMap<>();
}

public TaskExecutorFactory taskFactory() {
return taskFactory;
}

public static Builder builder() {
return new Builder();
}

public ExpressionFactory expressionFactory() {
return exprFactory;
}

public SchemaValidatorFactory validatorFactory() {
return schemaValidatorFactory;
}

public ResourceLoaderFactory resourceLoaderFactory() {
return resourceLoaderFactory;
}

public Collection<WorkflowExecutionListener> listeners() {
return listeners;
}

public static class Builder {
private TaskExecutorFactory taskFactory = DefaultTaskExecutorFactory.get();
private ExpressionFactory exprFactory = JQExpressionFactory.get();
private Collection<WorkflowExecutionListener> listeners;
private ResourceLoaderFactory resourceLoaderFactory = DefaultResourceLoaderFactory.get();
private SchemaValidatorFactory schemaValidatorFactory = DefaultSchemaValidatorFactory.get();

private Builder() {}

public Builder withListener(WorkflowExecutionListener listener) {
if (listeners == null) {
listeners = new HashSet<>();
}
listeners.add(listener);
return this;
}

public Builder withTaskExecutorFactory(TaskExecutorFactory factory) {
this.taskFactory = factory;
return this;
}

public Builder withExpressionFactory(ExpressionFactory factory) {
this.exprFactory = factory;
return this;
}

public Builder withResourceLoaderFactory(ResourceLoaderFactory resourceLoader) {
this.resourceLoaderFactory = resourceLoader;
return this;
}

public Builder withSchemaValidatorFactory(SchemaValidatorFactory factory) {
this.schemaValidatorFactory = factory;
return this;
}

public WorkflowApplication build() {
return new WorkflowApplication(
taskFactory,
exprFactory,
resourceLoaderFactory,
schemaValidatorFactory,
listeners == null
? Collections.emptySet()
: Collections.unmodifiableCollection(listeners));
}
}

private static record WorkflowId(String namespace, String name, String version) {
static WorkflowId of(Document document) {
return new WorkflowId(document.getNamespace(), document.getName(), document.getVersion());
}
}

public WorkflowDefinition workflowDefinition(Workflow workflow) {
return definitions.computeIfAbsent(
WorkflowId.of(workflow.getDocument()), k -> WorkflowDefinition.of(this, workflow));
}

@Override
public void close() throws Exception {
for (WorkflowDefinition definition : definitions.values()) {
definition.close();
}
definitions.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,31 @@

public class WorkflowContext {
private final WorkflowPosition position;
private JsonNode context;
private final WorkflowDefinition definition;
private final JsonNode input;
private JsonNode current;
private JsonNode context;

private WorkflowContext(WorkflowPosition position, JsonNode input) {
private WorkflowContext(
WorkflowPosition position, WorkflowDefinition definition, JsonNode input) {
this.position = position;
this.definition = definition;
this.input = input;
this.current = input.deepCopy();
this.context = JsonUtils.mapper().createObjectNode();
}

public static Builder builder(JsonNode input) {
return new Builder(input);
public static Builder builder(WorkflowDefinition definition, JsonNode input) {
return new Builder(definition, input);
}

public static class Builder {
private WorkflowPosition position = new DefaultWorkflowPosition();
private WorkflowDefinition definition;
private JsonNode input;

private Builder(JsonNode input) {
private Builder(WorkflowDefinition definition, JsonNode input) {
this.definition = definition;
this.input = input;
}

Expand All @@ -47,7 +54,7 @@ public Builder position(WorkflowPosition position) {
}

public WorkflowContext build() {
return new WorkflowContext(position, input);
return new WorkflowContext(position, definition, input);
}
}

Expand All @@ -66,4 +73,16 @@ public void context(JsonNode context) {
public JsonNode rawInput() {
return input;
}

public void current(JsonNode output) {
this.current = output;
}

public JsonNode current() {
return current;
}

public WorkflowDefinition definition() {
return definition;
}
}
Loading
Loading