Search in sources :

Example 6 with AnalysisResult

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

the class QueryElementBuilder method processPositional.

private void processPositional(RuleBuildContext context, QueryImpl query, Declaration[] params, QueryArgument[] arguments, List<Declaration> requiredDeclarations, InternalReadAccessor arrayReader, Pattern pattern, BaseDescr base, String expression, ConstraintConnectiveDescr result) {
    int pos = ((ExprConstraintDescr) base).getPosition();
    if (pos >= arguments.length) {
        context.addError(new DescrBuildError(context.getParentDescr(), base, null, "Unable to parse query '" + query.getName() + "', as postion " + pos + " for expression '" + expression + "' does not exist on query size " + arguments.length));
        return;
    }
    boolean isVariable = isVariable(expression);
    DeclarationScopeResolver declarationResolver = context.getDeclarationResolver();
    Declaration declr = isVariable ? declarationResolver.getDeclaration(expression) : null;
    if (declr != null) {
        // it exists, so it's an input
        requiredDeclarations.add(declr);
        arguments[pos] = new QueryArgument.Declr(declr);
    } else if (isVariable && expression.indexOf('.') < 0) {
        arguments[pos] = getVariableQueryArgument(arrayReader, params, pos, pattern, expression);
    } else {
        // it's an expression and thus an input
        AnalysisResult analysisResult = analyzeExpression(context, base, expression);
        if (analysisResult == null || analysisResult.getIdentifiers().isEmpty()) {
            arguments[pos] = getLiteralQueryArgument(context, base, result);
        } else {
            List<Declaration> declarations = new ArrayList<Declaration>();
            for (String identifier : analysisResult.getIdentifiers()) {
                Declaration declaration = declarationResolver.getDeclaration(identifier);
                if (declaration != null) {
                    declarations.add(declaration);
                }
            }
            if (declarations.size() == analysisResult.getIdentifiers().size()) {
                arguments[pos] = new QueryArgument.Expression(declarations, expression, getParserContext(context));
            } else {
                arguments[pos] = getLiteralQueryArgument(context, base, result);
            }
        }
    }
}
Also used : QueryArgument(org.drools.core.rule.QueryArgument) DeclarationScopeResolver(org.drools.core.spi.DeclarationScopeResolver) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) ArrayList(java.util.ArrayList) List(java.util.List) Declaration(org.drools.core.rule.Declaration) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr)

Example 7 with AnalysisResult

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

the class PatternBuilder method getDeclarationsForReturnValue.

private Declaration[] getDeclarationsForReturnValue(RuleBuildContext context, RelationalExprDescr relDescr, String operator, String value2) {
    Pattern pattern = (Pattern) context.getDeclarationResolver().peekBuildStack();
    ReturnValueRestrictionDescr returnValueRestrictionDescr = new ReturnValueRestrictionDescr(operator, relDescr, value2);
    AnalysisResult analysis = context.getDialect().analyzeExpression(context, returnValueRestrictionDescr, returnValueRestrictionDescr.getContent(), new BoundIdentifiers(pattern, context, null, pattern.getObjectType().getClassType()));
    if (analysis == null) {
        // something bad happened
        return null;
    }
    final BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
    final List<Declaration> tupleDeclarations = new ArrayList<Declaration>();
    final List<Declaration> factDeclarations = new ArrayList<Declaration>();
    for (String id : usedIdentifiers.getDeclrClasses().keySet()) {
        Declaration decl = context.getDeclarationResolver().getDeclaration(id);
        if (decl.getPattern() == pattern) {
            factDeclarations.add(decl);
        } else {
            tupleDeclarations.add(decl);
        }
    }
    createImplicitBindings(context, pattern, analysis.getNotBoundedIdentifiers(), usedIdentifiers, factDeclarations);
    final Declaration[] previousDeclarations = tupleDeclarations.toArray(new Declaration[tupleDeclarations.size()]);
    final Declaration[] localDeclarations = factDeclarations.toArray(new Declaration[factDeclarations.size()]);
    Arrays.sort(previousDeclarations, SortDeclarations.instance);
    Arrays.sort(localDeclarations, SortDeclarations.instance);
    final String[] requiredGlobals = usedIdentifiers.getGlobals().keySet().toArray(new String[usedIdentifiers.getGlobals().size()]);
    Declaration[] requiredDeclarations = new Declaration[previousDeclarations.length + localDeclarations.length];
    System.arraycopy(previousDeclarations, 0, requiredDeclarations, 0, previousDeclarations.length);
    System.arraycopy(localDeclarations, 0, requiredDeclarations, previousDeclarations.length, localDeclarations.length);
    Declaration[] declarations = new Declaration[requiredDeclarations.length + requiredGlobals.length];
    int i = 0;
    for (Declaration requiredDeclaration : requiredDeclarations) {
        declarations[i++] = requiredDeclaration;
    }
    for (String requiredGlobal : requiredGlobals) {
        declarations[i++] = context.getDeclarationResolver().getDeclaration(requiredGlobal);
    }
    return declarations;
}
Also used : Pattern(org.drools.core.rule.Pattern) ReturnValueRestrictionDescr(org.drools.compiler.lang.descr.ReturnValueRestrictionDescr) ArrayList(java.util.ArrayList) Declaration(org.drools.core.rule.Declaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) MVELAnalysisResult(org.drools.compiler.rule.builder.dialect.mvel.MVELAnalysisResult) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) NegConstraint(org.drools.core.rule.constraint.NegConstraint) Constraint(org.drools.core.spi.Constraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) EvaluatorConstraint(org.drools.core.rule.constraint.EvaluatorConstraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers)

