@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 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 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();
  }
示例#4
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 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();
  }