Search in sources :

Example 21 with BoundIdentifiers

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

the class MVELAccumulateBuilder method build.

@SuppressWarnings("unchecked")
public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
    boolean typesafe = context.isTypesafe();
    try {
        final AccumulateDescr accumDescr = (AccumulateDescr) descr;
        if (!accumDescr.hasValidInput()) {
            return null;
        }
        final RuleConditionBuilder builder = (RuleConditionBuilder) context.getDialect().getBuilder(accumDescr.getInput().getClass());
        // create source CE
        final RuleConditionElement source = builder.build(context, accumDescr.getInput());
        if (source == null) {
            return null;
        }
        MVELDialect dialect = (MVELDialect) context.getDialect();
        Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule());
        Map<String, Declaration> sourceOuterDeclr = source.getOuterDeclarations();
        Map<String, Class<?>> declarationClasses = DeclarationScopeResolver.getDeclarationClasses(decls);
        declarationClasses.putAll(DeclarationScopeResolver.getDeclarationClasses(sourceOuterDeclr));
        BoundIdentifiers boundIds = new BoundIdentifiers(declarationClasses, context);
        final boolean readLocalsFromTuple = PackageBuilderUtil.isReadLocalsFromTuple(context, accumDescr, source);
        Accumulator[] accumulators;
        if (accumDescr.isExternalFunction()) {
            // uses accumulate functions
            accumulators = buildExternalFunctions(context, accumDescr, dialect, decls, sourceOuterDeclr, boundIds, readLocalsFromTuple);
        } else {
            // it is a custom accumulate
            accumulators = buildCustomAccumulate(context, accumDescr, dialect, decls, sourceOuterDeclr, boundIds, readLocalsFromTuple);
        }
        List<Declaration> requiredDeclarations = new ArrayList<Declaration>();
        for (Accumulator acc : accumulators) {
            MvelAccumulator mvelAcc = (MvelAccumulator) acc;
            Collections.addAll(requiredDeclarations, mvelAcc.getRequiredDeclarations());
        }
        MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
        Accumulate accumulate;
        if (accumDescr.isMultiFunction()) {
            accumulate = new MultiAccumulate(source, requiredDeclarations.toArray(new Declaration[requiredDeclarations.size()]), accumulators);
            int index = 0;
            for (Accumulator accumulator : accumulators) {
                data.addCompileable(((MultiAccumulate) accumulate).new Wirer(index++), (MVELCompileable) accumulator);
                ((MVELCompileable) accumulator).compile(data, context.getRule());
            }
        } else {
            accumulate = new SingleAccumulate(source, requiredDeclarations.toArray(new Declaration[requiredDeclarations.size()]), accumulators[0]);
            data.addCompileable(((SingleAccumulate) accumulate).new Wirer(), (MVELCompileable) accumulators[0]);
            ((MVELCompileable) accumulators[0]).compile(data, context.getRule());
        }
        return accumulate;
    } catch (Exception e) {
        DialectUtil.copyErrorLocation(e, descr);
        context.addError(new DescrBuildError(context.getParentDescr(), descr, e, "Unable to build expression for 'accumulate' : " + e.getMessage()));
        return null;
    } finally {
        context.setTypesafe(typesafe);
    }
}
Also used : MVELAccumulator(org.drools.core.base.mvel.MVELAccumulator) Accumulator(org.drools.core.spi.Accumulator) MvelAccumulator(org.drools.core.spi.MvelAccumulator) MVELCompileable(org.drools.core.base.mvel.MVELCompileable) ArrayList(java.util.ArrayList) AccumulateDescr(org.drools.compiler.lang.descr.AccumulateDescr) MultiAccumulate(org.drools.core.rule.MultiAccumulate) SingleAccumulate(org.drools.core.rule.SingleAccumulate) Accumulate(org.drools.core.rule.Accumulate) MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) Declaration(org.drools.core.rule.Declaration) MultiAccumulate(org.drools.core.rule.MultiAccumulate) RuleConditionBuilder(org.drools.compiler.rule.builder.RuleConditionBuilder) RuleConditionElement(org.drools.core.rule.RuleConditionElement) SingleAccumulate(org.drools.core.rule.SingleAccumulate) MutableTypeConstraint(org.drools.core.rule.MutableTypeConstraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) Constraint(org.drools.core.spi.Constraint) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) MvelAccumulator(org.drools.core.spi.MvelAccumulator)