Example 8 with AnalysisResult

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

the class PatternBuilder method buildEval.

@SuppressWarnings("unchecked")
protected Constraint buildEval(final RuleBuildContext context, final Pattern pattern, final PredicateDescr predicateDescr, final Map<String, OperatorDescr> aliases, final String expr, final boolean isEvalExpression) {
    AnalysisResult analysis = buildAnalysis(context, pattern, predicateDescr, aliases);
    if (analysis == null) {
        // something bad happened
        return null;
    }
    Declaration[][] usedDeclarations = getUsedDeclarations(context, pattern, analysis);
    Declaration[] previousDeclarations = usedDeclarations[0];
    Declaration[] localDeclarations = usedDeclarations[1];
    BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
    Arrays.sort(previousDeclarations, SortDeclarations.instance);
    Arrays.sort(localDeclarations, SortDeclarations.instance);
    boolean isJavaEval = isEvalExpression && context.getDialect() instanceof JavaDialect;
    if (isJavaEval) {
        final PredicateConstraint predicateConstraint = new PredicateConstraint(null, previousDeclarations, localDeclarations);
        final PredicateBuilder builder = context.getDialect().getPredicateBuilder();
        builder.build(context, usedIdentifiers, previousDeclarations, localDeclarations, predicateConstraint, predicateDescr, analysis);
        return predicateConstraint;
    }
    MVELCompilationUnit compilationUnit = getConstraintBuilder(context).buildCompilationUnit(context, previousDeclarations, localDeclarations, predicateDescr, analysis);
    String[] requiredGlobals = usedIdentifiers.getGlobals().keySet().toArray(new String[usedIdentifiers.getGlobals().size()]);
    Declaration[] mvelDeclarations = new Declaration[previousDeclarations.length + localDeclarations.length + requiredGlobals.length];
    int i = 0;
    for (Declaration d : previousDeclarations) {
        mvelDeclarations[i++] = d;
    }
    for (Declaration d : localDeclarations) {
        mvelDeclarations[i++] = d;
    }
    for (String global : requiredGlobals) {
        mvelDeclarations[i++] = context.getDeclarationResolver().getDeclaration(global);
    }
    boolean isDynamic = !pattern.getObjectType().getClassType().isArray() && !context.getKnowledgeBuilder().getTypeDeclaration(pattern.getObjectType().getClassType()).isTypesafe();
    return getConstraintBuilder(context).buildMvelConstraint(context.getPkg().getName(), expr, mvelDeclarations, getOperators(usedIdentifiers.getOperators()), compilationUnit, isDynamic);
}
Also used : MVELCompilationUnit(org.drools.core.base.mvel.MVELCompilationUnit) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) MVELAnalysisResult(org.drools.compiler.rule.builder.dialect.mvel.MVELAnalysisResult) JavaDialect(org.drools.compiler.rule.builder.dialect.java.JavaDialect) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) NegConstraint(org.drools.core.rule.constraint.NegConstraint) Constraint(org.drools.core.spi.Constraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) EvaluatorConstraint(org.drools.core.rule.constraint.EvaluatorConstraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) Declaration(org.drools.core.rule.Declaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration) PredicateConstraint(org.drools.core.rule.PredicateConstraint)

Example 9 with AnalysisResult

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

the class MVELEvalBuilder method build.

/**
 * Builds and returns an Eval Conditional Element
 *
 * @param context The current build context
 * @param descr The Eval Descriptor to build the eval conditional element from
 *
 * @return the Eval Conditional Element
 */
public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
    boolean typesafe = context.isTypesafe();
    // it must be an EvalDescr
    final EvalDescr evalDescr = (EvalDescr) descr;
    try {
        MVELDialect dialect = (MVELDialect) context.getDialect("mvel");
        Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule());
        AnalysisResult analysis = context.getDialect().analyzeExpression(context, evalDescr, evalDescr.getContent(), new BoundIdentifiers(DeclarationScopeResolver.getDeclarationClasses(decls), context));
        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, SortDeclarations.instance);
        MVELCompilationUnit unit = dialect.getMVELCompilationUnit((String) evalDescr.getContent(), analysis, previousDeclarations, null, null, context, "drools", KnowledgeHelper.class, false, MVELCompilationUnit.Scope.EXPRESSION);
        final EvalCondition eval = new EvalCondition(previousDeclarations);
        MVELEvalExpression expr = new MVELEvalExpression(unit, dialect.getId());
        eval.setEvalExpression(KiePolicyHelper.isPolicyEnabled() ? new SafeEvalExpression(expr) : expr);
        MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
        data.addCompileable(eval, expr);
        expr.compile(data, context.getRule());
        return eval;
    } catch (final Exception e) {
        copyErrorLocation(e, evalDescr);
        context.addError(new DescrBuildError(context.getParentDescr(), evalDescr, e, "Unable to build expression for 'eval':" + e.getMessage() + " '" + evalDescr.getContent() + "'"));
        return null;
    } finally {
        context.setTypesafe(typesafe);
    }
}
Also used : MVELCompilationUnit(org.drools.core.base.mvel.MVELCompilationUnit) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) EvalCondition(org.drools.core.rule.EvalCondition) MVELEvalExpression(org.drools.core.base.mvel.MVELEvalExpression) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) EvalDescr(org.drools.compiler.lang.descr.EvalDescr) Declaration(org.drools.core.rule.Declaration) SafeEvalExpression(org.drools.core.rule.EvalCondition.SafeEvalExpression)

