예제 #1
0
  @Test
  public void testNullSafeBinding() {
    String str =
        "import org.drools.compiler.*;\n"
            + "rule R1 when\n"
            + "   Person( $streetName : address!.street ) \n"
            + "then\n"
            + "end";

    KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

    ksession.insert(new Person("Mario", 38));

    Person mark = new Person("Mark", 37);
    mark.setAddress(new Address("Main Street"));
    ksession.insert(mark);

    Person edson = new Person("Edson", 34);
    edson.setAddress(new Address(null));
    ksession.insert(edson);

    assertEquals(2, ksession.fireAllRules());
    ksession.dispose();
  }
예제 #2
0
  @Test
  public void testNullSafeNullComparisonReverse() {
    // DROOLS-82
    String str =
        "import org.drools.compiler.*;\n"
            + "rule R1 when\n"
            + "   Person( \"Main Street\".equalsIgnoreCase(address!.street) )\n"
            + "then\n"
            + "end";

    KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    ksession.insert(new Person("Mario", 38));

    Person mark = new Person("Mark", 37);
    mark.setAddress(new Address("Main Street"));
    ksession.insert(mark);

    Person edson = new Person("Edson", 34);
    edson.setAddress(new Address(null));
    ksession.insert(edson);

    assertEquals(1, ksession.fireAllRules());
    ksession.dispose();
  }
  @Test
  public void testLocalTransactionPerStatement() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(ruleString.getBytes()), ResourceType.DRL);
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();

    if (kbuilder.hasErrors()) {
      fail(kbuilder.getErrors().toString());
    }

    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

    StatefulKnowledgeSession ksession =
        InfinispanKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    List<?> list = new ArrayList<Object>();

    ksession.setGlobal("list", list);

    ksession.insert(1);
    ksession.insert(2);
    ksession.insert(3);

    ksession.fireAllRules();

    assertEquals(3, list.size());
  }
예제 #4
0
  @Test
  public void testDoubleNullSafe() {
    String str =
        "import org.drools.compiler.*;\n"
            + "rule R1 when\n"
            + "   Person( address!.street!.length > 15 ) \n"
            + "then\n"
            + "end";

    KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

    ksession.insert(new Person("Mario", 38));

    Person mark = new Person("Mark", 37);
    mark.setAddress(new Address("Main Street"));
    ksession.insert(mark);

    Person edson = new Person("Edson", 34);
    edson.setAddress(new Address(null));
    ksession.insert(edson);

    Person alex = new Person("Alex", 34);
    alex.setAddress(new Address("The Main Very Big Street"));
    ksession.insert(alex);

    assertEquals(1, ksession.fireAllRules());
    ksession.dispose();
  }
예제 #5
0
  @Test
  public void testFixedPattern() throws FileNotFoundException {

    DecisionTableConfiguration dtconf = KnowledgeBuilderFactory.newDecisionTableConfiguration();
    dtconf.setInputType(DecisionTableInputType.XLS);
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(
        ResourceFactory.newClassPathResource("fixedPattern.xls", getClass()),
        ResourceType.DTABLE,
        dtconf);
    if (kbuilder.hasErrors()) {
      fail(kbuilder.getErrors().toString());
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

    List<Long> list = new ArrayList<Long>();
    ksession.setGlobal("list", list);

    ksession.insert(1L);
    ksession.insert(2);
    ksession.fireAllRules();

    assertEquals(1, list.size());
    assertEquals(1L, (long) list.get(0));

    ksession.dispose();
  }
예제 #6
0
  // JBRULES-2526
  @Test
  public void testOrWithVariableResolution2() throws Exception {
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(
        ResourceFactory.newClassPathResource(
            "test_OrCEFollowedByMultipleEval2.drl", FirstOrderLogicTest.class),
        ResourceType.DRL);

    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());

    final KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

    final StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);

    final AgendaEventListener al = mock(AgendaEventListener.class);
    ksession.addEventListener(al);

    ksession.insert(new FactA("a"));
    ksession.insert(new FactB("b"));
    ksession.insert(new FactC("c"));

    ksession.fireAllRules();
    verify(al, times(8)).afterMatchFired(any(AfterMatchFiredEvent.class));
  }
 public void execute(int repNr) {
   for (int i = 0; i < objectNr; i++) {
     ksession.insert(as[i]);
     ksession.insert(bs[i]);
   }
   ksession.fireAllRules();
 }
