Search in sources :

Example 6 with PersonInterface

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

the class ExecutionFlowControlTest method testSalienceExpression.

@Test
public void testSalienceExpression() throws Exception {
    KieBase kbase = loadKnowledgeBase("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.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.compiler.Person) Test(org.junit.Test)

Example 7 with PersonInterface

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

the class FirstOrderLogicTest method testNotWithBindings.

@Test
public void testNotWithBindings() throws Exception {
    KieBase kbase = loadKnowledgeBase("not_with_bindings_rule_test.drl");
    KieSession wm = createKnowledgeSession(kbase);
    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.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.compiler.Cheese) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 8 with PersonInterface

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

the class DynamicRulesTest method testDynamicRuleRemovals.

@Test(timeout = 10000)
public void testDynamicRuleRemovals() throws Exception {
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) SerializationHelper.serializeObject(loadKnowledgeBase("test_Dynamic1.drl", "test_Dynamic3.drl", "test_Dynamic4.drl"));
    Collection<KiePackage> kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_Dynamic2.drl"));
    kbase.addPackages(kpkgs);
    KieSession wm = createKnowledgeSession(kbase);
    // 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.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.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.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.compiler.Cheese) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 9 with PersonInterface

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

the class NullTest method testNullValuesIndexing.

@Test
public void testNullValuesIndexing() throws Exception {
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_NullValuesIndexing.drl"));
    final 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.compiler.PersonInterface) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 10 with PersonInterface

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

the class NullTest method testNullConstraint.

@Test
public void testNullConstraint() throws Exception {
    final KieBase kbase = loadKnowledgeBase("null_constraint.drl");
    KieSession session = kbase.newKieSession();
    final List foo = new ArrayList();
    session.setGlobal("messages", foo);
    final PersonInterface p1 = new Person(null, "food", 40);
    final Primitives p2 = new Primitives();
    p2.setArrayAttribute(null);
    session.insert(p1);
    session.insert(p2);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
    session.fireAllRules();
    assertEquals(2, ((List) session.getGlobal("messages")).size());
}
Also used : PersonInterface(org.drools.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) Primitives(org.drools.compiler.Primitives) Person(org.drools.compiler.Person) Test(org.junit.Test)

Aggregations

PersonInterface (org.drools.compiler.PersonInterface)14 Test (org.junit.Test)14 KieSession (org.kie.api.runtime.KieSession)14 Person (org.drools.compiler.Person)13 ArrayList (java.util.ArrayList)12 KieBase (org.kie.api.KieBase)12 List (java.util.List)10 Cheese (org.drools.compiler.Cheese)4 BigDecimal (java.math.BigDecimal)2 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)2 KiePackage (org.kie.api.definition.KiePackage)2 FactHandle (org.kie.api.runtime.rule.FactHandle)2 BigInteger (java.math.BigInteger)1 Primitives (org.drools.compiler.Primitives)1 InternalFactHandle (org.drools.core.common.InternalFactHandle)1 Person (org.drools.core.test.model.Person)1