Search in sources :

Example 1 with DefaultConditionDefinition

use of io.serverlessworkflow.api.defaultdef.DefaultConditionDefinition in project kogito-runtimes by kiegroup.

the class SwitchValidatorTest method validateDefaultConditionWithEventConditionsTransitionButTimeoutNotSetError.

@Test
void validateDefaultConditionWithEventConditionsTransitionButTimeoutNotSetError() {
    switchState.getEventConditions().add(mock(EventCondition.class));
    DefaultConditionDefinition defaultCondition = mockDefaultConditionWithTransition();
    Transition transition = defaultCondition.getTransition();
    doReturn(NEXT_STATE).when(transition).getNextState();
    StateHandler<?> stateHandler = mock(StateHandler.class);
    doReturn(stateHandler).when(parserContext).getStateHandler(NEXT_STATE);
    Assertions.assertThatThrownBy(() -> SwitchValidator.validateDefaultCondition(defaultCondition, switchState, workflow, parserContext)).isInstanceOf(IllegalArgumentException.class).hasMessage(String.format(EVENT_TIMEOUT_REQUIRED_ERROR, SWITCH_STATE_NAME, WORKFLOW_NAME));
}
Also used : Transition(io.serverlessworkflow.api.transitions.Transition) EventCondition(io.serverlessworkflow.api.switchconditions.EventCondition) DefaultConditionDefinition(io.serverlessworkflow.api.defaultdef.DefaultConditionDefinition) Test(org.junit.jupiter.api.Test)

Example 2 with DefaultConditionDefinition

use of io.serverlessworkflow.api.defaultdef.DefaultConditionDefinition in project kogito-runtimes by kiegroup.

the class SwitchHandler method finalizeEventBasedSwitchState.

private void finalizeEventBasedSwitchState(RuleFlowNodeContainerFactory<?, ?> factory) {
    NodeFactory<?, ?> splitNode = getNode();
    List<EventCondition> conditions = state.getEventConditions();
    DefaultConditionDefinition defaultCondition = state.getDefaultCondition();
    if (defaultCondition != null) {
        validateDefaultCondition(defaultCondition, state, workflow, parserContext);
        String eventTimeout = resolveEventTimeout(state, workflow);
        // Create the timer for controlling the eventTimeout and connect it with the exclusive split.
        TimerNodeFactory<?> eventTimeoutTimerNode = timerNode(factory.timerNode(parserContext.newId()), eventTimeout);
        connect(splitNode, eventTimeoutTimerNode);
        Transition transition = defaultCondition.getTransition();
        if (transition != null) {
            StateHandler<?> targetState = parserContext.getStateHandler(transition);
            // Connect the timer with the target state.
            targetState.connect(factory, eventTimeoutTimerNode.getNode().getId());
        } else {
            // Connect the timer with a process finalization sequence that might produce events.
            endIt(eventTimeoutTimerNode.getNode().getId(), factory, defaultCondition.getEnd().getProduceEvents());
        }
    }
    // Process the event conditions.
    for (EventCondition eventCondition : conditions) {
        StateHandler<?> targetState = parserContext.getStateHandler(eventCondition.getTransition());
        // Create the event reception and later processing sequence.
        MakeNodeResult eventNode = filterAndMergeNode(factory, eventCondition.getEventDataFilter(), (f, inputVar, outputVar) -> consumeEventNode(f, eventCondition.getEventRef(), inputVar, outputVar));
        // Connect it with the split node
        factory.connection(splitNode.getNode().getId(), eventNode.getIncomingNode().getNode().getId());
        // Connect the last node of the sequence with the target state.
        targetState.connect(factory, eventNode.getOutgoingNode().getNode().getId());
    }
}
Also used : Transition(io.serverlessworkflow.api.transitions.Transition) EventCondition(io.serverlessworkflow.api.switchconditions.EventCondition) DefaultConditionDefinition(io.serverlessworkflow.api.defaultdef.DefaultConditionDefinition)

Example 3 with DefaultConditionDefinition

use of io.serverlessworkflow.api.defaultdef.DefaultConditionDefinition in project kogito-runtimes by kiegroup.

the class SwitchValidatorTest method validateDefaultConditionTransitionWithoutNextError.

