use of org.drools.kiesession.entrypoints.NamedEntryPoint in project drools by kiegroup.
the class TruthMaintenanceSystemKnowledgeHelper method bolster.
@Override
public InternalFactHandle bolster(final Object object, final Object value) {
if (object == null || !activation.isMatched()) {
return null;
}
InternalFactHandle handle = getFactHandleFromWM(object);
NamedEntryPoint ep = (NamedEntryPoint) reteEvaluator.getEntryPoint(EntryPointId.DEFAULT.getEntryPointId());
ObjectTypeConf otc = ep.getObjectTypeConfigurationRegistry().getOrCreateObjectTypeConf(ep.getEntryPoint(), object);
BeliefSet beliefSet = null;
if (handle == null) {
handle = RuntimeComponentFactory.get().getFactHandleFactoryService().newFactHandle(object, otc, reteEvaluator, ep);
}
if (handle.getEqualityKey() == null) {
handle.setEqualityKey(new TruthMaintenanceSystemEqualityKey(handle, EqualityKey.STATED));
} else {
beliefSet = ((TruthMaintenanceSystemEqualityKey) handle.getEqualityKey()).getBeliefSet();
}
BeliefSystem beliefSystem = value instanceof BeliefSystemMode ? ((BeliefSystemMode) value).getBeliefSystem() : ((TruthMaintenanceSystemImpl) TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem(toStatefulKnowledgeSession())).getBeliefSystem();
if (beliefSet == null) {
beliefSet = beliefSystem.newBeliefSet(handle);
((TruthMaintenanceSystemEqualityKey) handle.getEqualityKey()).setBeliefSet(beliefSet);
}
return beliefSystem.insert(beliefSystem.asMode(value), activation.getRule(), (TruthMaintenanceSystemActivation) activation, object, beliefSet, activation.getPropagationContext(), otc).getFactHandle();
}
use of org.drools.kiesession.entrypoints.NamedEntryPoint 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.mvel.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 = TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem(ep).getEqualityKeyMap();
// Only Person type is logical
assertEquals(1, equalityMap.size());
org.drools.core.util.Iterator it = equalityMap.iterator();
TruthMaintenanceSystemEqualityKey key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
key = (TruthMaintenanceSystemEqualityKey) ((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 = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
key = (TruthMaintenanceSystemEqualityKey) ((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 = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
}
assertEquals(1, key.getBeliefSet().size());
assertEquals(new Integer(2), ((Person) key.getBeliefSet().getFactHandle().getObject()).getNotInEqualTestObject());
}
use of org.drools.kiesession.entrypoints.NamedEntryPoint in project drools by kiegroup.
the class JTMSTest method testConflictStrict.
@Test(timeout = 10000)
public void testConflictStrict() {
KieSession kSession = getSessionFromFile("posNegConflict.drl");
ArrayList list = new ArrayList();
kSession.setGlobal("list", list);
NamedEntryPoint ep = (NamedEntryPoint) ((StatefulKnowledgeSessionImpl) kSession).getEntryPoint("DEFAULT");
JTMSBeliefSystem bs = (JTMSBeliefSystem) ((TruthMaintenanceSystemImpl) TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem(ep)).getBeliefSystem();
bs.STRICT = true;
try {
kSession.fireAllRules();
fail("A fact and its negation should have been asserted, but no exception was trhown in strict mode");
} catch (Exception e) {
} finally {
bs.STRICT = false;
}
}
use of org.drools.kiesession.entrypoints.NamedEntryPoint 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.mvel.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 = TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem(ep).getEqualityKeyMap();
// Only Person type is logical
assertEquals(1, equalityMap.size());
org.drools.core.util.Iterator it = equalityMap.iterator();
TruthMaintenanceSystemEqualityKey key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
key = (TruthMaintenanceSystemEqualityKey) ((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 = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
key = (TruthMaintenanceSystemEqualityKey) ((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 = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
}
assertEquals(1, key.getBeliefSet().size());
assertEquals(new Integer(2), ((Person) key.getBeliefSet().getFactHandle().getObject()).getNotInEqualTestObject());
}
use of org.drools.kiesession.entrypoints.NamedEntryPoint in project drools by kiegroup.
the class TraitHelperImpl method updateCore.
private <T> void updateCore(TraitableBean inner, Object core, Class<T> trait, boolean logical, Activation activation) {
FactHandle handle = lookupFactHandle(inner);
InternalFactHandle h = (InternalFactHandle) handle;
if (handle != null) {
TraitFieldTMS fieldTMS = inner._getFieldTMS();
BitMask mask = fieldTMS == null ? onlyTraitBitSetMask() : fieldTMS.getModificationMask();
Object o = h.getObject();
NamedEntryPoint nep = (NamedEntryPoint) h.getEntryPoint(workingMemory);
PropagationContext propagationContext = nep.getPctxFactory().createPropagationContext(nep.getReteEvaluator().getNextPropagationIdCounter(), PropagationContext.Type.MODIFICATION, activation.getRule(), activation.getTuple().getTupleSink(), h, nep.getEntryPoint(), mask, core.getClass(), null);
nep.update(h, o, o, nep.getObjectTypeConfigurationRegistry().getObjectTypeConf(o), propagationContext);
} else {
handle = this.workingMemory.insert(inner, false, activation.getRule(), activation.getTuple().getTupleSink());
}
}
Aggregations