use of org.drools.core.common.TruthMaintenanceSystem 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.core.common.TruthMaintenanceSystem in project drools by kiegroup.
the class TruthMaintenanceTest method testLogicalWithDeleteException.
@Test(timeout = 10000)
public void testLogicalWithDeleteException() {
String droolsSource = "package org.drools.tms.test; \n" + "global java.util.List list; \n" + "rule Justify \n" + "when \n" + " String( this == 'go1' ) " + "then \n" + " insertLogical( 'f1' ); \n" + "end \n" + "";
KieBaseConfiguration kieConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kieConf.setOption(EqualityBehaviorOption.IDENTITY);
KieBase kbase = loadKnowledgeBaseFromString(kieConf, droolsSource);
KieSession session = kbase.newKieSession();
List list = new ArrayList();
session.setGlobal("list", list);
session.insert("go1");
session.fireAllRules();
TruthMaintenanceSystem tms = ((StatefulKnowledgeSessionImpl) session).getTruthMaintenanceSystem();
InternalFactHandle jfh1 = tms.get("f1").getLogicalFactHandle();
assertSame(jfh1, session.getFactHandle("f1"));
}
use of org.drools.core.common.TruthMaintenanceSystem in project drools by kiegroup.
the class TruthMaintenanceTest method testLogicalInsertOrder.
@Test(timeout = 10000)
public void testLogicalInsertOrder() throws Exception {
// JBRULES-1602
// "rule 1" is never logical inserted, as it's rule is unmatched prior to calling logical insert
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("test_LogicalInsertOrder.drl", getClass()), ResourceType.DRL);
InternalKnowledgeBase kbase = (InternalKnowledgeBase) getKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
kbase = SerializationHelper.serializeObject(kbase);
final KieSession session = createKnowledgeSession(kbase);
RuleRuntimeEventListener wmel = mock(RuleRuntimeEventListener.class);
session.addEventListener(wmel);
Person bob = new Person("bob");
bob.setStatus("hungry");
Person mark = new Person("mark");
mark.setStatus("thirsty");
session.insert(bob);
session.insert(mark);
int count = session.fireAllRules();
assertEquals(2, count);
assertEquals(2, session.getObjects().size());
TruthMaintenanceSystem tms = ((NamedEntryPoint) session.getEntryPoint(EntryPointId.DEFAULT.getEntryPointId())).getTruthMaintenanceSystem();
assertTrue(tms.getEqualityKeyMap().isEmpty());
}
use of org.drools.core.common.TruthMaintenanceSystem in project drools by kiegroup.
the class TruthMaintenanceTest method testLogicalThenUpdateAsStatedShadowSingleOccurance.
@Test(timeout = 10000)
public void testLogicalThenUpdateAsStatedShadowSingleOccurance() {
String droolsSource = "package org.drools.tms.test; \n" + "global java.util.List list; \n" + "rule Justify \n" + "when \n" + " String( this == 'go1' ) " + "then \n" + " insertLogical( 'f1' ); \n" + "end \n" + "rule StillHere \n" + "when \n" + " String( this == 'go2' ) " + " s : String( this == 'f1' ) " + "then \n" + " list.add( s ); \n" + "end \n" + "";
KieBaseConfiguration kieConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kieConf.setOption(EqualityBehaviorOption.IDENTITY);
KieBase kbase = loadKnowledgeBaseFromString(kieConf, droolsSource);
KieSession session = kbase.newKieSession();
List list = new ArrayList();
session.setGlobal("list", list);
session.insert("go1");
session.fireAllRules();
TruthMaintenanceSystem tms = ((StatefulKnowledgeSessionImpl) session).getTruthMaintenanceSystem();
InternalFactHandle jfh1 = tms.get("f1").getLogicalFactHandle();
assertEquals(EqualityKey.JUSTIFIED, jfh1.getEqualityKey().getStatus());
InternalFactHandle fh1 = (InternalFactHandle) session.insert("f1");
InternalFactHandle fh2 = (InternalFactHandle) session.insert("f2");
session.insert("go2");
session.fireAllRules();
assertEquals(EqualityKey.STATED, fh1.getEqualityKey().getStatus());
assertSame(fh1.getEqualityKey(), jfh1.getEqualityKey());
assertNotSame(fh1, jfh1);
// Make sure f1 only occurs once
assertEquals(1, list.size());
assertEquals("f1", list.get(0));
}
use of org.drools.core.common.TruthMaintenanceSystem in project drools by kiegroup.
the class DefeasibilityTest method testDefeatOutcomePosNeg.
@Test(timeout = 10000)
public void testDefeatOutcomePosNeg() {
KieSession kSession = getSession("org/drools/compiler/beliefsystem/defeasible/negDefeatPos.drl");
ArrayList list = new ArrayList();
kSession.setGlobal("list", list);
kSession.fireAllRules();
TruthMaintenanceSystem tms = ((NamedEntryPoint) kSession.getEntryPoint("DEFAULT")).getTruthMaintenanceSystem();
FactType Xtype = kSession.getKieBase().getFactType("org.drools.defeasible", "X");
ObjectHashMap keys = tms.getEqualityKeyMap();
Iterator iter = keys.iterator();
ObjectHashMap.ObjectEntry entry;
while ((entry = (ObjectHashMap.ObjectEntry) iter.next()) != null) {
EqualityKey key = (EqualityKey) entry.getValue();
Class factClass = key.getFactHandle().getObject().getClass();
if (factClass == Xtype.getFactClass()) {
checkStatus(key, 1, DefeasibilityStatus.DEFEASIBLY);
} else {
fail("Unrecognized object has been logically justified : " + factClass);
}
}
assertEquals(2, kSession.getObjects().size());
assertEquals(1, getNegativeObjects(kSession).size());
assertEquals(1, list.size());
assertTrue(list.contains("-1"));
}
Aggregations