예제 #8
0
  @Test
  public void testNewConstructor() {
    String str =
        ""
            + "package org.drools.compiler.test \n"
            + "import "
            + Person.class.getCanonicalName()
            + "\n"
            + "import "
            + Address.class.getCanonicalName()
            + "\n"
            + "global java.util.List list \n"
            + "rule \"show\" \n"
            + "when  \n"
            + "    $m : Person( address == new Address('s1')) \n"
            + "then \n"
            + "    list.add('r1'); \n"
            + "end \n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);

    if (kbuilder.hasErrors()) {
      fail(kbuilder.getErrors().toString());
    }

    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    List list = new ArrayList();
    ksession.setGlobal("list", list);

    Person p = new Person("yoda");
    p.setAddress(new Address("s1"));

    ksession.insert(p);

    ksession.fireAllRules();

    assertEquals("r1", list.get(0));

    // Check it was built with MVELReturnValueExpression constraint
    List<ObjectTypeNode> nodes = ((InternalKnowledgeBase) kbase).getRete().getObjectTypeNodes();
    ObjectTypeNode node = null;
    for (ObjectTypeNode n : nodes) {
      if (((ClassObjectType) n.getObjectType()).getClassType() == Person.class) {
        node = n;
        break;
      }
    }

    AlphaNode alphanode = (AlphaNode) node.getSinkPropagator().getSinks()[0];
    AlphaNodeFieldConstraint constraint = alphanode.getConstraint();

    if (constraint instanceof MvelConstraint) {
      assertTrue(((MvelConstraint) constraint).getFieldExtractor() instanceof ClassFieldReader);
      FieldValue r = ((MvelConstraint) constraint).getField();
      assertEquals(p.getAddress(), r.getValue());
    }
  }
  @Test
  public void testPersistenceRuleSet() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("RuleSetProcess.rf"), ResourceType.DRF);
    kbuilder.add(new ClassPathResource("RuleSetRules.drl"), ResourceType.DRL);
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

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

    ksession.insert(new ArrayList<Object>());

    ksession = InfinispanKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
    ProcessInstance processInstance = ksession.startProcess("org.drools.test.TestProcess");

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

    ksession = InfinispanKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
    ksession.fireAllRules();
    processInstance = ksession.getProcessInstance(processInstance.getId());
    assertNull(processInstance);
  }
예제 #10
0
  @Test
  public void testIncrementOperator() throws Exception {
    String str = "";
    str += "package org.kie \n";
    str += "global java.util.List list \n";
    str += "rule rule1 \n";
    str += "    dialect \"mvel\" \n";
    str += "when \n";
    str += "    $I : Integer() \n";
    str += "then \n";
    str += "    i = $I.intValue(); \n";
    str += "    i += 5; \n";
    str += "    list.add( i ); \n";
    str += "end \n";

    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);

    assertFalse(kbuilder.hasErrors());

    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.insert(5);

    ksession.fireAllRules();

    assertEquals(1, list.size());
    assertEquals(10, list.get(0));
  }
예제 #11
0
  @Test
  public void testNestedEnum() {
    String str =
        ""
            + "package org.drools.compiler.test \n"
            + "import "
            + Triangle.class.getCanonicalName()
            + "\n"
            + "global java.util.List list \n"
            + "rule \"show\" \n"
            + "when  \n"
            + "    $t: Triangle(t == Triangle.Type.ACUTE) \n"
            + "then \n"
            + "    list.add($t.getT()); \n"
            + "end \n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);

    if (kbuilder.hasErrors()) {
      fail(kbuilder.getErrors().toString());
    }

    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    Triangle t = new Triangle(Triangle.Type.ACUTE);
    ksession.insert(t);

    ksession.fireAllRules();

    assertEquals(Triangle.Type.ACUTE, list.get(0));
  }
