Search in sources :

Example 66 with RuleFlowProcess

use of org.jbpm.ruleflow.core.RuleFlowProcess in project jbpm by kiegroup.

the class ProcessRuntimeImpl method initProcessEventListener.

private void initProcessEventListener(Process process) {
    if (process instanceof RuleFlowProcess) {
        for (Node node : ((RuleFlowProcess) process).getNodes()) {
            if (node instanceof StartNode) {
                StartNode startNode = (StartNode) node;
                if (startNode != null) {
                    List<Trigger> triggers = startNode.getTriggers();
                    if (triggers != null) {
                        for (Trigger trigger : triggers) {
                            if (trigger instanceof EventTrigger) {
                                final List<EventFilter> filters = ((EventTrigger) trigger).getEventFilters();
                                String type = null;
                                for (EventFilter filter : filters) {
                                    if (filter instanceof EventTypeFilter) {
                                        type = ((EventTypeFilter) filter).getType();
                                    }
                                }
                                StartProcessEventListener listener = new StartProcessEventListener(process.getId(), filters, trigger.getInMappings(), startNode.getEventTransformer());
                                signalManager.addEventListener(type, listener);
                                ((RuleFlowProcess) process).getRuntimeMetaData().put("StartProcessEventType", type);
                                ((RuleFlowProcess) process).getRuntimeMetaData().put("StartProcessEventListener", listener);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) StartNode(org.jbpm.workflow.core.node.StartNode) EventTypeFilter(org.jbpm.process.core.event.EventTypeFilter) EventTrigger(org.jbpm.workflow.core.node.EventTrigger) Trigger(org.jbpm.workflow.core.node.Trigger) StartNode(org.jbpm.workflow.core.node.StartNode) Node(org.kie.api.definition.process.Node) EventFilter(org.jbpm.process.core.event.EventFilter) EventTrigger(org.jbpm.workflow.core.node.EventTrigger)

Example 67 with RuleFlowProcess

use of org.jbpm.ruleflow.core.RuleFlowProcess in project jbpm by kiegroup.

the class JbpmBpmn2TestCase method buildAndDumpBPMN2Process.

// Important to test this since persistence relies on this
protected List<Resource> buildAndDumpBPMN2Process(String process) throws SAXException, IOException {
    KnowledgeBuilderConfiguration conf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
    ((KnowledgeBuilderConfigurationImpl) conf).initSemanticModules();
    ((KnowledgeBuilderConfigurationImpl) conf).addSemanticModule(new BPMNSemanticModule());
    ((KnowledgeBuilderConfigurationImpl) conf).addSemanticModule(new BPMNDISemanticModule());
    ((KnowledgeBuilderConfigurationImpl) conf).addSemanticModule(new BPMNExtensionsSemanticModule());
    Resource classpathResource = ResourceFactory.newClassPathResource(process);
    // Dump and reread
    XmlProcessReader processReader = new XmlProcessReader(((KnowledgeBuilderConfigurationImpl) conf).getSemanticModules(), getClass().getClassLoader());
    List<Process> processes = processReader.read(this.getClass().getResourceAsStream("/" + process));
    List<Resource> resources = new ArrayList<Resource>();
    for (Process p : processes) {
        RuleFlowProcess ruleFlowProcess = (RuleFlowProcess) p;
        String dumpedString = XmlBPMNProcessDumper.INSTANCE.dump(ruleFlowProcess);
        Resource resource = ResourceFactory.newReaderResource(new StringReader(dumpedString));
        resource.setSourcePath(classpathResource.getSourcePath());
        resource.setTargetPath(classpathResource.getTargetPath());
        resources.add(resource);
    }
    return resources;
}
Also used : RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) XmlProcessReader(org.jbpm.compiler.xml.XmlProcessReader) Resource(org.kie.api.io.Resource) ArrayList(java.util.ArrayList) Process(org.kie.api.definition.process.Process) RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) BPMNExtensionsSemanticModule(org.jbpm.bpmn2.xml.BPMNExtensionsSemanticModule) BPMNDISemanticModule(org.jbpm.bpmn2.xml.BPMNDISemanticModule) BPMNSemanticModule(org.jbpm.bpmn2.xml.BPMNSemanticModule) StringReader(java.io.StringReader) KnowledgeBuilderConfiguration(org.kie.internal.builder.KnowledgeBuilderConfiguration)

Example 68 with RuleFlowProcess

use of org.jbpm.ruleflow.core.RuleFlowProcess in project jbpm by kiegroup.

the class ProcessFactoryTest method testCompositeNode.

@Test
public void testCompositeNode() throws Exception {
    RuleFlowProcessFactory factory = RuleFlowProcessFactory.createProcess("org.jbpm.process");
    factory.name("My process").packageName("org.jbpm").startNode(1).name("Start").done().compositeNode(2).name("SubProcess").startNode(1).name("SubProcess Start").done().actionNode(2).name("SubProcess Action").action("java", "System.out.println(\"SubProcess Action\");").done().endNode(3).name("SubProcess End").terminate(true).done().connection(1, 2).connection(2, 3).done().endNode(3).name("End").done().connection(1, 2).connection(2, 3);
    RuleFlowProcess process = factory.validate().getProcess();
    assertEquals("SubProcess", process.getNode(2).getName());
    Resource res = ResourceFactory.newByteArrayResource(XmlBPMNProcessDumper.INSTANCE.dump(process).getBytes());
    // source path or target path must be set to be added into kbase
    res.setSourcePath("/tmp/processFactory.bpmn2");
    KieBase kbase = createKnowledgeBaseFromResources(res);
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    ProcessInstance pi = ksession.startProcess("org.jbpm.process");
    assertEquals(ProcessInstance.STATE_COMPLETED, pi.getState());
    ksession.dispose();
}
Also used : RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) KieBase(org.kie.api.KieBase) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) Resource(org.kie.api.io.Resource) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) RuleFlowProcessFactory(org.jbpm.ruleflow.core.RuleFlowProcessFactory) Test(org.junit.Test)

Example 69 with RuleFlowProcess

use of org.jbpm.ruleflow.core.RuleFlowProcess in project jbpm by kiegroup.

the class ProcessFactoryTest method testActionNodeIsDroolsAction.

@Test(timeout = 10000)
public void testActionNodeIsDroolsAction() throws Exception {
    RuleFlowProcessFactory factory = RuleFlowProcessFactory.createProcess("org.jbpm.process");
    factory.name("ActionNodeActionProcess").version("1").startNode(1).name("Start").done().endNode(3).name("End").done().actionNode(2).name("printTextActionNode").action("java", "System.out.println(\"test print\");", true).done().connection(1, 2).connection(2, 3);
    RuleFlowProcess process = factory.validate().getProcess();
    assertNotNull(process);
    Resource res = ResourceFactory.newByteArrayResource(XmlBPMNProcessDumper.INSTANCE.dump(process).getBytes());
    res.setSourcePath("/tmp/processFactory.bpmn2");
    KieBase kbase = createKnowledgeBaseFromResources(res);
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    ProcessInstance pi = ksession.startProcess("org.jbpm.process");
    assertNotNull(pi);
    assertEquals(ProcessInstance.STATE_COMPLETED, pi.getState());
    ksession.dispose();
}
Also used : RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) KieBase(org.kie.api.KieBase) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) Resource(org.kie.api.io.Resource) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) RuleFlowProcessFactory(org.jbpm.ruleflow.core.RuleFlowProcessFactory) Test(org.junit.Test)

