Search in sources :

Example 1 with TupleKey

use of org.drools.core.marshalling.impl.ProtobufInputMarshaller.TupleKey in project drools by kiegroup.

the class PhreakTimerNode method doLeftDeletes.

public void doLeftDeletes(TimerNode timerNode, TimerNodeMemory tm, PathMemory pmem, LeftTupleSink sink, InternalAgenda agenda, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
    TimerService timerService = agenda.getWorkingMemory().getTimerService();
    TupleList leftTuples = tm.getInsertOrUpdateLeftTuples();
    TupleList deletes = tm.getDeleteLeftTuples();
    if (!deletes.isEmpty()) {
        for (LeftTuple leftTuple = (LeftTuple) deletes.getFirst(); leftTuple != null; ) {
            LeftTuple next = (LeftTuple) leftTuple.getNext();
            srcLeftTuples.addDelete(leftTuple);
            if (log.isTraceEnabled()) {
                log.trace("Timer Add Postponed Delete {}", leftTuple);
            }
            leftTuple.clear();
            leftTuple = next;
        }
        deletes.clear();
    }
    for (LeftTuple leftTuple = srcLeftTuples.getDeleteFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        PropagationContext pctx = leftTuple.getPropagationContext();
        Object obj = leftTuple.getContextObject();
        if (obj instanceof DefaultJobHandle) {
            timerService.removeJob((DefaultJobHandle) obj);
        } else if (obj instanceof TupleKey && pctx.getReaderContext() != null) {
            pctx.getReaderContext().removeTimerNodeScheduler(timerNode.getId(), (TupleKey) obj);
        }
        pctx = RuleTerminalNode.findMostRecentPropagationContext(leftTuple, pctx);
        if (leftTuple.getMemory() != null) {
            // it gets removed either way.
            leftTuples.remove(leftTuple);
            if (pctx.getFactHandle().isExpired()) {
                // a expire clashes with insert or update, allow it to propagate once, will handle the expire the second time around
                doPropagateChildLeftTuple(sink, trgLeftTuples, stagedLeftTuples, leftTuple);
                tm.getDeleteLeftTuples().add(leftTuple);
                // make sure it's dirty, so it'll evaluate again
                pmem.doLinkRule(agenda);
                if (log.isTraceEnabled()) {
                    log.trace("Timer Postponed Delete {}", leftTuple);
                }
            }
        }
        if (leftTuple.getMemory() == null) {
            // if it's != null, then it's already been postponed, and the existing child propagated
            // only has one child
            LeftTuple childLeftTuple = leftTuple.getFirstChild();
            if (childLeftTuple != null) {
                childLeftTuple.setPropagationContext(leftTuple.getPropagationContext());
                RuleNetworkEvaluator.deleteChildLeftTuple(childLeftTuple, trgLeftTuples, stagedLeftTuples);
                if (log.isTraceEnabled()) {
                    log.trace("Timer Delete {}", leftTuple);
                }
            }
        }
        leftTuple.clearStaged();
        leftTuple = next;
    }
}
Also used : TupleList(org.drools.core.util.index.TupleList) PropagationContext(org.drools.core.spi.PropagationContext) TupleKey(org.drools.core.marshalling.impl.ProtobufInputMarshaller.TupleKey) LeftTuple(org.drools.core.reteoo.LeftTuple) TimerService(org.drools.core.time.TimerService) DefaultJobHandle(org.drools.core.time.impl.DefaultJobHandle)

Example 2 with TupleKey

use of org.drools.core.marshalling.impl.ProtobufInputMarshaller.TupleKey in project drools by kiegroup.

the class PhreakTimerNode method scheduleLeftTuple.

private void scheduleLeftTuple(final TimerNode timerNode, final TimerNodeMemory tm, final PathMemory pmem, final SegmentMemory smem, final LeftTupleSink sink, final InternalAgenda agenda, final Timer timer, final TimerService timerService, final long timestamp, final String[] calendarNames, final Calendars calendars, final LeftTuple leftTuple, final TupleSets<LeftTuple> trgLeftTuples, final TupleSets<LeftTuple> stagedLeftTuples) {
    InternalWorkingMemory wm = agenda.getWorkingMemory();
    if (leftTuple.getPropagationContext().getReaderContext() == null) {
        final Trigger trigger = createTrigger(timerNode, wm, timer, timestamp, calendarNames, calendars, leftTuple);
        // regular propagation
        scheduleTimer(timerNode, tm, smem, sink, wm, timerService, timestamp, leftTuple, trgLeftTuples, stagedLeftTuples, trigger);
    } else {
        // de-serializing, so we need to correlate timers before scheduling them
        Scheduler scheduler = new Scheduler() {

            @Override
            public void schedule(Trigger t) {
                scheduleTimer(timerNode, tm, smem, sink, wm, timerService, timestamp, leftTuple, trgLeftTuples, stagedLeftTuples, t);
                evaluate(pmem, agenda, sink, tm, trgLeftTuples);
            }

            @Override
            public Trigger getTrigger() {
                return createTrigger(timerNode, wm, timer, timestamp, calendarNames, calendars, leftTuple);
            }
        };
        TupleKey key = PersisterHelper.createTupleKey(leftTuple);
        leftTuple.getPropagationContext().getReaderContext().addTimerNodeScheduler(timerNode.getId(), key, scheduler);
        leftTuple.setContextObject(key);
    }
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) Trigger(org.drools.core.time.Trigger) TupleKey(org.drools.core.marshalling.impl.ProtobufInputMarshaller.TupleKey)