예제 #12
0
  @Test
  @SuppressWarnings("unchecked")
  public void test1() throws Exception {
    String rule = "package org.test;\n";
    rule += "import org.jbpm.integrationtests.test.Person\n";
    rule += "global java.util.List list\n";
    rule += "rule \"Rule 1\"\n";
    rule += "  ruleflow-group \"hello\"\n";
    rule += "when\n";
    rule += "    $p : Person( ) \n";
    rule += "then\n";
    rule += "    list.add( $p );\n";
    rule += "end";

    String process =
        "<?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=\"ruleflow\" id=\"org.test.ruleflow\" package-name=\"org.test\" >\n"
            + "  <header>\n"
            + "  </header>\n"
            + "  <nodes>\n"
            + "    <start id=\"1\" name=\"Start\" />\n"
            + "    <ruleSet id=\"2\" name=\"Hello\" ruleFlowGroup=\"hello\" />\n"
            + "    <end id=\"3\" name=\"End\" />\n"
            + "  </nodes>\n"
            + "  <connections>\n"
            + "    <connection from=\"1\" to=\"2\"/>\n"
            + "    <connection from=\"2\" to=\"3\"/>\n"
            + "  </connections>\n"
            + "</process>";

    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newReaderResource(new StringReader(rule)), ResourceType.DRL);
    kbuilder.add(ResourceFactory.newReaderResource(new StringReader(process)), ResourceType.DRF);

    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

    List<Object> list = new ArrayList<Object>();
    ksession.setGlobal("list", list);

    Person p = new Person("bobba fet", 32);
    ksession.insert(p);
    ksession.startProcess("org.test.ruleflow");

    assertEquals(1, ksession.getProcessInstances().size());

    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    assertEquals(1, ksession.getProcessInstances().size());

    ksession.fireAllRules();

    assertEquals(1, ((List<Object>) ksession.getGlobal("list")).size());
    assertEquals(p, ((List<Object>) ksession.getGlobal("list")).get(0));
    assertEquals(0, ksession.getProcessInstances().size());
  }
예제 #13
0
  @Test
  public void memoryTest() throws InterruptedException {

    final StatefulKnowledgeSession ksession = createKnowledgeSession();
    Event myevent = new Event();
    ksession.setGlobal("event", myevent);

    final Server node1 = new Server(1500, "server1");

    new Thread(
            new Runnable() {
              public void run() {
                ksession.fireUntilHalt();
              }
            })
        .start();

    serverHandle = ksession.insert(node1);

    int memory = 1500;
    for (int i = 0; i < 5; i++) {
      Thread.sleep(6000);
      memory = memory - 200;

      node1.setMemory(memory);
      ksession.update(serverHandle, node1);
    }

    ksession.halt();
    System.out.println(myevent.getEvents());
  }
  @Test
  public void testPersistenceWorkItems() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("WorkItemsProcess.rf"), ResourceType.DRF);
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

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

    ProcessInstance processInstance = ksession.startProcess("org.drools.test.TestProcess");
    ksession.insert("TestString");
    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);

    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);

    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);

    workItem = handler.getWorkItem();
    assertNull(workItem);

    ksession = InfinispanKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
    processInstance = ksession.getProcessInstance(processInstance.getId());
    assertEquals(origNumObjects + 1, ksession.getObjects().size());
    for (Object o : ksession.getObjects()) {
      logger.debug(o.toString());
    }
    assertNull(processInstance);
  }
 public void execute(Object object) {
   StatefulKnowledgeSession ksession = newWorkingMemory();
   try {
     ksession.insert(object);
     ksession.fireAllRules();
   } finally {
     dispose(ksession);
   }
 }
  @Test
  public void testPersistenceWorkItems2() throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("WorkItemsProcess.rf"), ResourceType.DRF);
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

    StatefulKnowledgeSession ksession =
        JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    int id = ksession.getId();

    UserTransaction ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
    ut.begin();

    ProcessInstance processInstance = ksession.startProcess("org.drools.test.TestProcess");
    ksession.insert("TestString");
    logger.debug("Started process instance {}", processInstance.getId());

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

    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);

    workItem = handler.getWorkItem();
    assertNotNull(workItem);

    ut.commit();

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

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

    workItem = handler.getWorkItem();
    assertNotNull(workItem);

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

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

    workItem = handler.getWorkItem();
    assertNull(workItem);

    ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
    processInstance = ksession.getProcessInstance(processInstance.getId());
    assertEquals(1, ksession.getObjects().size());
    for (Object o : ksession.getObjects()) {
      logger.debug(o.toString());
    }
    assertNull(processInstance);
  }
