Search in sources :

Example 1 with KogitoNode

use of org.kie.kogito.internal.process.runtime.KogitoNode in project kogito-runtimes by kiegroup.

the class ProcessHandler method postProcessNodes.

private void postProcessNodes(RuleFlowProcess process, NodeContainer container) {
    List<String> eventSubProcessHandlers = new ArrayList<String>();
    for (Node node : container.getNodes()) {
        if (node instanceof StateNode) {
            StateNode stateNode = (StateNode) node;
            String condition = (String) stateNode.getMetaData("Condition");
            Constraint constraint = new ConstraintImpl();
            constraint.setConstraint(condition);
            constraint.setType("rule");
            for (org.kie.api.definition.process.Connection connection : stateNode.getDefaultOutgoingConnections()) {
                stateNode.setConstraint(connection, constraint);
            }
        } else if (node instanceof NodeContainer) {
            // prepare event sub process
            if (node instanceof EventSubProcessNode) {
                EventSubProcessNode eventSubProcessNode = (EventSubProcessNode) node;
                Node[] nodes = eventSubProcessNode.getNodes();
                for (Node subNode : nodes) {
                    // avoids cyclomatic complexity
                    if (subNode == null || !(subNode instanceof StartNode)) {
                        continue;
                    }
                    List<Trigger> triggers = ((StartNode) subNode).getTriggers();
                    if (triggers == null) {
                        continue;
                    }
                    for (Trigger trigger : triggers) {
                        if (trigger instanceof EventTrigger) {
                            final List<EventFilter> filters = ((EventTrigger) trigger).getEventFilters();
                            for (EventFilter filter : filters) {
                                if (filter instanceof EventTypeFilter) {
                                    eventSubProcessNode.addEvent((EventTypeFilter) filter);
                                    String type = ((EventTypeFilter) filter).getType();
                                    if (type.startsWith("Error-") || type.startsWith("Escalation")) {
                                        String faultCode = (String) subNode.getMetaData().get("FaultCode");
                                        String replaceRegExp = "Error-|Escalation-";
                                        final String signalType = type;
                                        ExceptionScope exceptionScope = (ExceptionScope) ((ContextContainer) eventSubProcessNode.getParentContainer()).getDefaultContext(ExceptionScope.EXCEPTION_SCOPE);
                                        if (exceptionScope == null) {
                                            exceptionScope = new ExceptionScope();
                                            ((ContextContainer) eventSubProcessNode.getParentContainer()).addContext(exceptionScope);
                                            ((ContextContainer) eventSubProcessNode.getParentContainer()).setDefaultContext(exceptionScope);
                                        }
                                        String faultVariable = null;
                                        if (trigger.getInAssociations() != null && !trigger.getInAssociations().isEmpty()) {
                                            faultVariable = findVariable(trigger.getInAssociations().get(0).getTarget().getLabel(), process.getVariableScope());
                                        }
                                        ActionExceptionHandler exceptionHandler = new ActionExceptionHandler();
                                        DroolsConsequenceAction action = new DroolsConsequenceAction("java", "");
                                        action.setMetaData("Action", new SignalProcessInstanceAction(signalType, faultVariable, null, SignalProcessInstanceAction.PROCESS_INSTANCE_SCOPE));
                                        exceptionHandler.setAction(action);
                                        exceptionHandler.setFaultVariable(faultVariable);
                                        if (faultCode != null) {
                                            String trimmedType = type.replaceFirst(replaceRegExp, "");
                                            exceptionScope.setExceptionHandler(trimmedType, exceptionHandler);
                                            eventSubProcessHandlers.add(trimmedType);
                                        } else {
                                            exceptionScope.setExceptionHandler(faultCode, exceptionHandler);
                                        }
                                    } else if (type.equals("Compensation")) {
                                        // 1. Find the parent sub-process to this event sub-process
                                        NodeContainer parentSubProcess = null;
                                        NodeContainer subProcess = eventSubProcessNode.getParentContainer();
                                        Object isForCompensationObj = eventSubProcessNode.getMetaData("isForCompensation");
                                        if (isForCompensationObj == null) {
                                            eventSubProcessNode.setMetaData("isForCompensation", true);
                                            logger.warn("Overriding empty value of \"isForCompensation\" attribute on Event Sub-Process [{}] and setting it to true.", eventSubProcessNode.getMetaData("UniqueId"));
                                        }
                                        String compensationHandlerId = "";
                                        if (subProcess instanceof RuleFlowProcess) {
                                            // ..how do you expect to signal compensation on the completed process (instance)?!?
                                            throw new ProcessParsingValidationException("Compensation Event Sub-Processes at the process level are not supported.");
                                        }
                                        if (subProcess instanceof Node) {
                                            parentSubProcess = ((KogitoNode) subProcess).getParentContainer();
                                            compensationHandlerId = (String) ((CompositeNode) subProcess).getMetaData(Metadata.UNIQUE_ID);
                                        }
                                        // 2. The event filter (never fires, purely for dumping purposes) has already been added
                                        // 3. Add compensation scope
                                        addCompensationScope(process, eventSubProcessNode, parentSubProcess, compensationHandlerId);
                                    }
                                }
                            }
                        } else if (trigger instanceof ConstraintTrigger) {
                            ConstraintTrigger constraintTrigger = (ConstraintTrigger) trigger;
                            if (constraintTrigger.getConstraint() != null) {
                                String processId = ((RuleFlowProcess) container).getId();
                                String type = "RuleFlowStateEventSubProcess-Event-" + processId + "-" + eventSubProcessNode.getUniqueId();
                                EventTypeFilter eventTypeFilter = new EventTypeFilter();
                                eventTypeFilter.setType(type);
                                eventSubProcessNode.addEvent(eventTypeFilter);
                            }
                        }
                    }
                }
            // for( Node subNode : nodes)
            }
            postProcessNodes(process, (NodeContainer) node);
        } else if (node instanceof EndNode) {
            handleIntermediateOrEndThrowCompensationEvent((EndNode) node);
        } else if (node instanceof ActionNode) {
            handleIntermediateOrEndThrowCompensationEvent((ActionNode) node);
        } else if (node instanceof EventNode) {
            final EventNode eventNode = (EventNode) node;
            if (!(eventNode instanceof BoundaryEventNode) && eventNode.getDefaultIncomingConnections().size() == 0) {
                throw new ProcessParsingValidationException("Event node '" + node.getName() + "' [" + node.getId() + "] has no incoming connection");
            }
        }
    }
    // process fault node to disable termnate parent if there is event subprocess handler
    for (Node node : container.getNodes()) {
        if (node instanceof FaultNode) {
            FaultNode faultNode = (FaultNode) node;
            if (eventSubProcessHandlers.contains(faultNode.getFaultName())) {
                faultNode.setTerminateParent(false);
            }
        }
    }
}
Also used : RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) Constraint(org.jbpm.workflow.core.Constraint) EventSubProcessNode(org.jbpm.workflow.core.node.EventSubProcessNode) HumanTaskNode(org.jbpm.workflow.core.node.HumanTaskNode) BoundaryEventNode(org.jbpm.workflow.core.node.BoundaryEventNode) StartNode(org.jbpm.workflow.core.node.StartNode) CompositeNode(org.jbpm.workflow.core.node.CompositeNode) StateNode(org.jbpm.workflow.core.node.StateNode) RuleSetNode(org.jbpm.workflow.core.node.RuleSetNode) SubProcessNode(org.jbpm.workflow.core.node.SubProcessNode) CompositeContextNode(org.jbpm.workflow.core.node.CompositeContextNode) KogitoNode(org.kie.kogito.internal.process.runtime.KogitoNode) StateBasedNode(org.jbpm.workflow.core.node.StateBasedNode) EventSubProcessNode(org.jbpm.workflow.core.node.EventSubProcessNode) FaultNode(org.jbpm.workflow.core.node.FaultNode) WorkItemNode(org.jbpm.workflow.core.node.WorkItemNode) ActionNode(org.jbpm.workflow.core.node.ActionNode) EndNode(org.jbpm.workflow.core.node.EndNode) EventNode(org.jbpm.workflow.core.node.EventNode) Node(org.kie.api.definition.process.Node) ArrayList(java.util.ArrayList) StateNode(org.jbpm.workflow.core.node.StateNode) ActionNode(org.jbpm.workflow.core.node.ActionNode) NodeContainer(org.kie.api.definition.process.NodeContainer) ExceptionScope(org.jbpm.process.core.context.exception.ExceptionScope) EventTypeFilter(org.jbpm.process.core.event.EventTypeFilter) BoundaryEventNode(org.jbpm.workflow.core.node.BoundaryEventNode) EventNode(org.jbpm.workflow.core.node.EventNode) ConstraintTrigger(org.jbpm.workflow.core.node.ConstraintTrigger) EventTrigger(org.jbpm.workflow.core.node.EventTrigger) Trigger(org.jbpm.workflow.core.node.Trigger) ConstraintImpl(org.jbpm.workflow.core.impl.ConstraintImpl) List(java.util.List) ArrayList(java.util.ArrayList) EventTrigger(org.jbpm.workflow.core.node.EventTrigger) ConstraintTrigger(org.jbpm.workflow.core.node.ConstraintTrigger) StartNode(org.jbpm.workflow.core.node.StartNode) SignalProcessInstanceAction(org.jbpm.process.instance.impl.actions.SignalProcessInstanceAction) BoundaryEventNode(org.jbpm.workflow.core.node.BoundaryEventNode) EventFilter(org.jbpm.process.core.event.EventFilter) ActionExceptionHandler(org.jbpm.process.core.context.exception.ActionExceptionHandler) FaultNode(org.jbpm.workflow.core.node.FaultNode) ContextContainer(org.jbpm.process.core.ContextContainer) CompositeNode(org.jbpm.workflow.core.node.CompositeNode) EndNode(org.jbpm.workflow.core.node.EndNode)

