Search in sources :

Example 21 with TruthMaintenanceSystem

use of org.drools.core.common.TruthMaintenanceSystem in project drools by kiegroup.

the class DefeasibilityTest method testMultipleDefeats.

@Test(timeout = 10000)
public void testMultipleDefeats() {
    KieSession kSession = getSession("org/drools/compiler/beliefsystem/defeasible/multiDefeat.drl");
    kSession.fireAllRules();
    TruthMaintenanceSystem tms = ((NamedEntryPoint) kSession.getEntryPoint("DEFAULT")).getTruthMaintenanceSystem();
    FactType Xtype = kSession.getKieBase().getFactType("org.drools.defeasible", "X");
    ObjectHashMap keys = tms.getEqualityKeyMap();
    Iterator iter = keys.iterator();
    ObjectHashMap.ObjectEntry entry;
    while ((entry = (ObjectHashMap.ObjectEntry) iter.next()) != null) {
        EqualityKey key = (EqualityKey) entry.getValue();
        Class factClass = key.getFactHandle().getObject().getClass();
        if (factClass == Xtype.getFactClass()) {
            checkStatus(key, 2, DefeasibilityStatus.DEFEATEDLY);
        } else {
            fail("Unrecognized object has been logically justified : " + factClass);
        }
    }
    for (Object o : kSession.getObjects()) {
        System.out.println(o);
    }
    assertEquals(2, kSession.getObjects().size());
    kSession.fireAllRules();
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) EqualityKey(org.drools.core.common.EqualityKey) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) Iterator(org.drools.core.util.Iterator) ObjectHashMap(org.drools.core.util.ObjectHashMap) KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 22 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 = ((NamedEntryPoint) wmep).getTruthMaintenanceSystem();
    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 (key.getBeliefSet() != null) {
                writeBeliefSet(context, key.getBeliefSet(), _key);
            }
            _tms.addKey(_key.build());
        }
        _epb.setTms(_tms.build());
    }
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) ObjectHashMap(org.drools.core.util.ObjectHashMap) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) EqualityKey(org.drools.core.common.EqualityKey) FastIterator(org.drools.core.util.FastIterator)

Aggregations

TruthMaintenanceSystem (org.drools.core.common.TruthMaintenanceSystem)22 Test (org.junit.Test)20 KieSession (org.kie.api.runtime.KieSession)19 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)17 EqualityKey (org.drools.core.common.EqualityKey)15 ObjectHashMap (org.drools.core.util.ObjectHashMap)13 InternalFactHandle (org.drools.core.common.InternalFactHandle)12 ArrayList (java.util.ArrayList)10 Iterator (org.drools.core.util.Iterator)10 FactType (org.kie.api.definition.type.FactType)10 KieBase (org.kie.api.KieBase)8 FactHandle (org.kie.api.runtime.rule.FactHandle)7 List (java.util.List)6 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)6 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)5 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)4 Collection (java.util.Collection)3 Person (org.drools.compiler.Person)3 CheeseEqual (org.drools.compiler.CheeseEqual)2 ClassObjectFilter (org.drools.core.ClassObjectFilter)2