Search in sources :

Example 61 with RuleFlowProcess

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

the class EventTest method testEvent3a.

@Test
public void testEvent3a() {
    RuleFlowProcess process = new RuleFlowProcess();
    process.setId("org.drools.core.process.event");
    process.setName("Event Process");
    List<Variable> variables = new ArrayList<Variable>();
    Variable variable = new Variable();
    variable.setName("event");
    ObjectDataType personDataType = new ObjectDataType();
    personDataType.setClassName("org.drools.Person");
    variable.setType(personDataType);
    variables.add(variable);
    process.getVariableScope().setVariables(variables);
    StartNode startNode = new StartNode();
    startNode.setName("Start");
    startNode.setId(1);
    process.addNode(startNode);
    EventNode eventNode = new EventNode();
    EventTypeFilter eventFilter = new EventTypeFilter();
    eventFilter.setType("myEvent");
    eventNode.addEventFilter(eventFilter);
    eventNode.setVariableName("event");
    eventNode.setId(3);
    process.addNode(eventNode);
    final List<String> myList = new ArrayList<String>();
    ActionNode actionNode = new ActionNode();
    actionNode.setName("Print");
    DroolsAction action = new DroolsConsequenceAction("java", null);
    action.setMetaData("Action", new Action() {

        public void execute(ProcessContext context) throws Exception {
            logger.info("Detected event for person {}", ((Person) context.getVariable("event")).getName());
            myList.add("Executed action");
        }
    });
    actionNode.setAction(action);
    actionNode.setId(4);
    process.addNode(actionNode);
    new ConnectionImpl(eventNode, Node.CONNECTION_DEFAULT_TYPE, actionNode, Node.CONNECTION_DEFAULT_TYPE);
    EventNode eventNode2 = new EventNode();
    eventFilter = new EventTypeFilter();
    eventFilter.setType("myOtherEvent");
    eventNode2.addEventFilter(eventFilter);
    eventNode2.setVariableName("event");
    eventNode2.setId(5);
    process.addNode(eventNode2);
    ActionNode actionNode2 = new ActionNode();
    actionNode2.setName("Print");
    action = new DroolsConsequenceAction("java", null);
    action.setMetaData("Action", new Action() {

        public void execute(ProcessContext context) throws Exception {
            logger.info("Detected other event for person {}", ((Person) context.getVariable("event")).getName());
            myList.add("Executed action");
        }
    });
    actionNode2.setAction(action);
    actionNode2.setId(6);
    process.addNode(actionNode2);
    new ConnectionImpl(eventNode2, Node.CONNECTION_DEFAULT_TYPE, actionNode2, Node.CONNECTION_DEFAULT_TYPE);
    Join join = new Join();
    join.setName("AND Join");
    join.setType(Join.TYPE_AND);
    join.setId(7);
    process.addNode(join);
    new ConnectionImpl(startNode, Node.CONNECTION_DEFAULT_TYPE, join, Node.CONNECTION_DEFAULT_TYPE);
    new ConnectionImpl(actionNode, Node.CONNECTION_DEFAULT_TYPE, join, Node.CONNECTION_DEFAULT_TYPE);
    new ConnectionImpl(actionNode2, Node.CONNECTION_DEFAULT_TYPE, join, Node.CONNECTION_DEFAULT_TYPE);
    EndNode endNode = new EndNode();
    endNode.setName("EndNode");
    endNode.setId(8);
    process.addNode(endNode);
    new ConnectionImpl(join, Node.CONNECTION_DEFAULT_TYPE, endNode, Node.CONNECTION_DEFAULT_TYPE);
    KieSession ksession = createKieSession(process);
    TestProcessEventListener procEventListener = new TestProcessEventListener();
    ksession.addEventListener(procEventListener);
    System.setProperty("jbpm.loop.level.disabled", "true");
    ProcessInstance processInstance = ksession.startProcess("org.drools.core.process.event");
    assertEquals(0, myList.size());
    Person jack = new Person();
    jack.setName("Jack");
    processInstance.signalEvent("myEvent", jack);
    assertEquals(1, myList.size());
    assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
    processInstance.signalEvent("myEvent", jack);
    assertEquals(2, myList.size());
    assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
    Person john = new Person();
    john.setName("John");
    processInstance.signalEvent("myOtherEvent", john);
    assertEquals(3, myList.size());
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
    System.clearProperty("jbpm.loop.level.disabled");
    verifyEventHistory(test3aEventOrder, procEventListener.getEventHistory());
}
Also used : DroolsAction(org.jbpm.workflow.core.DroolsAction) RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) StartNode(org.jbpm.workflow.core.node.StartNode) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) DroolsAction(org.jbpm.workflow.core.DroolsAction) Action(org.jbpm.process.instance.impl.Action) Variable(org.jbpm.process.core.context.variable.Variable) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) ArrayList(java.util.ArrayList) ActionNode(org.jbpm.workflow.core.node.ActionNode) ConnectionImpl(org.jbpm.workflow.core.impl.ConnectionImpl) Join(org.jbpm.workflow.core.node.Join) ObjectDataType(org.jbpm.process.core.datatype.impl.type.ObjectDataType) ProcessContext(org.kie.api.runtime.process.ProcessContext) EventNode(org.jbpm.workflow.core.node.EventNode) EventTypeFilter(org.jbpm.process.core.event.EventTypeFilter) EndNode(org.jbpm.workflow.core.node.EndNode) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) Person(org.jbpm.process.test.Person) TestProcessEventListener(org.jbpm.process.test.TestProcessEventListener) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 62 with RuleFlowProcess

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

