Search in sources :

Example 6 with TruthMaintenanceSystemEqualityKey

use of org.drools.tms.TruthMaintenanceSystemEqualityKey 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 7 with TruthMaintenanceSystemEqualityKey

use of org.drools.tms.TruthMaintenanceSystemEqualityKey in project drools by kiegroup.

the class JTMSTest method testRetractHandleWhenOnlyNeg.

@Test(timeout = 10000)
@Ignore("Currently cannot support updates")
public void testRetractHandleWhenOnlyNeg() {
    String s = "package org.drools.core.beliefsystem.jtms;\n" + "\n" + "import java.util.List \n" + "import org.drools.core.common.AgendaItem;" + "global java.util.List list;\n" + "\n" + "rule \"go1_1\"\n" + "when\n" + "    String( this == 'go1' )\n" + "then\n" + "    insertLogical( new String( 'neg' ), 'neg' );\n" + "end\n" + "rule \"go1_2\"\n" + "when\n" + "    String( this == 'go1' )\n" + "then\n" + "    insertLogical( new String( 'neg' ), 'neg' );\n" + "end\n" + "rule \"go1_3\"\n" + "when\n" + "    String( this == 'go1' )\n" + "then\n" + "    insertLogical( new String( 'neg' ), 'neg' );\n" + "end\n" + "\n" + "rule \"Negative\"\n" + "when\n" + "    $n : String(  _.neg, this != 'go1' || == 'go2' ) \n" + "then\n" + "    final String s = '-' + $n; \n" + "    final List l = list; \n" + "    l.add( s ); \n" + "end\n";
    KieSession kSession = getSessionFromString(s);
    List list = new ArrayList();
    kSession.setGlobal("list", list);
    ((RuleEventManager) kSession).addEventListener(new RuleEventListener() {

        @Override
        public void onDeleteMatch(Match match) {
            String rule = match.getRule().getName();
            if (rule.equals("Negative")) {
                list.remove("-" + match.getDeclarationValue("$n"));
            }
        }
    });
    FactHandle fhGo1 = kSession.insert("go1");
    kSession.fireAllRules();
    assertTrue(list.contains("-neg"));
    // just go1
    assertEquals(1, kSession.getEntryPoint("DEFAULT").getObjects().size());
    assertEquals(1, getNegativeObjects(kSession).size());
    NamedEntryPoint ep = (NamedEntryPoint) ((StatefulKnowledgeSessionImpl) kSession).getEntryPoint("DEFAULT");
    TruthMaintenanceSystem tms = TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem(ep);
    ObjectHashMap equalityMap = tms.getEqualityKeyMap();
    // go1, neg are two different strings.
    assertEquals(2, equalityMap.size());
    org.drools.core.util.Iterator it = equalityMap.iterator();
    TruthMaintenanceSystemEqualityKey key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
    while (!key.getFactHandle().getObject().equals("neg")) {
        key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
    }
    assertEquals(3, key.getBeliefSet().size());
    tms.delete(key.getLogicalFactHandle());
    assertEquals(0, key.getBeliefSet().size());
    // just go1
    assertEquals(1, kSession.getEntryPoint("DEFAULT").getObjects().size());
    assertEquals(0, getNegativeObjects(kSession).size());
    assertEquals(0, key.getBeliefSet().size());
    assertEquals(1, tms.getEqualityKeyMap().size());
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) FactHandle(org.kie.api.runtime.rule.FactHandle) NamedEntryPoint(org.drools.kiesession.entrypoints.NamedEntryPoint) ArrayList(java.util.ArrayList) RuleEventListener(org.kie.internal.event.rule.RuleEventListener) ObjectHashMap(org.drools.core.util.ObjectHashMap) ObjectEntry(org.drools.core.util.ObjectHashMap.ObjectEntry) Match(org.kie.api.runtime.rule.Match) TruthMaintenanceSystemEqualityKey(org.drools.tms.TruthMaintenanceSystemEqualityKey) RuleEventManager(org.kie.internal.event.rule.RuleEventManager) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 8 with TruthMaintenanceSystemEqualityKey

use of org.drools.tms.TruthMaintenanceSystemEqualityKey in project drools by kiegroup.

the class JTMSBeliefSystem method delete.

