Search in sources :

Example 1 with PersonInterface

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

the class LiteralTest method testBigDecimalIntegerLiteral.

@Test
public void testBigDecimalIntegerLiteral() throws Exception {
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("big_decimal_and_literal.drl"));
    KieSession session = createKnowledgeSession(kbase);
    final List list = new ArrayList();
    session.setGlobal("list", list);
    final PersonInterface bill = new Person("bill", null, 12);
    bill.setBigDecimal(new BigDecimal("42"));
    bill.setBigInteger(new BigInteger("42"));
    session.insert(bill);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
    session.fireAllRules();
    assertEquals(6, ((List) session.getGlobal("list")).size());
}
Also used : PersonInterface(org.drools.compiler.PersonInterface) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.compiler.Person) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 2 with PersonInterface

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

the class EvalTest method testPredicate.

@Test
public void testPredicate() throws Exception {
    KieBase kbase = loadKnowledgeBase("predicate_rule_test.drl");
    kbase = SerializationHelper.serializeObject(kbase);
    KieSession ksession = kbase.newKieSession();
    ksession.setGlobal("two", 2);
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    final PersonInterface peter = new Person("peter", null, 12);
    ksession.insert(peter);
    final PersonInterface jane = new Person("jane", null, 10);
    ksession.insert(jane);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    assertEquals(jane, ((List) ksession.getGlobal("list")).get(0));
    assertEquals(peter, ((List) ksession.getGlobal("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 3 with PersonInterface

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

the class MathTest method testAddition.

@Test
public void testAddition() throws Exception {
    KieBase kbase = loadKnowledgeBase("returnvalue_rule_test.drl");
    kbase = SerializationHelper.serializeObject(kbase);
    KieSession ksession = kbase.newKieSession();
    ksession.setGlobal("two", 2);
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    final PersonInterface peter = new Person("peter", null, 12);
    ksession.insert(peter);
    final PersonInterface jane = new Person("jane", null, 10);
    ksession.insert(jane);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    assertEquals(jane, ((List) ksession.getGlobal("list")).get(0));
    assertEquals(peter, ((List) ksession.getGlobal("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 4 with PersonInterface

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

the class ExecutionFlowControlTest method testSalienceIntegerAndLoadOrder.

@Test(timeout = 10000)
public void testSalienceIntegerAndLoadOrder() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_salienceIntegerRule.drl");
    KieSession ksession = kbase.newKieSession();
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    final PersonInterface person = new Person("Edson", "cheese");
    ksession.insert(person);
    ksession.fireAllRules();
    assertEquals("Three rules should have been fired", 3, list.size());
    assertEquals("Rule 4 should have been fired first", "Rule 4", list.get(0));
    assertEquals("Rule 2 should have been fired second", "Rule 2", list.get(1));
    assertEquals("Rule 3 should have been fired third", "Rule 3", list.get(2));
}
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 5 with PersonInterface

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

the class DynamicRulesTest method testDynamicRuleAdditions.

@Test(timeout = 10000)
public void testDynamicRuleAdditions() throws Exception {
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) SerializationHelper.serializeObject(loadKnowledgeBase("test_Dynamic1.drl"));
    KieSession workingMemory = createKnowledgeSession(kbase);
    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 = SerializationHelper.serializeObject(loadKnowledgePackages("test_Dynamic2.drl"));
    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 = SerializationHelper.serializeObject(loadKnowledgePackages("test_Dynamic3.drl"));
    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 = SerializationHelper.serializeObject(loadKnowledgePackages("test_Dynamic4.drl"));
    kbase.addPackages(kpkgs);
    workingMemory.fireAllRules();
    kbase = SerializationHelper.serializeObject(kbase);
    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.compiler.PersonInterface) KiePackage(org.kie.api.definition.KiePackage) 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)

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