use of org.drools.core.common.TruthMaintenanceSystem in project drools by kiegroup.
the class DefeasibilityTest method testRemoveDefeasibleJustifier.
@Test(timeout = 10000)
public void testRemoveDefeasibleJustifier() {
KieSession kSession = getSession("org/drools/compiler/beliefsystem/defeasible/defeaterRetract.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.DEFINITELY);
} 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 testDefeaterNeutrality.
@Test(timeout = 10000)
public void testDefeaterNeutrality() {
KieSession kSession = getSession("org/drools/compiler/beliefsystem/defeasible/defeaterOnly.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.DEFEATEDLY);
} else {
fail("Unrecognized object has been logically justified : " + factClass);
}
}
for (Object o : kSession.getObjects()) {
System.out.println(o);
}
assertEquals(0, list.size());
assertEquals(1, kSession.getFactCount());
}
use of org.drools.core.common.TruthMaintenanceSystem in project drools by kiegroup.
the class DefeasibilityTest method testRemoveDefeasibleEntailmentMultiActivationWithDefeat.
@Test(timeout = 10000)
public void testRemoveDefeasibleEntailmentMultiActivationWithDefeat() {
KieSession kSession = getSession("org/drools/compiler/beliefsystem/defeasible/defeatDefeaterRetract.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.DEFEATEDLY);
} else {
fail("Unrecognized object has been logically justified : " + factClass);
}
}
assertEquals(1, list.size());
assertTrue(list.contains("Stage1"));
assertEquals(3, kSession.getObjects().size());
for (Object o : kSession.getObjects()) {
System.out.println(o);
}
FactHandle h = kSession.insert("go");
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 == Xtype.getFactClass()) {
checkStatus(key, 3, DefeasibilityStatus.DEFEASIBLY);
} else {
fail("Unrecognized object has been logically justified : " + factClass);
}
}
for (Object o : kSession.getObjects()) {
System.out.println(o);
}
// A, A, B, X, GO
assertEquals(5, kSession.getObjects().size());
assertEquals(3, list.size());
assertTrue(list.contains("Stage1"));
assertTrue(list.contains("Stage2"));
kSession.retract(h);
kSession.fireAllRules();
for (Object o : kSession.getObjects()) {
System.out.println(o);
}
// A, A, B, X, GO
assertEquals(3, kSession.getObjects().size());
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 == Xtype.getFactClass()) {
checkStatus(key, 1, DefeasibilityStatus.DEFEATEDLY);
} 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 testDefeaterPositiveVsNegative.
@Test(timeout = 10000)
public void testDefeaterPositiveVsNegative() {
KieSession kSession = getSession("org/drools/compiler/beliefsystem/defeasible/defeatersPosNeg.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();
Object fact = key.getFactHandle().getObject();
Class factClass = fact.getClass();
if (factClass == Xtype.getFactClass()) {
Integer val = (Integer) Xtype.get(fact, "id");
switch(val) {
case -1:
checkStatus(key, 2, DefeasibilityStatus.UNDECIDABLY);
break;
case 3:
checkStatus(key, 1, DefeasibilityStatus.DEFEATEDLY);
break;
case -35:
checkStatus(key, 3, DefeasibilityStatus.UNDECIDABLY);
break;
case 44:
checkStatus(key, 2, DefeasibilityStatus.DEFEASIBLY);
break;
default:
fail("Unrecognized fact");
}
} else {
fail("Unrecognized object has been logically justified : " + factClass);
}
}
assertEquals(1, list.size());
assertTrue(list.contains(-44));
assertTrue(!list.contains(-35));
assertEquals(2, kSession.getFactCount());
assertEquals(1, getNegativeObjects(kSession).size());
}
use of org.drools.core.common.TruthMaintenanceSystem in project drools by kiegroup.
the class ReteooWorkingMemoryTest method testBasicWorkingMemoryActions.
/*
* @see JBRULES-356
*/
@Test
@Ignore
public void testBasicWorkingMemoryActions() {
InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
final TruthMaintenanceSystem tms = ((NamedEntryPoint) ksession.getWorkingMemoryEntryPoint(EntryPointId.DEFAULT.getEntryPointId())).getTruthMaintenanceSystem();
final String string = "test";
FactHandle fd = ksession.insert(string);
FactHandle fz = ksession.getTruthMaintenanceSystem().insert(string, null, null, new MockActivation());
assertEquals(1, tms.getEqualityKeyMap().size());
EqualityKey key = tms.get(string);
assertSame(fz, key.getFactHandle());
assertEquals(2, key.size());
ksession.update(fd, string);
assertEquals(1, tms.getEqualityKeyMap().size());
key = tms.get(string);
assertSame(fz, key.getFactHandle());
assertEquals(2, key.size());
ksession.retract(fd);
assertEquals(1, tms.getEqualityKeyMap().size());
key = tms.get(string);
fd = ksession.insert(string);
assertEquals(1, tms.getEqualityKeyMap().size());
assertEquals(1, tms.getEqualityKeyMap().size());
key = tms.get(string);
assertSame(fd, key.getFactHandle());
assertEquals(1, key.size());
}
Aggregations