Search in sources :

Example 1 with Message

use of org.jbpm.integrationtests.test.Message in project jbpm by kiegroup.

the class ProcessActionTest method testActionContextJava.

@Test
public void testActionContextJava() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    Reader source = new StringReader("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + "         xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "         xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + "         type=\"RuleFlow\" name=\"flow\" id=\"org.drools.actions\" package-name=\"org.drools\" version=\"1\" >\n" + "\n" + "  <header>\n" + "    <imports>\n" + "      <import name=\"org.jbpm.integrationtests.test.Message\" />\n" + "    </imports>\n" + "    <globals>\n" + "      <global identifier=\"list\" type=\"java.util.List\" />\n" + "    </globals>\n" + "    <variables>\n" + "      <variable name=\"variable\" >\n" + "        <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "        <value>SomeText</value>\n" + "      </variable>\n" + "    </variables>\n" + "  </header>\n" + "\n" + "  <nodes>\n" + "    <start id=\"1\" name=\"Start\" />\n" + "    <actionNode id=\"2\" name=\"MyActionNode\" >\n" + "      <action type=\"expression\" dialect=\"java\" >System.out.println(\"Triggered\");\n" + "String myVariable = (String) kcontext.getVariable(\"variable\");\n" + "list.add(myVariable);\n" + "String nodeName = kcontext.getNodeInstance().getNodeName();\n" + "list.add(nodeName);\n" + "insert( new Message() );\n" + "</action>\n" + "    </actionNode>\n" + "    <end id=\"3\" name=\"End\" />\n" + "  </nodes>\n" + "\n" + "  <connections>\n" + "    <connection from=\"1\" to=\"2\" />\n" + "    <connection from=\"2\" to=\"3\" />\n" + "  </connections>\n" + "\n" + "</process>");
    kbuilder.add(new ReaderResource(source), ResourceType.DRF);
    KieBase kbase = kbuilder.newKieBase();
    KieSession ksession = kbase.newKieSession();
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ProcessInstance processInstance = ksession.startProcess("org.drools.actions");
    assertEquals(2, list.size());
    assertEquals("SomeText", list.get(0));
    assertEquals("MyActionNode", list.get(1));
    Collection<FactHandle> factHandles = ksession.getFactHandles(new ObjectFilter() {

        public boolean accept(Object object) {
            return object instanceof Message;
        }
    });
    assertFalse(factHandles.isEmpty());
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
}
Also used : Message(org.jbpm.integrationtests.test.Message) FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) Reader(java.io.Reader) StringReader(java.io.StringReader) ReaderResource(org.drools.core.io.impl.ReaderResource) ObjectFilter(org.kie.api.runtime.ObjectFilter) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KieBase(org.kie.api.KieBase) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 2 with Message

use of org.jbpm.integrationtests.test.Message in project jbpm by kiegroup.

the class ProcessActionTest method testActionContextMVEL.

