diff --git a/core/src/main/java/iwf/core/StateDecision.java b/core/src/main/java/iwf/core/StateDecision.java index 26d7c7f1..1e3eaa2f 100644 --- a/core/src/main/java/iwf/core/StateDecision.java +++ b/core/src/main/java/iwf/core/StateDecision.java @@ -22,9 +22,21 @@ public abstract class StateDecision { public static final StateDecision WAIT_FOR_MORE_RESULTS = ImmutableStateDecision.builder().waitForMoreCommandResults(true).build(); - public static StateDecision completeWorkflow(Object output) { + public static StateDecision gracefulCompleteWorkflow(final Object output) { return ImmutableStateDecision.builder().nextStates(Arrays.asList( - StateMovement.completeWorkflow(output) + StateMovement.gracefulCompleteWorkflow(output) )).build(); } + + public static StateDecision singleNextState(final String stateId, final Object stateInput) { + return ImmutableStateDecision.builder().nextStates(Arrays.asList( + ImmutableStateMovement.builder().stateId(stateId) + .nextStateInput(stateInput) + .build() + )).build(); + } + + public static StateDecision multiNextStates(final StateMovement... stateMovements) { + return ImmutableStateDecision.builder().nextStates(Arrays.asList(stateMovements)).build(); + } } diff --git a/core/src/main/java/iwf/core/StateDef.java b/core/src/main/java/iwf/core/StateDef.java index 86959132..6c6a058f 100644 --- a/core/src/main/java/iwf/core/StateDef.java +++ b/core/src/main/java/iwf/core/StateDef.java @@ -12,4 +12,22 @@ public abstract class StateDef { // indicates if this state can be used to start a workflow public abstract boolean getCanStartWorkflow(); + + public static StateDef startingState(WorkflowState state) { + return ImmutableStateDef.builder() + .canStartWorkflow(true) + .workflowState( + state + ) + .build(); + } + + public static StateDef nonStartingState(WorkflowState state) { + return ImmutableStateDef.builder() + .canStartWorkflow(false) + .workflowState( + state + ) + .build(); + } } diff --git a/core/src/main/java/iwf/core/StateMovement.java b/core/src/main/java/iwf/core/StateMovement.java index 0b15dab5..46c3945c 100644 --- a/core/src/main/java/iwf/core/StateMovement.java +++ b/core/src/main/java/iwf/core/StateMovement.java @@ -16,9 +16,20 @@ public abstract class StateMovement { public static final StateMovement GRACEFUL_COMPLETING_WORKFLOW = ImmutableStateMovement.builder().stateId(GRACEFUL_COMPLETING_WORKFLOW_STATE_ID).build(); public static final StateMovement FORCE_FAILING_WORKFLOW_MOVEMENT = ImmutableStateMovement.builder().stateId(FORCE_FAILING_WORKFLOW_STATE_ID).build(); - public static StateMovement completeWorkflow(Object output) { + public static StateMovement gracefulCompleteWorkflow(final Object output) { return ImmutableStateMovement.builder().stateId(GRACEFUL_COMPLETING_WORKFLOW_STATE_ID) .nextStateInput(output) .build(); } + + public static StateMovement create(final String stateId, final Object stateInput) { + return ImmutableStateMovement.builder().stateId(stateId) + .nextStateInput(stateInput) + .build(); + } + + public static StateMovement create(final String stateId) { + return ImmutableStateMovement.builder().stateId(stateId) + .build(); + } } diff --git a/core/src/main/java/iwf/core/WorkflowStartOptions.java b/core/src/main/java/iwf/core/WorkflowStartOptions.java index 2a6aba54..ef249c2b 100644 --- a/core/src/main/java/iwf/core/WorkflowStartOptions.java +++ b/core/src/main/java/iwf/core/WorkflowStartOptions.java @@ -1,10 +1,8 @@ package iwf.core; -import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import org.immutables.value.Value; @Value.Immutable -@JsonDeserialize(as = ImmutableWorkflowStartOptions.class) public abstract class WorkflowStartOptions { public abstract Integer getWorkflowTimeoutSeconds(); diff --git a/core/src/main/java/iwf/core/WorkflowState.java b/core/src/main/java/iwf/core/WorkflowState.java index 460d10f2..34ea2976 100644 --- a/core/src/main/java/iwf/core/WorkflowState.java +++ b/core/src/main/java/iwf/core/WorkflowState.java @@ -39,8 +39,6 @@ default StateOptions getStateOptions() { * @param queryAttributes the query attributes that can be used as readOnly * @param searchAttributes the search attributes that can be used as readOnly * @return the requested commands for this step - * NOTE: it's readonly here for simplifying the implementation(execute can be reverted in some edge cases), - * We could change to support R+W if necessary. */ CommandRequest start( final Context context, I input, diff --git a/core/src/test/java/iwf/integ/basic/BasicTest.java b/core/src/test/java/iwf/integ/basic/BasicTest.java index 449a56c0..aab20ec5 100644 --- a/core/src/test/java/iwf/integ/basic/BasicTest.java +++ b/core/src/test/java/iwf/integ/basic/BasicTest.java @@ -2,7 +2,6 @@ import iwf.core.Client; import iwf.core.ClientOptions; -import iwf.core.ImmutableWorkflowStartOptions; import iwf.core.Registry; import iwf.core.WorkflowStartOptions; import iwf.spring.TestSingletonWorkerService; @@ -18,7 +17,7 @@ public void setup() { } @Test - public void testBasicWorkflow() { + public void testBasicWorkflow() throws InterruptedException { final Registry registry = new Registry(); final BasicWorkflow wf = new BasicWorkflow(); registry.addWorkflow(wf); @@ -26,9 +25,10 @@ public void testBasicWorkflow() { final Client client = new Client(registry, ClientOptions.localDefault); final String wfId = "basic-test-id" + System.currentTimeMillis() / 1000; final WorkflowStartOptions startOptions = WorkflowStartOptions.minimum(10); - client.StartWorkflow(BasicWorkflow.class, BasicWorkflowS1.StateId, startOptions, wfId, startOptions); + final Integer input = new Integer(0); + client.StartWorkflow(BasicWorkflow.class, BasicWorkflowS1.StateId, input, wfId, startOptions); // wait for workflow to finish - final ImmutableWorkflowStartOptions output = client.GetSingleWorkflowStateOutputWithLongWait(ImmutableWorkflowStartOptions.class, wfId); - Assertions.assertEquals(startOptions, output); + final Integer output = client.GetSingleWorkflowStateOutputWithLongWait(Integer.class, wfId); + Assertions.assertEquals(input + 2, output); } } diff --git a/core/src/test/java/iwf/integ/basic/BasicWorkflow.java b/core/src/test/java/iwf/integ/basic/BasicWorkflow.java index 6daa1a4b..8b2afb86 100644 --- a/core/src/test/java/iwf/integ/basic/BasicWorkflow.java +++ b/core/src/test/java/iwf/integ/basic/BasicWorkflow.java @@ -1,6 +1,5 @@ package iwf.integ.basic; -import iwf.core.ImmutableStateDef; import iwf.core.StateDef; import iwf.core.Workflow; @@ -8,15 +7,10 @@ import java.util.List; public class BasicWorkflow implements Workflow { - @Override public List getStates() { return Arrays.asList( - ImmutableStateDef.builder() - .canStartWorkflow(true) - .workflowState( - new BasicWorkflowS1() - ) - .build() + StateDef.startingState(new BasicWorkflowS1()), + StateDef.nonStartingState(new BasicWorkflowS2()) ); } } diff --git a/core/src/test/java/iwf/integ/basic/BasicWorkflowS1.java b/core/src/test/java/iwf/integ/basic/BasicWorkflowS1.java index c1597d09..039ed4b4 100644 --- a/core/src/test/java/iwf/integ/basic/BasicWorkflowS1.java +++ b/core/src/test/java/iwf/integ/basic/BasicWorkflowS1.java @@ -2,7 +2,6 @@ import iwf.core.Context; import iwf.core.StateDecision; -import iwf.core.WorkflowStartOptions; import iwf.core.WorkflowState; import iwf.core.attributes.QueryAttributesRW; import iwf.core.attributes.SearchAttributesRW; @@ -11,7 +10,7 @@ import iwf.core.command.CommandRequest; import iwf.core.command.CommandResults; -public class BasicWorkflowS1 implements WorkflowState { +public class BasicWorkflowS1 implements WorkflowState { public static final String StateId = "S1"; @@ -21,18 +20,18 @@ public String getStateId() { } @Override - public Class getInputType() { - return WorkflowStartOptions.class; + public Class getInputType() { + return Integer.class; } @Override - public CommandRequest start(final Context context, final Object input, final StateLocalAttributesW stateLocals, final SearchAttributesRW searchAttributes, final QueryAttributesRW queryAttributes) { + public CommandRequest start(final Context context, final Integer input, final StateLocalAttributesW stateLocals, final SearchAttributesRW searchAttributes, final QueryAttributesRW queryAttributes) { return CommandRequest.empty; } @Override - public StateDecision decide(final Context context, final Object input, final CommandResults commandResults, final StateLocalAttributesR stateLocals, final SearchAttributesRW searchAttributes, final QueryAttributesRW queryAttributes) { - final StateDecision out = StateDecision.completeWorkflow(input); - return out; + public StateDecision decide(final Context context, final Integer input, final CommandResults commandResults, final StateLocalAttributesR stateLocals, final SearchAttributesRW searchAttributes, final QueryAttributesRW queryAttributes) { + final int output = input + 1; + return StateDecision.singleNextState(BasicWorkflowS2.StateId, output); } } diff --git a/core/src/test/java/iwf/integ/basic/BasicWorkflowS2.java b/core/src/test/java/iwf/integ/basic/BasicWorkflowS2.java new file mode 100644 index 00000000..e97c3cb2 --- /dev/null +++ b/core/src/test/java/iwf/integ/basic/BasicWorkflowS2.java @@ -0,0 +1,37 @@ +package iwf.integ.basic; + +import iwf.core.Context; +import iwf.core.StateDecision; +import iwf.core.WorkflowState; +import iwf.core.attributes.QueryAttributesRW; +import iwf.core.attributes.SearchAttributesRW; +import iwf.core.attributes.StateLocalAttributesR; +import iwf.core.attributes.StateLocalAttributesW; +import iwf.core.command.CommandRequest; +import iwf.core.command.CommandResults; + +public class BasicWorkflowS2 implements WorkflowState { + + public static final String StateId = "S2"; + + @Override + public String getStateId() { + return StateId; + } + + @Override + public Class getInputType() { + return Integer.class; + } + + @Override + public CommandRequest start(final Context context, final Integer input, final StateLocalAttributesW stateLocals, final SearchAttributesRW searchAttributes, final QueryAttributesRW queryAttributes) { + return CommandRequest.empty; + } + + @Override + public StateDecision decide(final Context context, final Integer input, final CommandResults commandResults, final StateLocalAttributesR stateLocals, final SearchAttributesRW searchAttributes, final QueryAttributesRW queryAttributes) { + final int output = input + 1; + return StateDecision.gracefulCompleteWorkflow(output); + } +}