use of org.drools.compiler.compiler.DescrBuildError in project drools by kiegroup.
the class MVELConstraintBuilder method buildCompilationUnit.
public MVELCompilationUnit buildCompilationUnit(final RuleBuildContext context, final Declaration[] previousDeclarations, final Declaration[] localDeclarations, final PredicateDescr predicateDescr, final AnalysisResult analysis) {
if (context.isTypesafe() && analysis instanceof MVELAnalysisResult) {
Class<?> returnClass = ((MVELAnalysisResult) analysis).getReturnType();
if (returnClass != Boolean.class && returnClass != Boolean.TYPE) {
context.addError(new DescrBuildError(context.getParentDescr(), predicateDescr, null, "Predicate '" + predicateDescr.getContent() + "' must be a Boolean expression\n" + predicateDescr.positionAsString()));
}
}
MVELDialect dialect = (MVELDialect) context.getDialect("mvel");
MVELCompilationUnit unit = null;
try {
Map<String, Class<?>> declIds = context.getDeclarationResolver().getDeclarationClasses(context.getRule());
Pattern p = (Pattern) context.getDeclarationResolver().peekBuildStack();
if (p.getObjectType() instanceof ClassObjectType) {
declIds.put("this", ((ClassObjectType) p.getObjectType()).getClassType());
}
unit = dialect.getMVELCompilationUnit((String) predicateDescr.getContent(), analysis, previousDeclarations, localDeclarations, null, context, "drools", KnowledgeHelper.class, context.isInXpath(), MVELCompilationUnit.Scope.CONSTRAINT);
} catch (final Exception e) {
copyErrorLocation(e, predicateDescr);
context.addError(new DescrBuildError(context.getParentDescr(), predicateDescr, e, "Unable to build expression for 'inline-eval' : " + e.getMessage() + "'" + predicateDescr.getContent() + "'\n" + e.getMessage()));
}
return unit;
}
use of org.drools.compiler.compiler.DescrBuildError in project drools by kiegroup.
the class MVELConstraintBuilder method getEvaluator.
public Evaluator getEvaluator(final RuleBuildContext context, final BaseDescr descr, final ValueType valueType, final String evaluatorString, final boolean isNegated, final String parameters, final EvaluatorDefinition.Target left, final EvaluatorDefinition.Target right) {
final EvaluatorDefinition def = context.getConfiguration().getEvaluatorRegistry().getEvaluatorDefinition(evaluatorString);
if (def == null) {
context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "Unable to determine the Evaluator for ID '" + evaluatorString + "'"));
return null;
}
final Evaluator evaluator = def.getEvaluator(valueType, evaluatorString, isNegated, parameters, left, right);
if (evaluator == null) {
context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "Evaluator '" + (isNegated ? "not " : "") + evaluatorString + "' does not support type '" + valueType));
}
return evaluator;
}
use of org.drools.compiler.compiler.DescrBuildError in project drools by kiegroup.
the class MVELConstraintBuilder method buildLiteralConstraint.
public Constraint buildLiteralConstraint(RuleBuildContext context, Pattern pattern, ValueType vtype, FieldValue field, String expression, String leftValue, String operator, String rightValue, InternalReadAccessor extractor, LiteralRestrictionDescr restrictionDescr, Map<String, OperatorDescr> aliases) {
if (!isMvelOperator(operator)) {
Evaluator evaluator = buildLiteralEvaluator(context, extractor, restrictionDescr, vtype);
if (evaluator != null && evaluator.isTemporal()) {
try {
field = context.getCompilerFactory().getFieldFactory().getFieldValue(field.getValue(), ValueType.DATE_TYPE);
} catch (Exception e) {
context.addError(new DescrBuildError(context.getParentDescr(), restrictionDescr, null, e.getMessage()));
}
}
return new EvaluatorConstraint(field, evaluator, extractor);
}
String mvelExpr = normalizeMVELLiteralExpression(vtype, field, expression, leftValue, operator, rightValue, restrictionDescr);
IndexUtil.ConstraintType constraintType = IndexUtil.ConstraintType.decode(operator);
MVELCompilationUnit compilationUnit = buildCompilationUnit(context, pattern, mvelExpr, aliases);
EvaluatorWrapper[] operators = getOperators(buildOperators(context, pattern, restrictionDescr, aliases));
return new MvelConstraint(context.getPkg().getName(), mvelExpr, compilationUnit, constraintType, field, extractor, operators);
}
use of org.drools.compiler.compiler.DescrBuildError in project drools by kiegroup.
the class PackageBuildContext method init.
/**
* Default constructor
*/
public void init(final KnowledgeBuilderImpl kBuilder, final InternalKnowledgePackage pkg, final BaseDescr parentDescr, final DialectCompiletimeRegistry dialectRegistry, final Dialect defaultDialect, final Dialectable component) {
this.kBuilder = kBuilder;
this.pkg = pkg;
this.parentDescr = parentDescr;
this.dialectRegistry = dialectRegistry;
this.dialect = (component != null && component.getDialect() != null) ? this.dialectRegistry.getDialect(component.getDialect()) : defaultDialect;
this.typesafe = ((MVELDialect) dialectRegistry.getDialect("mvel")).isStrictMode();
if (dialect == null && (component != null && component.getDialect() != null)) {
this.errors.add(new DescrBuildError(null, parentDescr, component, "Unable to load Dialect '" + component.getDialect() + "'"));
// dialect is null, but fall back to default dialect so we can attempt to compile rest of rule.
this.dialect = defaultDialect;
}
}
use of org.drools.compiler.compiler.DescrBuildError in project drools by kiegroup.
the class QueryElementBuilder method parseExpression.
@SuppressWarnings("unchecked")
private ConstraintConnectiveDescr parseExpression(final RuleBuildContext context, final PatternDescr patternDescr, final String expression) {
DrlExprParser parser = new DrlExprParser(context.getConfiguration().getLanguageLevel());
ConstraintConnectiveDescr result = parser.parse(expression);
if (result == null || parser.hasErrors()) {
for (DroolsParserException error : parser.getErrors()) {
context.addError(new DescrBuildError(context.getParentDescr(), patternDescr, null, "Unable to parser pattern expression:\n" + error.getMessage()));
}
return null;
}
return result;
}
Aggregations