Search in sources :

Example 1 with Timer

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

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

the class PhreakTimerNode method doLeftInserts.

public void doLeftInserts(TimerNode timerNode, TimerNodeMemory tm, PathMemory pmem, SegmentMemory smem, LeftTupleSink sink, InternalAgenda agenda, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples) {
    Timer timer = timerNode.getTimer();
    TimerService timerService = agenda.getWorkingMemory().getTimerService();
    long timestamp = timerService.getCurrentTime();
    String[] calendarNames = timerNode.getCalendarNames();
    Calendars calendars = agenda.getWorkingMemory().getCalendars();
    for (LeftTuple leftTuple = srcLeftTuples.getInsertFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        scheduleLeftTuple(timerNode, tm, pmem, smem, sink, agenda, timer, timerService, timestamp, calendarNames, calendars, leftTuple, trgLeftTuples, null);
        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)

Example 3 with Timer

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

the class TimerBuilder method build.

public void build(final BuildContext context, final BuildUtils utils, final RuleConditionElement rce) {
    final Timer timer = (Timer) rce;
    context.pushRuleComponent(timer);
    Declaration[][] declrs = timer instanceof BaseTimer ? ((BaseTimer) timer).getTimerDeclarations(context.getSubRule().getOuterDeclarations()) : null;
    context.setTupleSource(utils.attachNode(context, context.getComponentFactory().getNodeFactoryService().buildTimerNode(context.getNextId(), timer, context.getRule().getCalendars(), declrs, context.getTupleSource(), context)));
    context.setAlphaConstraints(null);
    context.setBetaconstraints(null);
    context.popRuleComponent();
}
Also used : BaseTimer(org.drools.core.time.impl.BaseTimer) Timer(org.drools.core.time.impl.Timer) BaseTimer(org.drools.core.time.impl.BaseTimer)

Example 4 with Timer

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

the class RuleBuilder method buildTimer.

private static void buildTimer(RuleImpl rule, String timerString, RuleBuildContext context) {
    if (timerString.indexOf('(') >= 0) {
        timerString = timerString.substring(timerString.indexOf('(') + 1, timerString.lastIndexOf(')')).trim();
    }
    int colonPos = timerString.indexOf(":");
    int semicolonPos = timerString.indexOf(";");
    // default protocol
    String protocol = "int";
    if (colonPos == -1) {
        if (timerString.startsWith("int") || timerString.startsWith("cron") || timerString.startsWith("expr")) {
            DroolsError err = new RuleBuildError(rule, context.getParentDescr(), null, "Incorrect timer definition '" + timerString + "' - missing colon?");
            context.addError(err);
            return;
        }
    } else {
        protocol = timerString.substring(0, colonPos);
    }
    String startDate = extractParam(timerString, "start");
    String endDate = extractParam(timerString, "end");
    String repeatLimitString = extractParam(timerString, "repeat-limit");
    int repeatLimit = repeatLimitString != null ? Integer.parseInt(repeatLimitString) : -1;
    String body = timerString.substring(colonPos + 1, semicolonPos > 0 ? semicolonPos : timerString.length()).trim();
    Timer timer;
    if ("cron".equals(protocol)) {
        try {
            timer = new CronTimer(createMVELExpr(startDate, context), createMVELExpr(endDate, context), repeatLimit, new CronExpression(body));
        } catch (ParseException e) {
            DroolsError err = new RuleBuildError(rule, context.getParentDescr(), null, "Unable to build set timer '" + timerString + "'");
            context.addError(err);
            return;
        }
    } else if ("int".equals(protocol)) {
        String[] times = body.trim().split("\\s");
        long delay = 0;
        long period = 0;
        if (times.length > 2) {
            DroolsError err = new RuleBuildError(rule, context.getParentDescr(), null, "Incorrect number of arguments for interval timer '" + timerString + "'");
            context.addError(err);
            return;
        }
        try {
            if (times.length == 1) {
                // only defines a delay
                delay = TimeUtils.parseTimeString(times[0]);
            } else {
                // defines a delay and a period for intervals
                delay = TimeUtils.parseTimeString(times[0]);
                period = TimeUtils.parseTimeString(times[1]);
            }
        } catch (RuntimeException e) {
            DroolsError err = new RuleBuildError(rule, context.getParentDescr(), null, "Incorrect timer definition '" + timerString + "' " + e.getMessage());
            context.addError(err);
            return;
        }
        timer = new IntervalTimer(createMVELExpr(startDate, context), createMVELExpr(endDate, context), repeatLimit, delay, period);
    } else if ("expr".equals(protocol)) {
        body = body.trim();
        StringTokenizer tok = new StringTokenizer(body, ",;");
        if (tok.countTokens() > 2) {
            DroolsError err = new RuleBuildError(rule, context.getParentDescr(), null, "Incorrect number of arguments for expression timer '" + timerString + "'");
            context.addError(err);
            return;
        }
        MVELObjectExpression times = MVELObjectExpressionBuilder.build(tok.nextToken().trim(), context);
        MVELObjectExpression period = tok.hasMoreTokens() ? MVELObjectExpressionBuilder.build(tok.nextToken().trim(), context) : MVELObjectExpressionBuilder.build("0", context);
        timer = new ExpressionIntervalTimer(createMVELExpr(startDate, context), createMVELExpr(endDate, context), repeatLimit, times, period);
    } else {
        DroolsError err = new RuleBuildError(rule, context.getParentDescr(), null, "Protocol for timer does not exist '" + timerString + "'");
        context.addError(err);
        return;
    }
    rule.setTimer(timer);
}
Also used : RuleBuildError(org.drools.compiler.compiler.RuleBuildError) IntervalTimer(org.drools.core.time.impl.IntervalTimer) ExpressionIntervalTimer(org.drools.core.time.impl.ExpressionIntervalTimer) ExpressionIntervalTimer(org.drools.core.time.impl.ExpressionIntervalTimer) DroolsError(org.drools.compiler.compiler.DroolsError) StringTokenizer(java.util.StringTokenizer) MVELObjectExpression(org.drools.core.base.mvel.MVELObjectExpression) IntervalTimer(org.drools.core.time.impl.IntervalTimer) ExpressionIntervalTimer(org.drools.core.time.impl.ExpressionIntervalTimer) Timer(org.drools.core.time.impl.Timer) CronTimer(org.drools.core.time.impl.CronTimer) CronExpression(org.drools.core.time.impl.CronExpression) ParseException(java.text.ParseException) CronTimer(org.drools.core.time.impl.CronTimer)

Example 5 with Timer

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

the class NamedConsequenceBuilder method build.

public void build(BuildContext context, BuildUtils utils, RuleConditionElement rce) {
    NamedConsequence namedConsequence = (NamedConsequence) rce;
    Timer timer = context.getRule().getTimer();
    if (timer != null) {
        ReteooComponentBuilder builder = utils.getBuilderFor(Timer.class);
        builder.build(context, utils, context.getRule().getTimer());
    }
    RuleTerminalNode terminalNode = buildTerminalNodeForNamedConsequence(context, namedConsequence);
    terminalNode.attach(context);
    terminalNode.networkUpdated(new UpdateContext());
    // adds the terminal node to the list of nodes created/added by this sub-rule
    context.getNodes().add(terminalNode);
    if (timer != null) {
        context.setTupleSource(context.getTupleSource().getLeftTupleSource());
    }
    context.setTerminated(namedConsequence.isTerminal());
}
Also used : Timer(org.drools.core.time.impl.Timer) UpdateContext(org.drools.core.common.UpdateContext) NamedConsequence(org.drools.core.rule.NamedConsequence) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode)

