Search in sources :

Example 76 with Cheese

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

the class FirstOrderLogicTest method testForallSinglePatternWithExists.

@Test
public void testForallSinglePatternWithExists() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_ForallSinglePatternWithExists.drl");
    KieSession workingMemory = createKnowledgeSession(kbase);
    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());
}
Also used : Order(org.drools.compiler.Order) 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) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 77 with Cheese

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

the class FirstOrderLogicTest method testCollectModifyAlphaRestriction.

@Test
public void testCollectModifyAlphaRestriction() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_CollectAlphaRestriction.drl");
    KieSession wm = createKnowledgeSession(kbase);
    final List results = new ArrayList();
    wm.setGlobal("results", results);
    final Cheese[] cheese = new Cheese[] { new Cheese("stilton", 10), new Cheese("stilton", 2), new Cheese("stilton", 5), new Cheese("brie", 15), new Cheese("brie", 16), new Cheese("provolone", 8) };
    final FactHandle[] cheeseHandles = new FactHandle[cheese.length];
    for (int i = 0; i < cheese.length; i++) {
        cheeseHandles[i] = (FactHandle) wm.insert(cheese[i]);
    }
    // ---------------- 1st scenario
    int fireCount = 0;
    wm.fireAllRules();
    assertEquals(++fireCount, results.size());
    assertEquals(3, ((Collection) results.get(fireCount - 1)).size());
    assertEquals(ArrayList.class.getName(), results.get(fireCount - 1).getClass().getName());
    // ---------------- 2nd scenario
    final int index = 1;
    cheese[index].setType("brie");
    wm.update(cheeseHandles[index], cheese[index]);
    wm.fireAllRules();
    assertEquals(++fireCount, results.size());
    assertEquals(2, ((Collection) results.get(fireCount - 1)).size());
    assertEquals(ArrayList.class.getName(), results.get(fireCount - 1).getClass().getName());
    // ---------------- 3rd scenario
    wm.retract(cheeseHandles[2]);
    wm.fireAllRules();
    assertEquals(++fireCount, results.size());
    assertEquals(1, ((Collection) results.get(fireCount - 1)).size());
    assertEquals(ArrayList.class.getName(), results.get(fireCount - 1).getClass().getName());
}
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) Test(org.junit.Test)

Example 78 with Cheese

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

the class FirstOrderLogicTest method testCollectModify.

@Test
public void testCollectModify() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_Collect.drl");
    KieSession wm = createKnowledgeSession(kbase);
    List results = new ArrayList();
    wm.setGlobal("results", results);
    final Cheese[] cheese = new Cheese[] { new Cheese("stilton", 10), new Cheese("stilton", 2), new Cheese("stilton", 5), new Cheese("brie", 15), new Cheese("brie", 16), new Cheese("provolone", 8) };
    final Person bob = new Person("Bob", "stilton");
    final FactHandle[] cheeseHandles = new FactHandle[cheese.length];
    for (int i = 0; i < cheese.length; i++) {
        cheeseHandles[i] = (FactHandle) wm.insert(cheese[i]);
    }
    final FactHandle bobHandle = (FactHandle) wm.insert(bob);
    // ---------------- 1st scenario
    int fireCount = 0;
    wm.fireAllRules();
    assertEquals(++fireCount, results.size());
    assertEquals(3, ((Collection) results.get(fireCount - 1)).size());
    assertEquals(ArrayList.class.getName(), results.get(fireCount - 1).getClass().getName());
    // ---------------- 2nd scenario
    final int index = 1;
    cheese[index].setPrice(9);
    wm.update(cheeseHandles[index], cheese[index]);
    wm.fireAllRules();
    assertEquals(++fireCount, results.size());
    assertEquals(3, ((Collection) results.get(fireCount - 1)).size());
    assertEquals(ArrayList.class.getName(), results.get(fireCount - 1).getClass().getName());
    // ---------------- 3rd scenario
    bob.setLikes("brie");
    wm.update(bobHandle, bob);
    wm.fireAllRules();
    assertEquals(fireCount, results.size());
    // ---------------- 4th scenario
    wm.retract(cheeseHandles[3]);
    wm.fireAllRules();
    // should not have fired as per constraint
    assertEquals(fireCount, results.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) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 79 with Cheese

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

the class FirstOrderLogicTest method testForallSinglePattern.

@Test
public void testForallSinglePattern() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_ForallSinglePattern.drl");
    KieSession workingMemory = createKnowledgeSession(kbase);
    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 = (FactHandle) workingMemory.insert(new Cheese("stilton", 10));
    workingMemory.fireAllRules();
    assertEquals(fired, list.size());
    // only stilton, so should not fire again
    FactHandle stilton2 = (FactHandle) 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, list.size());
    // there is a brie, so should not fire
    FactHandle brie = (FactHandle) 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());
}
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) Test(org.junit.Test)

Example 80 with Cheese

use of org.drools.compiler.Cheese 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)

Aggregations

Cheese (org.drools.compiler.Cheese)222 Test (org.junit.Test)204 KieSession (org.kie.api.runtime.KieSession)195 ArrayList (java.util.ArrayList)152 KieBase (org.kie.api.KieBase)138 List (java.util.List)100 Person (org.drools.compiler.Person)92 FactHandle (org.kie.api.runtime.rule.FactHandle)53 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)36 KiePackage (org.kie.api.definition.KiePackage)22 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)21 InternalFactHandle (org.drools.core.common.InternalFactHandle)20 Cheesery (org.drools.compiler.Cheesery)14 IteratorToList (org.drools.compiler.integrationtests.IteratorToList)14 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)13 HashMap (java.util.HashMap)10 Command (org.kie.api.command.Command)8 ExecutionResults (org.kie.api.runtime.ExecutionResults)8 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)7 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)7