Search in sources :

Example 6 with PropagationContext

use of org.drools.core.spi.PropagationContext in project drools by kiegroup.

the class AddRemoveRule method insertLiaFacts.

private static void insertLiaFacts(LeftTupleNode startNode, InternalWorkingMemory wm) {
    // rule added with no sharing
    PropagationContextFactory pctxFactory = wm.getKnowledgeBase().getConfiguration().getComponentFactory().getPropagationContextFactory();
    final PropagationContext pctx = pctxFactory.createPropagationContext(wm.getNextPropagationIdCounter(), PropagationContext.Type.RULE_ADDITION, null, null, null);
    LeftInputAdapterNode lian = (LeftInputAdapterNode) startNode;
    RightTupleSinkAdapter liaAdapter = new RightTupleSinkAdapter(lian);
    lian.getObjectSource().updateSink(liaAdapter, pctx, wm);
}
Also used : PropagationContextFactory(org.drools.core.common.PropagationContextFactory) RightTupleSinkAdapter(org.drools.core.reteoo.LeftInputAdapterNode.RightTupleSinkAdapter) PropagationContext(org.drools.core.spi.PropagationContext) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode)

Example 7 with PropagationContext

use of org.drools.core.spi.PropagationContext in project drools by kiegroup.

the class PhreakFromNode method doLeftUpdates.

public void doLeftUpdates(FromNode fromNode, FromMemory fm, LeftTupleSink sink, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
    BetaMemory bm = fm.getBetaMemory();
    ContextEntry[] context = bm.getContext();
    BetaConstraints betaConstraints = fromNode.getBetaConstraints();
    AlphaNodeFieldConstraint[] alphaConstraints = fromNode.getAlphaConstraints();
    DataProvider dataProvider = fromNode.getDataProvider();
    Class<?> resultClass = fromNode.getResultClass();
    for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        PropagationContext propagationContext = leftTuple.getPropagationContext();
        final Map<Object, RightTuple> previousMatches = (Map<Object, RightTuple>) leftTuple.getContextObject();
        final Map<Object, RightTuple> newMatches = new HashMap<Object, RightTuple>();
        leftTuple.setContextObject(newMatches);
        betaConstraints.updateFromTuple(context, wm, leftTuple);
        FastIterator rightIt = LinkedList.fastIterator;
        for (final java.util.Iterator<?> it = dataProvider.getResults(leftTuple, wm, propagationContext, fm.providerContext); it.hasNext(); ) {
            final Object object = it.next();
            if ((object == null) || !resultClass.isAssignableFrom(object.getClass())) {
                // skip anything if it not assignable
                continue;
            }
            RightTuple rightTuple = previousMatches.remove(object);
            if (rightTuple == null) {
                // new match, propagate assert
                rightTuple = fromNode.createRightTuple(leftTuple, propagationContext, wm, object);
            } else {
                // previous match, so reevaluate and propagate modify
                if (rightIt.next(rightTuple) != null) {
                    // handle the odd case where more than one object has the same hashcode/equals value
                    previousMatches.put(object, (RightTuple) rightIt.next(rightTuple));
                    rightTuple.setNext(null);
                }
            }
            checkConstraintsAndPropagate(sink, leftTuple, rightTuple, alphaConstraints, betaConstraints, propagationContext, wm, fm, context, true, trgLeftTuples, stagedLeftTuples);
            fromNode.addToCreatedHandlesMap(newMatches, rightTuple);
        }
        for (RightTuple rightTuple : previousMatches.values()) {
            for (RightTuple current = rightTuple; current != null; current = (RightTuple) rightIt.next(current)) {
                deleteChildLeftTuple(propagationContext, trgLeftTuples, stagedLeftTuples, current.getFirstChild());
            }
        }
        leftTuple.clearStaged();
        leftTuple = next;
    }
    betaConstraints.resetTuple(context);
}
Also used : BetaConstraints(org.drools.core.common.BetaConstraints) PropagationContext(org.drools.core.spi.PropagationContext) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) BetaMemory(org.drools.core.reteoo.BetaMemory) LeftTuple(org.drools.core.reteoo.LeftTuple) PhreakJoinNode.updateChildLeftTuple(org.drools.core.phreak.PhreakJoinNode.updateChildLeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) ContextEntry(org.drools.core.rule.ContextEntry) DataProvider(org.drools.core.spi.DataProvider) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) FastIterator(org.drools.core.util.FastIterator) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 8 with PropagationContext

