Search in sources :

Example 1 with ProcessInstanceCompensationAction

use of org.jbpm.process.instance.impl.actions.ProcessInstanceCompensationAction in project kogito-runtimes by kiegroup.

the class EndNodeVisitor method visitNode.

@Override
public void visitNode(String factoryField, EndNode node, BlockStmt body, VariableScope variableScope, ProcessMetaData metadata) {
    body.addStatement(getAssignedFactoryMethod(factoryField, EndNodeFactory.class, getNodeId(node), getNodeKey(), new LongLiteralExpr(node.getId()))).addStatement(getNameMethod(node, "End")).addStatement(getFactoryMethod(getNodeId(node), METHOD_TERMINATE, new BooleanLiteralExpr(node.isTerminate())));
    // if there is trigger defined on end event create TriggerMetaData for it
    Optional<ProcessInstanceCompensationAction> compensationAction = getAction(node, ProcessInstanceCompensationAction.class);
    Optional<ExpressionSupplier> supplier = getAction(node, ExpressionSupplier.class);
    if (compensationAction.isPresent()) {
        String compensateNode = CompensationScope.IMPLICIT_COMPENSATION_PREFIX + metadata.getProcessId();
        if (compensationAction.get().getActivityRef() != null) {
            compensateNode = compensationAction.get().getActivityRef();
        }
        LambdaExpr lambda = buildCompensationLambdaExpr(compensateNode);
        body.addStatement(getFactoryMethod(getNodeId(node), ActionNodeFactory.METHOD_ACTION, lambda));
    } else if (supplier.isPresent()) {
        body.addStatement(getFactoryMethod(getNodeId(node), ActionNodeFactory.METHOD_ACTION, supplier.get().get(node, metadata)));
    } else if (node.getMetaData(TRIGGER_REF) != null) {
        body.addStatement(getFactoryMethod(getNodeId(node), METHOD_ACTION, buildProducerAction(node, metadata)));
    } else if (node.getMetaData(REF) != null && EVENT_TYPE_SIGNAL.equals(node.getMetaData(EVENT_TYPE))) {
        body.addStatement(getFactoryMethod(getNodeId(node), METHOD_ACTION, buildAction((String) node.getMetaData(REF), (String) node.getMetaData(VARIABLE), (String) node.getMetaData(MAPPING_VARIABLE_INPUT), (String) node.getMetaData(CUSTOM_SCOPE))));
    }
    addNodeMappings(node, body, getNodeId(node));
    visitMetaData(node.getMetaData(), body, getNodeId(node));
    body.addStatement(getDoneMethod(getNodeId(node)));
}
Also used : BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) EndNodeFactory(org.jbpm.ruleflow.core.factory.EndNodeFactory) LongLiteralExpr(com.github.javaparser.ast.expr.LongLiteralExpr) ProcessInstanceCompensationAction(org.jbpm.process.instance.impl.actions.ProcessInstanceCompensationAction)

Example 2 with ProcessInstanceCompensationAction

use of org.jbpm.process.instance.impl.actions.ProcessInstanceCompensationAction in project kogito-runtimes by kiegroup.

the class ProcessHandler method handleIntermediateOrEndThrowCompensationEvent.

