Search in sources :

Example 21 with EqualityKey

use of org.drools.core.common.EqualityKey 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) {
                fh.getEntryPoint().getObjectStore().updateHandle(fh, object);
                fh.getEntryPoint().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);
        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 : EqualityKey(org.drools.core.common.EqualityKey) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Aggregations

EqualityKey (org.drools.core.common.EqualityKey)21 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)17 Test (org.junit.Test)16 TruthMaintenanceSystem (org.drools.core.common.TruthMaintenanceSystem)15 KieSession (org.kie.api.runtime.KieSession)15 ObjectHashMap (org.drools.core.util.ObjectHashMap)14 Iterator (org.drools.core.util.Iterator)10 FactType (org.kie.api.definition.type.FactType)10 ArrayList (java.util.ArrayList)9 InternalFactHandle (org.drools.core.common.InternalFactHandle)9 FactHandle (org.kie.api.runtime.rule.FactHandle)7 List (java.util.List)5 Ignore (org.junit.Ignore)4 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)3 ObjectEntry (org.drools.core.util.ObjectHashMap.ObjectEntry)3 Person (org.drools.compiler.Person)2 ObjectTypeConf (org.drools.core.reteoo.ObjectTypeConf)2 KieBase (org.kie.api.KieBase)2 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)2 HashSet (java.util.HashSet)1