@Override
public void delete(M mode, RuleImpl rule, Activation activation, Object payload, BeliefSet<M> beliefSet, PropagationContext context) {
    if (log.isTraceEnabled()) {
        log.trace("TMSDelete {} {}", payload, mode.getValue());
    }
    JTMSBeliefSet<M> jtmsBeliefSet = (JTMSBeliefSet<M>) beliefSet;
    boolean wasDecided = jtmsBeliefSet.isDecided();
    boolean wasNegated = jtmsBeliefSet.isNegated();
    InternalFactHandle fh = jtmsBeliefSet.getFactHandle();
    beliefSet.remove(mode);
    if (wasDecided && fh.isNegated() != beliefSet.isNegated()) {
        // if it was decided, first remove it and re-add it. So it's in the correct map
        ep.getObjectStore().removeHandle(fh);
        fh.setNegated(beliefSet.isNegated());
        ep.getObjectStore().addHandle(fh, fh.getObject());
    } else {
        fh.setNegated(beliefSet.isNegated());
    }
    if (beliefSet.isEmpty() && fh.getEqualityKey().getStatus() == EqualityKey.JUSTIFIED) {
        // the set is empty, so delete form the EP, so things are cleaned up.
        ep.delete(fh, fh.getObject(), getObjectTypeConf(beliefSet), context.getRuleOrigin(), null, activation != null ? activation.getTuple().getTupleSink() : null);
    } else if (!(processBeliefSet(rule, activation, payload, context, jtmsBeliefSet, wasDecided, wasNegated, fh) && beliefSet.isEmpty())) {
        // The state of the BS did not change, but maybe the prime did
        if (fh.getObject() == payload) {
            // prime, node.object which is the current fh.object,  has changed and object is decided, so update
            String value;
            Object object = null;
            if (jtmsBeliefSet.isNegated()) {
                value = MODE.NEGATIVE.getId();
                // Find the new node, and update the handle to it, Negatives iterate from the last
                for (JTMSMode entry = (JTMSMode) jtmsBeliefSet.getLast(); entry != null; entry = (JTMSMode) entry.getPrevious()) {
                    if (entry.getValue().equals(value)) {
                        object = entry.getLogicalDependency().getObject();
                        break;
                    }
                }
            } else {
                value = MODE.POSITIVE.getId();
                // Find the new node, and update the handle to it, Positives iterate from the front
                for (JTMSMode entry = jtmsBeliefSet.getFirst(); entry != null; entry = (JTMSMode) entry.getNext()) {
                    if (entry.getValue() == null || entry.getValue().equals(value)) {
                        object = entry.getLogicalDependency().getObject();
                        break;
                    }
                }
            }
            // Equality might have changed on the object, so remove (which uses the handle id) and add back in
            if (fh.getObject() != object) {
                WorkingMemoryEntryPoint wmep = fh.getEntryPoint(ep.getReteEvaluator());
                wmep.getObjectStore().updateHandle(fh, object);
                wmep.update(fh, fh.getObject(), allSetButTraitBitMask(), object.getClass(), null);
            }
        }
    }
    if (beliefSet.isEmpty()) {
        // if the beliefSet is empty, we must null the logical handle
        EqualityKey key = fh.getEqualityKey();
        key.setLogicalFactHandle(null);
        ((TruthMaintenanceSystemEqualityKey) key).setBeliefSet(null);
        if (key.getStatus() == EqualityKey.JUSTIFIED) {
            // if it's stated, there will be other handles, so leave it in the TMS
            tms.remove(key);
        }
    }
}
Also used : TruthMaintenanceSystemEqualityKey(org.drools.tms.TruthMaintenanceSystemEqualityKey) TruthMaintenanceSystemEqualityKey(org.drools.tms.TruthMaintenanceSystemEqualityKey) EqualityKey(org.drools.core.common.EqualityKey) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalWorkingMemoryEntryPoint(org.drools.core.common.InternalWorkingMemoryEntryPoint) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint)

Example 9 with TruthMaintenanceSystemEqualityKey

use of org.drools.tms.TruthMaintenanceSystemEqualityKey in project drools by kiegroup.

the class BeliefSystemLogicalCallback method execute.