Example 2 with KogitoNode

use of org.kie.kogito.internal.process.runtime.KogitoNode in project kogito-runtimes by kiegroup.

the class XmlBPMNProcessDumper method visitNodesDi.

private void visitNodesDi(org.kie.api.definition.process.Node[] nodes, StringBuilder xmlDump) {
    for (org.kie.api.definition.process.Node node : nodes) {
        Integer x = (Integer) node.getMetaData().get("x");
        Integer y = (Integer) node.getMetaData().get("y");
        Integer width = (Integer) node.getMetaData().get("width");
        Integer height = (Integer) node.getMetaData().get("height");
        if (x == null) {
            x = 0;
        }
        if (y == null) {
            y = 0;
        }
        if (width == null) {
            width = 48;
        }
        if (height == null) {
            height = 48;
        }
        if (node instanceof StartNode || node instanceof EndNode || node instanceof EventNode || node instanceof FaultNode) {
            int offsetX = (int) ((width - 48) / 2);
            width = 48;
            x = x + offsetX;
            int offsetY = (int) ((height - 48) / 2);
            y = y + offsetY;
            height = 48;
        } else if (node instanceof Join || node instanceof Split) {
            int offsetX = (int) ((width - 48) / 2);
            width = 48;
            x = x + offsetX;
            int offsetY = (int) ((height - 48) / 2);
            y = y + offsetY;
            height = 48;
        }
        int parentOffsetX = 0;
        int parentOffsetY = 0;
        NodeContainer nodeContainer = ((KogitoNode) node).getParentContainer();
        while (nodeContainer instanceof CompositeNode) {
            CompositeNode parent = (CompositeNode) nodeContainer;
            Integer parentX = (Integer) parent.getMetaData().get("x");
            if (parentX != null) {
                parentOffsetX += parentX;
            }
            Integer parentY = (Integer) parent.getMetaData().get("y");
            if (parentY != null) {
                parentOffsetY += (Integer) parent.getMetaData().get("y");
            }
            nodeContainer = parent.getParentContainer();
        }
        x += parentOffsetX;
        y += parentOffsetY;
        xmlDump.append("      <bpmndi:BPMNShape bpmnElement=\"" + getUniqueNodeId(node) + "\" >" + EOL + "        <dc:Bounds x=\"" + x + "\" " + "y=\"" + y + "\" " + "width=\"" + width + "\" " + "height=\"" + height + "\" />" + EOL + "      </bpmndi:BPMNShape>" + EOL);
        if (node instanceof CompositeNode) {
            visitNodesDi(((CompositeNode) node).getNodes(), xmlDump);
        }
    }
}
Also used : StartNode(org.jbpm.workflow.core.node.StartNode) KogitoNode(org.kie.kogito.internal.process.runtime.KogitoNode) Join(org.jbpm.workflow.core.node.Join) NodeContainer(org.kie.api.definition.process.NodeContainer) Constraint(org.jbpm.workflow.core.Constraint) FaultNode(org.jbpm.workflow.core.node.FaultNode) EventNode(org.jbpm.workflow.core.node.EventNode) CompositeNode(org.jbpm.workflow.core.node.CompositeNode) EndNode(org.jbpm.workflow.core.node.EndNode) Split(org.jbpm.workflow.core.node.Split)

