Search in sources :

Example 66 with Person

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

the class MarshallingTest method testSerializeAdd2.

/*
     *  Here I am inserting data points which are not used by any rule (e.g Person).
     *  Later adding rule (e.g. Rule: 'match Person') for those data points.
     *
     *  Result: Pkg/Rule addition is failing with ClassCastException
     */
@Test
public void testSerializeAdd2() throws Exception {
    // Create a rulebase, a session, and test it
    Collection<KiePackage> kpkgs = loadKnowledgePackages("test_Dynamic1_0.drl");
    kpkgs = SerializationHelper.serializeObject(kpkgs);
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
    kBase.addPackages(kpkgs);
    List results = new ArrayList();
    KieSession session = kBase.newKieSession();
    session.setGlobal("results", results);
    InternalFactHandle stilton1 = (InternalFactHandle) session.insert(new Cheese("stilton", 10));
    session.insert(new Cheese("brie", 10));
    InternalFactHandle bob = (InternalFactHandle) session.insert(new Person("bob", 10));
    // fire rules
    session.fireAllRules();
    // check the results are correct
    assertEquals(1, results.size());
    assertEquals(stilton1.getObject(), results.get(0));
    // serialize session and rulebase
    session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
    // dynamically add a new package
    kpkgs = loadKnowledgePackages("test_Dynamic3_0.drl");
    kBase.addPackages(SerializationHelper.serializeObject(kpkgs));
    session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
    InternalFactHandle stilton2 = (InternalFactHandle) session.insert(new Cheese("stilton", 20));
    session.insert(new Cheese("brie", 20));
    InternalFactHandle mark = (InternalFactHandle) session.insert(new Person("mark", 20));
    session.fireAllRules();
    results = (List) session.getGlobal("results");
    assertEquals(4, results.size());
    assertEquals(stilton2.getObject(), results.get(1));
    assertEquals(bob.getObject(), results.get(2));
    assertEquals(mark.getObject(), results.get(3));
    session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
    // dispose session
    session.dispose();
}
Also used : KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) IteratorToList(org.drools.mvel.integrationtests.IteratorToList) List(java.util.List) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.mvel.compiler.Cheese) InternalFactHandle(org.drools.core.common.InternalFactHandle) Person(org.drools.mvel.compiler.Person) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 67 with Person

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

the class TruthMaintenanceTest method testLogicalInsertionsUpdateEqual.

@Test(timeout = 10000)
@Ignore("Currently cannot support updates")
public // @Ignore("in Java 8, the byte[] generated by serialization are not the same and requires investigation")
void testLogicalInsertionsUpdateEqual() throws Exception {
    // calling update on a justified FH, states it
    KieBase kbase = loadKnowledgeBase("test_LogicalInsertionsUpdateEqual.drl");
    KieSession ksession = kbase.newKieSession();
    try {
        final Person p = new Person("person");
        p.setAge(2);
        FactHandle h = ksession.insert(p);
        assertEquals(1, ksession.getObjects().size());
        ksession.fireAllRules();
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        assertEquals(2, ksession.getObjects().size());
        Collection l = ksession.getObjects(new ClassObjectFilter(CheeseEqual.class));
        assertEquals(1, l.size());
        assertEquals(3, ((CheeseEqual) l.iterator().next()).getPrice());
        h = getFactHandle(h, ksession);
        ksession.delete(h);
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        Collection list = ksession.getObjects();
        // CheeseEqual was updated, making it stated, so it wouldn't have been logically deleted
        assertEquals(1, list.size());
        assertEquals(new CheeseEqual("person", 3), list.iterator().next());
        FactHandle fh = ksession.getFactHandle(list.iterator().next());
        ksession.delete(fh);
        list = ksession.getObjects();
        assertEquals(0, list.size());
        TruthMaintenanceSystem tms = TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem((ReteEvaluator) ksession);
        final java.lang.reflect.Field field = tms.getClass().getDeclaredField("equalityKeyMap");
        field.setAccessible(true);
        final ObjectHashMap m = (ObjectHashMap) field.get(tms);
        field.setAccessible(false);
        assertEquals("assertMap should be empty", 0, m.size());
    } finally {
        ksession.dispose();
    }
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) CheeseEqual(org.drools.mvel.compiler.CheeseEqual) ClassObjectFilter(org.drools.core.ClassObjectFilter) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) Collection(java.util.Collection) ObjectHashMap(org.drools.core.util.ObjectHashMap) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.mvel.compiler.Person) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 68 with Person

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

the class TruthMaintenanceTest method testLogicalInsertionsWithExists.

