use of org.drools.core.common.EqualityKey in project drools by kiegroup.
the class ProtobufInputMarshaller method readTruthMaintenanceSystem.
public static void readTruthMaintenanceSystem(MarshallerReaderContext context, EntryPoint wmep, ProtobufMessages.EntryPoint _ep, List<PropagationContext> pctxs) throws IOException, ClassNotFoundException {
TruthMaintenanceSystem tms = ((NamedEntryPoint) wmep).getTruthMaintenanceSystem();
// if 0, then the OTC was not serialized (older versions of drools)
boolean wasOTCSerialized = _ep.getOtcCount() > 0;
Set<String> tmsEnabled = new HashSet<String>();
for (ObjectTypeConfiguration _otc : _ep.getOtcList()) {
if (_otc.getTmsEnabled()) {
tmsEnabled.add(_otc.getType());
}
}
ProtobufMessages.TruthMaintenanceSystem _tms = _ep.getTms();
for (ProtobufMessages.EqualityKey _key : _tms.getKeyList()) {
InternalFactHandle handle = (InternalFactHandle) context.handles.get(_key.getHandleId());
// ObjectTypeConf state is not marshalled, so it needs to be re-determined
ObjectTypeConf typeConf = context.wm.getObjectTypeConfigurationRegistry().getObjectTypeConf(((NamedEntryPoint) handle.getEntryPoint()).getEntryPoint(), handle.getObject());
if (!typeConf.isTMSEnabled() && (!wasOTCSerialized || tmsEnabled.contains(typeConf.getTypeName()))) {
typeConf.enableTMS();
}
EqualityKey key = new EqualityKey(handle, _key.getStatus());
handle.setEqualityKey(key);
if (key.getStatus() == EqualityKey.JUSTIFIED) {
// not yet added to the object stores
((NamedEntryPoint) handle.getEntryPoint()).getObjectStore().addHandle(handle, handle.getObject());
// add handle to object type node
assertHandleIntoOTN(context, context.wm, handle, pctxs);
}
for (Integer factHandleId : _key.getOtherHandleList()) {
handle = (InternalFactHandle) context.handles.get(factHandleId.intValue());
key.addFactHandle(handle);
handle.setEqualityKey(key);
}
tms.put(key);
context.filter.fireRNEAs(context.wm);
readBeliefSet(context, tms, key, _key);
}
}
use of org.drools.core.common.EqualityKey in project drools by kiegroup.
the class TruthMaintenanceTest method testLogicalThenStatedShadowSingleOccurance.
@Test(timeout = 10000)
public void testLogicalThenStatedShadowSingleOccurance() {
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" + "rule StillHere \n" + "when \n" + " String( this == 'go2' ) " + " s : String( this == 'f1' ) " + "then \n" + " list.add( s ); \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);
session.insert("go1");
session.fireAllRules();
TruthMaintenanceSystem tms = ((StatefulKnowledgeSessionImpl) session).getTruthMaintenanceSystem();
InternalFactHandle jfh1 = tms.get("f1").getLogicalFactHandle();
assertEquals(EqualityKey.JUSTIFIED, jfh1.getEqualityKey().getStatus());
InternalFactHandle fh1 = (InternalFactHandle) session.insert("f1");
InternalFactHandle fh2 = (InternalFactHandle) session.insert("f2");
session.insert("go2");
session.fireAllRules();
assertEquals(EqualityKey.STATED, fh1.getEqualityKey().getStatus());
assertSame(fh1.getEqualityKey(), jfh1.getEqualityKey());
assertNotSame(fh1, jfh1);
EqualityKey key = jfh1.getEqualityKey();
assertSame(fh1.getEqualityKey(), key);
assertNotSame(fh1, jfh1);
assertEquals(2, key.size());
assertSame(jfh1, key.getLogicalFactHandle());
// Make sure f1 only occurs once
assertEquals(1, list.size());
assertEquals("f1", list.get(0));
}
use of org.drools.core.common.EqualityKey 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.EqualityKey in project drools by kiegroup.
the class SimpleBeliefSystem method delete.
@Override
public void delete(SimpleMode mode, RuleImpl rule, Activation activation, Object payload, BeliefSet<SimpleMode> beliefSet, PropagationContext context) {
beliefSet.remove(mode);
InternalFactHandle bfh = beliefSet.getFactHandle();
if (beliefSet.isEmpty() && bfh.getEqualityKey() != null && bfh.getEqualityKey().getStatus() == EqualityKey.JUSTIFIED) {
ep.delete(bfh, bfh.getObject(), getObjectTypeConf(beliefSet), context.getRuleOrigin(), null, activation != null ? activation.getTuple().getTupleSink() : null);
} else if (!beliefSet.isEmpty() && bfh.getObject() == payload && payload != bfh.getObject()) {
// prime has changed, to update new object
// Equality might have changed on the object, so remove (which uses the handle id) and add back in
bfh.getEntryPoint().getObjectStore().updateHandle(bfh, beliefSet.getFirst().getObject().getObject());
bfh.getEntryPoint().update(bfh, bfh.getObject(), allSetButTraitBitMask(), Object.class, null);
}
if (beliefSet.isEmpty() && bfh.getEqualityKey() != null) {
// if the beliefSet is empty, we must null the logical handle
EqualityKey key = bfh.getEqualityKey();
key.setLogicalFactHandle(null);
key.setBeliefSet(null);
if (key.getStatus() == EqualityKey.JUSTIFIED) {
// if it's stated, there will be other handles, so leave it in the TMS
tms.remove(key);
}
}
}
use of org.drools.core.common.EqualityKey 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());
}
Aggregations