the class CompensationEventListener method createNodeInstanceContainers.

private Stack<NodeInstance> createNodeInstanceContainers(Node toCompensateNode, boolean generalCompensation) {
    Stack<NodeContainer> nestedNodes = new Stack<NodeContainer>();
    Stack<NodeInstance> generatedInstances = new Stack<NodeInstance>();
    NodeContainer parentContainer = toCompensateNode.getNodeContainer();
    while (!(parentContainer instanceof RuleFlowProcess)) {
        nestedNodes.add(parentContainer);
        parentContainer = ((Node) parentContainer).getNodeContainer();
    }
    NodeInstanceContainer parentInstance;
    if (nestedNodes.isEmpty()) {
        // nestedNodes is empty
        parentInstance = (NodeInstanceContainer) getProcessInstance();
    } else {
        parentInstance = (NodeInstanceContainer) ((WorkflowProcessInstanceImpl) getProcessInstance()).getNodeInstance((Node) nestedNodes.pop());
        generatedInstances.add((NodeInstance) parentInstance);
    }
    NodeInstanceContainer childInstance = parentInstance;
    while (!nestedNodes.isEmpty()) {
        // generate
        childInstance = (NodeInstanceContainer) parentInstance.getNodeInstance((Node) nestedNodes.pop());
        assert childInstance instanceof CompositeNodeInstance : "A node with child nodes should end up creating a CompositeNodeInstance type.";
        // track and modify
        generatedInstances.add((NodeInstance) childInstance);
        // loop
        parentInstance = (CompositeContextNodeInstance) childInstance;
    }
    if (generalCompensation) {
        childInstance = (NodeInstanceContainer) parentInstance.getNodeInstance(toCompensateNode);
        generatedInstances.add((NodeInstance) childInstance);
    }
    return generatedInstances;
}
Also used : RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) NodeInstanceContainer(org.jbpm.workflow.instance.NodeInstanceContainer) CompositeNodeInstance(org.jbpm.workflow.instance.node.CompositeNodeInstance) NodeContainer(org.kie.api.definition.process.NodeContainer) NodeInstance(org.jbpm.workflow.instance.NodeInstance) CompositeNodeInstance(org.jbpm.workflow.instance.node.CompositeNodeInstance) CompositeContextNodeInstance(org.jbpm.workflow.instance.node.CompositeContextNodeInstance) Stack(java.util.Stack)

Example 63 with RuleFlowProcess

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

the class WorkflowProcessInstanceUpgrader method upgradeProcessInstanceByNodeNames.

