use of org.drools.core.common.TruthMaintenanceSystem in project drools by kiegroup.
the class TruthMaintenanceTest method testStatedWithShadowAndDeleteException.
@Test(timeout = 10000)
public void testStatedWithShadowAndDeleteException() {
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);
InternalFactHandle fh1 = (InternalFactHandle) session.insert("f1");
InternalFactHandle fh2 = (InternalFactHandle) session.insert("f2");
session.insert("go1");
session.fireAllRules();
// TMS is now enabled
assertNotNull(fh1.getEqualityKey());
assertNotNull(fh2.getEqualityKey());
// EqualtyKey shows both are stated
assertEquals(EqualityKey.STATED, fh1.getEqualityKey().getStatus());
assertEquals(EqualityKey.STATED, fh2.getEqualityKey().getStatus());
// Only fh1 has a logical
assertEquals(1, fh1.getEqualityKey().getBeliefSet().size());
assertNull(fh2.getEqualityKey().getBeliefSet());
// Get the logical Handle too
TruthMaintenanceSystem tms = ((StatefulKnowledgeSessionImpl) session).getTruthMaintenanceSystem();
InternalFactHandle jfh1 = tms.get("f1").getLogicalFactHandle();
EqualityKey key = jfh1.getEqualityKey();
assertSame(fh1.getEqualityKey(), key);
assertNotSame(fh1, jfh1);
assertEquals(2, key.size());
assertSame(jfh1, key.getLogicalFactHandle());
}
use of org.drools.core.common.TruthMaintenanceSystem in project drools by kiegroup.
the class DefeasibilityTest method testDefeasibleEntailmentWithStrictOverride.
@Test(timeout = 10000)
public void testDefeasibleEntailmentWithStrictOverride() {
KieSession kSession = getSession("org/drools/compiler/beliefsystem/defeasible/strictOverride.drl");
kSession.fireAllRules();
TruthMaintenanceSystem tms = ((NamedEntryPoint) kSession.getEntryPoint("DEFAULT")).getTruthMaintenanceSystem();
FactType Ctype = kSession.getKieBase().getFactType("org.drools.defeasible", "C");
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 == Ctype.getFactClass()) {
checkStatus(key, 1, DefeasibilityStatus.DEFINITELY);
} else if (factClass == Xtype.getFactClass()) {
checkStatus(key, 1, DefeasibilityStatus.DEFINITELY);
} else {
fail("Unrecognized object has been logically justified : " + factClass);
}
}
assertEquals(5, kSession.getObjects().size());
}
use of org.drools.core.common.TruthMaintenanceSystem in project drools by kiegroup.
the class DefeasibilityTest method testRemoveDefiniteJustifier.
@Test(timeout = 10000)
public void testRemoveDefiniteJustifier() {
KieSession kSession = getSession("org/drools/compiler/beliefsystem/defeasible/strictRetract.drl");
FactHandle h = kSession.insert("go");
kSession.fireAllRules();
TruthMaintenanceSystem tms = ((NamedEntryPoint) kSession.getEntryPoint("DEFAULT")).getTruthMaintenanceSystem();
FactType Ctype = kSession.getKieBase().getFactType("org.drools.defeasible", "C");
FactType Atype = kSession.getKieBase().getFactType("org.drools.defeasible", "A");
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 == Ctype.getFactClass()) {
checkStatus(key, 1, DefeasibilityStatus.DEFINITELY);
} else if (factClass == Atype.getFactClass()) {
checkStatus(key, 1, DefeasibilityStatus.DEFINITELY);
} else {
fail("Unrecognized object has been logically justified : " + factClass);
}
}
kSession.retract(h);
kSession.fireAllRules();
keys = tms.getEqualityKeyMap();
iter = keys.iterator();
while ((entry = (ObjectHashMap.ObjectEntry) iter.next()) != null) {
EqualityKey key = (EqualityKey) entry.getValue();
Class factClass = key.getFactHandle().getObject().getClass();
if (factClass == Ctype.getFactClass()) {
checkStatus(key, 1, DefeasibilityStatus.DEFEASIBLY);
} else {
fail("Unrecognized object has been logically justified : " + factClass);
}
}
}
use of org.drools.core.common.TruthMaintenanceSystem in project drools by kiegroup.
the class DefeasibilityTest method defeasibleEntailmentMultiActivation.
@Test(timeout = 10000)
public void defeasibleEntailmentMultiActivation() {
KieSession kSession = getSession("org/drools/compiler/beliefsystem/defeasible/defeat.drl");
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, 3, DefeasibilityStatus.UNDECIDABLY);
} else {
fail("Unrecognized object has been logically justified : " + factClass);
}
}
assertEquals(3, kSession.getObjects().size());
}
use of org.drools.core.common.TruthMaintenanceSystem in project drools by kiegroup.
the class DefeasibilityTest method testStrictEntailment.
@Test(timeout = 10000)
public void testStrictEntailment() {
KieSession kSession = getSession("org/drools/compiler/beliefsystem/defeasible/strict.drl");
kSession.fireAllRules();
TruthMaintenanceSystem tms = ((NamedEntryPoint) kSession.getEntryPoint("DEFAULT")).getTruthMaintenanceSystem();
FactType Ctype = kSession.getKieBase().getFactType("org.drools.defeasible", "C");
FactType Dtype = kSession.getKieBase().getFactType("org.drools.defeasible", "D");
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 == Ctype.getFactClass()) {
checkStatus(key, 2, DefeasibilityStatus.DEFINITELY);
} else if (factClass == Dtype.getFactClass()) {
checkStatus(key, 1, DefeasibilityStatus.DEFINITELY);
} else {
fail("Unrecognized object has been logically justified : " + factClass);
}
}
assertEquals(5, kSession.getObjects().size());
}
Aggregations