Example 3 with KogitoNode

use of org.kie.kogito.internal.process.runtime.KogitoNode in project kogito-runtimes by kiegroup.

the class HumanTaskServiceImpl method transition.

@Override
public ExtendedDataContext transition(LocalId id, DataContext dataContext) {
    ExtendedDataContext edc = dataContext.as(ExtendedDataContext.class);
    TaskMetaDataContext mdc = edc.meta().as(TaskMetaDataContext.class);
    SecurityPolicy securityPolicy = convertPolicyObject(mdc.policy());
    String phase = mdc.phase();
    Objects.requireNonNull(phase, "Phase must be specified");
    TaskInstanceId taskInstanceId = ProcessIdParser.select(id, TaskInstanceId.class);
    // must validate the task id
    TaskId taskId = taskInstanceId.taskId();
    ProcessInstanceId instanceId = taskId.processInstanceId();
    Process<MappableToModel<Model>> process = parseProcess(instanceId.processId());
    Collection<KogitoNode> tasks = process.findNodes(n -> n instanceof HumanTaskNode && ((HumanTaskNode) n).getWork().getParameter("TaskName").equals(taskId.taskId()));
    if (tasks.isEmpty()) {
        throw new IllegalArgumentException("No such taskId " + taskId.taskId());
    }
    String taskInstanceIdString = taskInstanceId.taskInstanceId();
    String processInstanceIdString = instanceId.processInstanceId();
    Map<String, Object> map = dataContext.as(MapDataContext.class).toMap();
    MappableToModel<Model> model = process.createModel();
    model.fromMap(map);
    Model result = svc.taskTransition(process, processInstanceIdString, taskInstanceIdString, phase, securityPolicy, model).orElseThrow();
    return ExtendedDataContext.ofData(MapDataContext.of(result.toMap()));
}
Also used : TaskMetaDataContext(org.kie.kogito.incubation.processes.services.contexts.TaskMetaDataContext) KogitoNode(org.kie.kogito.internal.process.runtime.KogitoNode) MappableToModel(org.kie.kogito.MappableToModel) SecurityPolicy(org.kie.kogito.auth.SecurityPolicy) MappableToModel(org.kie.kogito.MappableToModel) Model(org.kie.kogito.Model) HumanTaskNode(org.jbpm.workflow.core.node.HumanTaskNode)

