use of org.drools.core.base.mvel.MVELObjectExpression in project drools by kiegroup.
the class MVELObjectExpressionBuilder method build.
public static MVELObjectExpression build(String expression, RuleBuildContext context) {
boolean typesafe = context.isTypesafe();
// pushing consequence LHS into the stack for variable resolution
context.getDeclarationResolver().pushOnBuildStack(context.getRule().getLhs());
try {
// This builder is re-usable in other dialects, so specify by name
MVELDialect dialect = (MVELDialect) context.getDialect("mvel");
Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule());
MVELAnalysisResult analysis = (MVELAnalysisResult) dialect.analyzeExpression(context, context.getRuleDescr(), expression, new BoundIdentifiers(DeclarationScopeResolver.getDeclarationClasses(decls), context));
context.setTypesafe(analysis.isTypesafe());
final BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
int i = usedIdentifiers.getDeclrClasses().keySet().size();
Declaration[] previousDeclarations = new Declaration[i];
i = 0;
for (String id : usedIdentifiers.getDeclrClasses().keySet()) {
previousDeclarations[i++] = decls.get(id);
}
Arrays.sort(previousDeclarations, RuleTerminalNode.SortDeclarations.instance);
MVELCompilationUnit unit = dialect.getMVELCompilationUnit(expression, analysis, previousDeclarations, null, null, context, "drools", KnowledgeHelper.class, false, MVELCompilationUnit.Scope.EXPRESSION);
MVELObjectExpression expr = new MVELObjectExpression(unit, dialect.getId());
MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
data.addCompileable(context.getRule(), expr);
expr.compile(data);
return expr;
} catch (final Exception e) {
DialectUtil.copyErrorLocation(e, context.getRuleDescr());
context.addError(new DescrBuildError(context.getParentDescr(), context.getRuleDescr(), null, "Unable to build expression : " + e.getMessage() + "'" + expression + "'"));
return null;
} finally {
context.setTypesafe(typesafe);
}
}
use of org.drools.core.base.mvel.MVELObjectExpression 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);
}
Aggregations