Search in sources :

Example 31 with DroolsConsequenceAction

use of org.jbpm.workflow.core.impl.DroolsConsequenceAction in project jbpm by kiegroup.

the class EventTest method testEvent2.

@Test
public void testEvent2() {
    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);
    MilestoneNode milestoneNode = new MilestoneNode();
    milestoneNode.setName("Milestone");
    milestoneNode.setConstraint("eval(false)");
    milestoneNode.setId(2);
    process.addNode(milestoneNode);
    new ConnectionImpl(startNode, Node.CONNECTION_DEFAULT_TYPE, milestoneNode, Node.CONNECTION_DEFAULT_TYPE);
    EndNode endNode = new EndNode();
    endNode.setName("EndNode");
    endNode.setId(3);
    process.addNode(endNode);
    new ConnectionImpl(milestoneNode, Node.CONNECTION_DEFAULT_TYPE, endNode, Node.CONNECTION_DEFAULT_TYPE);
    EventNode eventNode = new EventNode();
    EventTypeFilter eventFilter = new EventTypeFilter();
    eventFilter.setType("myEvent");
    eventNode.addEventFilter(eventFilter);
    eventNode.setVariableName("event");
    eventNode.setId(4);
    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(5);
    process.addNode(actionNode);
    new ConnectionImpl(eventNode, Node.CONNECTION_DEFAULT_TYPE, actionNode, Node.CONNECTION_DEFAULT_TYPE);
    EndNode endNode2 = new EndNode();
    endNode2.setName("EndNode");
    endNode2.setTerminate(false);
    endNode2.setId(6);
    process.addNode(endNode2);
    new ConnectionImpl(actionNode, Node.CONNECTION_DEFAULT_TYPE, endNode2, Node.CONNECTION_DEFAULT_TYPE);
    KieSession ksession = createKieSession(process);
    TestProcessEventListener procEventListener = new TestProcessEventListener();
    ksession.addEventListener(procEventListener);
    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());
    Person john = new Person();
    john.setName("John");
    processInstance.signalEvent("myEvent", john);
    assertEquals(2, myList.size());
    verifyEventHistory(test2EventOrder, 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) ObjectDataType(org.jbpm.process.core.datatype.impl.type.ObjectDataType) MilestoneNode(org.jbpm.workflow.core.node.MilestoneNode) 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 32 with DroolsConsequenceAction

use of org.jbpm.workflow.core.impl.DroolsConsequenceAction in project jbpm by kiegroup.

the class EventTest method testEvent5.

@Test
public void testEvent5() {
    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);
    CompositeNode compositeNode = new CompositeNode();
    compositeNode.setName("CompositeNode");
    compositeNode.setId(2);
    process.addNode(compositeNode);
    new ConnectionImpl(startNode, Node.CONNECTION_DEFAULT_TYPE, compositeNode, Node.CONNECTION_DEFAULT_TYPE);
    MilestoneNode milestoneNode = new MilestoneNode();
    milestoneNode.setName("Milestone");
    milestoneNode.setConstraint("eval(false)");
    compositeNode.addNode(milestoneNode);
    compositeNode.linkIncomingConnections(Node.CONNECTION_DEFAULT_TYPE, milestoneNode.getId(), Node.CONNECTION_DEFAULT_TYPE);
    EventNode eventNode = new EventNode();
    EventTypeFilter eventFilter = new EventTypeFilter();
    eventFilter.setType("myEvent");
    eventNode.addEventFilter(eventFilter);
    eventNode.setVariableName("event");
    compositeNode.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);
    compositeNode.addNode(actionNode);
    new ConnectionImpl(eventNode, Node.CONNECTION_DEFAULT_TYPE, actionNode, Node.CONNECTION_DEFAULT_TYPE);
    Join join = new Join();
    join.setName("XOR Join");
    join.setType(Join.TYPE_XOR);
    compositeNode.addNode(join);
    new ConnectionImpl(milestoneNode, Node.CONNECTION_DEFAULT_TYPE, join, Node.CONNECTION_DEFAULT_TYPE);
    new ConnectionImpl(actionNode, Node.CONNECTION_DEFAULT_TYPE, join, Node.CONNECTION_DEFAULT_TYPE);
    compositeNode.linkOutgoingConnections(join.getId(), Node.CONNECTION_DEFAULT_TYPE, Node.CONNECTION_DEFAULT_TYPE);
    EndNode endNode = new EndNode();
    endNode.setName("EndNode");
    endNode.setId(3);
    process.addNode(endNode);
    new ConnectionImpl(compositeNode, Node.CONNECTION_DEFAULT_TYPE, endNode, Node.CONNECTION_DEFAULT_TYPE);
    KieSession ksession = createKieSession(process);
    TestProcessEventListener procEventListener = new TestProcessEventListener();
    ksession.addEventListener(procEventListener);
    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_COMPLETED, processInstance.getState());
    verifyEventHistory(test5EventOrder, procEventListener.getEventHistory());
}
Also used : RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) 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) ObjectDataType(org.jbpm.process.core.datatype.impl.type.ObjectDataType) MilestoneNode(org.jbpm.workflow.core.node.MilestoneNode) ProcessContext(org.kie.api.runtime.process.ProcessContext) EventNode(org.jbpm.workflow.core.node.EventNode) EventTypeFilter(org.jbpm.process.core.event.EventTypeFilter) KieSession(org.kie.api.runtime.KieSession) DroolsAction(org.jbpm.workflow.core.DroolsAction) StartNode(org.jbpm.workflow.core.node.StartNode) Join(org.jbpm.workflow.core.node.Join) CompositeNode(org.jbpm.workflow.core.node.CompositeNode) EndNode(org.jbpm.workflow.core.node.EndNode) 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 33 with DroolsConsequenceAction