Example 4 with KogitoNode

use of org.kie.kogito.internal.process.runtime.KogitoNode in project kogito-runtimes by kiegroup.

the class ProcessHandler method linkAssociations.

public static void linkAssociations(Definitions definitions, NodeContainer nodeContainer, List<Association> associations) {
    if (associations != null) {
        for (Association association : associations) {
            String sourceRef = association.getSourceRef();
            Object source = null;
            try {
                source = findNodeOrDataStoreByUniqueId(definitions, nodeContainer, sourceRef, "Could not find source [" + sourceRef + "] for association " + association.getId() + "]");
            } catch (IllegalArgumentException e) {
            // source not found
            }
            String targetRef = association.getTargetRef();
            Object target = null;
            try {
                target = findNodeOrDataStoreByUniqueId(definitions, nodeContainer, targetRef, "Could not find target [" + targetRef + "] for association [" + association.getId() + "]");
            } catch (IllegalArgumentException e) {
            // target not found
            }
            if (source == null || target == null) {
            // TODO: ignoring this association for now
            } else if (target instanceof DataStore || source instanceof DataStore) {
            // TODO: ignoring data store associations for now
            } else if (source instanceof EventNode) {
                EventNode sourceNode = (EventNode) source;
                KogitoNode targetNode = (KogitoNode) target;
                checkBoundaryEventCompensationHandler(association, sourceNode, targetNode);
                // make sure IsForCompensation is set to true on target
                NodeImpl targetNodeImpl = (NodeImpl) target;
                String isForCompensation = "isForCompensation";
                Object compensationObject = targetNodeImpl.getMetaData(isForCompensation);
                if (compensationObject == null) {
                    targetNodeImpl.setMetaData(isForCompensation, true);
                    logger.warn("Setting {} attribute to true for node {}", isForCompensation, targetRef);
                } else if (!Boolean.parseBoolean(compensationObject.toString())) {
                    throw new ProcessParsingValidationException(isForCompensation + " attribute [" + compensationObject + "] should be true for Compensation Activity [" + targetRef + "]");
                }
                // put Compensation Handler in CompensationHandlerNode
                NodeContainer sourceParent = sourceNode.getParentContainer();
                NodeContainer targetParent = targetNode.getParentContainer();
                if (!sourceParent.equals(targetParent)) {
                    throw new ProcessParsingValidationException("Compensation Associations may not cross (sub-)process boundaries,");
                }
                // connect boundary event to compensation activity
                ConnectionImpl connection = new ConnectionImpl(sourceNode, NodeImpl.CONNECTION_DEFAULT_TYPE, targetNode, NodeImpl.CONNECTION_DEFAULT_TYPE);
                connection.setMetaData("UniqueId", null);
                connection.setMetaData("hidden", true);
                connection.setMetaData("association", true);
            // Compensation use cases:
            // - boundary event --associated-> activity
            // - implicit sub process compensation handler + recursive?
            /**
             * BPMN2 spec, p.442:
             * "A Compensation Event Sub-process becomes enabled when its parent Activity transitions into state
             * Completed. At that time, a snapshot of the data associated with the parent Acitivity is taken and kept for
             * later usage by the Compensation Event Sub-Process."
             */
            }
        }
    }
}
Also used : BoundaryEventNode(org.jbpm.workflow.core.node.BoundaryEventNode) EventNode(org.jbpm.workflow.core.node.EventNode) Association(org.jbpm.bpmn2.core.Association) NodeImpl(org.jbpm.workflow.core.impl.NodeImpl) ExtendedNodeImpl(org.jbpm.workflow.core.impl.ExtendedNodeImpl) DataStore(org.jbpm.bpmn2.core.DataStore) KogitoNode(org.kie.kogito.internal.process.runtime.KogitoNode) ConnectionImpl(org.jbpm.workflow.core.impl.ConnectionImpl) NodeContainer(org.kie.api.definition.process.NodeContainer)