@Test(timeout = 10000)
public // @Ignore("in Java 8, the byte[] generated by serialization are not the same and requires investigation")
void testLogicalInsertionsWithExists() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_LogicalInsertionWithExists.drl");
    KieSession ksession = kbase.newKieSession();
    try {
        final Person p1 = new Person("p1", "stilton", 20);
        p1.setStatus("europe");
        FactHandle c1FactHandle = ksession.insert(p1);
        final Person p2 = new Person("p2", "stilton", 30);
        p2.setStatus("europe");
        FactHandle c2FactHandle = ksession.insert(p2);
        final Person p3 = new Person("p3", "stilton", 40);
        p3.setStatus("europe");
        FactHandle c3FactHandle = ksession.insert(p3);
        ksession.fireAllRules();
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        // all 3 in europe, so, 2 cheese
        Collection cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
        assertEquals(2, cheeseList.size());
        // europe=[ 1, 2 ], america=[ 3 ]
        p3.setStatus("america");
        c3FactHandle = getFactHandle(c3FactHandle, ksession);
        ksession.update(c3FactHandle, p3);
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        ksession.fireAllRules();
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
        assertEquals(1, cheeseList.size());
        // europe=[ 1 ], america=[ 2, 3 ]
        p2.setStatus("america");
        c2FactHandle = getFactHandle(c2FactHandle, ksession);
        ksession.update(c2FactHandle, p2);
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        ksession.fireAllRules();
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
        assertEquals(1, cheeseList.size());
        // europe=[ ], america=[ 1, 2, 3 ]
        p1.setStatus("america");
        c1FactHandle = getFactHandle(c1FactHandle, ksession);
        ksession.update(c1FactHandle, p1);
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        ksession.fireAllRules();
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
        assertEquals(2, cheeseList.size());
        // europe=[ 2 ], america=[ 1, 3 ]
        p2.setStatus("europe");
        c2FactHandle = getFactHandle(c2FactHandle, ksession);
        ksession.update(c2FactHandle, p2);
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        ksession.fireAllRules();
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
        assertEquals(1, cheeseList.size());
        // europe=[ 1, 2 ], america=[ 3 ]
        p1.setStatus("europe");
        c1FactHandle = getFactHandle(c1FactHandle, ksession);
        ksession.update(c1FactHandle, p1);
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        ksession.fireAllRules();
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
        assertEquals(1, cheeseList.size());
        // europe=[ 1, 2, 3 ], america=[ ]
        p3.setStatus("europe");
        c3FactHandle = getFactHandle(c3FactHandle, ksession);
        ksession.update(c3FactHandle, p3);
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        ksession.fireAllRules();
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
        assertEquals(2, cheeseList.size());
    } finally {
        ksession.dispose();
    }
}
Also used : ClassObjectFilter(org.drools.core.ClassObjectFilter) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.mvel.compiler.Cheese) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 69 with Person

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

the class TruthMaintenanceTest method testLogicalInsertionsNot.

@Test(timeout = 10000)
public // @Ignore("in Java 8, the byte[] generated by serialization are not the same and requires investigation")
void testLogicalInsertionsNot() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_LogicalInsertionsNot.drl");
    KieSession ksession = kbase.newKieSession();
    try {
        final Person a = new Person("a");
        final Cheese cheese = new Cheese("brie", 1);
        ksession.setGlobal("cheese", cheese);
        ksession.fireAllRules();
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        Collection list = ksession.getObjects();
        assertEquals("i was not asserted by not a => i.", 1, list.size());
        assertEquals("i was not asserted by not a => i.", cheese, list.iterator().next());
        FactHandle h = ksession.insert(a);
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        // no need to fire rules, assertion alone removes justification for i,
        // so it should be deleted.
        // workingMemory.fireAllRules();
        ksession.fireAllRules();
        list = ksession.getObjects();
        assertEquals("a was not asserted or i not deleted.", 1, list.size());
        assertEquals("a was asserted.", a, list.iterator().next());
        assertFalse("i was not rectracted.", list.contains(cheese));
        // no rules should fire, but nevertheless...
        // workingMemory.fireAllRules();
        assertEquals("agenda should be empty.", 0, ((InternalAgenda) ((StatefulKnowledgeSessionImpl) ksession).getAgenda()).getAgendaGroupsManager().agendaSize());
        h = getFactHandle(h, ksession);
        ksession.delete(h);
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        ksession.fireAllRules();
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        list = ksession.getObjects();
        assertEquals("i was not asserted by not a => i.", 1, list.size());
        assertEquals("i was not asserted by not a => i.", cheese, list.iterator().next());
    } finally {
        ksession.dispose();
    }
}
Also used : InternalAgenda(org.drools.core.common.InternalAgenda) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.mvel.compiler.Cheese) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 70 with Person

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

the class MVELTest method testTypeCoercionLongDivByInt.

@Test
public void testTypeCoercionLongDivByInt() {
    // DROOLS-5051
    String str = "package com.sample\n" + "import " + Person.class.getCanonicalName() + ";\n" + "rule R1\n" + "no-loop true\n" + "dialect \"mvel\"\n" + "when\n" + "  $p : Person()\n" + "then\n" + "  modify ($p) { setBigDecimal(15 * Math.round( new java.math.BigDecimal(\"49.4\") ) / 100 ) }\n" + "end";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    KieSession ksession = kbase.newKieSession();
    Person p = new Person("Toshiya");
    ksession.insert(p);
    ksession.fireAllRules();
    assertEquals(new BigDecimal(7.35d, MathContext.DECIMAL32), p.getBigDecimal().round(MathContext.DECIMAL32));
}
Also used : KieBase(org.kie.api.KieBase) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.mvel.compiler.Person) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Aggregations

Person (org.drools.mvel.compiler.Person)196 Test (org.junit.Test)185 KieSession (org.kie.api.runtime.KieSession)178 KieBase (org.kie.api.KieBase)171 ArrayList (java.util.ArrayList)98 List (java.util.List)72 Cheese (org.drools.mvel.compiler.Cheese)46 FactHandle (org.kie.api.runtime.rule.FactHandle)38 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)34 Address (org.drools.mvel.compiler.Address)33 FactWithString (org.drools.mvel.integrationtests.facts.FactWithString)24 InternalFactHandle (org.drools.core.common.InternalFactHandle)23 KiePackage (org.kie.api.definition.KiePackage)18 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)16 IteratorToList (org.drools.mvel.integrationtests.IteratorToList)15 HashMap (java.util.HashMap)12 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)12 Collection (java.util.Collection)11 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)8 AlphaNode (org.drools.core.reteoo.AlphaNode)8