use of org.jbpm.workflow.core.impl.DroolsConsequenceAction in project jbpm by kiegroup.

the class ForEachTest method testForEach.

@Test
public void testForEach() {
    RuleFlowProcess process = new RuleFlowProcess();
    process.setId("org.drools.core.process.foreach");
    process.setName("ForEach Process");
    List<Variable> variables = new ArrayList<Variable>();
    Variable variable = new Variable();
    variable.setName("persons");
    ListDataType listDataType = new ListDataType();
    ObjectDataType personDataType = new ObjectDataType();
    personDataType.setClassName("org.jbpm.process.test.Person");
    listDataType.setType(personDataType);
    variable.setType(listDataType);
    variables.add(variable);
    process.getVariableScope().setVariables(variables);
    StartNode startNode = new StartNode();
    startNode.setName("Start");
    startNode.setId(1);
    process.addNode(startNode);
    EndNode endNode = new EndNode();
    endNode.setName("EndNode");
    endNode.setId(2);
    process.addNode(endNode);
    ForEachNode forEachNode = new ForEachNode();
    forEachNode.setName("ForEach");
    forEachNode.setId(3);
    forEachNode.setCollectionExpression("persons");
    personDataType = new ObjectDataType();
    personDataType.setClassName("org.drools.Person");
    process.addNode(forEachNode);
    new ConnectionImpl(startNode, Node.CONNECTION_DEFAULT_TYPE, forEachNode, Node.CONNECTION_DEFAULT_TYPE);
    new ConnectionImpl(forEachNode, Node.CONNECTION_DEFAULT_TYPE, endNode, Node.CONNECTION_DEFAULT_TYPE);
    final List<String> myList = new ArrayList<String>();
    ActionNode actionNode = new ActionNode();
    actionNode.setName("Print child");
    DroolsAction action = new DroolsConsequenceAction("java", null);
    action.setMetaData("Action", new Action() {

        public void execute(ProcessContext context) throws Exception {
            logger.info("Executed action for child {}", ((Person) context.getVariable("child")).getName());
            myList.add("Executed action");
        }
    });
    actionNode.setAction(action);
    forEachNode.addNode(actionNode);
    forEachNode.linkIncomingConnections(Node.CONNECTION_DEFAULT_TYPE, actionNode.getId(), Node.CONNECTION_DEFAULT_TYPE);
    forEachNode.linkOutgoingConnections(actionNode.getId(), Node.CONNECTION_DEFAULT_TYPE, Node.CONNECTION_DEFAULT_TYPE);
    forEachNode.setVariable("child", personDataType);
    KieSession ksession = createKieSession(process);
    Map<String, Object> parameters = new HashMap<String, Object>();
    List<Person> persons = new ArrayList<Person>();
    persons.add(new Person("John Doe"));
    persons.add(new Person("Jane Doe"));
    persons.add(new Person("Jack"));
    parameters.put("persons", persons);
    TestProcessEventListener procEventListener = new TestProcessEventListener();
    ksession.addEventListener(procEventListener);
    ksession.startProcess("org.drools.core.process.foreach", parameters);
    assertEquals(3, myList.size());
    verifyEventHistory(eventOrder, procEventListener.getEventHistory());
}
Also used : RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) 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) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ActionNode(org.jbpm.workflow.core.node.ActionNode) ConnectionImpl(org.jbpm.workflow.core.impl.ConnectionImpl) ObjectDataType(org.jbpm.process.core.datatype.impl.type.ObjectDataType) ProcessContext(org.kie.api.runtime.process.ProcessContext) KieSession(org.kie.api.runtime.KieSession) DroolsAction(org.jbpm.workflow.core.DroolsAction) StartNode(org.jbpm.workflow.core.node.StartNode) ListDataType(org.jbpm.process.core.datatype.impl.type.ListDataType) EndNode(org.jbpm.workflow.core.node.EndNode) ForEachNode(org.jbpm.workflow.core.node.ForEachNode) Person(org.jbpm.process.test.Person) TestProcessEventListener(org.jbpm.process.test.TestProcessEventListener) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 34 with DroolsConsequenceAction

use of org.jbpm.workflow.core.impl.DroolsConsequenceAction in project jbpm by kiegroup.

the class SubProcessTest method testSynchronousSubProcess.