protected void handleIntermediateOrEndThrowCompensationEvent(ExtendedNodeImpl throwEventNode) {
    if (throwEventNode.getMetaData("compensation-activityRef") != null) {
        String activityRef = (String) throwEventNode.getMetaData().remove("compensation-activityRef");
        NodeContainer nodeParent = throwEventNode.getParentContainer();
        if (nodeParent instanceof EventSubProcessNode) {
            boolean compensationEventSubProcess = false;
            List<Trigger> startTriggers = ((EventSubProcessNode) nodeParent).findStartNode().getTriggers();
            CESP_CHECK: for (Trigger trigger : startTriggers) {
                if (trigger instanceof EventTrigger) {
                    for (EventFilter filter : ((EventTrigger) trigger).getEventFilters()) {
                        if (((EventTypeFilter) filter).getType().equals(Metadata.EVENT_TYPE_COMPENSATION)) {
                            compensationEventSubProcess = true;
                            break CESP_CHECK;
                        }
                    }
                }
            }
            if (compensationEventSubProcess) {
                // BPMN2 spec, p. 252, p. 248: intermediate and end compensation event visibility scope
                nodeParent = ((NodeImpl) nodeParent).getParentContainer();
            }
        }
        String parentId;
        if (nodeParent instanceof RuleFlowProcess) {
            parentId = ((RuleFlowProcess) nodeParent).getId();
        } else {
            parentId = (String) ((NodeImpl) nodeParent).getMetaData("UniqueId");
        }
        String compensationEvent;
        if (activityRef.isEmpty()) {
            // general/implicit compensation
            compensationEvent = CompensationScope.IMPLICIT_COMPENSATION_PREFIX + parentId;
        } else {
            // specific compensation
            compensationEvent = activityRef;
        }
        DroolsConsequenceAction compensationAction = new DroolsConsequenceAction("java", "");
        compensationAction.setMetaData("Action", new ProcessInstanceCompensationAction(compensationEvent));
        if (throwEventNode instanceof ActionNode) {
            ((ActionNode) throwEventNode).setAction(compensationAction);
        } else if (throwEventNode instanceof EndNode) {
            List<DroolsAction> actions = new ArrayList<>();
            actions.add(compensationAction);
            ((EndNode) throwEventNode).setActions(EndNode.EVENT_NODE_ENTER, actions);
        }
    }
}
Also used : RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) NodeImpl(org.jbpm.workflow.core.impl.NodeImpl) ExtendedNodeImpl(org.jbpm.workflow.core.impl.ExtendedNodeImpl) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) EventSubProcessNode(org.jbpm.workflow.core.node.EventSubProcessNode) ActionNode(org.jbpm.workflow.core.node.ActionNode) NodeContainer(org.kie.api.definition.process.NodeContainer) EventFilter(org.jbpm.process.core.event.EventFilter) ProcessInstanceCompensationAction(org.jbpm.process.instance.impl.actions.ProcessInstanceCompensationAction) EventTypeFilter(org.jbpm.process.core.event.EventTypeFilter) ConstraintTrigger(org.jbpm.workflow.core.node.ConstraintTrigger) EventTrigger(org.jbpm.workflow.core.node.EventTrigger) Trigger(org.jbpm.workflow.core.node.Trigger) EndNode(org.jbpm.workflow.core.node.EndNode) List(java.util.List) ArrayList(java.util.ArrayList) EventTrigger(org.jbpm.workflow.core.node.EventTrigger)

Example 3 with ProcessInstanceCompensationAction

use of org.jbpm.process.instance.impl.actions.ProcessInstanceCompensationAction in project kogito-runtimes by kiegroup.

the class ActionNodeVisitor method visitNode.

