Example #1
0
  @Test
  public void testCollectWithNestedFromWithParams() throws Exception {
    final PackageBuilder builder = new PackageBuilder();
    builder.addPackageFromDrl(
        new InputStreamReader(getClass().getResourceAsStream("test_CollectWithNestedFrom.drl")));
    final Package pkg = builder.getPackage();

    RuleBase ruleBase = getSinglethreadRuleBase();
    ruleBase.addPackage(pkg);
    ruleBase = SerializationHelper.serializeObject(ruleBase);

    final WorkingMemory workingMemory = ruleBase.newStatefulSession();
    final List results = new ArrayList();
    workingMemory.setGlobal("results", results);

    final Person bob = new Person("bob", "stilton");

    Cheesery cheesery = new Cheesery();
    cheesery.addCheese(new Cheese("stilton", 10));
    cheesery.addCheese(new Cheese("brie", 20));
    cheesery.addCheese(new Cheese("muzzarela", 8));
    cheesery.addCheese(new Cheese("stilton", 5));
    cheesery.addCheese(new Cheese("provolone", 1));

    workingMemory.insert(bob);
    workingMemory.insert(cheesery);

    workingMemory.fireAllRules();

    assertEquals(1, results.size());
    List cheeses = (List) results.get(0);
    assertEquals(2, cheeses.size());
    assertEquals(bob.getLikes(), ((Cheese) cheeses.get(0)).getType());
    assertEquals(bob.getLikes(), ((Cheese) cheeses.get(1)).getType());
  }
Example #2
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());
    }
  }
Example #3
0
  @Test
  public void testForall() throws Exception {
    final PackageBuilder builder = new PackageBuilder();
    builder.addPackageFromDrl(
        new InputStreamReader(getClass().getResourceAsStream("test_Forall.drl")));
    final Package pkg = builder.getPackage();

    RuleBase ruleBase = getSinglethreadRuleBase();
    ruleBase.addPackage(pkg);
    ruleBase = SerializationHelper.serializeObject(ruleBase);
    final WorkingMemory workingMemory = ruleBase.newStatefulSession();

    final List list = new ArrayList();
    workingMemory.setGlobal("results", list);

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

    final Person bob = new Person("Bob");
    bob.setStatus(state.getState());
    bob.setLikes("stilton");
    workingMemory.insert(bob);

    workingMemory.fireAllRules();

    assertEquals(0, list.size());

    workingMemory.insert(new Cheese(bob.getLikes(), 10));
    workingMemory.fireAllRules();

    assertEquals(1, list.size());
  }
Example #4
0
  @Test
  public void testCollectFromMVELAfterOr() throws Exception {

    // read in the source
    final Reader reader =
        new InputStreamReader(getClass().getResourceAsStream("test_CollectFromMVELAfterOr.drl"));
    RuleBase ruleBase = loadRuleBase(reader);

    StatefulSession wm = ruleBase.newStatefulSession();
    List results = new ArrayList();

    wm.setGlobal("results", results);

    Person jill = new Person("jill");

    Person bob = new Person("bob");
    List addresses = new ArrayList();
    addresses.add(new Address("a"));
    addresses.add(new Address("b"));
    addresses.add(new Address("c"));
    bob.setAddresses(addresses);

    wm.insert(jill);
    wm.insert(bob);

    wm = SerializationHelper.getSerialisedStatefulSession(wm);
    results = (List) wm.getGlobal("results");

    wm.fireAllRules();

    assertEquals(2, results.size());
    assertEquals(3, ((Collection) results.get(0)).size());
  }
Example #5
0
  @Test
  public void testCollectModify() throws Exception {
    // read in the source
    final Reader reader = new InputStreamReader(getClass().getResourceAsStream("test_Collect.drl"));
    RuleBase ruleBase = loadRuleBase(reader);

    StatefulSession wm = ruleBase.newStatefulSession();
    List results = new ArrayList();

    wm.setGlobal("results", results);

    final Cheese[] cheese =
        new Cheese[] {
          new Cheese("stilton", 10),
          new Cheese("stilton", 2),
          new Cheese("stilton", 5),
          new Cheese("brie", 15),
          new Cheese("brie", 16),
          new Cheese("provolone", 8)
        };
    final Person bob = new Person("Bob", "stilton");

    final FactHandle[] cheeseHandles = new FactHandle[cheese.length];
    for (int i = 0; i < cheese.length; i++) {
      cheeseHandles[i] = wm.insert(cheese[i]);
    }
    final FactHandle bobHandle = wm.insert(bob);

    // ---------------- 1st scenario
    int fireCount = 0;
    wm.fireAllRules();
    assertEquals(++fireCount, results.size());
    assertEquals(3, ((Collection) results.get(fireCount - 1)).size());
    assertEquals(ArrayList.class.getName(), results.get(fireCount - 1).getClass().getName());

    // ---------------- 2nd scenario
    final int index = 1;
    cheese[index].setPrice(9);
    wm.update(cheeseHandles[index], cheese[index]);

    wm.fireAllRules();

    assertEquals(++fireCount, results.size());
    assertEquals(3, ((Collection) results.get(fireCount - 1)).size());
    assertEquals(ArrayList.class.getName(), results.get(fireCount - 1).getClass().getName());

    // ---------------- 3rd scenario
    bob.setLikes("brie");
    wm.update(bobHandle, bob);
    wm.fireAllRules();

    assertEquals(fireCount, results.size());

    // ---------------- 4th scenario
    wm.retract(cheeseHandles[3]);
    wm.fireAllRules();

    // should not have fired as per constraint
    assertEquals(fireCount, results.size());
  }
  @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 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();
  }
Example #8
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));
  }
  @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();
  }
  @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();
  }
Example #11
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());
  }