Search in sources :

Example 1 with DescrBuildError

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;
}
Also used : Pattern(org.drools.core.rule.Pattern) ClassObjectType(org.drools.core.base.ClassObjectType) MVELCompilationUnit(org.drools.core.base.mvel.MVELCompilationUnit) MVELDialect(org.drools.compiler.rule.builder.dialect.mvel.MVELDialect) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) MVELAnalysisResult(org.drools.compiler.rule.builder.dialect.mvel.MVELAnalysisResult) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper)

Example 2 with DescrBuildError

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;
}
Also used : DescrBuildError(org.drools.compiler.compiler.DescrBuildError) EvaluatorDefinition(org.drools.core.base.evaluators.EvaluatorDefinition) Evaluator(org.drools.core.spi.Evaluator)

Example 3 with DescrBuildError

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);
}
Also used : DescrBuildError(org.drools.compiler.compiler.DescrBuildError) EvaluatorWrapper(org.drools.core.base.EvaluatorWrapper) EvaluatorConstraint(org.drools.core.rule.constraint.EvaluatorConstraint) IndexUtil(org.drools.core.util.index.IndexUtil) MVELCompilationUnit(org.drools.core.base.mvel.MVELCompilationUnit) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) Evaluator(org.drools.core.spi.Evaluator)

Example 4 with DescrBuildError

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;
    }
}
Also used : DescrBuildError(org.drools.compiler.compiler.DescrBuildError)

Example 5 with DescrBuildError

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;
}
Also used : DescrBuildError(org.drools.compiler.compiler.DescrBuildError) ConstraintConnectiveDescr(org.drools.compiler.lang.descr.ConstraintConnectiveDescr) DrlExprParser(org.drools.compiler.compiler.DrlExprParser) DroolsParserException(org.drools.compiler.compiler.DroolsParserException)

Aggregations

DescrBuildError (org.drools.compiler.compiler.DescrBuildError)29 Declaration (org.drools.core.rule.Declaration)16 MVELCompilationUnit (org.drools.core.base.mvel.MVELCompilationUnit)10 MVELDialectRuntimeData (org.drools.core.rule.MVELDialectRuntimeData)8 AnalysisResult (org.drools.compiler.compiler.AnalysisResult)7 BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)7 Pattern (org.drools.core.rule.Pattern)6 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)5 ArrayElementReader (org.drools.core.base.extractors.ArrayElementReader)4 MutableTypeConstraint (org.drools.core.rule.MutableTypeConstraint)4 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)4 ArrayList (java.util.ArrayList)3 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)3 ConstraintConnectiveDescr (org.drools.compiler.lang.descr.ConstraintConnectiveDescr)3 SelfReferenceClassFieldReader (org.drools.core.base.extractors.SelfReferenceClassFieldReader)3 QueryArgument (org.drools.core.rule.QueryArgument)3 Constraint (org.drools.core.spi.Constraint)3 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2