@Override
public void visitNode(String factoryField, ActionNode node, BlockStmt body, VariableScope variableScope, ProcessMetaData metadata) {
    body.addStatement(getAssignedFactoryMethod(factoryField, ActionNodeFactory.class, getNodeId(node), getNodeKey(), new LongLiteralExpr(node.getId()))).addStatement(getNameMethod(node, "Script"));
    Optional<ExpressionSupplier> supplierAction = getAction(node, ExpressionSupplier.class);
    if (isIntermediateCompensation(node)) {
        ProcessInstanceCompensationAction action = (ProcessInstanceCompensationAction) node.getAction().getMetaData(Metadata.ACTION);
        String compensateNode = CompensationScope.IMPLICIT_COMPENSATION_PREFIX + metadata.getProcessId();
        if (action != null) {
            compensateNode = action.getActivityRef();
        }
        LambdaExpr lambda = buildCompensationLambdaExpr(compensateNode);
        body.addStatement(getFactoryMethod(getNodeId(node), METHOD_ACTION, lambda));
    } else if (supplierAction.isPresent()) {
        body.addStatement(getFactoryMethod(getNodeId(node), METHOD_ACTION, supplierAction.get().get(node, metadata)));
    } else if (node.getMetaData(TRIGGER_REF) != null) {
        // if there is trigger defined on end event create TriggerMetaData for it
        body.addStatement(getFactoryMethod(getNodeId(node), METHOD_ACTION, buildProducerAction(node, metadata)));
    } else if (node.getMetaData(REF) != null && EVENT_TYPE_SIGNAL.equals(node.getMetaData(EVENT_TYPE))) {
        body.addStatement(getFactoryMethod(getNodeId(node), METHOD_ACTION, buildAction((String) node.getMetaData(REF), (String) node.getMetaData(VARIABLE), (String) node.getMetaData(MAPPING_VARIABLE_INPUT), (String) node.getMetaData(CUSTOM_SCOPE))));
    } else if (node.getAction() instanceof DroolsConsequenceAction) {
        String consequence = getActionConsequence(node.getAction());
        if (consequence == null || consequence.trim().isEmpty()) {
            throw new IllegalStateException("Action node " + node.getId() + " name " + node.getName() + " has no action defined");
        }
        BlockStmt actionBody = new BlockStmt();
        List<Variable> variables = variableScope.getVariables();
        variables.stream().filter(v -> consequence.contains(v.getName())).map(ActionNodeVisitor::makeAssignment).forEach(actionBody::addStatement);
        BlockStmt blockStmt = StaticJavaParser.parseBlock("{" + consequence + "}");
        blockStmt.getStatements().forEach(actionBody::addStatement);
        LambdaExpr lambda = new LambdaExpr(// (kcontext) ->
        new Parameter(new UnknownType(), KCONTEXT_VAR), actionBody);
        body.addStatement(getFactoryMethod(getNodeId(node), METHOD_ACTION, lambda));
    }
    addNodeMappings(node, body, getNodeId(node));
    visitMetaData(node.getMetaData(), body, getNodeId(node));
    body.addStatement(getDoneMethod(getNodeId(node)));
}
Also used : Variable(org.jbpm.process.core.context.variable.Variable) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ProcessInstanceCompensationAction(org.jbpm.process.instance.impl.actions.ProcessInstanceCompensationAction) UnknownType(com.github.javaparser.ast.type.UnknownType) LongLiteralExpr(com.github.javaparser.ast.expr.LongLiteralExpr) Parameter(com.github.javaparser.ast.body.Parameter)

Aggregations

ProcessInstanceCompensationAction (org.jbpm.process.instance.impl.actions.ProcessInstanceCompensationAction)3 LambdaExpr (com.github.javaparser.ast.expr.LambdaExpr)2 LongLiteralExpr (com.github.javaparser.ast.expr.LongLiteralExpr)2 DroolsConsequenceAction (org.jbpm.workflow.core.impl.DroolsConsequenceAction)2 Parameter (com.github.javaparser.ast.body.Parameter)1 BooleanLiteralExpr (com.github.javaparser.ast.expr.BooleanLiteralExpr)1 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)1 UnknownType (com.github.javaparser.ast.type.UnknownType)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Variable (org.jbpm.process.core.context.variable.Variable)1 EventFilter (org.jbpm.process.core.event.EventFilter)1 EventTypeFilter (org.jbpm.process.core.event.EventTypeFilter)1 RuleFlowProcess (org.jbpm.ruleflow.core.RuleFlowProcess)1 EndNodeFactory (org.jbpm.ruleflow.core.factory.EndNodeFactory)1 ExtendedNodeImpl (org.jbpm.workflow.core.impl.ExtendedNodeImpl)1 NodeImpl (org.jbpm.workflow.core.impl.NodeImpl)1 ActionNode (org.jbpm.workflow.core.node.ActionNode)1 ConstraintTrigger (org.jbpm.workflow.core.node.ConstraintTrigger)1 EndNode (org.jbpm.workflow.core.node.EndNode)1