Search in sources :

Example 1 with PersonInterface

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

the class FirstOrderLogicTest method testNotWithBindings.

@Test
public void testNotWithBindings() throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "not_with_bindings_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);
    final PersonInterface paul = new Person("paul", "stilton", 12);
    wm.insert(paul);
    wm.fireAllRules();
    assertEquals(0, list.size());
    wm.retract(stiltonHandle);
    wm.fireAllRules();
    assertEquals(1, list.size());
}
Also used : PersonInterface(org.drools.mvel.compiler.PersonInterface) 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 2 with PersonInterface

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

the class NullTest method testNullValuesIndexing.

@Test
public void testNullValuesIndexing() throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_NullValuesIndexing.drl");
    KieSession ksession = kbase.newKieSession();
    // Adding person with null name and likes attributes
    final PersonInterface bob = new Person(null, null);
    bob.setStatus("P1");
    final PersonInterface pete = new Person(null, null);
    bob.setStatus("P2");
    ksession.insert(bob);
    ksession.insert(pete);
    ksession.fireAllRules();
    assertEquals("Indexing with null values is not working correctly.", "OK", bob.getStatus());
    assertEquals("Indexing with null values is not working correctly.", "OK", pete.getStatus());
}
Also used : PersonInterface(org.drools.mvel.compiler.PersonInterface) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 3 with PersonInterface

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

the class DynamicRulesTest method testDynamicRuleRemovals.

@Test(timeout = 10000)
public void testDynamicRuleRemovals() throws Exception {
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.getKieBaseFromClasspathResources("test", getClass(), kieBaseTestConfiguration, "test_Dynamic1.drl", "test_Dynamic3.drl", "test_Dynamic4.drl");
    Collection<KiePackage> kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_Dynamic2.drl").getKiePackages();
    kbase.addPackages(kpkgs);
    KieSession wm = kbase.newKieSession();
    // AgendaEventListener ael = mock( AgendaEventListener.class );
    // wm.addEventListener( ael );
    final List<?> list = new ArrayList<Object>();
    wm.setGlobal("list", list);
    final PersonInterface bob = new Person("bob", "stilton");
    bob.setStatus("Not evaluated");
    FactHandle fh0 = wm.insert(bob);
    final Cheese stilton1 = new Cheese("stilton", 5);
    FactHandle fh1 = wm.insert(stilton1);
    final Cheese stilton2 = new Cheese("stilton", 3);
    FactHandle fh2 = wm.insert(stilton2);
    final Cheese stilton3 = new Cheese("stilton", 1);
    FactHandle fh3 = wm.insert(stilton3);
    final Cheese cheddar = new Cheese("cheddar", 5);
    FactHandle fh4 = wm.insert(cheddar);
    wm.fireAllRules();
    assertEquals(15, list.size());
    list.clear();
    kbase.removeRule("org.drools.mvel.compiler.test", "Who likes Stilton");
    wm.update(fh0, bob);
    wm.update(fh1, stilton1);
    wm.update(fh2, stilton2);
    wm.update(fh3, stilton3);
    wm.update(fh4, cheddar);
    wm.fireAllRules();
    assertEquals(12, list.size());
    list.clear();
    kbase.removeRule("org.drools.mvel.compiler.test", "like cheese");
    wm.update(fh0, bob);
    wm.update(fh1, stilton1);
    wm.update(fh2, stilton2);
    wm.update(fh3, stilton3);
    wm.update(fh4, cheddar);
    wm.fireAllRules();
    assertEquals(8, list.size());
    list.clear();
    final Cheese muzzarela = new Cheese("muzzarela", 5);
    wm.insert(muzzarela);
    wm.fireAllRules();
    assertEquals(1, list.size());
    list.clear();
}
Also used : PersonInterface(org.drools.mvel.compiler.PersonInterface) KiePackage(org.kie.api.definition.KiePackage) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.mvel.compiler.Cheese) Person(org.drools.mvel.compiler.Person) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 4 with PersonInterface

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