@Test
public void testSynchronousSubProcess() {
    RuleFlowProcess process = new RuleFlowProcess();
    process.setId("org.drools.core.process.process");
    process.setName("Process");
    StartNode startNode = new StartNode();
    startNode.setName("Start");
    startNode.setId(1);
    process.addNode(startNode);
    EndNode endNode = new EndNode();
    endNode.setName("EndNode");
    endNode.setId(2);
    process.addNode(endNode);
    SubProcessNode subProcessNode = new SubProcessNode();
    subProcessNode.setName("SubProcessNode");
    subProcessNode.setId(3);
    subProcessNode.setProcessId("org.drools.core.process.subprocess");
    process.addNode(subProcessNode);
    new ConnectionImpl(startNode, Node.CONNECTION_DEFAULT_TYPE, subProcessNode, Node.CONNECTION_DEFAULT_TYPE);
    new ConnectionImpl(subProcessNode, Node.CONNECTION_DEFAULT_TYPE, endNode, Node.CONNECTION_DEFAULT_TYPE);
    RuleFlowProcess subprocess = new RuleFlowProcess();
    subprocess.setId("org.drools.core.process.subprocess");
    subprocess.setName("SubProcess");
    startNode = new StartNode();
    startNode.setName("Start");
    startNode.setId(1);
    subprocess.addNode(startNode);
    endNode = new EndNode();
    endNode.setName("EndNode");
    endNode.setId(2);
    subprocess.addNode(endNode);
    ActionNode actionNode = new ActionNode();
    actionNode.setName("Action");
    DroolsAction action = new DroolsConsequenceAction("java", null);
    action.setMetaData("Action", new Action() {

        public void execute(ProcessContext context) throws Exception {
            logger.info("Executed action");
            executed = true;
        }
    });
    actionNode.setAction(action);
    actionNode.setId(3);
    subprocess.addNode(actionNode);
    new ConnectionImpl(startNode, Node.CONNECTION_DEFAULT_TYPE, actionNode, Node.CONNECTION_DEFAULT_TYPE);
    new ConnectionImpl(actionNode, Node.CONNECTION_DEFAULT_TYPE, endNode, Node.CONNECTION_DEFAULT_TYPE);
    KieSession ksession = createKieSession(process, subprocess);
    TestProcessEventListener procEventListener = new TestProcessEventListener();
    ksession.addEventListener(procEventListener);
    ksession.startProcess("org.drools.core.process.process");
    assertTrue(executed);
    assertEquals(0, ksession.getProcessInstances().size());
    verifyEventHistory(syncEventorder, 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) Action(org.jbpm.process.instance.impl.Action) DroolsAction(org.jbpm.workflow.core.DroolsAction) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) ActionNode(org.jbpm.workflow.core.node.ActionNode) ConnectionImpl(org.jbpm.workflow.core.impl.ConnectionImpl) ProcessContext(org.kie.api.runtime.process.ProcessContext) EndNode(org.jbpm.workflow.core.node.EndNode) SubProcessNode(org.jbpm.workflow.core.node.SubProcessNode) KieSession(org.kie.api.runtime.KieSession) TestProcessEventListener(org.jbpm.process.test.TestProcessEventListener) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 35 with DroolsConsequenceAction

use of org.jbpm.workflow.core.impl.DroolsConsequenceAction in project jbpm by kiegroup.

the class StoreHandler method end.

public Object end(final String uri, final String localName, final ExtensibleXmlParser xmlPackageReader) throws SAXException {
    final Element element = xmlPackageReader.endElementBuilder();
    ActionNode actionNode = (ActionNode) xmlPackageReader.getCurrent();
    String text = ((org.w3c.dom.Text) element.getChildNodes().item(0)).getWholeText();
    DroolsConsequenceAction actionText = new DroolsConsequenceAction("mvel", "list.add(\"" + text + "\")");
    actionNode.setAction(actionText);
    return actionNode;
}
Also used : DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) Element(org.w3c.dom.Element) ActionNode(org.jbpm.workflow.core.node.ActionNode)

Aggregations

DroolsConsequenceAction (org.jbpm.workflow.core.impl.DroolsConsequenceAction)73 ActionNode (org.jbpm.workflow.core.node.ActionNode)41 DroolsAction (org.jbpm.workflow.core.DroolsAction)40 ArrayList (java.util.ArrayList)32 EndNode (org.jbpm.workflow.core.node.EndNode)28 StartNode (org.jbpm.workflow.core.node.StartNode)25 RuleFlowProcess (org.jbpm.ruleflow.core.RuleFlowProcess)24 Action (org.jbpm.process.instance.impl.Action)21 EventTypeFilter (org.jbpm.process.core.event.EventTypeFilter)18 Test (org.junit.Test)18 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)17 ConnectionImpl (org.jbpm.workflow.core.impl.ConnectionImpl)17 KieSession (org.kie.api.runtime.KieSession)17 ProcessContext (org.kie.api.runtime.process.ProcessContext)17 EventNode (org.jbpm.workflow.core.node.EventNode)16 Timer (org.jbpm.process.core.timer.Timer)11 Variable (org.jbpm.process.core.context.variable.Variable)10 ObjectDataType (org.jbpm.process.core.datatype.impl.type.ObjectDataType)10 TestProcessEventListener (org.jbpm.process.test.TestProcessEventListener)9 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)8