private Collection<KnowledgePackage> getProcessWorkItems() {
    RuleFlowProcess process = new RuleFlowProcess();
    process.setId("org.drools.test.TestProcess");
    process.setName("TestProcess");
    process.setPackageName("org.drools.test");
    StartNode start = new StartNode();
    start.setId(1);
    start.setName("Start");
    process.addNode(start);
    ActionNode actionNode = new ActionNode();
    actionNode.setId(2);
    actionNode.setName("Action");
    DroolsConsequenceAction action = new DroolsConsequenceAction();
    action.setDialect("java");
    action.setConsequence("System.out.println(\"Executed action\");");
    actionNode.setAction(action);
    process.addNode(actionNode);
    new ConnectionImpl(
        start, Node.CONNECTION_DEFAULT_TYPE, actionNode, Node.CONNECTION_DEFAULT_TYPE);
    WorkItemNode workItemNode = new WorkItemNode();
    workItemNode.setId(3);
    workItemNode.setName("WorkItem1");
    Work work = new WorkImpl();
    work.setName("MyWork");
    workItemNode.setWork(work);
    process.addNode(workItemNode);
    new ConnectionImpl(
        actionNode, Node.CONNECTION_DEFAULT_TYPE, workItemNode, Node.CONNECTION_DEFAULT_TYPE);
    WorkItemNode workItemNode2 = new WorkItemNode();
    workItemNode2.setId(4);
    workItemNode2.setName("WorkItem2");
    work = new WorkImpl();
    work.setName("MyWork");
    workItemNode2.setWork(work);
    process.addNode(workItemNode2);
    new ConnectionImpl(
        workItemNode, Node.CONNECTION_DEFAULT_TYPE, workItemNode2, Node.CONNECTION_DEFAULT_TYPE);
    WorkItemNode workItemNode3 = new WorkItemNode();
    workItemNode3.setId(5);
    workItemNode3.setName("WorkItem3");
    work = new WorkImpl();
    work.setName("MyWork");
    workItemNode3.setWork(work);
    process.addNode(workItemNode3);
    new ConnectionImpl(
        workItemNode2, Node.CONNECTION_DEFAULT_TYPE, workItemNode3, Node.CONNECTION_DEFAULT_TYPE);
    EndNode end = new EndNode();
    end.setId(6);
    end.setName("End");
    process.addNode(end);
    new ConnectionImpl(
        workItemNode3, Node.CONNECTION_DEFAULT_TYPE, end, Node.CONNECTION_DEFAULT_TYPE);

    PackageBuilder packageBuilder = new PackageBuilder();
    ProcessBuilderImpl processBuilder = new ProcessBuilderImpl(packageBuilder);
    processBuilder.buildProcess(process, null);
    List<KnowledgePackage> list = new ArrayList<KnowledgePackage>();
    list.add(new KnowledgePackageImp(packageBuilder.getPackage()));
    return list;
  }
  @SuppressWarnings("unused")
  private List<KnowledgePackage> getProcessTimer2() {
    final RuleFlowProcess process = new RuleFlowProcess();
    process.setId("org.drools.test.TestProcess");
    process.setName("TestProcess");
    process.setPackageName("org.drools.test");
    final StartNode start = new StartNode();
    start.setId(1);
    start.setName("Start");
    process.addNode(start);
    final TimerNode timerNode = new TimerNode();
    timerNode.setId(2);
    timerNode.setName("Timer");
    final Timer timer = new Timer();
    timer.setDelay("0");
    timerNode.setTimer(timer);
    process.addNode(timerNode);
    new ConnectionImpl(
        start, Node.CONNECTION_DEFAULT_TYPE, timerNode, Node.CONNECTION_DEFAULT_TYPE);
    final ActionNode actionNode = new ActionNode();
    actionNode.setId(3);
    actionNode.setName("Action");
    final DroolsConsequenceAction action = new DroolsConsequenceAction();
    action.setDialect("java");
    action.setConsequence(
        "try { Thread.sleep(1000); } catch (Throwable t) {} System.out.println(\"Executed action\");");
    actionNode.setAction(action);
    process.addNode(actionNode);
    new ConnectionImpl(
        timerNode, Node.CONNECTION_DEFAULT_TYPE, actionNode, Node.CONNECTION_DEFAULT_TYPE);
    final EndNode end = new EndNode();
    end.setId(6);
    end.setName("End");
    process.addNode(end);
    new ConnectionImpl(actionNode, Node.CONNECTION_DEFAULT_TYPE, end, Node.CONNECTION_DEFAULT_TYPE);

    final PackageBuilder packageBuilder = new PackageBuilder();
    final ProcessBuilderImpl processBuilder = new ProcessBuilderImpl(packageBuilder);
    processBuilder.buildProcess(process, null);
    final List<KnowledgePackage> list = new ArrayList<KnowledgePackage>();
    list.add(new KnowledgePackageImp(packageBuilder.getPackage()));
    return list;
  }
  private List<KnowledgePackage> getProcessTimer() {
    RuleFlowProcess process = new RuleFlowProcess();
    process.setId("org.drools.test.TestProcess");
    process.setName("TestProcess");
    process.setPackageName("org.drools.test");
    StartNode start = new StartNode();
    start.setId(1);
    start.setName("Start");
    process.addNode(start);
    TimerNode timerNode = new TimerNode();
    timerNode.setId(2);
    timerNode.setName("Timer");
    Timer timer = new Timer();
    timer.setDelay("2000");
    timerNode.setTimer(timer);
    process.addNode(timerNode);
    new ConnectionImpl(
        start, Node.CONNECTION_DEFAULT_TYPE, timerNode, Node.CONNECTION_DEFAULT_TYPE);
    ActionNode actionNode = new ActionNode();
    actionNode.setId(3);
    actionNode.setName("Action");
    DroolsConsequenceAction action = new DroolsConsequenceAction();
    action.setDialect("java");
    action.setConsequence("System.out.println(\"Executed action\");");
    actionNode.setAction(action);
    process.addNode(actionNode);
    new ConnectionImpl(
        timerNode, Node.CONNECTION_DEFAULT_TYPE, actionNode, Node.CONNECTION_DEFAULT_TYPE);
    EndNode end = new EndNode();
    end.setId(6);
    end.setName("End");
    process.addNode(end);
    new ConnectionImpl(actionNode, Node.CONNECTION_DEFAULT_TYPE, end, Node.CONNECTION_DEFAULT_TYPE);

    PackageBuilder packageBuilder = new PackageBuilder();
    ProcessBuilderImpl processBuilder = new ProcessBuilderImpl(packageBuilder);
    processBuilder.buildProcess(process, null);
    List<KnowledgePackage> list = new ArrayList<KnowledgePackage>();
    list.add(new KnowledgePackageImp(packageBuilder.getPackage()));
    return list;
  }
  private Package getProcessSubProcess() {
    RuleFlowProcess process = new RuleFlowProcess();
    process.setId("org.drools.test.TestProcess");
    process.setName("TestProcess");
    process.setPackageName("org.drools.test");

    StartNode start = new StartNode();
    start.setId(1);
    start.setName("Start");
    process.addNode(start);

    ActionNode actionNode = new ActionNode();
    actionNode.setId(2);
    actionNode.setName("Action");

    DroolsConsequenceAction action = new DroolsConsequenceAction();
    action.setDialect("java");
    action.setConsequence("System.out.println(\"Executed action\");");
    actionNode.setAction(action);
    process.addNode(actionNode);

    new ConnectionImpl(
        start, Node.CONNECTION_DEFAULT_TYPE, actionNode, Node.CONNECTION_DEFAULT_TYPE);

    SubProcessNode subProcessNode = new SubProcessNode();
    subProcessNode.setId(3);
    subProcessNode.setName("SubProcess");
    subProcessNode.setProcessId("org.drools.test.SubProcess");
    process.addNode(subProcessNode);

    new ConnectionImpl(
        actionNode, Node.CONNECTION_DEFAULT_TYPE, subProcessNode, Node.CONNECTION_DEFAULT_TYPE);

    EndNode end = new EndNode();
    end.setId(4);
    end.setName("End");
    process.addNode(end);

    new ConnectionImpl(
        subProcessNode, Node.CONNECTION_DEFAULT_TYPE, end, Node.CONNECTION_DEFAULT_TYPE);

    PackageBuilder packageBuilder = new PackageBuilder();
    ProcessBuilderImpl processBuilder = new ProcessBuilderImpl(packageBuilder);
    processBuilder.buildProcess(process, null);

    process = new RuleFlowProcess();
    process.setId("org.drools.test.SubProcess");
    process.setName("SubProcess");
    process.setPackageName("org.drools.test");

    start = new StartNode();
    start.setId(1);
    start.setName("Start");
    process.addNode(start);

    actionNode = new ActionNode();
    actionNode.setId(2);
    actionNode.setName("Action");

    action = new DroolsConsequenceAction();
    action.setDialect("java");
    action.setConsequence("System.out.println(\"Executed action\");");
    actionNode.setAction(action);
    process.addNode(actionNode);

    new ConnectionImpl(
        start, Node.CONNECTION_DEFAULT_TYPE, actionNode, Node.CONNECTION_DEFAULT_TYPE);

    WorkItemNode workItemNode = new WorkItemNode();
    workItemNode.setId(3);
    workItemNode.setName("WorkItem1");

    Work work = new WorkImpl();
    work.setName("MyWork");
    workItemNode.setWork(work);
    process.addNode(workItemNode);

    new ConnectionImpl(
        actionNode, Node.CONNECTION_DEFAULT_TYPE, workItemNode, Node.CONNECTION_DEFAULT_TYPE);

    end = new EndNode();
    end.setId(6);
    end.setName("End");
    process.addNode(end);

    new ConnectionImpl(
        workItemNode, Node.CONNECTION_DEFAULT_TYPE, end, Node.CONNECTION_DEFAULT_TYPE);

    processBuilder.buildProcess(process, null);
    return packageBuilder.getPackage();
  }
Beispiel #5
0
  public void testEvent1() {
    RuleFlowProcess process = new RuleFlowProcess();
    process.setId("org.drools.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);

    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 {
            System.out.println(
                "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);

    Join join = new Join();
    join.setName("XOR Join");
    join.setType(Join.TYPE_XOR);
    join.setId(5);
    process.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);

    EndNode endNode = new EndNode();
    endNode.setName("EndNode");
    endNode.setId(6);
    process.addNode(endNode);
    new ConnectionImpl(
        join, Node.CONNECTION_DEFAULT_TYPE,
        endNode, Node.CONNECTION_DEFAULT_TYPE);

    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    ((AbstractRuleBase) ((InternalKnowledgeBase) kbase).getRuleBase()).addProcess(process);
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

    ProcessInstance processInstance = ksession.startProcess("org.drools.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());
  }
  @Test
  public void testProcessEventListener() 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);
    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");
    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());
  }
Beispiel #7
0
  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;
  }