the class DynamicRulesTest method testDynamicRuleAdditions.

@Test(timeout = 10000)
public void testDynamicRuleAdditions() throws Exception {
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.getKieBaseFromClasspathResources("test", getClass(), kieBaseTestConfiguration, "test_Dynamic1.drl");
    KieSession workingMemory = kbase.newKieSession();
    workingMemory.setGlobal("total", new Integer(0));
    final List<?> list = new ArrayList<Object>();
    workingMemory.setGlobal("list", list);
    // Adding person in advance. There is no Person() object
    // type node in memory yet, but the rule engine is supposed
    // to handle that correctly
    final PersonInterface bob = new Person("bob", "stilton");
    bob.setStatus("Not evaluated");
    workingMemory.insert(bob);
    final Cheese stilton = new Cheese("stilton", 5);
    workingMemory.insert(stilton);
    final Cheese cheddar = new Cheese("cheddar", 5);
    workingMemory.insert(cheddar);
    workingMemory.fireAllRules();
    assertEquals(1, list.size());
    assertEquals("stilton", list.get(0));
    Collection<KiePackage> kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_Dynamic2.drl").getKiePackages();
    kbase.addPackages(kpkgs);
    workingMemory.fireAllRules();
    assertEquals(5, list.size());
    assertEquals("stilton", list.get(0));
    assertTrue("cheddar".equals(list.get(1)) || "cheddar".equals(list.get(2)));
    assertTrue("stilton".equals(list.get(1)) || "stilton".equals(list.get(2)));
    list.clear();
    kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_Dynamic3.drl").getKiePackages();
    kbase.addPackages(kpkgs);
    // Package 3 has a rule working on Person instances.
    // As we added person instance in advance, rule should fire now
    workingMemory.fireAllRules();
    assertEquals("Rule from package 3 should have been fired", "match Person ok", bob.getStatus());
    assertEquals(1, list.size());
    assertEquals(bob, list.get(0));
    kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_Dynamic4.drl").getKiePackages();
    kbase.addPackages(kpkgs);
    workingMemory.fireAllRules();
    assertEquals("Rule from package 4 should have been fired", "Who likes Stilton ok", bob.getStatus());
    assertEquals(2, list.size());
    assertEquals(bob, list.get(1));
}
Also used : PersonInterface(org.drools.mvel.compiler.PersonInterface) KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.mvel.compiler.Cheese) Person(org.drools.mvel.compiler.Person) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 5 with PersonInterface

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

the class ExecutionFlowControlTest method testSalienceExpression.

@Test
public void testSalienceExpression() throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(this.getClass(), kieBaseTestConfiguration, "test_salienceExpressionRule.drl");
    KieSession ksession = kbase.newKieSession();
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    final PersonInterface person10 = new Person("bob", "cheese", 10);
    ksession.insert(person10);
    final PersonInterface person20 = new Person("mic", "cheese", 20);
    ksession.insert(person20);
    ksession.fireAllRules();
    assertEquals("Two rules should have been fired", 2, list.size());
    assertEquals("Rule 3 should have been fired first", "Rule 3", list.get(0));
    assertEquals("Rule 2 should have been fired second", "Rule 2", list.get(1));
}
Also used : PersonInterface(org.drools.mvel.compiler.PersonInterface) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Aggregations

PersonInterface (org.drools.mvel.compiler.PersonInterface)9 Test (org.junit.Test)9 KieSession (org.kie.api.runtime.KieSession)9 Person (org.drools.mvel.compiler.Person)8 ArrayList (java.util.ArrayList)7 KieBase (org.kie.api.KieBase)7 List (java.util.List)5 Cheese (org.drools.mvel.compiler.Cheese)3 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)2 KiePackage (org.kie.api.definition.KiePackage)2 FactHandle (org.kie.api.runtime.rule.FactHandle)2 InternalFactHandle (org.drools.core.common.InternalFactHandle)1 Person (org.drools.core.test.model.Person)1 Primitives (org.drools.mvel.compiler.Primitives)1