예제 #17
0
  @Test
  public void testCollectAfterOrCE() throws Exception {
    // Set up facts
    final Cheesery bonFromage = new Cheesery();
    bonFromage.addCheese(new Cheese("cheddar"));
    bonFromage.addCheese(new Cheese("cheddar"));

    // Test in memory compile of DRL
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(
        ResourceFactory.newClassPathResource("test_OrCEFollowedByCollect.drl", getClass()),
        ResourceType.DRL);
    KnowledgeBuilderErrors errors = kbuilder.getErrors();
    if (errors.size() > 0) {
      for (KnowledgeBuilderError error : errors) {
        logger.warn(error.toString());
      }
      throw new IllegalArgumentException("Could not parse knowledge.");
    }
    assertFalse(kbuilder.hasErrors());

    Collection<KnowledgePackage> knowledgePackages = kbuilder.getKnowledgePackages();

    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(knowledgePackages);

    StatefulKnowledgeSession session = createKnowledgeSession(kbase);
    session.insert(bonFromage);

    int rules = session.fireAllRules();
    assertEquals(2, rules);

    // Serialize and test again
    knowledgePackages = SerializationHelper.serializeObject(knowledgePackages);
    kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(knowledgePackages);

    session = createKnowledgeSession(kbase);
    session.insert(bonFromage);

    rules = session.fireAllRules();
    assertEquals(2, rules);
  }
예제 #18
0
 @Test
 public void testNegativeNumbers() throws Exception {
   KnowledgeBase kbase = readKnowledgeBase("/data/DT_WithNegativeNumbers.xls");
   StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
   IntHolder i1 = new IntHolder(1);
   IntHolder i2 = new IntHolder(-1);
   ksession.insert(i1);
   ksession.insert(i2);
   ksession.fireAllRules();
 }
 public void execute(int repNr) {
   int i = 0;
   for (Bean bean : beans) {
     factHandles[i++] = ksession.insert(bean);
   }
   ksession.fireAllRules();
   for (FactHandle factHandle : factHandles) {
     ksession.retract(factHandle);
   }
   ksession.fireAllRules();
 }
  @Test
  public void testSetFocus() {
    String str = "";
    str += "package org.drools.test\n";
    str += "global java.util.List list\n";
    str += "rule rule1\n";
    str += "agenda-group \"badfocus\"";
    str += "when\n";
    str += "  Integer(intValue > 0)\n";
    str += "then\n";
    str += "  list.add( 1 );\n";
    str += "end\n";
    str += "\n";

    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();

    if (kbuilder.hasErrors()) {
      fail(kbuilder.getErrors().toString());
    }

    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

    StatefulKnowledgeSession ksession =
        InfinispanKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    List<?> list = new ArrayList<Object>();

    ksession.setGlobal("list", list);

    ksession.insert(1);
    ksession.insert(2);
    ksession.insert(3);
    ksession.getAgenda().getAgendaGroup("badfocus").setFocus();

    ksession.fireAllRules();

    assertEquals(3, list.size());
  }
  @Test
  public void testPersistenceWorkItems3() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("WorkItemsProcess.rf"), ResourceType.DRF);
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

    StatefulKnowledgeSession ksession =
        InfinispanKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    ksession.getWorkItemManager().registerWorkItemHandler("MyWork", new SystemOutWorkItemHandler());
    ProcessInstance processInstance = ksession.startProcess("org.drools.test.TestProcess");
    ksession.insert("TestString");
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
  }
