Search in sources :

Example 31 with WorkingMemoryEntryPoint

use of org.drools.core.WorkingMemoryEntryPoint 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)

Aggregations

WorkingMemoryEntryPoint (org.drools.core.WorkingMemoryEntryPoint)31 InternalFactHandle (org.drools.core.common.InternalFactHandle)14 InternalWorkingMemoryEntryPoint (org.drools.core.common.InternalWorkingMemoryEntryPoint)11 EntryPoint (org.kie.api.runtime.rule.EntryPoint)8 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)6 ObjectTypeConf (org.drools.core.reteoo.ObjectTypeConf)6 PropagationContext (org.drools.core.spi.PropagationContext)6 EventFactHandle (org.drools.core.common.EventFactHandle)5 EntryPointId (org.drools.core.rule.EntryPointId)5 NamedEntryPoint (org.drools.kiesession.entrypoints.NamedEntryPoint)5 PropagationContextFactory (org.drools.core.common.PropagationContextFactory)4 EntryPointNode (org.drools.core.reteoo.EntryPointNode)4 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)4 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)2 EqualityKey (org.drools.core.common.EqualityKey)2 ObjectStore (org.drools.core.common.ObjectStore)2