@Test
  public void testPersistenceEvents() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("EventsProcess.rf"), ResourceType.DRF);
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

    StatefulKnowledgeSession ksession =
        InfinispanKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    long id = ksession.getIdentifier();

    ProcessInstance processInstance = ksession.startProcess("org.drools.test.TestProcess");
    logger.debug("Started process instance " + processInstance.getId());

    TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
    WorkItem workItem = handler.getWorkItem();
    assertNotNull(workItem);

    ksession = InfinispanKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
    processInstance = ksession.getProcessInstance(processInstance.getId());
    assertNotNull(processInstance);

    ksession = InfinispanKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);

    ksession = InfinispanKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
    processInstance = ksession.getProcessInstance(processInstance.getId());
    assertNotNull(processInstance);

    ksession.signalEvent("MyEvent1", null, processInstance.getId());

    ksession = InfinispanKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
    processInstance = ksession.getProcessInstance(processInstance.getId());
    assertNotNull(processInstance);

    ksession.signalEvent("MyEvent2", null, processInstance.getId());

    ksession = InfinispanKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
    processInstance = ksession.getProcessInstance(processInstance.getId());
    assertNull(processInstance);
  }
Exemplo n.º 2
0
  @Test
  public void testProcessEventListenerWithStartEvent() throws Exception {
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();

    // create a simple package with one process to test the events
    final InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.drools.test");
    RuleFlowProcess process = new RuleFlowProcess();
    process.setId("org.drools.core.process.event");
    process.setName("Event Process");

    StartNode startNode = new StartNode();
    startNode.setName("Start");
    startNode.setId(1);
    EventTrigger trigger = new EventTrigger();
    EventTypeFilter eventFilter = new EventTypeFilter();
    eventFilter.setType("signal");
    trigger.addEventFilter(eventFilter);
    startNode.addTrigger(trigger);
    process.addNode(startNode);

    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("Executed action");
          }
        });
    actionNode.setAction(action);
    actionNode.setId(2);
    process.addNode(actionNode);
    new ConnectionImpl(
        startNode, Node.CONNECTION_DEFAULT_TYPE,
        actionNode, Node.CONNECTION_DEFAULT_TYPE);

    EndNode endNode = new EndNode();
    endNode.setName("End");
    endNode.setId(3);
    process.addNode(endNode);
    new ConnectionImpl(
        actionNode, Node.CONNECTION_DEFAULT_TYPE,
        endNode, Node.CONNECTION_DEFAULT_TYPE);

    pkg.addProcess(process);
    List<KnowledgePackage> pkgs = new ArrayList<KnowledgePackage>();
    pkgs.add(pkg);
    kbase.addKnowledgePackages(pkgs);

    StatefulKnowledgeSession session = kbase.newStatefulKnowledgeSession();
    final List<ProcessEvent> processEventList = new ArrayList<ProcessEvent>();
    final ProcessEventListener processEventListener =
        new ProcessEventListener() {

          public void afterNodeLeft(ProcessNodeLeftEvent event) {
            processEventList.add(event);
          }

          public void afterNodeTriggered(ProcessNodeTriggeredEvent event) {
            processEventList.add(event);
          }

          public void afterProcessCompleted(ProcessCompletedEvent event) {
            processEventList.add(event);
          }

          public void afterProcessStarted(ProcessStartedEvent event) {
            processEventList.add(event);
          }

          public void beforeNodeLeft(ProcessNodeLeftEvent event) {
            processEventList.add(event);
          }

          public void beforeNodeTriggered(ProcessNodeTriggeredEvent event) {
            processEventList.add(event);
          }

          public void beforeProcessCompleted(ProcessCompletedEvent event) {
            processEventList.add(event);
          }

          public void beforeProcessStarted(ProcessStartedEvent event) {
            processEventList.add(event);
          }

          public void beforeVariableChanged(ProcessVariableChangedEvent event) {
            processEventList.add(event);
          }

          public void afterVariableChanged(ProcessVariableChangedEvent event) {
            processEventList.add(event);
          }
        };
    session.addEventListener(processEventListener);

    // execute the process
    //        session.startProcess("org.drools.core.process.event");
    session.signalEvent("signal", null);
    assertEquals(16, processEventList.size());
    assertEquals(
        "org.drools.core.process.event",
        ((ProcessStartedEvent) processEventList.get(0)).getProcessInstance().getProcessId());
    assertEquals(
        "Start",
        ((ProcessNodeTriggeredEvent) processEventList.get(1)).getNodeInstance().getNodeName());
    assertEquals(
        "Start", ((ProcessNodeLeftEvent) processEventList.get(2)).getNodeInstance().getNodeName());
    assertEquals(
        "Print",
        ((ProcessNodeTriggeredEvent) processEventList.get(3)).getNodeInstance().getNodeName());
    assertEquals(
        "Print", ((ProcessNodeLeftEvent) processEventList.get(4)).getNodeInstance().getNodeName());
    assertEquals(
        "End",
        ((ProcessNodeTriggeredEvent) processEventList.get(5)).getNodeInstance().getNodeName());
    assertEquals(
        "End", ((ProcessNodeLeftEvent) processEventList.get(6)).getNodeInstance().getNodeName());
    assertEquals(
        "org.drools.core.process.event",
        ((ProcessCompletedEvent) processEventList.get(7)).getProcessInstance().getProcessId());
    assertEquals(
        "org.drools.core.process.event",
        ((ProcessCompletedEvent) processEventList.get(8)).getProcessInstance().getProcessId());
    assertEquals(
        "End", ((ProcessNodeLeftEvent) processEventList.get(9)).getNodeInstance().getNodeName());
    assertEquals(
        "End",
        ((ProcessNodeTriggeredEvent) processEventList.get(10)).getNodeInstance().getNodeName());
    assertEquals(
        "Print", ((ProcessNodeLeftEvent) processEventList.get(11)).getNodeInstance().getNodeName());
    assertEquals(
        "Print",
        ((ProcessNodeTriggeredEvent) processEventList.get(12)).getNodeInstance().getNodeName());
    assertEquals(
        "Start", ((ProcessNodeLeftEvent) processEventList.get(13)).getNodeInstance().getNodeName());
    assertEquals(
        "Start",
        ((ProcessNodeTriggeredEvent) processEventList.get(14)).getNodeInstance().getNodeName());
    assertEquals(
        "org.drools.core.process.event",
        ((ProcessStartedEvent) processEventList.get(15)).getProcessInstance().getProcessId());
  }