Example 5 with KogitoNode

use of org.kie.kogito.internal.process.runtime.KogitoNode in project kogito-runtimes by kiegroup.

the class XmlBPMNProcessDumper method getUniqueNodeId.

public static String getUniqueNodeId(org.kie.api.definition.process.Node node) {
    String result = (String) node.getMetaData().get("UniqueId");
    if (result != null) {
        return result;
    }
    result = node.getId() + "";
    NodeContainer nodeContainer = ((KogitoNode) node).getParentContainer();
    while (nodeContainer instanceof CompositeNode) {
        CompositeNode composite = (CompositeNode) nodeContainer;
        result = composite.getId() + "-" + result;
        nodeContainer = composite.getParentContainer();
    }
    return "_" + result;
}
Also used : CompositeNode(org.jbpm.workflow.core.node.CompositeNode) KogitoNode(org.kie.kogito.internal.process.runtime.KogitoNode) NodeContainer(org.kie.api.definition.process.NodeContainer)

Aggregations

KogitoNode (org.kie.kogito.internal.process.runtime.KogitoNode)8 NodeContainer (org.kie.api.definition.process.NodeContainer)7 CompositeNode (org.jbpm.workflow.core.node.CompositeNode)5 EventNode (org.jbpm.workflow.core.node.EventNode)4 Node (org.kie.api.definition.process.Node)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ContextContainer (org.jbpm.process.core.ContextContainer)2 ActionExceptionHandler (org.jbpm.process.core.context.exception.ActionExceptionHandler)2 ExceptionScope (org.jbpm.process.core.context.exception.ExceptionScope)2 EventFilter (org.jbpm.process.core.event.EventFilter)2 EventTypeFilter (org.jbpm.process.core.event.EventTypeFilter)2 SignalProcessInstanceAction (org.jbpm.process.instance.impl.actions.SignalProcessInstanceAction)2 Constraint (org.jbpm.workflow.core.Constraint)2 EndNode (org.jbpm.workflow.core.node.EndNode)2 FaultNode (org.jbpm.workflow.core.node.FaultNode)2 StartNode (org.jbpm.workflow.core.node.StartNode)2 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1