コード例 #1
0
  @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());
  }
コード例 #2
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);
  }
コード例 #3
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());
  }