Example 70 with RuleFlowProcess

use of org.jbpm.ruleflow.core.RuleFlowProcess in project jbpm 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.getNodeContainer()).getDefaultContext(ExceptionScope.EXCEPTION_SCOPE);
                                        if (exceptionScope == null) {
                                            exceptionScope = new ExceptionScope();
                                            ((ContextContainer) eventSubProcessNode.getNodeContainer()).addContext(exceptionScope);
                                            ((ContextContainer) eventSubProcessNode.getNodeContainer()).setDefaultContext(exceptionScope);
                                        }
                                        String faultVariable = null;
                                        if (trigger.getInAssociations() != null && !trigger.getInAssociations().isEmpty()) {
                                            faultVariable = trigger.getInAssociations().get(0).getTarget();
                                        }
                                        ActionExceptionHandler exceptionHandler = new ActionExceptionHandler();
                                        DroolsConsequenceAction action = new DroolsConsequenceAction("java", PROCESS_INSTANCE_SIGNAL_EVENT + signalType + "\", " + (faultVariable == null ? "null" : "kcontext.getVariable(\"" + faultVariable + "\")") + ");");
                                        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;
                                        NodeContainer subProcess = eventSubProcessNode.getNodeContainer();
                                        Object isForCompensationObj = eventSubProcessNode.getMetaData("isForCompensation");
                                        if (isForCompensationObj == null) {
                                            eventSubProcessNode.setMetaData("isForCompensation", true);
                                            logger.warn("Overriding empty or false value of \"isForCompensation\" attribute on Event Sub-Process [" + eventSubProcessNode.getMetaData("UniqueId") + "] and setting it to true.");
                                        }
                                        if (subProcess instanceof RuleFlowProcess) {
                                            // ..how do you expect to signal compensation on the completed process (instance)?!?
                                            throw new IllegalArgumentException("Compensation Event Sub-Processes at the process level are not supported.");
                                        }
                                        parentSubProcess = ((Node) subProcess).getNodeContainer();
                                        // 2. The event filter (never fires, purely for dumping purposes) has already been added
                                        // 3. Add compensation scope
                                        String compensationHandlerId = (String) ((CompositeNode) subProcess).getMetaData("UniqueId");
                                        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 IllegalArgumentException("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) 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) 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) EndNode(org.jbpm.workflow.core.node.EndNode)

Aggregations

RuleFlowProcess (org.jbpm.ruleflow.core.RuleFlowProcess)85 Test (org.junit.Test)47 ArrayList (java.util.ArrayList)38 StartNode (org.jbpm.workflow.core.node.StartNode)38 EndNode (org.jbpm.workflow.core.node.EndNode)37 ActionNode (org.jbpm.workflow.core.node.ActionNode)33 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)32 DroolsConsequenceAction (org.jbpm.workflow.core.impl.DroolsConsequenceAction)24 KieSession (org.kie.api.runtime.KieSession)20 DroolsAction (org.jbpm.workflow.core.DroolsAction)19 ConnectionImpl (org.jbpm.workflow.core.impl.ConnectionImpl)19 WorkItemNode (org.jbpm.workflow.core.node.WorkItemNode)19 EventTypeFilter (org.jbpm.process.core.event.EventTypeFilter)17 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)17 Variable (org.jbpm.process.core.context.variable.Variable)16 ObjectDataType (org.jbpm.process.core.datatype.impl.type.ObjectDataType)16 Action (org.jbpm.process.instance.impl.Action)16 CompositeNode (org.jbpm.workflow.core.node.CompositeNode)16 ProcessContext (org.kie.api.runtime.process.ProcessContext)16 KieBase (org.kie.api.KieBase)15