Search in sources :

Example 1 with MvelAccumulator

use of org.drools.core.spi.MvelAccumulator 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 2 with MvelAccumulator

use of org.drools.core.spi.MvelAccumulator in project drools by kiegroup.

the class MVELAccumulateBuilder method buildCustomAccumulate.

private Accumulator[] buildCustomAccumulate(final RuleBuildContext context, final AccumulateDescr accumDescr, MVELDialect dialect, Map<String, Declaration> decls, Map<String, Declaration> sourceOuterDeclr, BoundIdentifiers boundIds, boolean readLocalsFromTuple) {
    Accumulator[] accumulators;
    final MVELAnalysisResult initCodeAnalysis = (MVELAnalysisResult) dialect.analyzeBlock(context, accumDescr, accumDescr.getInitCode(), boundIds);
    // need to copy boundIds, as this as a "this" object.
    final MVELAnalysisResult actionCodeAnalysis = (MVELAnalysisResult) dialect.analyzeBlock(context, accumDescr.getActionCode(), boundIds, initCodeAnalysis.getMvelVariables(), "drools", KnowledgeHelper.class);
    final MVELAnalysisResult resultCodeAnalysis = (MVELAnalysisResult) dialect.analyzeExpression(context, accumDescr, accumDescr.getResultCode(), boundIds, initCodeAnalysis.getMvelVariables());
    context.setTypesafe(initCodeAnalysis.isTypesafe());
    MVELCompilationUnit initUnit = dialect.getMVELCompilationUnit(accumDescr.getInitCode(), initCodeAnalysis, getUsedDeclarations(decls, initCodeAnalysis), getUsedDeclarations(sourceOuterDeclr, initCodeAnalysis), initCodeAnalysis.getMvelVariables(), context, "drools", KnowledgeHelper.class, readLocalsFromTuple, MVELCompilationUnit.Scope.CONSTRAINT);
    context.setTypesafe(actionCodeAnalysis.isTypesafe());
    MVELCompilationUnit actionUnit = dialect.getMVELCompilationUnit(accumDescr.getActionCode(), actionCodeAnalysis, getUsedDeclarations(decls, actionCodeAnalysis), getUsedDeclarations(sourceOuterDeclr, actionCodeAnalysis), initCodeAnalysis.getMvelVariables(), context, "drools", KnowledgeHelper.class, readLocalsFromTuple, MVELCompilationUnit.Scope.CONSTRAINT);
    MVELCompilationUnit reverseUnit = null;
    if (accumDescr.getReverseCode() != null) {
        context.setTypesafe(actionCodeAnalysis.isTypesafe());
        reverseUnit = dialect.getMVELCompilationUnit(accumDescr.getReverseCode(), actionCodeAnalysis, getUsedDeclarations(decls, actionCodeAnalysis), getUsedDeclarations(sourceOuterDeclr, actionCodeAnalysis), initCodeAnalysis.getMvelVariables(), context, "drools", KnowledgeHelper.class, readLocalsFromTuple, MVELCompilationUnit.Scope.CONSTRAINT);
    }
    context.setTypesafe(resultCodeAnalysis.isTypesafe());
    MVELCompilationUnit resultUnit = dialect.getMVELCompilationUnit(accumDescr.getResultCode(), resultCodeAnalysis, getUsedDeclarations(decls, resultCodeAnalysis), getUsedDeclarations(sourceOuterDeclr, resultCodeAnalysis), initCodeAnalysis.getMvelVariables(), context, "drools", KnowledgeHelper.class, readLocalsFromTuple, MVELCompilationUnit.Scope.CONSTRAINT);
    accumulators = new Accumulator[] { new MVELAccumulator(initUnit, actionUnit, reverseUnit, resultUnit) };
    return accumulators;
}
Also used : MVELAccumulator(org.drools.core.base.mvel.MVELAccumulator) Accumulator(org.drools.core.spi.Accumulator) MvelAccumulator(org.drools.core.spi.MvelAccumulator) MVELAccumulator(org.drools.core.base.mvel.MVELAccumulator) MVELCompilationUnit(org.drools.core.base.mvel.MVELCompilationUnit) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper)

Example 3 with MvelAccumulator

use of org.drools.core.spi.MvelAccumulator in project drools by kiegroup.

the class MVELAccumulateBuilder method buildCustomAccumulate.