Example 22 with BoundIdentifiers

use of org.drools.compiler.compiler.BoundIdentifiers 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 23 with BoundIdentifiers

use of org.drools.compiler.compiler.BoundIdentifiers 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)

Example 24 with BoundIdentifiers

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

the class JavaAccumulateBuilder method buildInlineAccumulate.

private Accumulate buildInlineAccumulate(final RuleBuildContext context, final AccumulateDescr accumDescr, final RuleConditionElement source, Map<String, Declaration> decls, Map<String, Class<?>> declCls, final boolean readLocalsFromTuple) {
    // ELSE, if it is not an external function, build it using the regular java builder
    final String className = "Accumulate" + context.getNextId();
    accumDescr.setClassName(className);
    BoundIdentifiers available = new BoundIdentifiers(declCls, context);
    final JavaAnalysisResult initCodeAnalysis = (JavaAnalysisResult) context.getDialect().analyzeBlock(context, accumDescr, accumDescr.getInitCode(), available);
    final AnalysisResult actionCodeAnalysis = context.getDialect().analyzeBlock(context, accumDescr, accumDescr.getActionCode(), available);
    final AnalysisResult resultCodeAnalysis = context.getDialect().analyzeExpression(context, accumDescr, accumDescr.getResultCode(), available);
    if (initCodeAnalysis == null || actionCodeAnalysis == null || resultCodeAnalysis == null) {
        // not possible to get the analysis results - compilation error has been already logged
        return null;
    }
    final Set<String> requiredDeclarations = new HashSet<String>(initCodeAnalysis.getBoundIdentifiers().getDeclrClasses().keySet());
    requiredDeclarations.addAll(actionCodeAnalysis.getBoundIdentifiers().getDeclrClasses().keySet());
    requiredDeclarations.addAll(resultCodeAnalysis.getBoundIdentifiers().getDeclrClasses().keySet());
    final Map<String, Class<?>> requiredGlobals = new HashMap<String, Class<?>>(initCodeAnalysis.getBoundIdentifiers().getGlobals());
    requiredGlobals.putAll(actionCodeAnalysis.getBoundIdentifiers().getGlobals());
    requiredGlobals.putAll(resultCodeAnalysis.getBoundIdentifiers().getGlobals());
    if (accumDescr.getReverseCode() != null) {
        final AnalysisResult reverseCodeAnalysis = context.getDialect().analyzeBlock(context, accumDescr, accumDescr.getActionCode(), available);
        requiredDeclarations.addAll(reverseCodeAnalysis.getBoundIdentifiers().getDeclrClasses().keySet());
        requiredGlobals.putAll(reverseCodeAnalysis.getBoundIdentifiers().getGlobals());
    }
    final Declaration[] declarations = new Declaration[requiredDeclarations.size()];
    int i = 0;
    for (Iterator<String> it = requiredDeclarations.iterator(); it.hasNext(); i++) {
        declarations[i] = decls.get(it.next());
    }
    final Declaration[] sourceDeclArr = source.getOuterDeclarations().values().toArray(new Declaration[source.getOuterDeclarations().size()]);
    Arrays.sort(sourceDeclArr, RuleTerminalNode.SortDeclarations.instance);
    final Map<String, Object> map = createVariableContext(className, null, context, declarations, null, requiredGlobals);
    map.put("className", accumDescr.getClassName());
    map.put("innerDeclarations", sourceDeclArr);
    map.put("isMultiPattern", readLocalsFromTuple ? Boolean.TRUE : Boolean.FALSE);
    final String initCode = this.fixInitCode(initCodeAnalysis, accumDescr.getInitCode());
    final String actionCode = accumDescr.getActionCode();
    final String resultCode = accumDescr.getResultCode();
    String[] attributesTypes = new String[initCodeAnalysis.getLocalVariablesMap().size()];
    String[] attributes = new String[initCodeAnalysis.getLocalVariablesMap().size()];
    int index = 0;
    for (Map.Entry<String, JavaLocalDeclarationDescr> entry : initCodeAnalysis.getLocalVariablesMap().entrySet()) {
        attributes[index] = entry.getKey();
        attributesTypes[index] = entry.getValue().getType();
        index++;
    }
    map.put("attributes", attributes);
    map.put("attributesTypes", attributesTypes);
    map.put("initCode", initCode);
    map.put("actionCode", actionCode);
    map.put("resultCode", resultCode);
    if (accumDescr.getReverseCode() == null) {
        map.put("reverseCode", "");
        map.put("supportsReverse", "false");
    } else {
        map.put("reverseCode", accumDescr.getReverseCode());
        map.put("supportsReverse", "true");
    }
    map.put("hashCode", actionCode.hashCode());
    SingleAccumulate accumulate = new SingleAccumulate(source, declarations);
    generateTemplates("accumulateInnerClass", "accumulateInvoker", context, className, map, accumulate.new Wirer(), accumDescr);
    return accumulate;
}
Also used : HashMap(java.util.HashMap) SingleAccumulate(org.drools.core.rule.SingleAccumulate) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) MutableTypeConstraint(org.drools.core.rule.MutableTypeConstraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) Constraint(org.drools.core.spi.Constraint) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) JavaLocalDeclarationDescr(org.drools.compiler.rule.builder.dialect.java.parser.JavaLocalDeclarationDescr) Declaration(org.drools.core.rule.Declaration) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 25 with BoundIdentifiers

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

