Пример #1
0
  public boolean equals(final Object object) {
    if (object == this) {
      return true;
    }

    if (object == null || !(object instanceof Cheese)) {
      return false;
    }

    final Cheese other = (Cheese) object;

    return (this.type.equals(other.getType()) && this.price == other.getPrice());
  }
  public void testExistsWithBinding() throws Exception {
    RuleBase ruleBase =
        loadRuleBase(
            new InputStreamReader(getClass().getResourceAsStream("test_ExistsWithBindings.drl")));
    StatefulSession workingMemory = ruleBase.newStatefulSession();

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

    final Cheese c = new Cheese("stilton", 10);
    final Person p = new Person("Mark", "stilton");
    workingMemory.insert(c);
    workingMemory.insert(p);

    ruleBase = serializeRuleBase(ruleBase);
    workingMemory = serializeWorkingMemory(ruleBase, workingMemory);
    results = (List) workingMemory.getGlobal("results");

    workingMemory.fireAllRules();

    assertTrue(results.contains(c.getType()));
    assertEquals(1, results.size());
  }
  @Test
  public void testLogicalInsertionsDynamicRule() throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(
        ResourceFactory.newClassPathResource("test_LogicalInsertionsDynamicRule.drl", getClass()),
        ResourceType.DRL);

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

    Collection<KnowledgePackage> kpkgs = kbuilder.getKnowledgePackages();
    KnowledgeBase kbase = getKnowledgeBase();
    kbase.addKnowledgePackages(kpkgs);
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);

    final Cheese c1 = new Cheese("a", 1);
    final Cheese c2 = new Cheese("b", 2);
    final Cheese c3 = new Cheese("c", 3);
    List list;

    ksession.insert(c1);
    FactHandle h = ksession.insert(c2);
    ksession.insert(c3);
    ksession.fireAllRules();

    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);

    // Check logical Insertions where made for c2 and c3
    list = new ArrayList(ksession.getObjects(new ClassObjectFilter(Person.class)));
    assertEquals(2, list.size());
    assertFalse(list.contains(new Person(c1.getType())));
    assertTrue(list.contains(new Person(c2.getType())));
    assertTrue(list.contains(new Person(c3.getType())));

    // this rule will make a logical assertion for c1 too
    kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(
        ResourceFactory.newClassPathResource("test_LogicalInsertionsDynamicRule2.drl", getClass()),
        ResourceType.DRL);
    if (kbuilder.hasErrors()) {
      fail(kbuilder.getErrors().toString());
    }
    Collection<KnowledgePackage> kpkgs2 = kbuilder.getKnowledgePackages();
    kbase.addKnowledgePackages(kpkgs2);
    kbase = SerializationHelper.serializeObject(kbase);

    ksession.fireAllRules();

    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);

    kbase = ksession.getKnowledgeBase();

    // check all now have just one logical assertion each
    list = new ArrayList(ksession.getObjects(new ClassObjectFilter(Person.class)));
    assertEquals(3, list.size());
    assertTrue(list.contains(new Person(c1.getType())));
    assertTrue(list.contains(new Person(c2.getType())));
    assertTrue(list.contains(new Person(c3.getType())));

    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);

    // check the packages are correctly populated
    assertEquals(2, kbase.getKnowledgePackages().size());
    KnowledgePackage test = null, test2 = null;
    // different JVMs return the package list in different order
    for (KnowledgePackage kpkg : kbase.getKnowledgePackages()) {
      if (kpkg.getName().equals("org.drools.test")) {
        test = kpkg;
      } else if (kpkg.getName().equals("org.drools.test2")) {
        test2 = kpkg;
      }
    }

    assertNotNull(test);
    assertNotNull(test2);
    assertEquals("rule1", test.getRules().iterator().next().getName());
    assertEquals("rule2", test2.getRules().iterator().next().getName());

    // now remove the first rule
    kbase.removeRule(test.getName(), test.getRules().iterator().next().getName());
    // different JVMs return the package list in different order
    for (KnowledgePackage kpkg : kbase.getKnowledgePackages()) {
      if (kpkg.getName().equals("org.drools.test")) {
        test = kpkg;
      } else if (kpkg.getName().equals("org.drools.test2")) {
        test2 = kpkg;
      }
    }
    assertNotNull(test);
    assertNotNull(test2);

    // Check the rule was correctly remove
    assertEquals(0, test.getRules().size());
    assertEquals(1, test2.getRules().size());
    assertEquals("rule2", test2.getRules().iterator().next().getName());

    list = new ArrayList(ksession.getObjects(new ClassObjectFilter(Person.class)));
    assertEquals(
        "removal of the rule should result in retraction of c3's logical assertion",
        2,
        list.size());
    assertTrue(
        "c1's logical assertion should not be retracted", list.contains(new Person(c1.getType())));
    assertTrue(
        "c2's logical assertion should  not be retracted", list.contains(new Person(c2.getType())));
    assertFalse(
        "c3's logical assertion should be  retracted", list.contains(new Person(c3.getType())));

    c2.setPrice(3);
    h = getFactHandle(h, ksession);
    ksession.update(h, c2);
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    list = new ArrayList(ksession.getObjects(new ClassObjectFilter(Person.class)));
    assertEquals(
        "c2 now has a higher price, its logical assertion should  be cancelled", 1, list.size());
    assertFalse(
        "The logical assertion cor c2 should have been retracted",
        list.contains(new Person(c2.getType())));
    assertTrue(
        "The logical assertion  for c1 should exist", list.contains(new Person(c1.getType())));

    // different JVMs return the package list in different order
    for (KnowledgePackage kpkg : kbase.getKnowledgePackages()) {
      if (kpkg.getName().equals("org.drools.test")) {
        test = kpkg;
      } else if (kpkg.getName().equals("org.drools.test2")) {
        test2 = kpkg;
      }
    }
    assertNotNull(test);
    assertNotNull(test2);

    kbase.removeRule(test2.getName(), test2.getRules().iterator().next().getName());
    kbase = SerializationHelper.serializeObject(kbase);

    // different JVMs return the package list in different order
    for (KnowledgePackage kpkg : kbase.getKnowledgePackages()) {
      if (kpkg.getName().equals("org.drools.test")) {
        test = kpkg;
      } else if (kpkg.getName().equals("org.drools.test2")) {
        test2 = kpkg;
      }
    }
    assertNotNull(test);
    assertNotNull(test2);

    assertEquals(0, test.getRules().size());
    assertEquals(0, test2.getRules().size());
    list = new ArrayList(ksession.getObjects(new ClassObjectFilter(Person.class)));
    assertEquals(0, list.size());
  }
  @Test
  public void testLogicalInsertionsBacking() throws Exception {
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(
        ResourceFactory.newClassPathResource("test_LogicalInsertionsBacking.drl", getClass()),
        ResourceType.DRL);
    Collection<KnowledgePackage> kpkgs = kbuilder.getKnowledgePackages();

    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kpkgs);
    kbase = SerializationHelper.serializeObject(kbase);
    StatefulKnowledgeSession session = createKnowledgeSession(kbase);

    final Cheese cheese1 = new Cheese("c", 1);
    final Cheese cheese2 = new Cheese(cheese1.getType(), 1);

    FactHandle h1 = session.insert(cheese1);
    session.fireAllRules();

    session = getSerialisedStatefulKnowledgeSession(session, true);

    Collection<?> list = session.getObjects(new ClassObjectFilter(cheese1.getType().getClass()));
    assertEquals(1, list.size());
    // probably dangerous, as contains works with equals, not identity
    assertEquals(cheese1.getType(), list.iterator().next());

    FactHandle h2 = session.insert(cheese2);
    session.fireAllRules();

    session = getSerialisedStatefulKnowledgeSession(session, true);

    list = session.getObjects(new ClassObjectFilter(cheese1.getType().getClass()));
    assertEquals(1, list.size());
    assertEquals(cheese1.getType(), list.iterator().next());

    assertEquals(3, session.getObjects().size());

    h1 = getFactHandle(h1, session);
    session.retract(h1);
    session = getSerialisedStatefulKnowledgeSession(session, true);
    session.fireAllRules();
    session = getSerialisedStatefulKnowledgeSession(session, true);
    list = session.getObjects(new ClassObjectFilter(cheese1.getType().getClass()));
    assertEquals(
        "cheese-type "
            + cheese1.getType()
            + " was retracted, but should not. Backed by cheese2 => type.",
        1,
        list.size());
    assertEquals(
        "cheese-type "
            + cheese1.getType()
            + " was retracted, but should not. Backed by cheese2 => type.",
        cheese1.getType(),
        list.iterator().next());

    h2 = getFactHandle(h2, session);
    session.retract(h2);
    session = getSerialisedStatefulKnowledgeSession(session, true);
    session.fireAllRules();
    session = getSerialisedStatefulKnowledgeSession(session, true);
    list = session.getObjects(new ClassObjectFilter(cheese1.getType().getClass()));
    assertEquals(
        "cheese-type "
            + cheese1.getType()
            + " was not retracted, but should have. Neither  cheese1 => type nor cheese2 => type is true.",
        0,
        list.size());
  }