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, ActivationsManager activationsManager, 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 = activationsManager.getReteEvaluator().getTimerService();
long timestamp = timerService.getCurrentTime();
String[] calendarNames = timerNode.getCalendarNames();
Calendars calendars = activationsManager.getReteEvaluator().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, activationsManager, timer, timerService, timestamp, calendarNames, calendars, leftTuple, trgLeftTuples, stagedLeftTuples);
leftTuple.clearStaged();
leftTuple = next;
}
}
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 InternalWorkingMemory wm, 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.getDeclarations(), wm);
}
use of org.drools.core.time.impl.DefaultJobHandle in project drools by kiegroup.
the class PhreakTimerNode method scheduleTimer.
private void scheduleTimer(TimerNode timerNode, TimerNodeMemory tm, SegmentMemory smem, LeftTupleSink sink, InternalWorkingMemory wm, TimerService timerService, long timestamp, LeftTuple leftTuple, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples, Trigger trigger) {
if (trigger.hasNextFireTime() == null) {
return;
}
if (trigger.hasNextFireTime().getTime() <= timestamp) {
// first execution is straight away, so void Scheduling
if (log.isTraceEnabled()) {
log.trace("Timer Fire Now {}", leftTuple);
}
LeftTuple childLeftTuple = doPropagateChildLeftTuple(sink, trgLeftTuples, stagedLeftTuples, leftTuple);
if (childLeftTuple.getStagedType() != LeftTuple.NONE) {
// Flag the newly created childLeftTuple to avoid a reevaluation in case it gets
// rescheduled before the end of this doNode loop
childLeftTuple.setContextObject(Boolean.TRUE);
}
trigger.nextFireTime();
Date nextFireTime = trigger.hasNextFireTime();
if (nextFireTime != null && nextFireTime.getTime() <= timestamp) {
throw new IllegalStateException("Trigger.nextFireTime is not increasing");
}
}
if (trigger.hasNextFireTime() != null) {
// can be null, if the system timestamp has surpassed when this was suppose to fire
TimerNodeJob job = new TimerNodeJob();
TimerNodeJobContext jobCtx = new TimerNodeJobContext(timerNode.getId(), trigger, leftTuple, tm, sink, smem.getPathMemories(), wm);
DefaultJobHandle jobHandle = (DefaultJobHandle) timerService.scheduleJob(job, jobCtx, trigger);
leftTuple.setContextObject(jobHandle);
if (log.isTraceEnabled()) {
log.trace("Timer Scheduled {}", leftTuple);
}
}
}
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, ActivationsManager activationsManager, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
TimerService timerService = activationsManager.getReteEvaluator().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.findMostRecentPropagationContext();
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);
}
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(activationsManager);
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;
}
}
use of org.drools.core.time.impl.DefaultJobHandle in project drools by kiegroup.
the class PhreakTimerNode method scheduleTimer.
private void scheduleTimer(TimerNode timerNode, TimerNodeMemory tm, SegmentMemory smem, LeftTupleSink sink, ReteEvaluator reteEvaluator, TimerService timerService, long timestamp, LeftTuple leftTuple, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples, Trigger trigger) {
if (trigger.hasNextFireTime() == null) {
return;
}
if (trigger.hasNextFireTime().getTime() <= timestamp) {
// first execution is straight away, so void Scheduling
if (log.isTraceEnabled()) {
log.trace("Timer Fire Now {}", leftTuple);
}
LeftTuple childLeftTuple = doPropagateChildLeftTuple(sink, trgLeftTuples, stagedLeftTuples, leftTuple);
if (childLeftTuple.getStagedType() != LeftTuple.NONE) {
// Flag the newly created childLeftTuple to avoid a reevaluation in case it gets
// rescheduled before the end of this doNode loop
childLeftTuple.setContextObject(Boolean.TRUE);
}
trigger.nextFireTime();
Date nextFireTime = trigger.hasNextFireTime();
if (nextFireTime != null && nextFireTime.getTime() <= timestamp) {
throw new IllegalStateException("Trigger.nextFireTime is not increasing");
}
}
if (trigger.hasNextFireTime() != null) {
// can be null, if the system timestamp has surpassed when this was suppose to fire
TimerNodeJob job = new TimerNodeJob();
TimerNodeJobContext jobCtx = new TimerNodeJobContext(timerNode.getId(), trigger, leftTuple, tm, sink, smem.getPathMemories(), reteEvaluator);
DefaultJobHandle jobHandle = (DefaultJobHandle) timerService.scheduleJob(job, jobCtx, trigger);
leftTuple.setContextObject(jobHandle);
if (log.isTraceEnabled()) {
log.trace("Timer Scheduled {}", leftTuple);
}
}
}
Aggregations