Search in sources :

Example 1 with CheeseEqual

use of org.drools.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();
    final Person p = new Person("person");
    p.setAge(2);
    FactHandle h = ksession.insert(p);
    assertEquals(1, ksession.getObjects().size());
    ksession.fireAllRules();
    ksession = SerializationHelper.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.retract(h);
    ksession = SerializationHelper.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.retract(fh);
    list = ksession.getObjects();
    assertEquals(0, list.size());
    TruthMaintenanceSystem tms = ((NamedEntryPoint) ksession.getEntryPoint(EntryPointId.DEFAULT.getEntryPointId())).getTruthMaintenanceSystem();
    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());
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) ObjectHashMap(org.drools.core.util.ObjectHashMap) CheeseEqual(org.drools.compiler.CheeseEqual) ClassObjectFilter(org.drools.core.ClassObjectFilter) KieBase(org.kie.api.KieBase) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with CheeseEqual

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

the class StatefulSessionTest method testGetFactHandleEqualityBehavior.

@Test
public void testGetFactHandleEqualityBehavior() throws Exception {
    final KieBaseConfiguration kbc = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbc.setOption(EqualityBehaviorOption.EQUALITY);
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase(kbc));
    final KieSession ksession = createKnowledgeSession(kbase);
    final CheeseEqual cheese = new CheeseEqual("stilton", 10);
    ksession.insert(cheese);
    final FactHandle fh = ksession.getFactHandle(new CheeseEqual("stilton", 10));
    assertNotNull(fh);
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) CheeseEqual(org.drools.compiler.CheeseEqual) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 3 with CheeseEqual

use of org.drools.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();
    final Person p = new Person("person");
    p.setAge(2);
    FactHandle h = ksession.insert(p);
    assertEquals(1, ksession.getObjects().size());
    ksession.fireAllRules();
    ksession = SerializationHelper.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.retract(h);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, false);
    assertEquals(0, ksession.getObjects().size());
    TruthMaintenanceSystem tms = ((NamedEntryPoint) ksession.getEntryPoint(EntryPointId.DEFAULT.getEntryPointId())).getTruthMaintenanceSystem();
    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());
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) ObjectHashMap(org.drools.core.util.ObjectHashMap) CheeseEqual(org.drools.compiler.CheeseEqual) ClassObjectFilter(org.drools.core.ClassObjectFilter) KieBase(org.kie.api.KieBase) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 4 with CheeseEqual

use of org.drools.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");
    kbase = SerializationHelper.serializeObject(kbase);
    KieSession ksession = kbase.newKieSession();
    ksession.setGlobal("ga", "a");
    ksession.setGlobal("gb", "b");
    ksession.setGlobal("gs", new Short((short) 3));
    ksession.fireAllRules();
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    FactHandle h = ksession.insert(new Integer(6));
    assertEquals(1, ksession.getObjects().size());
    ksession.fireAllRules();
    ksession = SerializationHelper.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.retract(h);
    ksession.fireAllRules();
    for (Object o : ksession.getObjects()) {
        System.out.println(o);
    }
    ksession = SerializationHelper.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());
}
Also used : CheeseEqual(org.drools.compiler.CheeseEqual) ClassObjectFilter(org.drools.core.ClassObjectFilter) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 5 with CheeseEqual

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

the class StatefulSessionTest method testGetFactHandleIdentityBehavior.

@Test
public void testGetFactHandleIdentityBehavior() throws Exception {
    final KieBaseConfiguration kbc = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbc.setOption(EqualityBehaviorOption.IDENTITY);
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase(kbc));
    final KieSession ksession = createKnowledgeSession(kbase);
    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 : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) CheeseEqual(org.drools.compiler.CheeseEqual) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) Test(org.junit.Test)

Aggregations

CheeseEqual (org.drools.compiler.CheeseEqual)5 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)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 Person (org.drools.compiler.Person)2 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)2 TruthMaintenanceSystem (org.drools.core.common.TruthMaintenanceSystem)2 ObjectHashMap (org.drools.core.util.ObjectHashMap)2 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)2 Cheese (org.drools.compiler.Cheese)1 Ignore (org.junit.Ignore)1