@Test
public void testActionContextMVEL() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    Reader source = new StringReader("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + "         xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "         xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + "         type=\"RuleFlow\" name=\"flow\" id=\"org.drools.actions\" package-name=\"org.drools\" version=\"1\" >\n" + "\n" + "  <header>\n" + "    <imports>\n" + "      <import name=\"org.jbpm.integrationtests.test.Message\" />\n" + "    </imports>\n" + "    <globals>\n" + "      <global identifier=\"list\" type=\"java.util.List\" />\n" + "    </globals>\n" + "    <variables>\n" + "      <variable name=\"variable\" >\n" + "        <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "        <value>SomeText</value>\n" + "      </variable>\n" + "    </variables>\n" + "  </header>\n" + "\n" + "  <nodes>\n" + "    <start id=\"1\" name=\"Start\" />\n" + "    <actionNode id=\"2\" name=\"MyActionNode\" >\n" + "      <action type=\"expression\" dialect=\"mvel\" >System.out.println(\"Triggered\");\n" + "System.out.println(kcontext.getKnowledgeRuntime());\n" + "String myVariable = (String) kcontext.getVariable(\"variable\");\n" + "list.add(myVariable);\n" + "String nodeName = kcontext.getNodeInstance().getNodeName();\n" + "list.add(nodeName);\n" + "insert( new Message() );\n" + "</action>\n" + "    </actionNode>\n" + "    <end id=\"3\" name=\"End\" />\n" + "  </nodes>\n" + "\n" + "  <connections>\n" + "    <connection from=\"1\" to=\"2\" />\n" + "    <connection from=\"2\" to=\"3\" />\n" + "  </connections>\n" + "\n" + "</process>");
    kbuilder.add(new ReaderResource(source), ResourceType.DRF);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    KieBase kbase = kbuilder.newKieBase();
    KieSession ksession = kbase.newKieSession();
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ProcessInstance processInstance = ksession.startProcess("org.drools.actions");
    assertEquals(2, list.size());
    assertEquals("SomeText", list.get(0));
    assertEquals("MyActionNode", list.get(1));
    Collection<FactHandle> factHandles = ksession.getFactHandles(new ObjectFilter() {

        public boolean accept(Object object) {
            return object instanceof Message;
        }
    });
    assertFalse(factHandles.isEmpty());
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
}
Also used : Message(org.jbpm.integrationtests.test.Message) FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) Reader(java.io.Reader) StringReader(java.io.StringReader) ReaderResource(org.drools.core.io.impl.ReaderResource) ObjectFilter(org.kie.api.runtime.ObjectFilter) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KieBase(org.kie.api.KieBase) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 3 with Message

use of org.jbpm.integrationtests.test.Message in project jbpm by kiegroup.

the class ProcessTimerTest method testVariableSimpleProcess.

@Test
public void testVariableSimpleProcess() throws Exception {
    Reader source = new StringReader("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + "         xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "         xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + "         type=\"RuleFlow\" name=\"flow\" id=\"org.drools.timer\" package-name=\"org.jbpm\" version=\"1\" >\n" + "\n" + "  <header>\n" + "    <imports>\n" + "      <import name=\"org.jbpm.integrationtests.test.Message\" />\n" + "    </imports>\n" + "    <globals>\n" + "      <global identifier=\"myList\" type=\"java.util.List\" />\n" + "    </globals>\n" + "    <variables>\n" + "      <variable name=\"x\" >\n" + "        <type name=\"org.jbpm.process.core.datatype.impl.type.IntegerDataType\" />\n" + "      </variable>\n" + "      <variable name=\"y\" >\n" + "        <type name=\"org.jbpm.process.core.datatype.impl.type.IntegerDataType\" />\n" + "      </variable>\n" + "    </variables>\n" + "  </header>\n" + "\n" + "  <nodes>\n" + "    <start id=\"1\" name=\"Start\" />\n" + "    <end id=\"2\" name=\"End\" />\n" + "    <timerNode id=\"3\" name=\"Timer\" delay=\"#{x}ms\" period=\"#{y}ms\" />\n" + "    <actionNode id=\"4\" name=\"Action\" >\n" + "      <action type=\"expression\" dialect=\"java\" >System.out.println(\"Triggered\");\n" + "myList.add( new Message() );\n" + "insert( new Message() );\n" + "</action>\n" + "    </actionNode>\n" + "    <milestone id=\"5\" name=\"Wait\" >\n" + "      <constraint type=\"rule\" dialect=\"mvel\" >Number( intValue &gt;= 5 ) from accumulate ( m: Message( ), count( m ) )</constraint>\n" + "    </milestone>\n" + "  </nodes>\n" + "\n" + "  <connections>\n" + "    <connection from=\"5\" to=\"2\" />\n" + "    <connection from=\"1\" to=\"3\" />\n" + "    <connection from=\"3\" to=\"4\" />\n" + "    <connection from=\"4\" to=\"5\" />\n" + "  </connections>\n" + "\n" + "</process>");
    builder.addRuleFlow(source);
    if (!builder.getErrors().isEmpty()) {
        for (DroolsError error : builder.getErrors().getErrors()) {
            logger.error(error.toString());
        }
        fail("Could not build process");
    }
    KieSession session = createKieSession(builder.getPackages());
    List<Message> myList = new ArrayList<Message>();
    session.setGlobal("myList", myList);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("x", 800);
    params.put("y", 200);
    ProcessInstance processInstance = (ProcessInstance) session.startProcess("org.drools.timer", params);
    assertEquals(0, myList.size());
    assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
    assertEquals(1, ((InternalProcessRuntime) ((InternalWorkingMemory) session).getProcessRuntime()).getTimerManager().getTimers().size());
    // test that the delay works
    try {
        Thread.sleep(400);
    } catch (InterruptedException e) {
    // do nothing
    }
    assertEquals(0, myList.size());
    // test that the period works
    try {
        Thread.sleep(1300);
    } catch (InterruptedException e) {
    // do nothing
    }
    assertEquals(5, myList.size());
    try {
        Thread.sleep(200);
    } catch (InterruptedException e) {
    // do nothing
    }
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
    session.dispose();
}
Also used : Message(org.jbpm.integrationtests.test.Message) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Reader(java.io.Reader) StringReader(java.io.StringReader) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) DroolsError(org.drools.compiler.compiler.DroolsError) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.jbpm.process.instance.ProcessInstance) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 4 with Message