use of org.drools.core.spi.PropagationContext in project drools by kiegroup.

the class ProtobufInputMarshaller method readSession.

public static StatefulKnowledgeSessionImpl readSession(ProtobufMessages.KnowledgeSession _session, StatefulKnowledgeSessionImpl session, InternalAgenda agenda, MarshallerReaderContext context) throws IOException, ClassNotFoundException {
    GlobalResolver globalResolver = (GlobalResolver) context.env.get(EnvironmentName.GLOBALS);
    if (globalResolver != null) {
        session.setGlobalResolver(globalResolver);
    }
    if (session.getTimerService() instanceof PseudoClockScheduler) {
        PseudoClockScheduler clock = (PseudoClockScheduler) session.getTimerService();
        clock.advanceTime(_session.getTime(), TimeUnit.MILLISECONDS);
    }
    // RuleFlowGroups need to reference the session
    // for ( InternalAgendaGroup group : agenda.getAgendaGroupsMap().values() ) {
    // ((RuleFlowGroupImpl) group).setWorkingMemory( session );
    // }
    context.wm = session;
    // need to read node memories before reading the fact handles
    // because this data is required during fact propagation
    readNodeMemories(context, _session.getRuleData());
    List<PropagationContext> pctxs = new ArrayList<PropagationContext>();
    if (_session.getRuleData().hasInitialFact()) {
        session.setInitialFactHandle(session.initInitialFact(context.kBase, context));
        context.handles.put(session.getInitialFactHandle().getId(), session.getInitialFactHandle());
    }
    for (ProtobufMessages.EntryPoint _ep : _session.getRuleData().getEntryPointList()) {
        EntryPoint wmep = ((StatefulKnowledgeSessionImpl) context.wm).getEntryPointMap().get(_ep.getEntryPointId());
        readFactHandles(context, _ep, ((WorkingMemoryEntryPoint) wmep).getObjectStore(), pctxs);
        context.filter.fireRNEAs(context.wm);
        readTruthMaintenanceSystem(context, wmep, _ep, pctxs);
    }
    cleanReaderContexts(pctxs);
    readActionQueue(context, _session.getRuleData());
    if (processMarshaller != null) {
        if (_session.hasProcessData()) {
            context.parameterObject = _session.getProcessData();
            processMarshaller.readProcessInstances(context);
            context.parameterObject = _session.getProcessData();
            processMarshaller.readWorkItems(context);
            // This actually does ALL timers, due to backwards compatability issues
            // It will read in old JBPM binaries, but always write to the new binary format.
            context.parameterObject = _session.getProcessData();
            processMarshaller.readProcessTimers(context);
        }
    } else {
        if (_session.hasProcessData()) {
            throw new IllegalStateException("No process marshaller, unable to unmarshall process data.");
        }
    }
    if (_session.hasTimers()) {
        for (ProtobufMessages.Timers.Timer _timer : _session.getTimers().getTimerList()) {
            readTimer(context, _timer);
        }
    }
    // need to process any eventual left over timer node timers
    if (!context.timerNodeSchedulers.isEmpty()) {
        for (Map<TupleKey, Scheduler> schedulers : context.timerNodeSchedulers.values()) {
            for (Scheduler scheduler : schedulers.values()) {
                scheduler.schedule(scheduler.getTrigger());
            }
        }
        context.timerNodeSchedulers.clear();
    }
    // remove the activations filter
    agenda.setActivationsFilter(null);
    return session;
}
Also used : PropagationContext(org.drools.core.spi.PropagationContext) Scheduler(org.drools.core.phreak.PhreakTimerNode.Scheduler) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) ArrayList(java.util.ArrayList) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) GlobalResolver(org.drools.core.spi.GlobalResolver) Timer(org.drools.core.marshalling.impl.ProtobufMessages.Timers.Timer)

Example 9 with PropagationContext

use of org.drools.core.spi.PropagationContext in project drools by kiegroup.

the class ProtobufInputMarshaller method assertHandleIntoOTN.

