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();
try {
final Person p = new Person("person");
p.setAge(2);
FactHandle h = ksession.insert(p);
assertEquals(1, ksession.getObjects().size());
ksession.fireAllRules();
ksession = 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.delete(h);
ksession = 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.delete(fh);
list = ksession.getObjects();
assertEquals(0, list.size());
TruthMaintenanceSystem tms = TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem((ReteEvaluator) ksession);
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());
} finally {
ksession.dispose();
}
}
use of org.drools.core.common.TruthMaintenanceSystem in project drools by kiegroup.
the class ProtobufInputMarshaller method readTruthMaintenanceSystem.
public static void readTruthMaintenanceSystem(ProtobufMarshallerReaderContext context, EntryPoint wmep, ProtobufMessages.EntryPoint _ep, List<PropagationContext> pctxs) throws IOException, ClassNotFoundException {
TruthMaintenanceSystem tms = TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem((NamedEntryPoint) wmep);
// 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.getHandles().get(_key.getHandleId());
// ObjectTypeConf state is not marshalled, so it needs to be re-determined
ObjectTypeConf typeConf = context.getWorkingMemory().getObjectTypeConfigurationRegistry().getOrCreateObjectTypeConf(handle.getEntryPointId(), handle.getObject());
if (!typeConf.isTMSEnabled() && (!wasOTCSerialized || tmsEnabled.contains(typeConf.getTypeName()))) {
typeConf.enableTMS();
}
EqualityKey key = new TruthMaintenanceSystemEqualityKey(handle, _key.getStatus());
handle.setEqualityKey(key);
if (key.getStatus() == EqualityKey.JUSTIFIED) {
// not yet added to the object stores
handle.getEntryPoint(((NamedEntryPoint) wmep).getReteEvaluator()).getObjectStore().addHandle(handle, handle.getObject());
// add handle to object type node
assertHandleIntoOTN(context, context.getWorkingMemory(), handle, pctxs);
}
for (Long factHandleId : _key.getOtherHandleList()) {
handle = context.getHandles().get(factHandleId);
key.addFactHandle(handle);
handle.setEqualityKey(key);
}
tms.put(key);
context.getFilter().fireRNEAs(context.getWorkingMemory());
readBeliefSet(context, tms, _key);
}
}
use of org.drools.core.common.TruthMaintenanceSystem in project drools by kiegroup.
the class ProtobufOutputMarshaller method writeTruthMaintenanceSystem.
public static void writeTruthMaintenanceSystem(MarshallerWriteContext context, EntryPoint wmep, ProtobufMessages.EntryPoint.Builder _epb) throws IOException {
TruthMaintenanceSystem tms = TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem((NamedEntryPoint) wmep);
ObjectHashMap justifiedMap = tms.getEqualityKeyMap();
if (!justifiedMap.isEmpty()) {
EqualityKey[] keys = new EqualityKey[justifiedMap.size()];
org.drools.core.util.Iterator it = justifiedMap.iterator();
int i = 0;
for (org.drools.core.util.ObjectHashMap.ObjectEntry entry = (org.drools.core.util.ObjectHashMap.ObjectEntry) it.next(); entry != null; entry = (org.drools.core.util.ObjectHashMap.ObjectEntry) it.next()) {
EqualityKey key = (EqualityKey) entry.getKey();
keys[i++] = key;
}
Arrays.sort(keys, EqualityKeySorter.instance);
ProtobufMessages.TruthMaintenanceSystem.Builder _tms = ProtobufMessages.TruthMaintenanceSystem.newBuilder();
// write the assert map of Equality keys
for (EqualityKey key : keys) {
ProtobufMessages.EqualityKey.Builder _key = ProtobufMessages.EqualityKey.newBuilder();
_key.setStatus(key.getStatus());
_key.setHandleId(key.getFactHandle().getId());
if (key.size() > 1) {
// add all the other key's if they exist
FastIterator keyIter = key.fastIterator();
for (DefaultFactHandle handle = key.getFirst().getNext(); handle != null; handle = (DefaultFactHandle) keyIter.next(handle)) {
_key.addOtherHandle(handle.getId());
}
}
if (((TruthMaintenanceSystemEqualityKey) key).getBeliefSet() != null) {
writeBeliefSet(context, ((TruthMaintenanceSystemEqualityKey) key).getBeliefSet(), _key);
}
_tms.addKey(_key.build());
}
_epb.setTms(_tms.build());
}
}
use of org.drools.core.common.TruthMaintenanceSystem 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.common.TruthMaintenanceSystem in project drools by kiegroup.
the class TruthMaintenanceTest method testLogicalWithDeleteException.
@Test(timeout = 10000)
public void testLogicalWithDeleteException() {
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);
session.insert("go1");
session.fireAllRules();
TruthMaintenanceSystem tms = ((StatefulKnowledgeSessionImpl) session).getTruthMaintenanceSystem();
InternalFactHandle jfh1 = tms.get("f1").getLogicalFactHandle();
assertSame(jfh1, session.getFactHandle("f1"));
}
Aggregations