Search in sources :

Example 6 with Cheese

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

the class FunctionsTest method testFunction.

@SuppressWarnings("unchecked")
@Test
public void testFunction() throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(this.getClass(), kieBaseTestConfiguration, "test_FunctionInConsequence.drl");
    KieSession ksession = kbase.newKieSession();
    final List<Integer> list = new ArrayList<Integer>();
    ksession.setGlobal("list", list);
    final Cheese stilton = new Cheese("stilton", 5);
    ksession.insert(stilton);
    ksession.fireAllRules();
    assertEquals(new Integer(5), ((List<Integer>) ksession.getGlobal("list")).get(0));
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.mvel.compiler.Cheese) Test(org.junit.Test)

Example 7 with Cheese

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

the class IntegrationInterfacesTest method testGlobals2.

@SuppressWarnings("unchecked")
@Test
public void testGlobals2() throws Exception {
    final KieBase kbase = getKnowledgeBase("test_globalsAsConstraints.drl");
    KieSession ksession = kbase.newKieSession();
    final List<Object> results = mock(List.class);
    ksession.setGlobal("results", results);
    final List<String> cheeseTypes = mock(List.class);
    ksession.setGlobal("cheeseTypes", cheeseTypes);
    when(cheeseTypes.contains("stilton")).thenReturn(Boolean.TRUE);
    when(cheeseTypes.contains("muzzarela")).thenReturn(Boolean.TRUE);
    final Cheese stilton = new Cheese("stilton", 5);
    ksession.insert(stilton);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    verify(results, times(1)).add("memberOf");
    final Cheese brie = new Cheese("brie", 5);
    ksession.insert(brie);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    verify(results, times(1)).add("not memberOf");
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.mvel.compiler.Cheese) Test(org.junit.Test)

Example 8 with Cheese

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

the class FirstOrderLogicTest method testCollectResultConstraints.

@Test
public void testCollectResultConstraints() throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_CollectResultConstraints.drl");
    KieSession wm = kbase.newKieSession();
    List results = new ArrayList();
    wm.setGlobal("results", results);
    wm.insert(new Cheese("stilton", 10));
    wm = SerializationHelper.getSerialisedStatefulKnowledgeSession(wm, true);
    results = (List) wm.getGlobal("results");
    wm.fireAllRules();
    assertEquals(1, results.size());
    assertEquals(1, ((Collection) results.get(0)).size());
    wm.insert(new Cheese("stilton", 7));
    wm.insert(new Cheese("stilton", 8));
    wm.fireAllRules();
    wm = SerializationHelper.getSerialisedStatefulKnowledgeSession(wm, true);
    results = (List) wm.getGlobal("results");
    assertEquals(1, results.size());
    // It's 3 as while the rule does not fire, it does continue to evaluate and update the collection
    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) 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 9 with Cheese

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

the class FirstOrderLogicTest method testOr.

@Test
public void testOr() throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_OrNesting.drl");
    KieSession workingMemory = kbase.newKieSession();
    final List list = new ArrayList();
    workingMemory.setGlobal("results", list);
    final Cheese cheddar = new Cheese("cheddar", 7);
    final Cheese provolone = new Cheese("provolone", 5);
    final Cheese brie = new Cheese("brie", 15);
    final Person mark = new Person("mark", "stilton");
    FactHandle ch = (FactHandle) workingMemory.insert(cheddar);
    FactHandle ph = (FactHandle) workingMemory.insert(provolone);
    FactHandle bh = (FactHandle) workingMemory.insert(brie);
    FactHandle markh = (FactHandle) workingMemory.insert(mark);
    workingMemory.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) 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 10 with Cheese

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

the class FirstOrderLogicTest method testForall.

@Test
public void testForall() throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_Forall.drl");
    KieSession workingMemory = kbase.newKieSession();
    final List list = new ArrayList();
    workingMemory.setGlobal("results", list);
    final State state = new State("SP");
    workingMemory.insert(state);
    final Person bob = new Person("Bob");
    bob.setStatus(state.getState());
    bob.setLikes("stilton");
    workingMemory.insert(bob);
    workingMemory.fireAllRules();
    assertEquals(0, list.size());
    workingMemory.insert(new Cheese(bob.getLikes(), 10));
    workingMemory.fireAllRules();
    assertEquals(1, list.size());
}
Also used : KieBase(org.kie.api.KieBase) State(org.drools.mvel.compiler.State) 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