private static void assertHandleIntoOTN(MarshallerReaderContext context, InternalWorkingMemory wm, InternalFactHandle handle, List<PropagationContext> pctxs) {
    Object object = handle.getObject();
    WorkingMemoryEntryPoint ep = handle.getEntryPoint();
    ObjectTypeConf typeConf = ep.getObjectTypeConfigurationRegistry().getObjectTypeConf(ep.getEntryPoint(), object);
    PropagationContextFactory pctxFactory = wm.getKnowledgeBase().getConfiguration().getComponentFactory().getPropagationContextFactory();
    PropagationContext propagationContext = pctxFactory.createPropagationContext(wm.getNextPropagationIdCounter(), PropagationContext.Type.INSERTION, null, null, handle, ep.getEntryPoint(), context);
    // keeping this list for a later cleanup is necessary because of the lazy propagations that might occur
    pctxs.add(propagationContext);
    ep.getEntryPointNode().assertObject(handle, propagationContext, typeConf, wm);
    wm.flushPropagations();
}
Also used : PropagationContextFactory(org.drools.core.common.PropagationContextFactory) ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) PropagationContext(org.drools.core.spi.PropagationContext) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint)

Example 10 with PropagationContext

use of org.drools.core.spi.PropagationContext in project drools by kiegroup.

the class NamedEntryPoint method insert.

public FactHandle insert(final Object object, final boolean dynamic, final RuleImpl rule, final TerminalNode terminalNode) {
    if (object == null) {
        // you cannot assert a null object
        return null;
    }
    try {
        this.wm.startOperation();
        ObjectTypeConf typeConf = getObjectTypeConfigurationRegistry().getObjectTypeConf(this.entryPoint, object);
        final PropagationContext propagationContext = this.pctxFactory.createPropagationContext(this.wm.getNextPropagationIdCounter(), PropagationContext.Type.INSERTION, rule, terminalNode, null, entryPoint);
        if (this.wm.isSequential()) {
            InternalFactHandle handle = createHandle(object, typeConf);
            propagationContext.setFactHandle(handle);
            insert(handle, object, rule, typeConf, propagationContext);
            return handle;
        }
        InternalFactHandle handle;
        try {
            this.lock.lock();
            // check if the object already exists in the WM
            handle = this.objectStore.getHandleForObject(object);
            if (!typeConf.isTMSEnabled()) {
                // TMS not enabled for this object type
                if (handle != null) {
                    return handle;
                }
                handle = createHandle(object, typeConf);
            } else {
                TruthMaintenanceSystem tms = getTruthMaintenanceSystem();
                EqualityKey key;
                if (handle != null && handle.getEqualityKey().getStatus() == EqualityKey.STATED) {
                    // it's already stated, so just return the handle
                    return handle;
                } else {
                    key = tms.get(object);
                }
                if (key != null && key.getStatus() == EqualityKey.JUSTIFIED) {
                    // The justified set needs to be staged, before we can continue with the stated insert
                    BeliefSet bs = handle.getEqualityKey().getBeliefSet();
                    // staging will set it's status to stated
                    bs.getBeliefSystem().stage(propagationContext, bs);
                }
                handle = createHandle(object, // we know the handle is null
                typeConf);
                if (key == null) {
                    key = new EqualityKey(handle, EqualityKey.STATED);
                    tms.put(key);
                } else {
                    key.addFactHandle(handle);
                }
                handle.setEqualityKey(key);
            }
            propagationContext.setFactHandle(handle);
            // @propertyChangeSupport
            if (dynamic || typeConf.isDynamic()) {
                addPropertyChangeListener(handle, dynamic);
            }
            insert(handle, object, rule, typeConf, propagationContext);
        } finally {
            this.lock.unlock();
        }
        return handle;
    } finally {
        this.wm.endOperation();
    }
}
Also used : ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) PropagationContext(org.drools.core.spi.PropagationContext) BeliefSet(org.drools.core.beliefsystem.BeliefSet)

Aggregations

PropagationContext (org.drools.core.spi.PropagationContext)35 PropagationContextFactory (org.drools.core.common.PropagationContextFactory)8 LeftTuple (org.drools.core.reteoo.LeftTuple)8 DroolsQuery (org.drools.core.base.DroolsQuery)6 InternalFactHandle (org.drools.core.common.InternalFactHandle)6 ObjectTypeConf (org.drools.core.reteoo.ObjectTypeConf)6 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)4 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)4 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)3 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)3 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)3 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 WorkingMemoryEntryPoint (org.drools.core.WorkingMemoryEntryPoint)2 DefaultKnowledgeHelper (org.drools.core.base.DefaultKnowledgeHelper)2 BetaConstraints (org.drools.core.common.BetaConstraints)2 EventFactHandle (org.drools.core.common.EventFactHandle)2 EventSupport (org.drools.core.common.EventSupport)2