use of org.jbpm.integrationtests.test.Message in project jbpm by kiegroup.

the class ProcessTimerTest method testSimpleProcess.

@Test
public void testSimpleProcess() throws Exception {
    Reader source = new StringReader("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + "         xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "         xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + "         type=\"RuleFlow\" name=\"flow\" id=\"org.drools.timer\" package-name=\"org.jbpm\" version=\"1\" >\n" + "\n" + "  <header>\n" + "    <imports>\n" + "      <import name=\"org.jbpm.integrationtests.test.Message\" />\n" + "    </imports>\n" + "    <globals>\n" + "      <global identifier=\"myList\" type=\"java.util.List\" />\n" + "    </globals>\n" + "  </header>\n" + "\n" + "  <nodes>\n" + "    <start id=\"1\" name=\"Start\" />\n" + "    <end id=\"2\" name=\"End\" />\n" + "    <timerNode id=\"3\" name=\"Timer\" delay=\"800ms\" period=\"200ms\" />\n" + "    <actionNode id=\"4\" name=\"Action\" >\n" + "      <action type=\"expression\" dialect=\"java\" >System.out.println(\"Triggered\");\n" + "myList.add( new Message() );\n" + "insert( new Message() );\n" + "</action>\n" + "    </actionNode>\n" + "    <milestone id=\"5\" name=\"Wait\" >\n" + "      <constraint type=\"rule\" dialect=\"mvel\" >Number( intValue &gt;= 5 ) from accumulate ( m: Message( ), count( m ) )</constraint>\n" + "    </milestone>\n" + "  </nodes>\n" + "\n" + "  <connections>\n" + "    <connection from=\"5\" to=\"2\" />\n" + "    <connection from=\"1\" to=\"3\" />\n" + "    <connection from=\"3\" to=\"4\" />\n" + "    <connection from=\"4\" to=\"5\" />\n" + "  </connections>\n" + "\n" + "</process>");
    builder.addRuleFlow(source);
    if (!builder.getErrors().isEmpty()) {
        for (DroolsError error : builder.getErrors().getErrors()) {
            logger.error(error.toString());
        }
        fail("Could not build process");
    }
    KieSession session = createKieSession(builder.getPackages());
    List<Message> myList = new ArrayList<Message>();
    session.setGlobal("myList", myList);
    ProcessInstance processInstance = (ProcessInstance) session.startProcess("org.drools.timer");
    assertEquals(0, myList.size());
    assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
    assertEquals(1, ((InternalProcessRuntime) ((InternalWorkingMemory) session).getProcessRuntime()).getTimerManager().getTimers().size());
    // test that the delay works
    try {
        Thread.sleep(400);
    } catch (InterruptedException e) {
    // do nothing
    }
    assertEquals(0, myList.size());
    // test that the period works
    try {
        Thread.sleep(1300);
    } catch (InterruptedException e) {
    // do nothing
    }
    assertEquals(5, myList.size());
    try {
        Thread.sleep(200);
    } catch (InterruptedException e) {
    // do nothing
    }
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
    session.dispose();
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) DroolsError(org.drools.compiler.compiler.DroolsError) Message(org.jbpm.integrationtests.test.Message) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) Reader(java.io.Reader) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.jbpm.process.instance.ProcessInstance) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 5 with Message