the class MVELEnabledBuilder method build.

public void build(RuleBuildContext context) {
    // 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, Class<?>> otherVars = new HashMap<String, Class<?>>();
        otherVars.put("rule", RuleImpl.class);
        Map<String, Declaration> declrs = context.getDeclarationResolver().getDeclarations(context.getRule());
        AnalysisResult analysis = dialect.analyzeExpression(context, context.getRuleDescr(), context.getRuleDescr().getEnabled(), new BoundIdentifiers(DeclarationScopeResolver.getDeclarationClasses(declrs), context), otherVars);
        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++] = declrs.get(id);
        }
        Arrays.sort(previousDeclarations, SortDeclarations.instance);
        String exprStr = context.getRuleDescr().getEnabled();
        exprStr = exprStr.substring(1, exprStr.length() - 1) + " ";
        MVELCompilationUnit unit = dialect.getMVELCompilationUnit(exprStr, analysis, previousDeclarations, null, otherVars, context, "drools", KnowledgeHelper.class, false, MVELCompilationUnit.Scope.EXPRESSION);
        MVELEnabledExpression expr = new MVELEnabledExpression(unit, dialect.getId());
        context.getRule().setEnabled(KiePolicyHelper.isPolicyEnabled() ? new SafeEnabled(expr) : expr);
        MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
        data.addCompileable(context.getRule(), expr);
        expr.compile(data, context.getRule());
    } catch (final Exception e) {
        DialectUtil.copyErrorLocation(e, context.getRuleDescr());
        context.addError(new DescrBuildError(context.getParentDescr(), context.getRuleDescr(), null, "Unable to build expression for 'enabled' : " + e.getMessage() + " '" + context.getRuleDescr().getEnabled() + "'"));
    }
}
Also used : HashMap(java.util.HashMap) MVELCompilationUnit(org.drools.core.base.mvel.MVELCompilationUnit) SafeEnabled(org.drools.core.definitions.rule.impl.RuleImpl.SafeEnabled) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) MVELEnabledExpression(org.drools.core.base.mvel.MVELEnabledExpression) Declaration(org.drools.core.rule.Declaration)

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