Aggregations

Timer (org.drools.core.time.impl.Timer)6 TimerNodeTimer (org.drools.core.marshalling.impl.ProtobufMessages.Timers.TimerNodeTimer)2 LeftTuple (org.drools.core.reteoo.LeftTuple)2 TimerService (org.drools.core.time.TimerService)2 Calendars (org.kie.api.runtime.Calendars)2 ParseException (java.text.ParseException)1 StringTokenizer (java.util.StringTokenizer)1 DroolsError (org.drools.compiler.compiler.DroolsError)1 RuleBuildError (org.drools.compiler.compiler.RuleBuildError)1 MVELObjectExpression (org.drools.core.base.mvel.MVELObjectExpression)1 UpdateContext (org.drools.core.common.UpdateContext)1 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)1 Declaration (org.drools.core.rule.Declaration)1 IntervalProviderConstraint (org.drools.core.rule.IntervalProviderConstraint)1 NamedConsequence (org.drools.core.rule.NamedConsequence)1 TypeDeclaration (org.drools.core.rule.TypeDeclaration)1 BaseTimer (org.drools.core.time.impl.BaseTimer)1 CompositeMaxDurationTimer (org.drools.core.time.impl.CompositeMaxDurationTimer)1 CronExpression (org.drools.core.time.impl.CronExpression)1 CronTimer (org.drools.core.time.impl.CronTimer)1