@Test
void validateDefaultConditionTransitionWithoutNextError() {
    DefaultConditionDefinition defaultCondition = mockDefaultConditionWithTransition();
    Assertions.assertThatThrownBy(() -> SwitchValidator.validateDefaultCondition(defaultCondition, switchState, workflow, parserContext)).isInstanceOf(IllegalArgumentException.class).hasMessage(String.format(NEXT_STATE_REQUIRED_FOR_DEFAULT_CONDITION_ERROR, SWITCH_STATE_NAME, WORKFLOW_NAME));
}
Also used : DefaultConditionDefinition(io.serverlessworkflow.api.defaultdef.DefaultConditionDefinition) Test(org.junit.jupiter.api.Test)

Example 4 with DefaultConditionDefinition

use of io.serverlessworkflow.api.defaultdef.DefaultConditionDefinition in project kogito-runtimes by kiegroup.

the class SwitchHandler method finalizeDataBasedSwitchState.

private void finalizeDataBasedSwitchState(RuleFlowNodeContainerFactory<?, ?> factory) {
    final NodeFactory<?, ?> startNode = getNode();
    final long splitId = startNode.getNode().getId();
    DefaultConditionDefinition defaultCondition = state.getDefaultCondition();
    // set default connection
    if (defaultCondition != null) {
        validateDefaultCondition(defaultCondition, state, workflow, parserContext);
        Transition transition = defaultCondition.getTransition();
        if (transition != null) {
            StateHandler<?> stateHandler = parserContext.getStateHandler(transition);
            startNode.metaData(XORSPLITDEFAULT, concatId(splitId, stateHandler.getNode().getNode().getId()));
        } else {
            EndNodeFactory<?> endNodeFactory = endIt(splitId, factory, defaultCondition.getEnd().getProduceEvents());
            startNode.metaData(XORSPLITDEFAULT, concatId(splitId, endNodeFactory.getNode().getId()));
        }
    }
    List<DataCondition> conditions = state.getDataConditions();
    for (DataCondition condition : conditions) {
        handleTransition(factory, condition.getTransition(), splitId, Optional.of(new StateHandler.HandleTransitionCallBack() {

            @Override
            public void onStateTarget(StateHandler<?> targetState) {
                targetHandlers.add(() -> addConstraint(factory, startNode, targetState, condition));
            }

            @Override
            public void onIdTarget(long targetId) {
                addConstraint(startNode, targetId, condition);
            }

            @Override
            public void onEmptyTarget() {
                if (condition.getEnd() != null) {
                    EndNodeFactory<?> endNodeFactory = endIt(splitId, factory, condition.getEnd().getProduceEvents());
                    addConstraint(startNode, endNodeFactory.getNode().getId(), condition);
                } else {
                    throw new IllegalArgumentException("Invalid condition, not transition not end");
                }
            }
        }));
    }
}
Also used : Transition(io.serverlessworkflow.api.transitions.Transition) DataCondition(io.serverlessworkflow.api.switchconditions.DataCondition) DefaultConditionDefinition(io.serverlessworkflow.api.defaultdef.DefaultConditionDefinition)

Example 5 with DefaultConditionDefinition

use of io.serverlessworkflow.api.defaultdef.DefaultConditionDefinition in project kogito-runtimes by kiegroup.

the class SwitchValidatorTest method validateDefaultConditionTransitionNextStateNotFoundError.

@Test
void validateDefaultConditionTransitionNextStateNotFoundError() {
    DefaultConditionDefinition defaultCondition = mockDefaultConditionWithTransition();
    Transition transition = defaultCondition.getTransition();
    doReturn(NEXT_STATE).when(transition).getNextState();
    Assertions.assertThatThrownBy(() -> SwitchValidator.validateDefaultCondition(defaultCondition, switchState, workflow, parserContext)).isInstanceOf(IllegalArgumentException.class).hasMessage(String.format(NEXT_STATE_NOT_FOUND_FOR_DEFAULT_CONDITION_ERROR, NEXT_STATE, SWITCH_STATE_NAME, WORKFLOW_NAME));
}
Also used : Transition(io.serverlessworkflow.api.transitions.Transition) DefaultConditionDefinition(io.serverlessworkflow.api.defaultdef.DefaultConditionDefinition) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultConditionDefinition (io.serverlessworkflow.api.defaultdef.DefaultConditionDefinition)8 Transition (io.serverlessworkflow.api.transitions.Transition)5 Test (org.junit.jupiter.api.Test)5 EventCondition (io.serverlessworkflow.api.switchconditions.EventCondition)3 End (io.serverlessworkflow.api.end.End)1 DataCondition (io.serverlessworkflow.api.switchconditions.DataCondition)1