use of org.drools.core.common.TruthMaintenanceSystem in project drools by kiegroup.
the class TruthMaintenanceTest method testLogicalInsertions2.
@Test(timeout = 10000)
public void testLogicalInsertions2() throws Exception {
KieBase kbase = loadKnowledgeBase("test_LogicalInsertions2.drl");
KieSession ksession = kbase.newKieSession();
try {
final List events = new ArrayList();
ksession.setGlobal("events", events);
final Sensor sensor = new Sensor(80, 80);
FactHandle handle = ksession.insert(sensor);
// everything should be normal
ksession = getSerialisedStatefulKnowledgeSession(ksession, false);
ksession.fireAllRules();
Collection list = ksession.getObjects();
assertEquals("Only sensor is there", 1, list.size());
assertEquals("Only one event", 1, events.size());
// problems should be detected
sensor.setPressure(200);
sensor.setTemperature(200);
handle = getFactHandle(handle, ksession);
ksession.update(handle, sensor);
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
list = ksession.getObjects();
assertEquals("Only sensor is there", 1, list.size());
TruthMaintenanceSystem tms = TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem((ReteEvaluator) ksession);
assertTrue(tms.getEqualityKeyMap().isEmpty());
} finally {
ksession.dispose();
}
}
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();
try {
final Person p = new Person("person");
p.setAge(2);
FactHandle h = ksession.insert(p);
assertEquals(1, ksession.getObjects().size());
ksession.fireAllRules();
ksession = 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.delete(h);
ksession = getSerialisedStatefulKnowledgeSession(ksession, false);
assertEquals(0, ksession.getObjects().size());
TruthMaintenanceSystem tms = TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem((ReteEvaluator) ksession);
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());
} finally {
ksession.dispose();
}
}
use of org.drools.core.common.TruthMaintenanceSystem in project drools by kiegroup.
the class JTMSTest method testRetractHandleWhenOnlyNeg.
@Test(timeout = 10000)
@Ignore("Currently cannot support updates")
public void testRetractHandleWhenOnlyNeg() {
String s = "package org.drools.core.beliefsystem.jtms;\n" + "\n" + "import java.util.List \n" + "import org.drools.core.common.AgendaItem;" + "global java.util.List list;\n" + "\n" + "rule \"go1_1\"\n" + "when\n" + " String( this == 'go1' )\n" + "then\n" + " insertLogical( new String( 'neg' ), 'neg' );\n" + "end\n" + "rule \"go1_2\"\n" + "when\n" + " String( this == 'go1' )\n" + "then\n" + " insertLogical( new String( 'neg' ), 'neg' );\n" + "end\n" + "rule \"go1_3\"\n" + "when\n" + " String( this == 'go1' )\n" + "then\n" + " insertLogical( new String( 'neg' ), 'neg' );\n" + "end\n" + "\n" + "rule \"Negative\"\n" + "when\n" + " $n : String( _.neg, this != 'go1' || == 'go2' ) \n" + "then\n" + " final String s = '-' + $n; \n" + " final List l = list; \n" + " l.add( s ); \n" + "end\n";
KieSession kSession = getSessionFromString(s);
List list = new ArrayList();
kSession.setGlobal("list", list);
((RuleEventManager) kSession).addEventListener(new RuleEventListener() {
@Override
public void onDeleteMatch(Match match) {
String rule = match.getRule().getName();
if (rule.equals("Negative")) {
list.remove("-" + match.getDeclarationValue("$n"));
}
}
});
FactHandle fhGo1 = kSession.insert("go1");
kSession.fireAllRules();
assertTrue(list.contains("-neg"));
// just go1
assertEquals(1, kSession.getEntryPoint("DEFAULT").getObjects().size());
assertEquals(1, getNegativeObjects(kSession).size());
NamedEntryPoint ep = (NamedEntryPoint) ((StatefulKnowledgeSessionImpl) kSession).getEntryPoint("DEFAULT");
TruthMaintenanceSystem tms = TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem(ep);
ObjectHashMap equalityMap = tms.getEqualityKeyMap();
// go1, neg are two different strings.
assertEquals(2, equalityMap.size());
org.drools.core.util.Iterator it = equalityMap.iterator();
TruthMaintenanceSystemEqualityKey key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
while (!key.getFactHandle().getObject().equals("neg")) {
key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
}
assertEquals(3, key.getBeliefSet().size());
tms.delete(key.getLogicalFactHandle());
assertEquals(0, key.getBeliefSet().size());
// just go1
assertEquals(1, kSession.getEntryPoint("DEFAULT").getObjects().size());
assertEquals(0, getNegativeObjects(kSession).size());
assertEquals(0, key.getBeliefSet().size());
assertEquals(1, tms.getEqualityKeyMap().size());
}
use of org.drools.core.common.TruthMaintenanceSystem in project drools by kiegroup.
the class DefeasibilityTest method testMultipleDefeats.
@Test(timeout = 10000)
public void testMultipleDefeats() {
KieSession kSession = getSession("org/drools/mvel/compiler/beliefsystem/defeasible/multiDefeat.drl");
kSession.fireAllRules();
TruthMaintenanceSystem tms = TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem((ReteEvaluator) kSession);
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, 2, DefeasibilityStatus.DEFEATEDLY);
} else {
fail("Unrecognized object has been logically justified : " + factClass);
}
}
for (Object o : kSession.getObjects()) {
System.out.println(o);
}
assertEquals(2, kSession.getObjects().size());
kSession.fireAllRules();
}
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/mvel/compiler/beliefsystem/defeasible/defeaterRetract.drl");
FactHandle h = kSession.insert("go");
kSession.fireAllRules();
TruthMaintenanceSystem tms = TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem((ReteEvaluator) kSession);
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);
}
}
}
Aggregations