@Test
  public void testOr() throws Exception {
    final PackageBuilder builder = new PackageBuilder();
    builder.addPackageFromDrl(
        new InputStreamReader(getClass().getResourceAsStream("test_OrNesting.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 Cheese cheddar = new Cheese("cheddar", 7);
    final Cheese provolone = new Cheese("provolone", 5);
    final Cheese brie = new Cheese("brie", 15);
    final Person mark = new Person("mark", "stilton");

    FactHandle ch = workingMemory.insert(cheddar);
    FactHandle ph = workingMemory.insert(provolone);
    FactHandle bh = workingMemory.insert(brie);
    FactHandle markh = workingMemory.insert(mark);

    workingMemory.fireAllRules();
    assertEquals(1, list.size());
  }
  @Test
  public void testNestedCorelatedRulesWithForall() throws Exception {

    PackageBuilder builder = new PackageBuilder();
    builder.addPackageFromDrl(
        new InputStreamReader(
            FirstOrderLogicTest.class.getResourceAsStream(
                "test_NestedCorrelatedRulesWithForall.drl")));

    RuleBase ruleBase = RuleBaseFactory.newRuleBase();
    ruleBase.addPackage(builder.getPackage());
    ruleBase = SerializationHelper.serializeObject(ruleBase);
    StatefulSession session = ruleBase.newStatefulSession();

    List list1 = new ArrayList();
    List list2 = new ArrayList();
    List list3 = new ArrayList();
    List list4 = new ArrayList();

    session.setGlobal("list1", list1);
    session.setGlobal("list2", list2);
    session.setGlobal("list3", list3);
    session.setGlobal("list4", list4);

    SpecialString first42 = new SpecialString("42");
    SpecialString second42 = new SpecialString("42");
    SpecialString world = new SpecialString("World");

    // System.out.println( "Inserting ..." );

    session.insert(world);
    session.insert(first42);
    session.insert(second42);

    // System.out.println( "Done." );

    // System.out.println( "Firing rules ..." );

    // check all lists are empty
    assertTrue(list1.isEmpty());
    assertTrue(list2.isEmpty());
    assertTrue(list3.isEmpty());
    assertTrue(list4.isEmpty());

    session.fireAllRules();

    // System.out.println( "Done." );

    // check first list is populated correctly
    assertEquals(0, list1.size());

    // check second list is populated correctly
    assertEquals(0, list2.size());

    // check third list is populated correctly
    assertEquals(1, list3.size());

    // check fourth list is populated correctly
    assertEquals(0, list4.size());
  }
  @Test
  public void testFromInsideNotAndExists() throws Exception {
    final PackageBuilder builder = new PackageBuilder();
    builder.addPackageFromDrl(
        new InputStreamReader(getClass().getResourceAsStream("test_FromInsideNotAndExists.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 Cheese cheddar = new Cheese("cheddar", 7);
    final Cheese provolone = new Cheese("provolone", 5);
    final Cheesery cheesery = new Cheesery();

    cheesery.addCheese(cheddar);
    cheesery.addCheese(provolone);

    FactHandle handle = workingMemory.insert(cheesery);
    workingMemory.fireAllRules();
    assertEquals(0, list.size());

    cheesery.addCheese(new Cheese("stilton", 10));
    cheesery.removeCheese(cheddar);
    workingMemory.update(handle, cheesery);
    workingMemory.fireAllRules();
    assertEquals(2, list.size());
  }
  @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());
  }
  @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());
  }
  @Test
  public void testExists() throws Exception {
    final PackageBuilder builder = new PackageBuilder();
    builder.addPackageFromDrl(
        new InputStreamReader(getClass().getResourceAsStream("exists_rule_test.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("list", list);

    final Cheese cheddar = new Cheese("cheddar", 7);
    final FactHandle cheddarHandle = workingMemory.insert(cheddar);
    workingMemory.fireAllRules();

    assertEquals(0, list.size());

    final Cheese stilton = new Cheese("stilton", 5);
    final FactHandle stiltonHandle = workingMemory.insert(stilton);
    workingMemory.fireAllRules();

    assertEquals(1, list.size());

    final Cheese brie = new Cheese("brie", 5);
    final FactHandle brieHandle = workingMemory.insert(brie);
    workingMemory.fireAllRules();

    assertEquals(1, list.size());
  }
  @Test
  public void testNotWithBindings() throws Exception {
    final PackageBuilder builder = new PackageBuilder();
    builder.addPackageFromDrl(
        new InputStreamReader(getClass().getResourceAsStream("not_with_bindings_rule_test.drl")));
    final Package pkg = builder.getPackage();

    final Rule rule = pkg.getRules()[0];
    assertTrue(rule.isValid());
    assertEquals(0, builder.getErrors().getErrors().length);
    RuleBase ruleBase = getSinglethreadRuleBase();
    ruleBase.addPackage(pkg);
    ruleBase = SerializationHelper.serializeObject(ruleBase);
    final WorkingMemory workingMemory = ruleBase.newStatefulSession();

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

    final Cheese stilton = new Cheese("stilton", 5);
    final FactHandle stiltonHandle = workingMemory.insert(stilton);
    final Cheese cheddar = new Cheese("cheddar", 7);
    final FactHandle cheddarHandle = workingMemory.insert(cheddar);

    final PersonInterface paul = new Person("paul", "stilton", 12);
    workingMemory.insert(paul);
    workingMemory.fireAllRules();

    assertEquals(0, list.size());

    workingMemory.retract(stiltonHandle);

    workingMemory.fireAllRules();

    assertEquals(1, list.size());
  }
  @Test
  public void testCollectNodeSharing() throws Exception {
    final PackageBuilder builder = new PackageBuilder();
    builder.addPackageFromDrl(
        new InputStreamReader(getClass().getResourceAsStream("test_collectNodeSharing.drl")));
    final Package pkg = builder.getPackage();

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

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

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

    workingMemory.insert(new Cheese("stilton", 10));
    workingMemory = SerializationHelper.getSerialisedStatefulSession(workingMemory);
    results = (List) workingMemory.getGlobal("results");

    workingMemory.insert(new Cheese("brie", 15));

    workingMemory.fireAllRules();

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

    assertEquals(1, results.size());

    assertEquals(2, ((List) results.get(0)).size());
  }
  @Test
  public void testForallSinglePatternWithExists() throws Exception {
    final PackageBuilder builder = new PackageBuilder();
    builder.addPackageFromDrl(
        new InputStreamReader(
            getClass().getResourceAsStream("test_ForallSinglePatternWithExists.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);

    workingMemory.insert(new Cheese("stilton", 10));
    workingMemory.insert(new Cheese("brie", 10));
    workingMemory.insert(new Cheese("brie", 10));
    workingMemory.insert(new Order(1, "bob"));
    workingMemory.insert(new Person("bob", "stilton", 10));
    workingMemory.insert(new Person("mark", "stilton"));

    workingMemory.fireAllRules();

    assertEquals(1, list.size());
  }
Beispiel #10
0
  @Test
  @Ignore("This test requires us to fix the propagation order")
  public void testForallSinglePattern() throws Exception {
    final PackageBuilder builder = new PackageBuilder();
    builder.addPackageFromDrl(
        new InputStreamReader(getClass().getResourceAsStream("test_ForallSinglePattern.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);
    int fired = 0;

    // no cheeses, so should fire
    workingMemory.fireAllRules();
    assertEquals(++fired, list.size());

    // only stilton, so should not fire again
    FactHandle stilton1 = workingMemory.insert(new Cheese("stilton", 10));
    workingMemory.fireAllRules();
    assertEquals(fired, list.size());

    // only stilton, so should not fire again
    FactHandle stilton2 = workingMemory.insert(new Cheese("stilton", 11));
    workingMemory.fireAllRules();
    assertEquals(fired, list.size());

    // still only stilton, so should not fire
    workingMemory.retract(stilton1);
    workingMemory.fireAllRules();
    assertEquals(
        ++fired, // we need to fix forall to not fire in this situation
        list.size());

    // there is a brie, so should not fire
    FactHandle brie = workingMemory.insert(new Cheese("brie", 10));
    workingMemory.fireAllRules();
    assertEquals(fired, list.size());

    // no brie anymore, so should fire
    workingMemory.retract(brie);
    workingMemory.fireAllRules();
    assertEquals(++fired, list.size());

    // no more cheese, but since it already fired, should not fire again
    workingMemory.retract(stilton2);
    workingMemory.fireAllRules();
    assertEquals(fired, list.size());
  }
Beispiel #11
0
  @Test
  public void testRemoveIdentitiesSubNetwork() throws Exception {
    final PackageBuilder builder = new PackageBuilder();
    builder.addPackageFromDrl(
        new InputStreamReader(
            getClass().getResourceAsStream("test_removeIdentitiesSubNetwork.drl")));
    final Package pkg = builder.getPackage();

    final RuleBaseConfiguration config = new RuleBaseConfiguration();
    config.setRemoveIdentities(true);
    RuleBase ruleBase = getRuleBase(config);
    ruleBase.addPackage(pkg);
    ruleBase = SerializationHelper.serializeObject(ruleBase);
    final WorkingMemory workingMemory = ruleBase.newStatefulSession();

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

    final Person bob = new Person("bob", "stilton");
    workingMemory.insert(bob);

    final Person mark = new Person("mark", "stilton");
    workingMemory.insert(mark);

    final Cheese stilton1 = new Cheese("stilton", 6);
    final FactHandle stilton1Handle = workingMemory.insert(stilton1);
    final Cheese stilton2 = new Cheese("stilton", 7);
    final FactHandle stilton2Handle = workingMemory.insert(stilton2);

    workingMemory.fireAllRules();
    assertEquals(0, list.size());

    workingMemory.retract(stilton1Handle);

    workingMemory.fireAllRules();
    assertEquals(1, list.size());
    assertEquals(mark, list.get(0));

    workingMemory.retract(stilton2Handle);

    workingMemory.fireAllRules();
    assertEquals(2, list.size());
    assertEquals(bob, list.get(1));
  }
Beispiel #12
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);
  }
Beispiel #13
0
  @Test
  public void testCollectWithContainsOperators() throws Exception {
    final PackageBuilder builder = new PackageBuilder();
    builder.addPackageFromDrl(
        new InputStreamReader(getClass().getResourceAsStream("test_CollectContainsOperator.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 Order order1 = new Order(1, "bob");
    final OrderItem item11 = new OrderItem(order1, 1);
    final OrderItem item12 = new OrderItem(order1, 2);
    final Order order2 = new Order(2, "mark");
    final OrderItem item21 = new OrderItem(order2, 1);
    final OrderItem item22 = new OrderItem(order2, 2);

    workingMemory.insert(order1);
    workingMemory.insert(item11);
    workingMemory.insert(item12);
    workingMemory.insert(order2);
    workingMemory.insert(item21);
    workingMemory.insert(item22);

    workingMemory.fireAllRules();

    int index = 0;
    assertEquals(8, list.size());
    assertSame(order1, list.get(index++));
    assertSame(item11, list.get(index++));
    assertSame(order2, list.get(index++));
    assertSame(item21, list.get(index++));
    assertSame(order1, list.get(index++));
    assertSame(item11, list.get(index++));
    assertSame(order2, list.get(index++));
    assertSame(item21, list.get(index++));
  }
Beispiel #14
0
  @Test
  public void testExists2() throws Exception {
    final PackageBuilder builder = new PackageBuilder();
    builder.addPackageFromDrl(
        new InputStreamReader(getClass().getResourceAsStream("test_exists.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("list", list);

    final Cheese cheddar = new Cheese("cheddar", 7);
    final Cheese provolone = new Cheese("provolone", 5);
    final Person edson = new Person("Edson", "cheddar");
    final Person bob = new Person("Bob", "muzzarela");

    workingMemory.insert(cheddar);
    workingMemory.fireAllRules();
    assertEquals(0, list.size());

    workingMemory.insert(provolone);
    workingMemory.fireAllRules();
    assertEquals(0, list.size());

    workingMemory.insert(edson);
    workingMemory.fireAllRules();
    assertEquals(1, list.size());

    workingMemory.insert(bob);
    workingMemory.fireAllRules();
    assertEquals(1, list.size());
  }
Beispiel #15
0
  @Test
  public void testExistsWithBinding() throws Exception {
    final PackageBuilder builder = new PackageBuilder();
    builder.addPackageFromDrl(
        new InputStreamReader(getClass().getResourceAsStream("test_ExistsWithBindings.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 Cheese c = new Cheese("stilton", 10);
    final Person p = new Person("Mark", "stilton");
    workingMemory.insert(c);
    workingMemory.insert(p);
    workingMemory.fireAllRules();

    assertTrue(list.contains(c.getType()));
    assertEquals(1, list.size());
  }