Search in sources :

Example 1 with MVELEnabledExpression

use of org.drools.core.base.mvel.MVELEnabledExpression in project drools by kiegroup.

the class RuleTerminalNode method setDeclarations.

public void setDeclarations(Map<String, Declaration> decls) {
    if (rule.getSalience() instanceof MVELSalienceExpression) {
        MVELSalienceExpression expr = (MVELSalienceExpression) rule.getSalience();
        Declaration[] declrs = expr.getMVELCompilationUnit().getPreviousDeclarations();
        this.salienceDeclarations = new Declaration[declrs.length];
        int i = 0;
        for (Declaration declr : declrs) {
            this.salienceDeclarations[i++] = decls.get(declr.getIdentifier());
        }
        Arrays.sort(this.salienceDeclarations, SortDeclarations.instance);
    }
    if (rule.getEnabled() instanceof MVELEnabledExpression) {
        MVELEnabledExpression expr = (MVELEnabledExpression) rule.getEnabled();
        Declaration[] declrs = expr.getMVELCompilationUnit().getPreviousDeclarations();
        this.enabledDeclarations = new Declaration[declrs.length];
        int i = 0;
        for (Declaration declr : declrs) {
            this.enabledDeclarations[i++] = decls.get(declr.getIdentifier());
        }
        Arrays.sort(this.enabledDeclarations, SortDeclarations.instance);
    }
    if (rule.getTimer() instanceof BaseTimer) {
        this.timerDeclarations = ((BaseTimer) rule.getTimer()).getTimerDeclarations(decls);
    }
}
Also used : MVELEnabledExpression(org.drools.core.base.mvel.MVELEnabledExpression) MVELSalienceExpression(org.drools.core.base.mvel.MVELSalienceExpression) BaseTimer(org.drools.core.time.impl.BaseTimer) Declaration(org.drools.core.rule.Declaration)

Example 2 with MVELEnabledExpression

use of org.drools.core.base.mvel.MVELEnabledExpression 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

MVELEnabledExpression (org.drools.core.base.mvel.MVELEnabledExpression)2 Declaration (org.drools.core.rule.Declaration)2 HashMap (java.util.HashMap)1 AnalysisResult (org.drools.compiler.compiler.AnalysisResult)1 BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)1 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)1 MVELCompilationUnit (org.drools.core.base.mvel.MVELCompilationUnit)1 MVELSalienceExpression (org.drools.core.base.mvel.MVELSalienceExpression)1 SafeEnabled (org.drools.core.definitions.rule.impl.RuleImpl.SafeEnabled)1 MVELDialectRuntimeData (org.drools.core.rule.MVELDialectRuntimeData)1 BaseTimer (org.drools.core.time.impl.BaseTimer)1