Search in sources :

Example 6 with Cheesery

use of org.drools.compiler.Cheesery in project drools by kiegroup.

the class FirstOrderLogicTest method testCollectAfterOrCE.

@Test
public void testCollectAfterOrCE() throws Exception {
    Collection<KiePackage> pkgs = loadKnowledgePackages("test_OrCEFollowedByCollect.drl");
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(pkgs);
    KieSession session = createKnowledgeSession(kbase);
    // Set up facts
    final Cheesery bonFromage = new Cheesery();
    bonFromage.addCheese(new Cheese("cheddar"));
    bonFromage.addCheese(new Cheese("cheddar"));
    session.insert(bonFromage);
    int rules = session.fireAllRules();
    assertEquals(2, rules);
    // Serialize and test again
    pkgs = SerializationHelper.serializeObject(pkgs);
    kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(pkgs);
    session = createKnowledgeSession(kbase);
    session.insert(bonFromage);
    rules = session.fireAllRules();
    assertEquals(2, rules);
}
Also used : KiePackage(org.kie.api.definition.KiePackage) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) Cheesery(org.drools.compiler.Cheesery) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 7 with Cheesery

use of org.drools.compiler.Cheesery in project drools by kiegroup.

the class AccumulateTest method testAccumulateWithFromChaining.

@Test(timeout = 10000)
public void testAccumulateWithFromChaining() throws Exception {
    // read in the source
    KieSession wm = getKieSessionFromResources("test_AccumulateWithFromChaining.drl");
    final List<?> results = new ArrayList<Object>();
    wm.setGlobal("results", results);
    final Cheese[] cheese = new Cheese[] { new Cheese("stilton", 8), new Cheese("stilton", 10), new Cheese("stilton", 9), new Cheese("brie", 4), new Cheese("brie", 1), new Cheese("provolone", 8) };
    Cheesery cheesery = new Cheesery();
    for (int i = 0; i < cheese.length; i++) {
        cheesery.addCheese(cheese[i]);
    }
    FactHandle cheeseryHandle = wm.insert(cheesery);
    final Person bob = new Person("Bob", "stilton");
    final FactHandle bobHandle = wm.insert(bob);
    // ---------------- 1st scenario
    wm.fireAllRules();
    // one fire, as per rule constraints
    assertEquals(1, results.size());
    assertEquals(3, ((List) results.get(results.size() - 1)).size());
    // ---------------- 2nd scenario
    final int index = 1;
    cheese[index].setType("brie");
    wm.update(cheeseryHandle, cheesery);
    wm.fireAllRules();
    // no fire
    assertEquals(1, results.size());
    System.out.println(results);
    // ---------------- 3rd scenario
    bob.setLikes("brie");
    wm.update(bobHandle, bob);
    wm.fireAllRules();
    // 2 fires
    assertEquals(2, results.size());
    assertEquals(3, ((List) results.get(results.size() - 1)).size());
    // ---------------- 4th scenario
    cheesery.getCheeses().remove(cheese[3]);
    wm.update(cheeseryHandle, cheesery);
    wm.fireAllRules();
    // should not have fired as per constraint
    assertEquals(2, results.size());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) Cheesery(org.drools.compiler.Cheesery) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 8 with Cheesery

use of org.drools.compiler.Cheesery in project drools by kiegroup.

the class AlphaTest method testNPEOnMVELAlphaPredicates.

@Test
public void testNPEOnMVELAlphaPredicates() throws Exception {
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_NPEOnMVELPredicate.drl"));
    final KieSession session = createKnowledgeSession(kbase);
    final List list = new ArrayList();
    session.setGlobal("results", list);
    final Cheese cheese = new Cheese("stilton", 10);
    final Cheesery cheesery = new Cheesery();
    cheesery.addCheese(cheese);
    final Person bob = new Person("bob", "stilton");
    final Cheese cheese2 = new Cheese();
    bob.setCheese(cheese2);
    final FactHandle p = session.insert(bob);
    final FactHandle c = session.insert(cheesery);
    session.fireAllRules();
    assertEquals("should not have fired", 0, list.size());
    cheese2.setType("stilton");
    session.update(p, bob);
    session.fireAllRules();
    assertEquals(1, list.size());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.drools.compiler.Cheese) Cheesery(org.drools.compiler.Cheesery) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 9 with Cheesery

use of org.drools.compiler.Cheesery in project drools by kiegroup.

the class FirstOrderLogicTest method testCollectWithNestedFromWithParams.

@Test
public void testCollectWithNestedFromWithParams() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_CollectWithNestedFrom.drl");
    KieSession workingMemory = createKnowledgeSession(kbase);
    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());
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Cheese(org.drools.compiler.Cheese) Cheesery(org.drools.compiler.Cheesery) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 10 with Cheesery

use of org.drools.compiler.Cheesery in project drools by kiegroup.

the class FirstOrderLogicTest method testFromInsideNotAndExists.

@Test
public void testFromInsideNotAndExists() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_FromInsideNotAndExists.drl");
    KieSession workingMemory = createKnowledgeSession(kbase);
    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 = (FactHandle) 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());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Cheese(org.drools.compiler.Cheese) Cheesery(org.drools.compiler.Cheesery) Test(org.junit.Test)

Aggregations

Cheesery (org.drools.compiler.Cheesery)16 Test (org.junit.Test)16 KieSession (org.kie.api.runtime.KieSession)16 Cheese (org.drools.compiler.Cheese)14 ArrayList (java.util.ArrayList)13 KieBase (org.kie.api.KieBase)12 List (java.util.List)11 Person (org.drools.compiler.Person)6 FactHandle (org.kie.api.runtime.rule.FactHandle)6 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)3 Dimension (java.awt.Dimension)2 Worker (org.drools.compiler.Worker)2 SessionInspector (org.drools.core.util.debug.SessionInspector)2 StatefulKnowledgeSessionInfo (org.drools.core.util.debug.StatefulKnowledgeSessionInfo)2 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)2 KiePackage (org.kie.api.definition.KiePackage)1 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)1 KieHelper (org.kie.internal.utils.KieHelper)1