예제 #22
0
  @Test
  public void testForallSinglePattern2() throws Exception {
    final KnowledgeBase kbase = loadKnowledgeBase("test_ForallSinglePattern2.drl");
    final StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);

    ksession.insert(new Triangle(3, 3, 3));
    ksession.insert(new Triangle(3, 3, 3));

    // no cheeses, so should fire
    int fired = ksession.fireAllRules();
    assertEquals(1, fired);

    ksession.dispose();
  }
예제 #23
0
  @Test
  public void testForall2() throws Exception {
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(
        ResourceFactory.newInputStreamResource(getClass().getResourceAsStream("test_Forall2.drl")),
        ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());

    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    final StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);

    final List<String> list = new ArrayList<String>();
    ksession.setGlobal("results", list);

    final State state = new State("SP");
    ksession.insert(state);

    final Person bob = new Person("Bob");
    bob.setStatus(state.getState());
    bob.setAlive(true);
    ksession.insert(bob);

    ksession.fireAllRules();

    assertEquals(0, list.size());

    final State qc = new State("QC");
    ksession.insert(qc);
    final Person john = new Person("John");
    john.setStatus(qc.getState());
    john.setAlive(false);

    ksession.fireAllRules();

    assertEquals(1, list.size());
  }
예제 #24
0
  @Test
  public void testMixedNullSafes() {
    String str =
        "import org.drools.compiler.*;\n"
            + "rule R1 when\n"
            + " $p : Person( "
            + " address!.street!.length > 0 && ( address!.street!.length < 15 || > 20 && < 30 ) "
            + " && address!.zipCode!.length > 0 && address.zipCode == \"12345\" "
            + " ) \n"
            + "then\n"
            + " System.out.println( $p ); \n"
            + "end";

    KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

    ksession.insert(new Person("Mario", 38));

    Person mark = new Person("Mark", 37);
    mark.setAddress(new Address("Main Street", null, "12345"));
    ksession.insert(mark);

    Person edson = new Person("Edson", 34);
    edson.setAddress(new Address(null));
    ksession.insert(edson);

    Person alex = new Person("Alex", 34);
    alex.setAddress(new Address("The Main Verrry Long Street"));
    ksession.insert(alex);

    Person frank = new Person("Frank", 24);
    frank.setAddress(new Address("Long Street number 21", null, "12345"));
    ksession.insert(frank);

    assertEquals(2, ksession.fireAllRules());
    ksession.dispose();
  }
