Search in sources :

Example 6 with RuleBuildError

use of org.drools.compiler.compiler.RuleBuildError in project drools by kiegroup.

the class RuleBuilder method buildCalendars.

private static void buildCalendars(RuleImpl rule, String calendarsString, RuleBuildContext context) {
    Object val = null;
    try {
        val = MVELSafeHelper.getEvaluator().eval(calendarsString);
        String[] calNames = null;
        if (val instanceof List) {
            calNames = (String[]) ((List) val).toArray(new String[((List) val).size()]);
        } else if (val instanceof String) {
            calNames = new String[] { (String) val };
        } else {
            DroolsError err = new RuleBuildError(rule, context.getParentDescr(), null, "Calendars attribute did not return a String or String[] '" + val + "'");
            context.addError(err);
        }
        if (calNames != null) {
            rule.setCalendars(calNames);
        }
    } catch (Exception e) {
        DroolsError err = new RuleBuildError(rule, context.getParentDescr(), null, "Unable to build Calendars attribute '" + val + "'" + e.getMessage());
        context.addError(err);
    }
}
Also used : DroolsError(org.drools.compiler.compiler.DroolsError) RuleBuildError(org.drools.compiler.compiler.RuleBuildError) List(java.util.List) ParseException(java.text.ParseException)

Example 7 with RuleBuildError

use of org.drools.compiler.compiler.RuleBuildError in project drools by kiegroup.

the class KnowledgeBuilderImpl method reportHierarchyErrors.

private void reportHierarchyErrors(Map<String, List<RuleDescr>> parents, Map<String, RuleDescr> sorted) {
    boolean circularDep = false;
    for (List<RuleDescr> rds : parents.values()) {
        for (RuleDescr ruleDescr : rds) {
            if (parents.get(ruleDescr.getParentName()) != null && (sorted.containsKey(ruleDescr.getName()) || parents.containsKey(ruleDescr.getName()))) {
                circularDep = true;
                results.add(new RuleBuildError(ruleDescr.toRule(), ruleDescr, null, "Circular dependency in rules hierarchy"));
                break;
            }
            manageUnresolvedExtension(ruleDescr, sorted.values());
        }
        if (circularDep) {
            break;
        }
    }
}
Also used : RuleBuildError(org.drools.compiler.compiler.RuleBuildError) RuleDescr(org.drools.compiler.lang.descr.RuleDescr)

Example 8 with RuleBuildError

use of org.drools.compiler.compiler.RuleBuildError 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)

Aggregations

RuleBuildError (org.drools.compiler.compiler.RuleBuildError)8 DroolsError (org.drools.compiler.compiler.DroolsError)5 ParseException (java.text.ParseException)4 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)3 Calendar (java.util.Calendar)1 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 StringTokenizer (java.util.StringTokenizer)1 DroolsWarning (org.drools.compiler.compiler.DroolsWarning)1 RuleBuildWarning (org.drools.compiler.compiler.RuleBuildWarning)1 AttributeDescr (org.drools.compiler.lang.descr.AttributeDescr)1 ConditionalBranchDescr (org.drools.compiler.lang.descr.ConditionalBranchDescr)1 EvalDescr (org.drools.compiler.lang.descr.EvalDescr)1 NamedConsequenceDescr (org.drools.compiler.lang.descr.NamedConsequenceDescr)1 MVELObjectExpression (org.drools.core.base.mvel.MVELObjectExpression)1 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)1 ConditionalBranch (org.drools.core.rule.ConditionalBranch)1 EvalCondition (org.drools.core.rule.EvalCondition)1 NamedConsequence (org.drools.core.rule.NamedConsequence)1