private Accumulator[] buildCustomAccumulate(final RuleBuildContext context, final AccumulateDescr accumDescr, MVELDialect dialect, Map<String, Declaration> decls, Map<String, Declaration> sourceOuterDeclr, BoundIdentifiers boundIds, boolean readLocalsFromTuple) {
    Accumulator[] accumulators;
    final MVELAnalysisResult initCodeAnalysis = (MVELAnalysisResult) dialect.analyzeBlock(context, accumDescr, accumDescr.getInitCode(), boundIds);
    // need to copy boundIds, as this as a "this" object.
    final MVELAnalysisResult actionCodeAnalysis = (MVELAnalysisResult) dialect.analyzeBlock(context, accumDescr.getActionCode(), boundIds, initCodeAnalysis.getMvelVariables(), "drools", KnowledgeHelper.class);
    final MVELAnalysisResult resultCodeAnalysis = (MVELAnalysisResult) dialect.analyzeExpression(context, accumDescr, accumDescr.getResultCode(), boundIds, initCodeAnalysis.getMvelVariables());
    context.setTypesafe(initCodeAnalysis.isTypesafe());
    MVELCompilationUnit initUnit = dialect.getMVELCompilationUnit(accumDescr.getInitCode(), initCodeAnalysis, getUsedDeclarations(decls, initCodeAnalysis), getUsedDeclarations(sourceOuterDeclr, initCodeAnalysis), initCodeAnalysis.getMvelVariables(), context, "drools", KnowledgeHelper.class, readLocalsFromTuple, MVELCompilationUnit.Scope.CONSTRAINT);
    context.setTypesafe(actionCodeAnalysis.isTypesafe());
    MVELCompilationUnit actionUnit = dialect.getMVELCompilationUnit(accumDescr.getActionCode(), actionCodeAnalysis, getUsedDeclarations(decls, actionCodeAnalysis), getUsedDeclarations(sourceOuterDeclr, actionCodeAnalysis), initCodeAnalysis.getMvelVariables(), context, "drools", KnowledgeHelper.class, readLocalsFromTuple, MVELCompilationUnit.Scope.CONSTRAINT);
    MVELCompilationUnit reverseUnit = null;
    if (accumDescr.getReverseCode() != null) {
        context.setTypesafe(actionCodeAnalysis.isTypesafe());
        reverseUnit = dialect.getMVELCompilationUnit(accumDescr.getReverseCode(), actionCodeAnalysis, getUsedDeclarations(decls, actionCodeAnalysis), getUsedDeclarations(sourceOuterDeclr, actionCodeAnalysis), initCodeAnalysis.getMvelVariables(), context, "drools", KnowledgeHelper.class, readLocalsFromTuple, MVELCompilationUnit.Scope.CONSTRAINT);
    }
    context.setTypesafe(resultCodeAnalysis.isTypesafe());
    MVELCompilationUnit resultUnit = dialect.getMVELCompilationUnit(accumDescr.getResultCode(), resultCodeAnalysis, getUsedDeclarations(decls, resultCodeAnalysis), getUsedDeclarations(sourceOuterDeclr, resultCodeAnalysis), initCodeAnalysis.getMvelVariables(), context, "drools", KnowledgeHelper.class, readLocalsFromTuple, MVELCompilationUnit.Scope.CONSTRAINT);
    accumulators = new Accumulator[] { new MVELAccumulator(initUnit, actionUnit, reverseUnit, resultUnit) };
    return accumulators;
}
Also used : Accumulator(org.drools.core.spi.Accumulator) MVELAccumulator(org.drools.mvel.expr.MVELAccumulator) MvelAccumulator(org.drools.core.spi.MvelAccumulator) MVELAccumulator(org.drools.mvel.expr.MVELAccumulator) MVELCompilationUnit(org.drools.mvel.expr.MVELCompilationUnit) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper)

Example 4 with MvelAccumulator

use of org.drools.core.spi.MvelAccumulator 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, source, declarationClasses);
        } 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, accumulators.length);
            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) {
        AsmUtil.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 : Accumulator(org.drools.core.spi.Accumulator) MVELAccumulator(org.drools.mvel.expr.MVELAccumulator) MvelAccumulator(org.drools.core.spi.MvelAccumulator) MVELCompileable(org.drools.mvel.expr.MVELCompileable) ArrayList(java.util.ArrayList) AccumulateDescr(org.drools.drl.ast.descr.AccumulateDescr) MultiAccumulate(org.drools.core.rule.MultiAccumulate) SingleAccumulate(org.drools.core.rule.SingleAccumulate) Accumulate(org.drools.core.rule.Accumulate) MVELDialectRuntimeData(org.drools.mvel.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.mvel.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)

Aggregations

Accumulator (org.drools.core.spi.Accumulator)4 MvelAccumulator (org.drools.core.spi.MvelAccumulator)4 ArrayList (java.util.ArrayList)2 BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)2 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)2 RuleConditionBuilder (org.drools.compiler.rule.builder.RuleConditionBuilder)2 MVELAccumulator (org.drools.core.base.mvel.MVELAccumulator)2 Accumulate (org.drools.core.rule.Accumulate)2 Declaration (org.drools.core.rule.Declaration)2 MultiAccumulate (org.drools.core.rule.MultiAccumulate)2 MutableTypeConstraint (org.drools.core.rule.MutableTypeConstraint)2 RuleConditionElement (org.drools.core.rule.RuleConditionElement)2 SingleAccumulate (org.drools.core.rule.SingleAccumulate)2 Constraint (org.drools.core.spi.Constraint)2 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)2 MVELAccumulator (org.drools.mvel.expr.MVELAccumulator)2 AccumulateDescr (org.drools.compiler.lang.descr.AccumulateDescr)1 MVELCompilationUnit (org.drools.core.base.mvel.MVELCompilationUnit)1 MVELCompileable (org.drools.core.base.mvel.MVELCompileable)1 MVELDialectRuntimeData (org.drools.core.rule.MVELDialectRuntimeData)1