Search in sources :

Example 36 with RuleFlowProcess

use of org.jbpm.ruleflow.core.RuleFlowProcess 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 37 with RuleFlowProcess

use of org.jbpm.ruleflow.core.RuleFlowProcess 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 38 with RuleFlowProcess

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

the class SubProcessTest method testNonExistentSubProcess.

@Test
public void testNonExistentSubProcess() {
    String nonExistentSubProcessName = "nonexistent.process";
    RuleFlowProcess process = new RuleFlowProcess();
    process.setId("org.drools.core.process.process");
    process.setName("Process");
    StartNode startNode = new StartNode();
    startNode.setName("Start");
    startNode.setId(1);
    SubProcessNode subProcessNode = new SubProcessNode();
    subProcessNode.setName("SubProcessNode");
    subProcessNode.setId(2);
    subProcessNode.setProcessId(nonExistentSubProcessName);
    EndNode endNode = new EndNode();
    endNode.setName("End");
    endNode.setId(3);
    connect(startNode, subProcessNode);
    connect(subProcessNode, endNode);
    process.addNode(startNode);
    process.addNode(subProcessNode);
    process.addNode(endNode);
    KieSession ksession = createKieSession(process);
    try {
        ksession.startProcess("org.drools.core.process.process");
        fail("should throw exception");
    } catch (RuntimeException re) {
        assertTrue(re.getMessage().contains(nonExistentSubProcessName));
    }
}
Also used : RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) StartNode(org.jbpm.workflow.core.node.StartNode) EndNode(org.jbpm.workflow.core.node.EndNode) SubProcessNode(org.jbpm.workflow.core.node.SubProcessNode) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 39 with RuleFlowProcess

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

the class WorkItemTest method testReachNonRegisteredWorkItemHandler.

@Test
public void testReachNonRegisteredWorkItemHandler() {
    String processId = "org.drools.actions";
    String workName = "Unnexistent Task";
    RuleFlowProcess process = getWorkItemProcess(processId, workName);
    KieSession ksession = createKieSession(process);
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("UserName", "John Doe");
    parameters.put("Person", new Person("John Doe"));
    ProcessInstance processInstance = null;
    try {
        processInstance = ksession.startProcess("org.drools.actions", parameters);
        Assert.fail("should fail if WorkItemHandler for" + workName + "is not registered");
    } catch (Throwable e) {
    }
    Assert.assertNull(processInstance);
}
Also used : RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) HashMap(java.util.HashMap) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) Person(org.jbpm.process.test.Person) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 40 with RuleFlowProcess

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

the class WorkItemTest method getWorkItemProcess.

private RuleFlowProcess getWorkItemProcess(String processId, String workName) {
    RuleFlowProcess process = new RuleFlowProcess();
    process.setId(processId);
    List<Variable> variables = new ArrayList<Variable>();
    Variable variable = new Variable();
    variable.setName("UserName");
    variable.setType(new StringDataType());
    variables.add(variable);
    variable = new Variable();
    variable.setName("Person");
    variable.setType(new ObjectDataType(Person.class.getName()));
    variables.add(variable);
    variable = new Variable();
    variable.setName("MyObject");
    variable.setType(new ObjectDataType());
    variables.add(variable);
    variable = new Variable();
    variable.setName("Number");
    variable.setType(new IntegerDataType());
    variables.add(variable);
    process.getVariableScope().setVariables(variables);
    StartNode startNode = new StartNode();
    startNode.setName("Start");
    startNode.setId(1);
    WorkItemNode workItemNode = new WorkItemNode();
    workItemNode.setName("workItemNode");
    workItemNode.setId(2);
    workItemNode.addInMapping("Comment", "Person.name");
    workItemNode.addInMapping("Attachment", "MyObject");
    workItemNode.addOutMapping("Result", "MyObject");
    workItemNode.addOutMapping("Result.length()", "Number");
    Work work = new WorkImpl();
    work.setName(workName);
    Set<ParameterDefinition> parameterDefinitions = new HashSet<ParameterDefinition>();
    ParameterDefinition parameterDefinition = new ParameterDefinitionImpl("ActorId", new StringDataType());
    parameterDefinitions.add(parameterDefinition);
    parameterDefinition = new ParameterDefinitionImpl("Content", new StringDataType());
    parameterDefinitions.add(parameterDefinition);
    parameterDefinition = new ParameterDefinitionImpl("Comment", new StringDataType());
    parameterDefinitions.add(parameterDefinition);
    work.setParameterDefinitions(parameterDefinitions);
    work.setParameter("ActorId", "#{UserName}");
    work.setParameter("Content", "#{Person.name}");
    workItemNode.setWork(work);
    EndNode endNode = new EndNode();
    endNode.setName("End");
    endNode.setId(3);
    connect(startNode, workItemNode);
    connect(workItemNode, endNode);
    process.addNode(startNode);
    process.addNode(workItemNode);
    process.addNode(endNode);
    return process;
}
Also used : RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) StartNode(org.jbpm.workflow.core.node.StartNode) Variable(org.jbpm.process.core.context.variable.Variable) IntegerDataType(org.jbpm.process.core.datatype.impl.type.IntegerDataType) ArrayList(java.util.ArrayList) ObjectDataType(org.jbpm.process.core.datatype.impl.type.ObjectDataType) ParameterDefinitionImpl(org.jbpm.process.core.impl.ParameterDefinitionImpl) StringDataType(org.jbpm.process.core.datatype.impl.type.StringDataType) EndNode(org.jbpm.workflow.core.node.EndNode) WorkItemNode(org.jbpm.workflow.core.node.WorkItemNode) Work(org.jbpm.process.core.Work) WorkImpl(org.jbpm.process.core.impl.WorkImpl) ParameterDefinition(org.jbpm.process.core.ParameterDefinition) HashSet(java.util.HashSet)

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