예제 #25
0
  @Test
  @Ignore("Added 30-APR-2011 -Rikkola-")
  public void testNestedEnumFromJar() {
    String str =
        ""
            + "package org.drools.compiler.test \n"
            + "import org.kie.examples.eventing.EventRequest \n"
            + "global java.util.List list \n"
            + "rule 'zaa'\n  "
            + "when \n  "
            + "request: EventRequest( status == EventRequest.Status.ACTIVE )\n   "
            + "then \n "
            + "request.setStatus(EventRequest.Status.ACTIVE); \n  "
            + "end";

    JarInputStream jis = null;
    try {
      jis = new JarInputStream(this.getClass().getResourceAsStream("/eventing-example.jar"));
    } catch (IOException e) {
      fail("Failed to load the jar");
    }
    MapBackedClassLoader loader = createClassLoader(jis);

    KnowledgeBuilderConfiguration knowledgeBuilderConfiguration =
        KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(null, loader);
    KnowledgeBuilder kbuilder =
        KnowledgeBuilderFactory.newKnowledgeBuilder(knowledgeBuilderConfiguration);
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);

    if (kbuilder.hasErrors()) {
      fail(kbuilder.getErrors().toString());
    }

    KieBaseConfiguration knowledgeBaseConfiguration =
        KnowledgeBaseFactory.newKnowledgeBaseConfiguration(null, loader);

    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(knowledgeBaseConfiguration);
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    Triangle t = new Triangle(Triangle.Type.ACUTE);
    ksession.insert(t);

    ksession.fireAllRules();

    assertEquals(Triangle.Type.ACUTE, list.get(0));
  }
 public List executeWithResults(Iterable objects, ObjectFilter filter) {
   List list = new ArrayList();
   StatefulKnowledgeSession ksession = newWorkingMemory();
   try {
     for (Object object : objects) {
       ksession.insert(object);
     }
     ksession.fireAllRules();
     for (FactHandle fh : ksession.getFactHandles(filter)) {
       list.add(((InternalFactHandle) fh).getObject());
     }
   } finally {
     dispose(ksession);
   }
   return list;
 }
  @Test
  public void shouldFireAssertIfAllPresentFailedWithNull() {
    // Get Rule infomration for a Given Form

    val mapa = new LinkedHashMap<String, String>();
    mapa.put("name", "myproject");
    mapa.put("project_type", "");
    mapa.put("start_date", "");
    ruleObj.setData(mapa);

    kSession = kbase.newStatefulKnowledgeSession();

    List<String> list = new ArrayList<String>();
    Evaluator eval = (Evaluator) ruleObj;
    kSession.insert(eval);
    kSession.insert(list);

    int actualNumberOfRulesFired = kSession.fireAllRules();

    assertEquals(list.size(), 0);
  }
예제 #28
0
  @Test
  public void testFromWithOr() throws Exception {
    KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    builder.add(
        ResourceFactory.newInputStreamResource(
            getClass().getResourceAsStream("test_FromWithOr.drl")),
        ResourceType.DRL);

    if (builder.hasErrors()) {
      logger.info(builder.getErrors().toString());
    }
    assertFalse(builder.hasErrors());

    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(builder.getKnowledgePackages());

    final StatefulKnowledgeSession session = createKnowledgeSession(kbase);

    final List<Address> results = new ArrayList<Address>();
    session.setGlobal("results", results);

    Address a1 = new Address();
    a1.setZipCode("12345");
    Address a2 = new Address();
    a2.setZipCode("54321");
    Address a3 = new Address();
    a3.setZipCode("99999");

    Person p = new Person();
    p.addAddress(a1);
    p.addAddress(a2);
    p.addAddress(a3);

    session.insert(p);
    session.fireAllRules();

    assertEquals(2, results.size());
    assertTrue(results.contains(a1));
    assertTrue(results.contains(a2));
  }
예제 #29
0
  @Test
  public void testOrs() throws Exception {
    String str =
        "package org.drools.compiler.integrationtests\n"
            + "import "
            + Message.class.getName()
            + "\n"
            + "rule X\n"
            + "    when\n"
            + "        Message( message == 'test' )\n"
            + "        Message( !fired ) or eval( !false )\n"
            + "    then\n"
            + "end\n";

    KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);

    ksession.insert(new Message("test"));
    int rules = ksession.fireAllRules();
    assertEquals(2, rules);
    ksession.dispose();
  }
예제 #30
0
  @Test
  public void testEvalWithBigDecimal() throws Exception {
    String str = "";
    str += "package org.kie \n";
    str += "import java.math.BigDecimal; \n";
    str += "global java.util.List list \n";
    str += "rule rule1 \n";
    str += "    dialect \"mvel\" \n";
    str += "when \n";
    str += "    $bd : BigDecimal() \n";
    str += "    eval( $bd.compareTo( BigDecimal.ZERO ) > 0 ) \n";
    str += "then \n";
    str += "    list.add( $bd ); \n";
    str += "end \n";

    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);

    if (kbuilder.hasErrors()) {
      System.err.println(kbuilder.getErrors());
    }
    assertFalse(kbuilder.hasErrors());

    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.insert(new BigDecimal(1.5));

    ksession.fireAllRules();

    assertEquals(1, list.size());
    assertEquals(new BigDecimal(1.5), list.get(0));
  }