Example 10 with AnalysisResult

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

the class MVELFromBuilder method build.

public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
    String text = ((FromDescr) descr).getExpression();
    Optional<EntryPointId> entryPointId = context.getEntryPointId(text);
    if (entryPointId.isPresent()) {
        return entryPointId.get();
    }
    // This builder is re-usable in other dialects, so specify by name
    MVELDialect dialect = (MVELDialect) context.getDialect("mvel");
    boolean typeSafe = context.isTypesafe();
    if (!dialect.isStrictMode()) {
        context.setTypesafe(false);
    }
    try {
        Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule());
        AnalysisResult analysis = dialect.analyzeExpression(context, descr, text, new BoundIdentifiers(DeclarationScopeResolver.getDeclarationClasses(decls), context));
        if (analysis == null) {
            // something bad happened
            return null;
        }
        Class<?> returnType = ((MVELAnalysisResult) analysis).getReturnType();
        if (prefixPattern != null && !prefixPattern.isCompatibleWithFromReturnType(returnType)) {
            context.addError(new DescrBuildError(descr, context.getRuleDescr(), null, "Pattern of type: '" + prefixPattern.getObjectType() + "' on rule '" + context.getRuleDescr().getName() + "' is not compatible with type " + returnType.getCanonicalName() + " returned by source"));
            return null;
        }
        final BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
        final Declaration[] declarations = new Declaration[usedIdentifiers.getDeclrClasses().size()];
        int j = 0;
        for (String str : usedIdentifiers.getDeclrClasses().keySet()) {
            declarations[j++] = decls.get(str);
        }
        Arrays.sort(declarations, SortDeclarations.instance);
        MVELCompilationUnit unit = dialect.getMVELCompilationUnit(text, analysis, declarations, null, null, context, "drools", KnowledgeHelper.class, false, MVELCompilationUnit.Scope.CONSEQUENCE);
        MVELDataProvider dataProvider = new MVELDataProvider(unit, context.getDialect().getId());
        From from = new From(dataProvider);
        from.setResultPattern(prefixPattern);
        MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
        data.addCompileable(from, dataProvider);
        dataProvider.compile(data, context.getRule());
        return from;
    } catch (final Exception e) {
        DialectUtil.copyErrorLocation(e, descr);
        context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "Unable to build expression for 'from' : " + e.getMessage() + " '" + text + "'"));
        return null;
    } finally {
        context.setTypesafe(typeSafe);
    }
}
Also used : MVELDataProvider(org.drools.core.base.dataproviders.MVELDataProvider) MVELCompilationUnit(org.drools.core.base.mvel.MVELCompilationUnit) FromDescr(org.drools.compiler.lang.descr.FromDescr) From(org.drools.core.rule.From) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) EntryPointId(org.drools.core.rule.EntryPointId) Declaration(org.drools.core.rule.Declaration)

Aggregations

AnalysisResult (org.drools.compiler.compiler.AnalysisResult)14 Declaration (org.drools.core.rule.Declaration)12 BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)9 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)7 MVELCompilationUnit (org.drools.core.base.mvel.MVELCompilationUnit)6 MVELDialectRuntimeData (org.drools.core.rule.MVELDialectRuntimeData)5 MVELAnalysisResult (org.drools.compiler.rule.builder.dialect.mvel.MVELAnalysisResult)4 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)4 Constraint (org.drools.core.spi.Constraint)4 ArrayList (java.util.ArrayList)3 EvalDescr (org.drools.compiler.lang.descr.EvalDescr)3 TypeDeclaration (org.drools.core.rule.TypeDeclaration)3 HashMap (java.util.HashMap)2 Dialect (org.drools.compiler.compiler.Dialect)2 PredicateDescr (org.drools.compiler.lang.descr.PredicateDescr)2 JavaDialect (org.drools.compiler.rule.builder.dialect.java.JavaDialect)2 MVELDialect (org.drools.compiler.rule.builder.dialect.mvel.MVELDialect)2 MutableTypeConstraint (org.drools.core.rule.MutableTypeConstraint)2 Pattern (org.drools.core.rule.Pattern)2 PredicateConstraint (org.drools.core.rule.PredicateConstraint)2