use of org.drools.core.common.TruthMaintenanceSystem 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.TruthMaintenanceSystem in project drools by kiegroup.
the class TruthMaintenanceTest method testLogicalInsertionsUpdateEqual.
@Test(timeout = 10000)
@Ignore("Currently cannot support updates")
public // @Ignore("in Java 8, the byte[] generated by serialization are not the same and requires investigation")
void testLogicalInsertionsUpdateEqual() throws Exception {
// calling update on a justified FH, states it
KieBase kbase = loadKnowledgeBase("test_LogicalInsertionsUpdateEqual.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, true);
assertEquals(2, ksession.getObjects().size());
Collection l = ksession.getObjects(new ClassObjectFilter(CheeseEqual.class));
assertEquals(1, l.size());
assertEquals(3, ((CheeseEqual) l.iterator().next()).getPrice());
h = getFactHandle(h, ksession);
ksession.retract(h);
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
Collection list = ksession.getObjects();
// CheeseEqual was updated, making it stated, so it wouldn't have been logically deleted
assertEquals(1, list.size());
assertEquals(new CheeseEqual("person", 3), list.iterator().next());
FactHandle fh = ksession.getFactHandle(list.iterator().next());
ksession.retract(fh);
list = ksession.getObjects();
assertEquals(0, list.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.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();
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 = SerializationHelper.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 = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
list = ksession.getObjects();
assertEquals("Only sensor is there", 1, list.size());
TruthMaintenanceSystem tms = ((NamedEntryPoint) ksession.getEntryPoint(EntryPointId.DEFAULT.getEntryPointId())).getTruthMaintenanceSystem();
assertTrue(tms.getEqualityKeyMap().isEmpty());
}
use of org.drools.core.common.TruthMaintenanceSystem 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.TruthMaintenanceSystem in project drools by kiegroup.
the class TruthMaintenanceTest method testLogicalWithStatedShadowThenDeleteLogicalThenDeleteStated.
@Test(timeout = 10000)
public void testLogicalWithStatedShadowThenDeleteLogicalThenDeleteStated() {
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 in ('go2', 'go3', 'go4') ) " + " 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");
session.insert("go2");
session.fireAllRules();
assertEquals(EqualityKey.STATED, fh1.getEqualityKey().getStatus());
assertEquals(1, fh1.getEqualityKey().getBeliefSet().size());
assertSame(fh1.getEqualityKey(), jfh1.getEqualityKey());
assertNotSame(fh1, jfh1);
// Make sure f1 only occurs once
assertEquals(1, list.size());
assertEquals("f1", list.get(0));
list.clear();
tms.delete(jfh1);
session.insert("go3");
session.fireAllRules();
assertNull(fh1.getEqualityKey().getBeliefSet());
// Make sure f1 only occurs once
assertEquals(1, list.size());
assertEquals("f1", list.get(0));
list.clear();
session.delete(fh1);
session.insert("go4");
session.fireAllRules();
assertEquals(0, list.size());
}
Aggregations