use of org.drools.core.util.ObjectHashMap in project drools by kiegroup.
the class ObjectTypeNodeParser method traverseHashedAlphaNodes.
private void traverseHashedAlphaNodes(ObjectHashMap hashedAlphaNodes, NetworkHandler handler) {
if (hashedAlphaNodes != null && hashedAlphaNodes.size() > 0) {
AlphaNode firstAlpha = getFirstAlphaNode(hashedAlphaNodes);
ClassFieldReader hashedFieldReader = getClassFieldReaderForHashedAlpha(firstAlpha);
// start the hashed alphas
handler.startHashedAlphaNodes(hashedFieldReader);
Iterator iter = hashedAlphaNodes.iterator();
for (ObjectHashMap.ObjectEntry entry = (ObjectHashMap.ObjectEntry) iter.next(); entry != null; entry = (ObjectHashMap.ObjectEntry) iter.next()) {
CompositeObjectSinkAdapter.HashKey hashKey = (CompositeObjectSinkAdapter.HashKey) entry.getKey();
AlphaNode alphaNode = (AlphaNode) entry.getValue();
handler.startHashedAlphaNode(alphaNode, hashKey.getObjectValue());
// traverse the propagator for each alpha
traversePropagator(alphaNode.getObjectSinkPropagator(), handler);
handler.endHashedAlphaNode(alphaNode, hashKey.getObjectValue());
}
// end of the hashed alphas
handler.endHashedAlphaNodes(hashedFieldReader);
}
}
use of org.drools.core.util.ObjectHashMap 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.util.ObjectHashMap in project drools by kiegroup.
the class JTMSTest method testChangeInPositivePrime.
@Test(timeout = 10000)
@Ignore("Currently cannot support updates")
public void testChangeInPositivePrime() {
String s = "package org.drools.core.beliefsystem.jtms;\n" + "\n" + "import org.kie.internal.event.rule.ActivationUnMatchListener;\n" + "import java.util.List \n" + "import org.drools.core.common.AgendaItem;" + "import org.drools.compiler.Person;" + "global java.util.List list;\n" + "\n" + "rule \"go1\"\n" + "when\n" + " String( this == 'go1' )\n" + "then\n" + " Person p = new Person( 'darth' ); \n" + " p.setNotInEqualTestObject(1); \n" + " insertLogical( p );\n" + "end\n" + "rule \"go2\"\n" + "when\n" + " String( this == 'go2' )\n" + "then\n" + " Person p = new Person( 'darth' ); \n" + " p.setNotInEqualTestObject(2); \n" + " insertLogical( p );\n" + "end\n" + "rule \"go3\"\n" + "when\n" + " String( this == 'go3' )\n" + "then\n" + " Person p = new Person( 'darth' ); \n" + " p.setNotInEqualTestObject(3); \n" + " insertLogical( p );\n" + "end\n" + "\n";
KieSession kSession = getSessionFromString(s);
List list = new ArrayList();
kSession.setGlobal("list", list);
// We want to make sure go1 is prime, and then that it switches to go2
FactHandle fhGo1 = kSession.insert("go1");
kSession.fireAllRules();
FactHandle fhGo2 = kSession.insert("go2");
kSession.fireAllRules();
FactHandle fhGo3 = kSession.insert("go3");
kSession.fireAllRules();
NamedEntryPoint ep = (NamedEntryPoint) ((StatefulKnowledgeSessionImpl) kSession).getEntryPoint("DEFAULT");
// just go1, go2, go3, Person(darth)
assertEquals(4, ep.getObjects().size());
int count = 0;
for (Object object : ep.getObjects()) {
if (object instanceof Person) {
assertEquals(new Integer(1), ((Person) object).getNotInEqualTestObject());
count++;
}
}
assertEquals(1, count);
ObjectHashMap equalityMap = ep.getTruthMaintenanceSystem().getEqualityKeyMap();
// Only Person type is logical
assertEquals(1, equalityMap.size());
org.drools.core.util.Iterator it = equalityMap.iterator();
EqualityKey key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
}
assertEquals(3, key.getBeliefSet().size());
assertEquals(new Integer(1), ((Person) key.getBeliefSet().getFactHandle().getObject()).getNotInEqualTestObject());
kSession.retract(fhGo1);
kSession.fireAllRules();
it = equalityMap.iterator();
key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
}
assertEquals(2, key.getBeliefSet().size());
assertEquals(new Integer(3), ((Person) key.getBeliefSet().getFactHandle().getObject()).getNotInEqualTestObject());
kSession.retract(fhGo3);
kSession.fireAllRules();
it = equalityMap.iterator();
key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
}
assertEquals(1, key.getBeliefSet().size());
assertEquals(new Integer(2), ((Person) key.getBeliefSet().getFactHandle().getObject()).getNotInEqualTestObject());
}
use of org.drools.core.util.ObjectHashMap 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");
ObjectHashMap equalityMap = ep.getTruthMaintenanceSystem().getEqualityKeyMap();
// go1, neg are two different strings.
assertEquals(2, equalityMap.size());
org.drools.core.util.Iterator it = equalityMap.iterator();
EqualityKey key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
while (!key.getFactHandle().getObject().equals("neg")) {
key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
}
assertEquals(3, key.getBeliefSet().size());
ep.getTruthMaintenanceSystem().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, ep.getTruthMaintenanceSystem().getEqualityKeyMap().size());
}
use of org.drools.core.util.ObjectHashMap 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