Search in sources :

Example 26 with TruthMaintenanceSystem

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();
    }
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) CheeseEqual(org.drools.mvel.compiler.CheeseEqual) ClassObjectFilter(org.drools.core.ClassObjectFilter) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) Collection(java.util.Collection) ObjectHashMap(org.drools.core.util.ObjectHashMap) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.mvel.compiler.Person) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 27 with TruthMaintenanceSystem

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);
    }
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) ObjectTypeConfiguration(org.drools.serialization.protobuf.ProtobufMessages.ObjectTypeConfiguration) TruthMaintenanceSystemEqualityKey(org.drools.tms.TruthMaintenanceSystemEqualityKey) ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) TruthMaintenanceSystemEqualityKey(org.drools.tms.TruthMaintenanceSystemEqualityKey) EqualityKey(org.drools.core.common.EqualityKey) InternalFactHandle(org.drools.core.common.InternalFactHandle) HashSet(java.util.HashSet)

Example 28 with TruthMaintenanceSystem

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());
    }
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) ObjectHashMap(org.drools.core.util.ObjectHashMap) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) NamedEntryPoint(org.drools.kiesession.entrypoints.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) TruthMaintenanceSystemEqualityKey(org.drools.tms.TruthMaintenanceSystemEqualityKey) EqualityKey(org.drools.core.common.EqualityKey) FastIterator(org.drools.core.util.FastIterator)

Example 29 with TruthMaintenanceSystem

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());
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) ObjectHashMap(org.drools.core.util.ObjectHashMap) CheeseEqual(org.drools.compiler.CheeseEqual) ClassObjectFilter(org.drools.core.ClassObjectFilter) KieBase(org.kie.api.KieBase) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 30 with TruthMaintenanceSystem

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"));
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) KieBase(org.kie.api.KieBase) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Aggregations

TruthMaintenanceSystem (org.drools.core.common.TruthMaintenanceSystem)40 Test (org.junit.Test)36 KieSession (org.kie.api.runtime.KieSession)33 EqualityKey (org.drools.core.common.EqualityKey)28 ObjectHashMap (org.drools.core.util.ObjectHashMap)27 Iterator (org.drools.core.util.Iterator)20 FactType (org.kie.api.definition.type.FactType)20 InternalFactHandle (org.drools.core.common.InternalFactHandle)19 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)17 ArrayList (java.util.ArrayList)16 FactHandle (org.kie.api.runtime.rule.FactHandle)15 TruthMaintenanceSystemEqualityKey (org.drools.tms.TruthMaintenanceSystemEqualityKey)13 KieBase (org.kie.api.KieBase)11 List (java.util.List)8 Collection (java.util.Collection)6 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)6 Ignore (org.junit.Ignore)6 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)5 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)5 ClassObjectFilter (org.drools.core.ClassObjectFilter)4