use of org.drools.core.util.ObjectHashMap.ObjectEntry in project drools by kiegroup.
the class JTMSTest method testChangeInNegativePrime.
@Test(timeout = 10000)
@Ignore("Currently cannot support updates")
public void testChangeInNegativePrime() {
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" + "declare entry-point 'neg' end \n" + "" + "rule \"go1\"\n" + "when\n" + " String( this == 'go1' )\n" + "then\n" + " Person p = new Person( 'darth' ); \n" + " p.setNotInEqualTestObject(1); \n" + " insertLogical( p, 'neg' );\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, 'neg' );\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, 'neg' );\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
assertEquals(3, ep.getObjects().size());
// Person(darth)
assertEquals(1, getNegativeObjects(kSession).size());
int count = 0;
for (Object object : getNegativeObjects(kSession)) {
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) ((JTMSBeliefSetImpl) 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) ((JTMSBeliefSetImpl) 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) ((JTMSBeliefSetImpl) key.getBeliefSet()).getFactHandle().getObject()).getNotInEqualTestObject());
}
use of org.drools.core.util.ObjectHashMap.ObjectEntry in project drools by kiegroup.
the class ObjectHashSet method remove.
public boolean remove(final Object value) {
final int hashCode = this.comparator.hashCodeOf(value);
final int index = indexOf(hashCode, this.table.length);
ObjectEntry previous = (ObjectEntry) this.table[index];
ObjectEntry current = previous;
while (current != null) {
final ObjectEntry next = (ObjectEntry) current.getNext();
if (hashCode == current.cachedHashCode && this.comparator.equal(value, current.value)) {
if (previous == current) {
this.table[index] = next;
} else {
previous.setNext(next);
}
current.setNext(null);
this.size--;
return true;
}
previous = current;
current = next;
}
return false;
}
use of org.drools.core.util.ObjectHashMap.ObjectEntry in project drools by kiegroup.
the class ObjectHashSet method add.
public boolean add(final Object value, final boolean checkExists) {
final int hashCode = this.comparator.hashCodeOf(value);
final int index = indexOf(hashCode, this.table.length);
// scan the linked entries to see if it exists
if (checkExists) {
ObjectEntry current = (ObjectEntry) this.table[index];
while (current != null) {
if (hashCode == current.cachedHashCode && this.comparator.equal(value, current.value)) {
final Object oldValue = current.value;
current.value = value;
return true;
}
current = (ObjectEntry) current.getNext();
}
}
// We aren't checking the key exists, or it didn't find the key
final ObjectEntry entry = new ObjectEntry(value, hashCode);
entry.next = this.table[index];
this.table[index] = entry;
if (this.size++ >= this.threshold) {
resize(2 * this.table.length);
}
return false;
}
use of org.drools.core.util.ObjectHashMap.ObjectEntry 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.ObjectEntry 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());
}
Aggregations