Search in sources :

Example 1 with IntervalTimer

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

the class RuleBuilderTest method testBuildDurationExpression.

@Test
public void testBuildDurationExpression() throws Exception {
    // creates mock objects
    final RuleBuildContext context = mock(RuleBuildContext.class);
    final RuleImpl rule = mock(RuleImpl.class);
    // creates input object
    final RuleDescr ruleDescr = new RuleDescr("my rule");
    ruleDescr.addAttribute(new AttributeDescr("duration", "( 1h30m )"));
    ruleDescr.addAttribute(new AttributeDescr("calendars", "[\"cal1\", \"cal2\"]"));
    // defining expectations on the mock object
    when(context.getRule()).thenReturn(rule);
    when(context.getRuleDescr()).thenReturn(ruleDescr);
    // calling the build method
    RuleBuilder.buildAttributes(context);
    // check expectations
    verify(rule).setTimer(new IntervalTimer(null, null, -1, TimeUtils.parseTimeString("1h30m"), 0));
    verify(rule).setCalendars(new String[] { "cal1", "cal2" });
}
Also used : RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) IntervalTimer(org.drools.core.time.impl.IntervalTimer) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) Test(org.junit.Test)

Example 2 with IntervalTimer

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

the class RuleBuilderTest method testBuildDurationExpression.

@Test
public void testBuildDurationExpression() throws Exception {
    // creates mock objects
    final RuleBuildContext context = mock(RuleBuildContext.class);
    final RuleImpl rule = mock(RuleImpl.class);
    // creates input object
    final RuleDescr ruleDescr = new RuleDescr("my rule");
    ruleDescr.addAttribute(new AttributeDescr("duration", "( 1h30m )"));
    ruleDescr.addAttribute(new AttributeDescr("calendars", "[\"cal1\", \"cal2\"]"));
    // defining expectations on the mock object
    when(context.getRule()).thenReturn(rule);
    when(context.getRuleDescr()).thenReturn(ruleDescr);
    // calling the build method
    RuleBuilder.buildAttributes(context);
    // check expectations
    verify(rule).setTimer(new IntervalTimer(null, null, -1, TimeUtils.parseTimeString("1h30m"), 0));
    verify(rule).setCalendars(new String[] { "cal1", "cal2" });
}
Also used : RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) RuleDescr(org.drools.drl.ast.descr.RuleDescr) IntervalTimer(org.drools.core.time.impl.IntervalTimer) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr) Test(org.junit.Test)

Example 3 with IntervalTimer

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

Example 4 with IntervalTimer

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

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

the class RuleBuilderTest method testBuildAttributes.

@Test
public void testBuildAttributes() throws Exception {
    // creates mock objects
    final RuleBuildContext context = mock(RuleBuildContext.class);
    final RuleImpl rule = mock(RuleImpl.class);
    // creates input object
    final RuleDescr ruleDescr = new RuleDescr("my rule");
    ruleDescr.addAttribute(new AttributeDescr("no-loop", "true"));
    ruleDescr.addAttribute(new AttributeDescr("auto-focus", "false"));
    ruleDescr.addAttribute(new AttributeDescr("agenda-group", "my agenda"));
    ruleDescr.addAttribute(new AttributeDescr("activation-group", "my activation"));
    ruleDescr.addAttribute(new AttributeDescr("lock-on-active", ""));
    ruleDescr.addAttribute(new AttributeDescr("enabled", "false"));
    ruleDescr.addAttribute(new AttributeDescr("duration", "60"));
    ruleDescr.addAttribute(new AttributeDescr("calendars", "\"cal1\""));
    ruleDescr.addAttribute(new AttributeDescr("date-effective", "10-Jul-1974"));
    ruleDescr.addAttribute(new AttributeDescr("date-expires", "10-Jul-2040"));
    // creates expected results
    final Calendar effective = Calendar.getInstance();
    effective.setTime(DateUtils.parseDate("10-Jul-1974"));
    final Calendar expires = Calendar.getInstance();
    expires.setTime(DateUtils.parseDate("10-Jul-2040"));
    // defining expectations on the mock object
    when(context.getRule()).thenReturn(rule);
    when(context.getRuleDescr()).thenReturn(ruleDescr);
    when(context.getKnowledgeBuilder()).thenReturn(new KnowledgeBuilderImpl());
    // calling the build method
    RuleBuilder.buildAttributes(context);
    // check expectations
    verify(rule).setNoLoop(true);
    verify(rule).setAutoFocus(false);
    verify(rule).setAgendaGroup("my agenda");
    verify(rule).setActivationGroup("my activation");
    verify(rule).setLockOnActive(true);
    verify(rule).setEnabled(EnabledBoolean.ENABLED_FALSE);
    verify(rule).setTimer(new IntervalTimer(null, null, -1, TimeUtils.parseTimeString("60"), 0));
    verify(rule).setCalendars(new String[] { "cal1" });
    verify(rule).setDateEffective(effective);
    verify(rule).setDateExpires(expires);
}
Also used : RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) Calendar(java.util.Calendar) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) IntervalTimer(org.drools.core.time.impl.IntervalTimer) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) Test(org.junit.Test)

Aggregations

IntervalTimer (org.drools.core.time.impl.IntervalTimer)6 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)4 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)4 Test (org.junit.Test)4 ParseException (java.text.ParseException)2 Calendar (java.util.Calendar)2 StringTokenizer (java.util.StringTokenizer)2 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)2 AttributeDescr (org.drools.compiler.lang.descr.AttributeDescr)2 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)2 CronExpression (org.drools.core.time.impl.CronExpression)2 CronTimer (org.drools.core.time.impl.CronTimer)2 ExpressionIntervalTimer (org.drools.core.time.impl.ExpressionIntervalTimer)2 AttributeDescr (org.drools.drl.ast.descr.AttributeDescr)2 RuleDescr (org.drools.drl.ast.descr.RuleDescr)2 DroolsError (org.drools.compiler.compiler.DroolsError)1 RuleBuildError (org.drools.compiler.compiler.RuleBuildError)1 MVELObjectExpression (org.drools.core.base.mvel.MVELObjectExpression)1 TimerExpression (org.drools.core.time.TimerExpression)1 Timer (org.drools.core.time.impl.Timer)1