Search in sources :

Example 26 with BoundIdentifiers

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

the class MVELExprAnalyzer method analyze.

/**
 * Analyse an expression.
 * @throws RecognitionException
 *             If an error occurs in the parser.
 */
private static MVELAnalysisResult analyze(final Set<String> identifiers, final BoundIdentifiers availableIdentifiers) {
    MVELAnalysisResult result = new MVELAnalysisResult();
    result.setIdentifiers(identifiers);
    final Set<String> notBound = new HashSet<String>(identifiers);
    notBound.remove("this");
    Map<String, Class<?>> usedDecls = new HashMap<String, Class<?>>();
    Map<String, Class<?>> usedGlobals = new HashMap<String, Class<?>>();
    Map<String, EvaluatorWrapper> usedOperators = new HashMap<String, EvaluatorWrapper>();
    for (Entry<String, Class<?>> entry : availableIdentifiers.getDeclrClasses().entrySet()) {
        if (identifiers.contains(entry.getKey())) {
            usedDecls.put(entry.getKey(), entry.getValue());
            notBound.remove(entry.getKey());
        }
    }
    for (String identifier : identifiers) {
        Class<?> type = availableIdentifiers.resolveVarType(identifier);
        if (type != null) {
            usedGlobals.put(identifier, type);
            notBound.remove(identifier);
        }
    }
    for (Map.Entry<String, EvaluatorWrapper> op : availableIdentifiers.getOperators().entrySet()) {
        if (identifiers.contains(op.getKey())) {
            usedOperators.put(op.getKey(), op.getValue());
            notBound.remove(op.getKey());
        }
    }
    BoundIdentifiers boundIdentifiers = new BoundIdentifiers(usedDecls, availableIdentifiers.getContext(), usedOperators, availableIdentifiers.getThisClass());
    boundIdentifiers.setGlobals(usedGlobals);
    result.setBoundIdentifiers(boundIdentifiers);
    result.setNotBoundedIdentifiers(notBound);
    return result;
}
Also used : EvaluatorWrapper(org.drools.core.base.EvaluatorWrapper) HashMap(java.util.HashMap) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 27 with BoundIdentifiers

use of org.drools.compiler.compiler.BoundIdentifiers 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);
    }
}
Also used : MVELCompilationUnit(org.drools.core.base.mvel.MVELCompilationUnit) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) MVELObjectExpression(org.drools.core.base.mvel.MVELObjectExpression) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) Declaration(org.drools.core.rule.Declaration)

Example 28 with BoundIdentifiers

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

the class AbstractASMEvalBuilder method getUsedDeclarations.

private Declaration[] getUsedDeclarations(RuleBuildContext context, Pattern pattern, AnalysisResult analysis) {
    BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
    final List<Declaration> declarations = new ArrayList<Declaration>();
    for (String id : usedIdentifiers.getDeclrClasses().keySet()) {
        declarations.add(context.getDeclarationResolver().getDeclaration(id));
    }
    createImplicitBindings(context, pattern, analysis.getNotBoundedIdentifiers(), analysis.getBoundIdentifiers(), declarations);
    return declarations.toArray(new Declaration[declarations.size()]);
}
Also used : ArrayList(java.util.ArrayList) Declaration(org.drools.core.rule.Declaration) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers)

Example 29 with BoundIdentifiers

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

the class AbstractASMEvalBuilder method build.

public RuleConditionElement build(RuleBuildContext context, BaseDescr descr) {
    // it must be an EvalDescr
    final EvalDescr evalDescr = (EvalDescr) descr;
    Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule());
    AnalysisResult analysis = context.getDialect().analyzeExpression(context, evalDescr, evalDescr.getContent(), new BoundIdentifiers(DeclarationScopeResolver.getDeclarationClasses(decls), context));
    List<Declaration> requiredDeclarations = new ArrayList<Declaration>();
    for (String usedIdentifier : analysis.getIdentifiers()) {
        Declaration usedDec = decls.get(usedIdentifier);
        if (usedDec != null) {
            requiredDeclarations.add(usedDec);
        }
    }
    final Declaration[] declarations = requiredDeclarations.toArray(new Declaration[requiredDeclarations.size()]);
    return buildEval(context, evalDescr, analysis, declarations);
}
Also used : EvalDescr(org.drools.compiler.lang.descr.EvalDescr) ArrayList(java.util.ArrayList) Declaration(org.drools.core.rule.Declaration) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers)

Example 30 with BoundIdentifiers

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

the class JavaAccumulateBuilder method buildAccumulator.

private Accumulator buildAccumulator(RuleBuildContext context, AccumulateDescr accumDescr, Map<String, Declaration> declsInScope, Map<String, Class<?>> declCls, boolean readLocalsFromTuple, Declaration[] sourceDeclArr, Set<Declaration> requiredDecl, AccumulateFunctionCallDescr fc, AccumulateFunction function) {
    // analyze the expression
    final JavaAnalysisResult analysis = (JavaAnalysisResult) context.getDialect().analyzeBlock(context, accumDescr, fc.getParams().length > 0 ? fc.getParams()[0] : "\"\"", new BoundIdentifiers(declCls, context));
    if (analysis == null) {
        // not possible to get the analysis results - compilation error has been already logged
        return null;
    }
    final BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
    // create the array of used declarations
    final Declaration[] previousDeclarations = collectRequiredDeclarations(declsInScope, requiredDecl, usedIdentifiers);
    // generate the code template
    return generateFunctionCallCodeTemplate(context, accumDescr, sourceDeclArr, fc, function, usedIdentifiers, previousDeclarations, readLocalsFromTuple);
}
Also used : Declaration(org.drools.core.rule.Declaration) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers)

Aggregations

BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)34 Declaration (org.drools.core.rule.Declaration)17 ArrayList (java.util.ArrayList)16 HashMap (java.util.HashMap)15 Test (org.junit.Test)12 JavaBlockDescr (org.drools.compiler.rule.builder.dialect.java.parser.JavaBlockDescr)11 AnalysisResult (org.drools.compiler.compiler.AnalysisResult)9 MVELDialectRuntimeData (org.drools.core.rule.MVELDialectRuntimeData)8 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)7 MVELCompilationUnit (org.drools.core.base.mvel.MVELCompilationUnit)7 Cheese (org.drools.compiler.Cheese)4 MVELAnalysisResult (org.drools.compiler.rule.builder.dialect.mvel.MVELAnalysisResult)4 TypeDeclaration (org.drools.core.rule.TypeDeclaration)4 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)4 Constraint (org.drools.core.spi.Constraint)4 HashSet (java.util.HashSet)3 Map (java.util.Map)3 RecognitionException (org.antlr.runtime.RecognitionException)3 MVELDialect (org.drools.compiler.rule.builder.dialect.mvel.MVELDialect)3 EvalDescr (org.drools.compiler.lang.descr.EvalDescr)2