Search in sources :

Example 11 with Cheese

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

the class FirstOrderLogicTest method testCollectAfterOrCE.

@Test
public void testCollectAfterOrCE() throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_OrCEFollowedByCollect.drl");
    KieSession session = kbase.newKieSession();
    // 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);
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.mvel.compiler.Cheese) Cheesery(org.drools.mvel.compiler.Cheesery) Test(org.junit.Test)

Example 12 with Cheese

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

the class FirstOrderLogicTest method testExists2.

@Test
public void testExists2() throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_exists.drl");
    KieSession workingMemory = kbase.newKieSession();
    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());
}
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.mvel.compiler.Cheese) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 13 with Cheese

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

the class FirstOrderLogicTest method testNot.

@Test
public void testNot() throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "not_rule_test.drl");
    KieSession wm = kbase.newKieSession();
    final List list = new ArrayList();
    wm.setGlobal("list", list);
    final Cheese stilton = new Cheese("stilton", 5);
    final FactHandle stiltonHandle = (FactHandle) wm.insert(stilton);
    final Cheese cheddar = new Cheese("cheddar", 7);
    final FactHandle cheddarHandle = (FactHandle) wm.insert(cheddar);
    wm.fireAllRules();
    assertEquals(0, list.size());
    wm.retract(stiltonHandle);
    wm.fireAllRules();
    assertEquals(4, list.size());
    assertTrue(list.contains(new Integer(5)));
    assertTrue(list.contains(new Integer(6)));
    assertTrue(list.contains(new Integer(7)));
    assertTrue(list.contains(new Integer(8)));
}
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.mvel.compiler.Cheese) Test(org.junit.Test)

Example 14 with Cheese

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

the class FirstOrderLogicTest method testCollect.

@Test
public void testCollect() throws Exception {
    List results = new ArrayList();
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_Collect.drl");
    KieSession wm = kbase.newKieSession();
    wm.setGlobal("results", results);
    wm.insert(new Cheese("stilton", 10));
    wm.insert(new Cheese("stilton", 7));
    wm.insert(new Cheese("stilton", 8));
    wm.insert(new Cheese("brie", 5));
    wm.insert(new Cheese("provolone", 150));
    wm = SerializationHelper.getSerialisedStatefulKnowledgeSession(wm, true);
    results = (List) wm.getGlobal("results");
    wm.insert(new Cheese("provolone", 20));
    wm.insert(new Person("Bob", "stilton"));
    wm.insert(new Person("Mark", "provolone"));
    wm = SerializationHelper.getSerialisedStatefulKnowledgeSession(wm, true);
    results = (List) wm.getGlobal("results");
    wm.fireAllRules();
    wm = SerializationHelper.getSerialisedStatefulKnowledgeSession(wm, true);
    results = (List) wm.getGlobal("results");
    assertEquals(1, results.size());
    assertEquals(3, ((Collection) results.get(0)).size());
    assertEquals(ArrayList.class.getName(), results.get(0).getClass().getName());
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.mvel.compiler.Cheese) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 15 with Cheese

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

the class FirstOrderLogicTest method testExistsWithBinding.

@Test
public void testExistsWithBinding() throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_ExistsWithBindings.drl");
    KieSession wm = kbase.newKieSession();
    final List list = new ArrayList();
    wm.setGlobal("results", list);
    final Cheese c = new Cheese("stilton", 10);
    final Person p = new Person("Mark", "stilton");
    wm.insert(c);
    wm.insert(p);
    wm.fireAllRules();
    assertTrue(list.contains(c.getType()));
    assertEquals(1, list.size());
}
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.mvel.compiler.Cheese) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Aggregations

Cheese (org.drools.mvel.compiler.Cheese)136 Test (org.junit.Test)129 KieSession (org.kie.api.runtime.KieSession)112 KieBase (org.kie.api.KieBase)96 ArrayList (java.util.ArrayList)90 List (java.util.List)65 Person (org.drools.mvel.compiler.Person)47 FactHandle (org.kie.api.runtime.rule.FactHandle)29 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)24 KiePackage (org.kie.api.definition.KiePackage)23 InternalFactHandle (org.drools.core.common.InternalFactHandle)20 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)20 IteratorToList (org.drools.mvel.integrationtests.IteratorToList)14 Command (org.kie.api.command.Command)10 ExecutionResults (org.kie.api.runtime.ExecutionResults)10 PackageDescr (org.drools.drl.ast.descr.PackageDescr)7 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)6 Cheesery (org.drools.mvel.compiler.Cheesery)6 ClassObjectFilter (org.drools.core.ClassObjectFilter)5 ClassObjectType (org.drools.core.base.ClassObjectType)5