Search in sources :

Example 16 with Cheese

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

the class FirstOrderLogicTest method testCollectNodeSharing.

@Test
public void testCollectNodeSharing() throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_collectNodeSharing.drl");
    KieSession wm = kbase.newKieSession();
    List results = new ArrayList();
    wm.setGlobal("results", results);
    wm = SerializationHelper.getSerialisedStatefulKnowledgeSession(wm, true);
    results = (List) wm.getGlobal("results");
    wm.insert(new Cheese("stilton", 10));
    wm = SerializationHelper.getSerialisedStatefulKnowledgeSession(wm, true);
    results = (List) wm.getGlobal("results");
    wm.insert(new Cheese("brie", 15));
    wm.fireAllRules();
    wm = SerializationHelper.getSerialisedStatefulKnowledgeSession(wm, true);
    results = (List) wm.getGlobal("results");
    assertEquals(1, results.size());
    assertEquals(2, ((List) results.get(0)).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) Test(org.junit.Test)

Example 17 with Cheese

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

the class ExtendsTest method testExtendsBasic2.

@Test
public void testExtendsBasic2() {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_RuleExtend.drl");
    KieSession ksession = kbase.newKieSession();
    final List results = new ArrayList();
    ksession.setGlobal("results", results);
    final Cheese stilton = new Cheese("stilton", 5);
    final Cheese cheddar = new Cheese("cheddar", 7);
    final Cheese brie = new Cheese("brie", 5);
    ksession.insert(stilton);
    ksession.insert(cheddar);
    ksession.insert(brie);
    ksession.fireAllRules();
    assertEquals(2, results.size());
    assertEquals("stilton", results.get(0));
    assertEquals("brie", results.get(1));
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) Cheese(org.drools.mvel.compiler.Cheese) Test(org.junit.Test)

Example 18 with Cheese

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

the class FirstOrderLogicTest method testCollectWithNestedFromWithParams.

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

Example 19 with Cheese

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

the class FirstOrderLogicTest method testMVELCollect.

@Test
public void testMVELCollect() throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_MVELCollect.drl");
    KieSession wm = kbase.newKieSession();
    final List results = new ArrayList();
    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.insert(new Cheese("provolone", 20));
    wm.insert(new Person("Bob", "stilton"));
    wm.insert(new Person("Mark", "provolone"));
    wm.fireAllRules();
    assertEquals(1, results.size());
    assertEquals(6, ((List) results.get(0)).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 20 with Cheese

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

the class FirstOrderLogicTest method testCollectModifyAlphaRestriction.

@Test
public void testCollectModifyAlphaRestriction() throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_CollectAlphaRestriction.drl");
    KieSession wm = kbase.newKieSession();
    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.mvel.compiler.Cheese) 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