Search in sources :

Example 1 with TimerExpression

use of org.drools.core.time.TimerExpression in project drools by kiegroup.

the class RuleBuilder method buildTimer.

public static Timer buildTimer(String timerString, RuleBuildContext context, Function<String, TimerExpression> exprCreator, Consumer<String> errorManager) {
    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")) {
            errorManager.accept("Incorrect timer definition '" + timerString + "' - missing colon?");
            return null;
        }
    } 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();
    if ("cron".equals(protocol)) {
        try {
            return new CronTimer(exprCreator.apply(startDate), exprCreator.apply(endDate), repeatLimit, new CronExpression(body));
        } catch (ParseException e) {
            errorManager.accept("Unable to build set timer '" + timerString + "'");
            return null;
        }
    }
    if ("int".equals(protocol)) {
        String[] times = body.trim().split("\\s");
        long delay = 0;
        long period = 0;
        if (times.length > 2) {
            errorManager.accept("Incorrect number of arguments for interval timer '" + timerString + "'");
            return null;
        }
        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) {
            errorManager.accept("Incorrect timer definition '" + timerString + "' " + e.getMessage());
            return null;
        }
        return new IntervalTimer(exprCreator.apply(startDate), exprCreator.apply(endDate), repeatLimit, delay, period);
    }
    if ("expr".equals(protocol)) {
        body = body.trim();
        StringTokenizer tok = new StringTokenizer(body, ",;");
        if (tok.countTokens() > 2) {
            errorManager.accept("Incorrect number of arguments for expression timer '" + timerString + "'");
            return null;
        }
        TimerExpression times = createTimerExpression(context, exprCreator, tok.nextToken().trim());
        TimerExpression period = createTimerExpression(context, exprCreator, tok.hasMoreTokens() ? tok.nextToken().trim() : "0");
        return new ExpressionIntervalTimer(exprCreator.apply(startDate), exprCreator.apply(endDate), repeatLimit, times, period);
    }
    errorManager.accept("Protocol for timer does not exist '" + timerString + "'");
    return null;
}
Also used : StringTokenizer(java.util.StringTokenizer) TimerExpression(org.drools.core.time.TimerExpression) IntervalTimer(org.drools.core.time.impl.IntervalTimer) ExpressionIntervalTimer(org.drools.core.time.impl.ExpressionIntervalTimer) CronExpression(org.drools.core.time.impl.CronExpression) ParseException(java.text.ParseException) CronTimer(org.drools.core.time.impl.CronTimer) ExpressionIntervalTimer(org.drools.core.time.impl.ExpressionIntervalTimer)

Aggregations

ParseException (java.text.ParseException)1 StringTokenizer (java.util.StringTokenizer)1 TimerExpression (org.drools.core.time.TimerExpression)1 CronExpression (org.drools.core.time.impl.CronExpression)1 CronTimer (org.drools.core.time.impl.CronTimer)1 ExpressionIntervalTimer (org.drools.core.time.impl.ExpressionIntervalTimer)1 IntervalTimer (org.drools.core.time.impl.IntervalTimer)1