Search in sources :

Example 1 with DefaultJobHandle

use of org.drools.core.time.impl.DefaultJobHandle 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 DefaultJobHandle

use of org.drools.core.time.impl.DefaultJobHandle in project drools by kiegroup.

the class PhreakTimerNode method doLeftUpdates.

public void doLeftUpdates(TimerNode timerNode, TimerNodeMemory tm, PathMemory pmem, SegmentMemory smem, LeftTupleSink sink, InternalAgenda agenda, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
    Timer timer = timerNode.getTimer();
    // Variables may have changed for ExpressionIntervalTimer, so it must be rescheduled
    TimerService timerService = agenda.getWorkingMemory().getTimerService();
    long timestamp = timerService.getCurrentTime();
    String[] calendarNames = timerNode.getCalendarNames();
    Calendars calendars = agenda.getWorkingMemory().getCalendars();
    for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        DefaultJobHandle jobHandle = (DefaultJobHandle) leftTuple.getContextObject();
        if (jobHandle != null) {
            // jobHandle can be null, if the time fired straight away, and never ended up scheduling a job
            timerService.removeJob(jobHandle);
        }
        scheduleLeftTuple(timerNode, tm, pmem, smem, sink, agenda, timer, timerService, timestamp, calendarNames, calendars, leftTuple, trgLeftTuples, stagedLeftTuples);
        leftTuple.clearStaged();
        leftTuple = next;
    }
}
Also used : TimerNodeTimer(org.drools.core.marshalling.impl.ProtobufMessages.Timers.TimerNodeTimer) Timer(org.drools.core.time.impl.Timer) Calendars(org.kie.api.runtime.Calendars) LeftTuple(org.drools.core.reteoo.LeftTuple) TimerService(org.drools.core.time.TimerService) DefaultJobHandle(org.drools.core.time.impl.DefaultJobHandle)

Example 3 with DefaultJobHandle

use of org.drools.core.time.impl.DefaultJobHandle in project drools by kiegroup.

the class ExpireJobContextTimerOutputMarshaller method write.

public void write(JobContext jobCtx, MarshallerWriteContext outputCtx) throws IOException {
    // ExpireJob, no state
    ExpireJobContext ejobCtx = (ExpireJobContext) jobCtx;
    DefaultJobHandle jobHandle = (DefaultJobHandle) ejobCtx.getJobHandle();
    PointInTimeTrigger trigger = (PointInTimeTrigger) jobHandle.getTimerJobInstance().getTrigger();
    // There is no reason to serialize a timer when it has no future execution time.
    Date nextFireTime = trigger.hasNextFireTime();
    if (nextFireTime != null) {
        outputCtx.writeShort(PersisterEnums.EXPIRE_TIMER);
        outputCtx.writeLong(ejobCtx.getExpireAction().getFactHandle().getId());
        outputCtx.writeLong(nextFireTime.getTime());
    }
}
Also used : ExpireJobContext(org.drools.core.reteoo.ObjectTypeNode.ExpireJobContext) PointInTimeTrigger(org.drools.core.time.impl.PointInTimeTrigger) Date(java.util.Date) DefaultJobHandle(org.drools.core.time.impl.DefaultJobHandle)

Example 4 with DefaultJobHandle

use of org.drools.core.time.impl.DefaultJobHandle in project drools by kiegroup.

the class ExpireJobContextTimerOutputMarshaller method serialize.

@Override
public ProtobufMessages.Timers.Timer serialize(JobContext jobCtx, MarshallerWriteContext outputCtx) {
    // ExpireJob, no state
    ExpireJobContext ejobCtx = (ExpireJobContext) jobCtx;
    WorkingMemoryReteExpireAction expireAction = ejobCtx.getExpireAction();
    DefaultJobHandle jobHandle = (DefaultJobHandle) ejobCtx.getJobHandle();
    PointInTimeTrigger trigger = (PointInTimeTrigger) jobHandle.getTimerJobInstance().getTrigger();
    Date nextFireTime = trigger.hasNextFireTime();
    if (nextFireTime != null) {
        return ProtobufMessages.Timers.Timer.newBuilder().setType(ProtobufMessages.Timers.TimerType.EXPIRE).setExpire(ProtobufMessages.Timers.ExpireTimer.newBuilder().setHandleId(expireAction.getFactHandle().getId()).setNextFireTimestamp(nextFireTime.getTime()).build()).build();
    } else {
        // There is no reason to serialize a timer when it has no future execution time.
        return null;
    }
}
Also used : ExpireJobContext(org.drools.core.reteoo.ObjectTypeNode.ExpireJobContext) PointInTimeTrigger(org.drools.core.time.impl.PointInTimeTrigger) WorkingMemoryReteExpireAction(org.drools.core.impl.WorkingMemoryReteExpireAction) Date(java.util.Date) DefaultJobHandle(org.drools.core.time.impl.DefaultJobHandle)

Example 5 with DefaultJobHandle

use of org.drools.core.time.impl.DefaultJobHandle in project drools by kiegroup.

the class PhreakTimerNode method createTrigger.

private Trigger createTrigger(final TimerNode timerNode, final ReteEvaluator reteEvaluator, final Timer timer, final long timestamp, final String[] calendarNames, final Calendars calendars, final LeftTuple leftTuple) {
    Object obj = leftTuple.getContextObject();
    DefaultJobHandle jobHandle = obj instanceof DefaultJobHandle ? (DefaultJobHandle) obj : null;
    return timer.createTrigger(timestamp, leftTuple, jobHandle, calendarNames, calendars, timerNode.getStartEndDeclarations(), reteEvaluator);
}
Also used : DefaultJobHandle(org.drools.core.time.impl.DefaultJobHandle)

Aggregations

DefaultJobHandle (org.drools.core.time.impl.DefaultJobHandle)10 LeftTuple (org.drools.core.reteoo.LeftTuple)6 Date (java.util.Date)4 TimerService (org.drools.core.time.TimerService)4 ExpireJobContext (org.drools.core.reteoo.ObjectTypeNode.ExpireJobContext)2 PropagationContext (org.drools.core.spi.PropagationContext)2 PointInTimeTrigger (org.drools.core.time.impl.PointInTimeTrigger)2 Timer (org.drools.core.time.impl.Timer)2 TupleList (org.drools.core.util.index.TupleList)2 Calendars (org.kie.api.runtime.Calendars)2 WorkingMemoryReteExpireAction (org.drools.core.impl.WorkingMemoryReteExpireAction)1 TupleKey (org.drools.core.marshalling.TupleKey)1 TupleKey (org.drools.core.marshalling.impl.ProtobufInputMarshaller.TupleKey)1 TimerNodeTimer (org.drools.core.marshalling.impl.ProtobufMessages.Timers.TimerNodeTimer)1