Search in sources :

Example 1 with CheeseEqual

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

the class StatefulSessionTest method testGetFactHandleIdentityBehavior.

@Test
public void testGetFactHandleIdentityBehavior() throws Exception {
    KieBaseTestConfiguration identityConfig = TestParametersUtil.getIdentityInstanceOf(kieBaseTestConfiguration);
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", identityConfig);
    KieSession ksession = kbase.newKieSession();
    final CheeseEqual cheese = new CheeseEqual("stilton", 10);
    ksession.insert(cheese);
    final FactHandle fh1 = ksession.getFactHandle(new Cheese("stilton", 10));
    assertNull(fh1);
    final FactHandle fh2 = ksession.getFactHandle(cheese);
    assertNotNull(fh2);
}
Also used : CheeseEqual(org.drools.mvel.compiler.CheeseEqual) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) KieBaseTestConfiguration(org.drools.testcoverage.common.util.KieBaseTestConfiguration) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.mvel.compiler.Cheese) Test(org.junit.Test)

Example 2 with CheeseEqual

use of org.drools.mvel.compiler.CheeseEqual 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 3 with CheeseEqual

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

the class TruthMaintenanceTest method testLogicalInsertionsAccumulatorPattern.

@Test(timeout = 10000)
public void testLogicalInsertionsAccumulatorPattern() throws Exception {
    // JBRULES-449
    KieBase kbase = loadKnowledgeBase("test_LogicalInsertionsAccumulatorPattern.drl");
    KieSession ksession = kbase.newKieSession();
    try {
        ksession.setGlobal("ga", "a");
        ksession.setGlobal("gb", "b");
        ksession.setGlobal("gs", new Short((short) 3));
        ksession.fireAllRules();
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        FactHandle h = ksession.insert(new Integer(6));
        assertEquals(1, ksession.getObjects().size());
        ksession.fireAllRules();
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        assertEquals("There should be 2 CheeseEqual in Working Memory, 1 justified, 1 stated", 2, ksession.getObjects(new ClassObjectFilter(CheeseEqual.class)).size());
        assertEquals(6, ksession.getObjects().size());
        h = getFactHandle(h, ksession);
        ksession.delete(h);
        ksession.fireAllRules();
        for (Object o : ksession.getObjects()) {
            System.out.println(o);
        }
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        assertEquals(0, ksession.getObjects(new ClassObjectFilter(CheeseEqual.class)).size());
        assertEquals(0, ksession.getObjects(new ClassObjectFilter(Short.class)).size());
        assertEquals(0, ksession.getObjects().size());
    } finally {
        ksession.dispose();
    }
}
Also used : 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) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 4 with CheeseEqual

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

the class TruthMaintenanceTest method testLogicalInsertionsWithModify.

@Test(timeout = 10000)
public // @Ignore("in Java 8, the byte[] generated by serialization are not the same and requires investigation")
void testLogicalInsertionsWithModify() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_LogicalInsertionsWithUpdate.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, false);
        assertEquals(2, ksession.getObjects().size());
        Collection l = ksession.getObjects(new ClassObjectFilter(CheeseEqual.class));
        assertEquals(1, l.size());
        assertEquals(2, ((CheeseEqual) l.iterator().next()).getPrice());
        h = getFactHandle(h, ksession);
        ksession.delete(h);
        ksession = getSerialisedStatefulKnowledgeSession(ksession, false);
        assertEquals(0, ksession.getObjects().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) Test(org.junit.Test)

Example 5 with CheeseEqual

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

the class StatefulSessionTest method testGetFactHandleEqualityBehavior.

@Test
public void testGetFactHandleEqualityBehavior() throws Exception {
    KieBaseTestConfiguration equalityConfig = TestParametersUtil.getEqualityInstanceOf(kieBaseTestConfiguration);
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", equalityConfig);
    KieSession ksession = kbase.newKieSession();
    final CheeseEqual cheese = new CheeseEqual("stilton", 10);
    ksession.insert(cheese);
    final FactHandle fh = ksession.getFactHandle(new CheeseEqual("stilton", 10));
    assertNotNull(fh);
}
Also used : CheeseEqual(org.drools.mvel.compiler.CheeseEqual) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) KieBaseTestConfiguration(org.drools.testcoverage.common.util.KieBaseTestConfiguration) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Aggregations

CheeseEqual (org.drools.mvel.compiler.CheeseEqual)5 Test (org.junit.Test)5 KieBase (org.kie.api.KieBase)5 KieSession (org.kie.api.runtime.KieSession)5 FactHandle (org.kie.api.runtime.rule.FactHandle)5 ClassObjectFilter (org.drools.core.ClassObjectFilter)3 InternalFactHandle (org.drools.core.common.InternalFactHandle)3 Collection (java.util.Collection)2 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)2 TruthMaintenanceSystem (org.drools.core.common.TruthMaintenanceSystem)2 ObjectHashMap (org.drools.core.util.ObjectHashMap)2 Person (org.drools.mvel.compiler.Person)2 KieBaseTestConfiguration (org.drools.testcoverage.common.util.KieBaseTestConfiguration)2 Cheese (org.drools.mvel.compiler.Cheese)1 Ignore (org.junit.Ignore)1