/**
 * Do the same as upgradeProcessInstance() but user provides mapping by node names, not by node id's
 * @param kruntime
 * @param activeProcessId
 * @param newProcessId
 * @param nodeNamesMapping
 */
public static void upgradeProcessInstanceByNodeNames(KieRuntime kruntime, Long fromProcessId, String toProcessId, Map<String, String> nodeNamesMapping) {
    Map<String, Long> nodeIdMapping = new HashMap<String, Long>();
    String fromProcessIdString = kruntime.getProcessInstance(fromProcessId).getProcessId();
    Process processFrom = kruntime.getKieBase().getProcess(fromProcessIdString);
    Process processTo = kruntime.getKieBase().getProcess(toProcessId);
    for (Map.Entry<String, String> entry : nodeNamesMapping.entrySet()) {
        String from = null;
        Long to = null;
        if (processFrom instanceof WorkflowProcess) {
            from = getNodeId(((WorkflowProcess) processFrom).getNodes(), entry.getKey(), true);
        } else if (processFrom instanceof RuleFlowProcess) {
            from = getNodeId(((RuleFlowProcess) processFrom).getNodes(), entry.getKey(), true);
        } else if (processFrom != null) {
            throw new IllegalArgumentException("Suported processes are WorkflowProcess and RuleFlowProcess, it was:" + processFrom.getClass());
        } else {
            throw new IllegalArgumentException("Can not find process with id: " + fromProcessIdString);
        }
        if (processTo instanceof WorkflowProcess) {
            to = Long.valueOf(getNodeId(((WorkflowProcess) processTo).getNodes(), entry.getValue(), false));
        } else if (processTo instanceof RuleFlowProcess) {
            to = Long.valueOf(getNodeId(((RuleFlowProcess) processTo).getNodes(), entry.getValue(), false));
        } else if (processTo != null) {
            throw new IllegalArgumentException("Suported processes are WorkflowProcess and RuleFlowProcess, it was:" + processTo.getClass());
        } else {
            throw new IllegalArgumentException("Can not find process with id: " + toProcessId);
        }
        nodeIdMapping.put(from, to);
    }
    upgradeProcessInstance(kruntime, fromProcessId, toProcessId, nodeIdMapping);
}
Also used : RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) HashMap(java.util.HashMap) Process(org.kie.api.definition.process.Process) WorkflowProcess(org.kie.api.definition.process.WorkflowProcess) RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) HashMap(java.util.HashMap) Map(java.util.Map) WorkflowProcess(org.kie.api.definition.process.WorkflowProcess)

Example 64 with RuleFlowProcess

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

the class XmlProcessReader method processParserMessage.

protected String processParserMessage(LinkedList<Object> parents, Attributes attr, String errorMessage) {
    String nodeId = (attr == null || attr.getValue("id") == null) ? "" : attr.getValue("id");
    String nodeName = (attr == null || attr.getValue("name") == null) ? "" : attr.getValue("name");
    for (Object parent : parents) {
        if (parent != null && parent instanceof RuleFlowProcess) {
            RuleFlowProcess process = ((RuleFlowProcess) parent);
            return messageWithProcessInfo.format(new Object[] { process.getId(), process.getPackageName(), process.getName(), process.getVersion(), nodeId, nodeName, errorMessage });
        }
    }
    return message.format(new Object[] { nodeId, nodeName, errorMessage });
}
Also used : RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess)

Example 65 with RuleFlowProcess

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

the class ProcessRuntimeImpl method initStartTimers.

public void initStartTimers() {
    KieBase kbase = kruntime.getKieBase();
    Collection<Process> processes = kbase.getProcesses();
    for (Process process : processes) {
        RuleFlowProcess p = (RuleFlowProcess) process;
        List<StartNode> startNodes = p.getTimerStart();
        if (startNodes != null && !startNodes.isEmpty()) {
            kruntime.queueWorkingMemoryAction(new RegisterStartTimerAction(p.getId(), startNodes, this.timerManager));
        }
    }
}
Also used : RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) StartNode(org.jbpm.workflow.core.node.StartNode) KieBase(org.kie.api.KieBase) Process(org.kie.api.definition.process.Process) RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess)

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