public void execute(ReteEvaluator reteEvaluator) {
    NamedEntryPoint nep = (NamedEntryPoint) handle.getEntryPoint(reteEvaluator);
    BeliefSet bs = ((TruthMaintenanceSystemEqualityKey) handle.getEqualityKey()).getBeliefSet();
    bs.setWorkingMemoryAction(null);
    if (update) {
        if (!bs.isEmpty()) {
            // We need the isEmpty check, in case the BeliefSet was made empty (due to retract) after this was scheduled
            nep.update(handle, handle.getObject(), allSetButTraitBitMask(), Object.class, null);
        }
    } else {
        if (fullyRetract) {
            nep.delete(this.handle, context.getRuleOrigin(), this.activation.getTuple().getTupleSink());
        } else {
            ObjectTypeConf typeConf = nep.getObjectTypeConfigurationRegistry().getOrCreateObjectTypeConf(nep.getEntryPoint(), handle.getObject());
            nep.getEntryPointNode().retractObject(handle, context, typeConf, reteEvaluator);
        }
    }
}
Also used : TruthMaintenanceSystemEqualityKey(org.drools.tms.TruthMaintenanceSystemEqualityKey) ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) NamedEntryPoint(org.drools.kiesession.entrypoints.NamedEntryPoint) BeliefSet(org.drools.tms.beliefsystem.BeliefSet)

Example 10 with TruthMaintenanceSystemEqualityKey

use of org.drools.tms.TruthMaintenanceSystemEqualityKey in project drools by kiegroup.

the class SimpleBeliefSystem method delete.

@Override
public void delete(SimpleMode mode, RuleImpl rule, Activation activation, Object payload, BeliefSet<SimpleMode> beliefSet, PropagationContext context) {
    beliefSet.remove(mode);
    InternalFactHandle bfh = beliefSet.getFactHandle();
    if (beliefSet.isEmpty() && bfh.getEqualityKey() != null && bfh.getEqualityKey().getStatus() == EqualityKey.JUSTIFIED) {
        ep.delete(bfh, bfh.getObject(), getObjectTypeConf(beliefSet), context.getRuleOrigin(), null, activation != null ? activation.getTuple().getTupleSink() : null);
    } else if (!beliefSet.isEmpty() && bfh.getObject() == payload && payload != bfh.getObject()) {
        // prime has changed, to update new object
        // Equality might have changed on the object, so remove (which uses the handle id) and add back in
        WorkingMemoryEntryPoint ep = bfh.getEntryPoint(this.ep.getReteEvaluator());
        ep.getObjectStore().updateHandle(bfh, beliefSet.getFirst().getObject().getObject());
        ep.update(bfh, bfh.getObject(), allSetButTraitBitMask(), Object.class, null);
    }
    if (beliefSet.isEmpty() && bfh.getEqualityKey() != null) {
        // if the beliefSet is empty, we must null the logical handle
        EqualityKey key = bfh.getEqualityKey();
        key.setLogicalFactHandle(null);
        ((TruthMaintenanceSystemEqualityKey) key).setBeliefSet(null);
        if (key.getStatus() == EqualityKey.JUSTIFIED) {
            // if it's stated, there will be other handles, so leave it in the TMS
            tms.remove(key);
        }
    }
}
Also used : TruthMaintenanceSystemEqualityKey(org.drools.tms.TruthMaintenanceSystemEqualityKey) TruthMaintenanceSystemEqualityKey(org.drools.tms.TruthMaintenanceSystemEqualityKey) EqualityKey(org.drools.core.common.EqualityKey) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalWorkingMemoryEntryPoint(org.drools.core.common.InternalWorkingMemoryEntryPoint) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint)

Aggregations

TruthMaintenanceSystemEqualityKey (org.drools.tms.TruthMaintenanceSystemEqualityKey)11 InternalFactHandle (org.drools.core.common.InternalFactHandle)5 NamedEntryPoint (org.drools.kiesession.entrypoints.NamedEntryPoint)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 EqualityKey (org.drools.core.common.EqualityKey)4 ObjectHashMap (org.drools.core.util.ObjectHashMap)4 Test (org.junit.Test)4 KieSession (org.kie.api.runtime.KieSession)4 WorkingMemoryEntryPoint (org.drools.core.WorkingMemoryEntryPoint)3 TruthMaintenanceSystem (org.drools.core.common.TruthMaintenanceSystem)3 ObjectTypeConf (org.drools.core.reteoo.ObjectTypeConf)3 ObjectEntry (org.drools.core.util.ObjectHashMap.ObjectEntry)3 Ignore (org.junit.Ignore)3 FactHandle (org.kie.api.runtime.rule.FactHandle)3 InternalWorkingMemoryEntryPoint (org.drools.core.common.InternalWorkingMemoryEntryPoint)2 Person (org.drools.mvel.compiler.Person)2 BeliefSet (org.drools.tms.beliefsystem.BeliefSet)2 DefeasibleBeliefSet (org.drools.tms.beliefsystem.defeasible.DefeasibleBeliefSet)2 ObjectInputStream (java.io.ObjectInputStream)1