Search in sources :

Example 11 with AnalysisResult

use of org.drools.compiler.compiler.AnalysisResult 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 12 with AnalysisResult

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

Example 13 with AnalysisResult

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

the class AbstractASMEvalBuilder method build.

public RuleConditionElement build(RuleBuildContext context, BaseDescr descr, Pattern prefixPattern) {
    if (prefixPattern == null) {
        return build(context, descr);
    }
    EvalDescr evalDescr = (EvalDescr) descr;
    PredicateDescr predicateDescr = new PredicateDescr(context.getRuleDescr().getResource(), evalDescr.getContent());
    AnalysisResult analysis = buildAnalysis(context, prefixPattern, predicateDescr, null);
    Declaration[] declarations = getUsedDeclarations(context, prefixPattern, analysis);
    return buildEval(context, evalDescr, analysis, declarations);
}
Also used : EvalDescr(org.drools.compiler.lang.descr.EvalDescr) Declaration(org.drools.core.rule.Declaration) PredicateDescr(org.drools.compiler.lang.descr.PredicateDescr) AnalysisResult(org.drools.compiler.compiler.AnalysisResult)

Example 14 with AnalysisResult

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

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