Example 3 with TupleKey

use of org.drools.core.marshalling.impl.ProtobufInputMarshaller.TupleKey in project drools by kiegroup.

the class AccumulateNode method createResultFactHandle.

public InternalFactHandle createResultFactHandle(final PropagationContext context, final InternalWorkingMemory workingMemory, final LeftTuple leftTuple, final Object result) {
    InternalFactHandle handle;
    ProtobufMessages.FactHandle _handle = null;
    if (context.getReaderContext() != null) {
        Map<TupleKey, FactHandle> map = (Map<ProtobufInputMarshaller.TupleKey, ProtobufMessages.FactHandle>) context.getReaderContext().nodeMemories.get(getId());
        if (map != null) {
            _handle = map.get(PersisterHelper.createTupleKey(leftTuple));
        }
    }
    if (_handle != null) {
        // create a handle with the given id
        handle = workingMemory.getFactHandleFactory().newFactHandle(_handle.getId(), result, _handle.getRecency(), workingMemory.getObjectTypeConfigurationRegistry().getObjectTypeConf(context.getEntryPoint(), result), workingMemory, // so far, result is not an event
        null);
    } else {
        handle = workingMemory.getFactHandleFactory().newFactHandle(result, workingMemory.getObjectTypeConfigurationRegistry().getObjectTypeConf(context.getEntryPoint(), result), workingMemory, // so far, result is not an event
        null);
    }
    return handle;
}
Also used : ProtobufInputMarshaller(org.drools.core.marshalling.impl.ProtobufInputMarshaller) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.drools.core.marshalling.impl.ProtobufMessages.FactHandle) FactHandle(org.drools.core.marshalling.impl.ProtobufMessages.FactHandle) InternalFactHandle(org.drools.core.common.InternalFactHandle) ProtobufMessages(org.drools.core.marshalling.impl.ProtobufMessages) TupleKey(org.drools.core.marshalling.impl.ProtobufInputMarshaller.TupleKey) Map(java.util.Map)

Example 4 with TupleKey

use of org.drools.core.marshalling.impl.ProtobufInputMarshaller.TupleKey in project drools by kiegroup.

the class FromNode method createFactHandle.

public InternalFactHandle createFactHandle(Tuple leftTuple, PropagationContext context, InternalWorkingMemory workingMemory, Object object) {
    FactHandle _handle = null;
    if (objectTypeConf == null) {
        // use default entry point and object class. Notice that at this point object is assignable to resultClass
        objectTypeConf = new ClassObjectTypeConf(workingMemory.getEntryPoint(), resultClass, workingMemory.getKnowledgeBase());
    }
    if (context.getReaderContext() != null) {
        Map<TupleKey, List<FactHandle>> map = (Map<TupleKey, List<FactHandle>>) context.getReaderContext().nodeMemories.get(getId());
        if (map != null) {
            TupleKey key = PersisterHelper.createTupleKey(leftTuple);
            List<FactHandle> list = map.get(key);
            if (list != null && !list.isEmpty()) {
                // it is a linked list, so the operation is fairly efficient
                _handle = ((java.util.LinkedList<FactHandle>) list).removeFirst();
                if (list.isEmpty()) {
                    map.remove(key);
                }
            }
        }
    }
    InternalFactHandle handle;
    if (_handle != null) {
        // create a handle with the given id
        handle = workingMemory.getFactHandleFactory().newFactHandle(_handle.getId(), object, _handle.getRecency(), objectTypeConf, workingMemory, null);
    } else {
        handle = workingMemory.getFactHandleFactory().newFactHandle(object, objectTypeConf, workingMemory, null);
    }
    return handle;
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.drools.core.marshalling.impl.ProtobufMessages.FactHandle) TupleList(org.drools.core.util.index.TupleList) List(java.util.List) TupleKey(org.drools.core.marshalling.impl.ProtobufInputMarshaller.TupleKey) InternalFactHandle(org.drools.core.common.InternalFactHandle) Map(java.util.Map)

Aggregations

TupleKey (org.drools.core.marshalling.impl.ProtobufInputMarshaller.TupleKey)4 Map (java.util.Map)2 InternalFactHandle (org.drools.core.common.InternalFactHandle)2 FactHandle (org.drools.core.marshalling.impl.ProtobufMessages.FactHandle)2 TupleList (org.drools.core.util.index.TupleList)2 List (java.util.List)1 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)1 ProtobufInputMarshaller (org.drools.core.marshalling.impl.ProtobufInputMarshaller)1 ProtobufMessages (org.drools.core.marshalling.impl.ProtobufMessages)1 LeftTuple (org.drools.core.reteoo.LeftTuple)1 PropagationContext (org.drools.core.spi.PropagationContext)1 TimerService (org.drools.core.time.TimerService)1 Trigger (org.drools.core.time.Trigger)1 DefaultJobHandle (org.drools.core.time.impl.DefaultJobHandle)1