use of org.jbpm.integrationtests.test.Message in project jbpm by kiegroup.

the class ProcessTimerTest method testOnEntryTimerCancelled.

@Test
public void testOnEntryTimerCancelled() throws Exception {
    Reader source = new StringReader("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + "         xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "         xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + "         type=\"RuleFlow\" name=\"flow\" id=\"org.drools.timer\" package-name=\"org.drools\" version=\"1\" >\n" + "\n" + "  <header>\n" + "    <globals>\n" + "      <global identifier=\"myList\" type=\"java.util.List\" />\n" + "    </globals>\n" + "  </header>\n" + "\n" + "  <nodes>\n" + "    <start id=\"1\" name=\"Start\" />\n" + "    <milestone id=\"2\" name=\"Wait\" >\n" + "      <timers>\n" + "        <timer id=\"1\" delay=\"2000\" >\n" + "          <action type=\"expression\" dialect=\"java\" >myList.add(\"Executing timer\");</action>\n" + "        </timer>\n" + "      </timers>\n" + "      <constraint type=\"rule\" dialect=\"mvel\" >org.jbpm.integrationtests.test.Message( )</constraint>\n" + "    </milestone>\n" + "    <end id=\"3\" name=\"End\" />\n" + "  </nodes>\n" + "\n" + "  <connections>\n" + "    <connection from=\"1\" to=\"2\" />\n" + "    <connection from=\"2\" to=\"3\" />\n" + "  </connections>\n" + "\n" + "</process>");
    builder.addRuleFlow(source);
    KieSession session = createKieSession(builder.getPackages());
    List<String> myList = new ArrayList<String>();
    session.setGlobal("myList", myList);
    ProcessInstance processInstance = (ProcessInstance) session.startProcess("org.drools.timer");
    assertEquals(0, myList.size());
    assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
    assertEquals(1, ((InternalProcessRuntime) ((InternalWorkingMemory) session).getProcessRuntime()).getTimerManager().getTimers().size());
    session.insert(new Message());
    session.fireAllRules();
    assertEquals(0, myList.size());
    assertEquals(0, ((InternalProcessRuntime) ((InternalWorkingMemory) session).getProcessRuntime()).getTimerManager().getTimers().size());
    session.dispose();
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) Message(org.jbpm.integrationtests.test.Message) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) Reader(java.io.Reader) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.jbpm.process.instance.ProcessInstance) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Aggregations

Reader (java.io.Reader)8 StringReader (java.io.StringReader)8 ArrayList (java.util.ArrayList)8 Message (org.jbpm.integrationtests.test.Message)8 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)8 Test (org.junit.Test)8 KieSession (org.kie.api.runtime.KieSession)8 DroolsError (org.drools.compiler.compiler.DroolsError)4 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)3 ReaderResource (org.drools.core.io.impl.ReaderResource)3 ProcessInstance (org.jbpm.process.instance.ProcessInstance)3 KieBase (org.kie.api.KieBase)3 ObjectFilter (org.kie.api.runtime.ObjectFilter)3 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)3 FactHandle (org.kie.api.runtime.rule.FactHandle)3 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)3 HashMap (java.util.HashMap)1 Person (org.jbpm.integrationtests.test.Person)1