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());
}
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);
}
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());
}
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());
}
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);
}
Aggregations