Search in sources :

Example 1 with MutableTypeConstraint

use of org.drools.core.rule.MutableTypeConstraint in project drools by kiegroup.

the class MVELAccumulateBuilder method buildExternalFunctions.

private Accumulator[] buildExternalFunctions(final RuleBuildContext context, final AccumulateDescr accumDescr, MVELDialect dialect, Map<String, Declaration> decls, Map<String, Declaration> sourceOuterDeclr, BoundIdentifiers boundIds, boolean readLocalsFromTuple) {
    Accumulator[] accumulators;
    List<AccumulateFunctionCallDescr> functions = accumDescr.getFunctions();
    accumulators = new Accumulator[functions.size()];
    // creating the custom array reader
    InternalReadAccessor arrayReader = new SelfReferenceClassFieldReader(Object[].class);
    int index = 0;
    Pattern pattern = (Pattern) context.getDeclarationResolver().peekBuildStack();
    for (AccumulateFunctionCallDescr func : functions) {
        // build an external function executor
        AccumulateFunction function = context.getConfiguration().getAccumulateFunction(func.getFunction());
        if (function == null) {
            // might have been imported in the package
            function = context.getPkg().getAccumulateFunctions().get(func.getFunction());
        }
        if (function == null) {
            context.addError(new DescrBuildError(accumDescr, context.getRuleDescr(), null, "Unknown accumulate function: '" + func.getFunction() + "' on rule '" + context.getRuleDescr().getName() + "'. All accumulate functions must be registered before building a resource."));
            return null;
        }
        final AnalysisResult analysis = dialect.analyzeExpression(context, accumDescr, func.getParams().length > 0 ? func.getParams()[0] : "\"\"", boundIds);
        MVELCompilationUnit unit = dialect.getMVELCompilationUnit(func.getParams().length > 0 ? func.getParams()[0] : "\"\"", analysis, getUsedDeclarations(decls, analysis), getUsedDeclarations(sourceOuterDeclr, analysis), null, context, "drools", KnowledgeHelper.class, readLocalsFromTuple, MVELCompilationUnit.Scope.CONSTRAINT);
        accumulators[index] = new MVELAccumulatorFunctionExecutor(unit, function);
        // if there is a binding, create the binding
        if (func.getBind() != null) {
            if (context.getDeclarationResolver().isDuplicated(context.getRule(), func.getBind(), function.getResultType().getName())) {
                if (!func.isUnification()) {
                    context.addError(new DescrBuildError(context.getParentDescr(), accumDescr, null, "Duplicate declaration for variable '" + func.getBind() + "' in the rule '" + context.getRule().getName() + "'"));
                } else {
                    Declaration inner = context.getDeclarationResolver().getDeclaration(func.getBind());
                    Constraint c = new MvelConstraint(Collections.singletonList(context.getPkg().getName()), accumDescr.isMultiFunction() ? "this[ " + index + " ] == " + func.getBind() : "this == " + func.getBind(), new Declaration[] { inner }, null, null, IndexUtil.ConstraintType.EQUAL, context.getDeclarationResolver().getDeclaration(func.getBind()), accumDescr.isMultiFunction() ? new ArrayElementReader(arrayReader, index, function.getResultType()) : new SelfReferenceClassFieldReader(function.getResultType()), true);
                    ((MutableTypeConstraint) c).setType(Constraint.ConstraintType.BETA);
                    pattern.addConstraint(c);
                    index++;
                }
            } else {
                Declaration declr = pattern.addDeclaration(func.getBind());
                if (accumDescr.isMultiFunction()) {
                    declr.setReadAccessor(new ArrayElementReader(arrayReader, index, function.getResultType()));
                } else {
                    declr.setReadAccessor(new SelfReferenceClassFieldReader(function.getResultType()));
                }
            }
        }
        index++;
    }
    return accumulators;
}
Also used : MVELAccumulator(org.drools.core.base.mvel.MVELAccumulator) Accumulator(org.drools.core.spi.Accumulator) MvelAccumulator(org.drools.core.spi.MvelAccumulator) Pattern(org.drools.core.rule.Pattern) MVELAccumulatorFunctionExecutor(org.drools.core.base.accumulators.MVELAccumulatorFunctionExecutor) MutableTypeConstraint(org.drools.core.rule.MutableTypeConstraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) Constraint(org.drools.core.spi.Constraint) MVELCompilationUnit(org.drools.core.base.mvel.MVELCompilationUnit) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) MutableTypeConstraint(org.drools.core.rule.MutableTypeConstraint) MutableTypeConstraint(org.drools.core.rule.MutableTypeConstraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) Constraint(org.drools.core.spi.Constraint) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) SelfReferenceClassFieldReader(org.drools.core.base.extractors.SelfReferenceClassFieldReader) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) AccumulateFunctionCallDescr(org.drools.compiler.lang.descr.AccumulateDescr.AccumulateFunctionCallDescr) ArrayElementReader(org.drools.core.base.extractors.ArrayElementReader) Declaration(org.drools.core.rule.Declaration) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction)

Example 2 with MutableTypeConstraint

use of org.drools.core.rule.MutableTypeConstraint in project drools by kiegroup.

the class TripleBetaConstraintsMetric method cloneIfInUse.

@Override
public TripleBetaConstraintsMetric cloneIfInUse() {
    if (constraints[0] instanceof MutableTypeConstraint && ((MutableTypeConstraint) constraints[0]).setInUse()) {
        BetaNodeFieldConstraint[] clonedConstraints = new BetaNodeFieldConstraint[constraints.length];
        for (int i = 0; i < constraints.length; i++) {
            clonedConstraints[i] = constraints[i].cloneIfInUse();
        }
        TripleBetaConstraintsMetric clone = new TripleBetaConstraintsMetric(clonedConstraints, indexPrecedenceOption, disableIndexing);
        clone.indexed = indexed;
        return clone;
    }
    return this;
}
Also used : MutableTypeConstraint(org.drools.core.rule.MutableTypeConstraint) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) MutableTypeConstraint(org.drools.core.rule.MutableTypeConstraint)

Example 3 with MutableTypeConstraint

use of org.drools.core.rule.MutableTypeConstraint in project drools by kiegroup.

the class JavaAccumulateBuilder method bindReaderToDeclaration.

private void bindReaderToDeclaration(RuleBuildContext context, AccumulateDescr accumDescr, Pattern pattern, AccumulateFunctionCallDescr fc, InternalReadAccessor readAccessor, Class<?> resultType, int index) {
    if (fc.getBind() != null) {
        if (context.getDeclarationResolver().isDuplicated(context.getRule(), fc.getBind(), resultType.getName())) {
            if (!fc.isUnification()) {
                context.addError(new DescrBuildError(context.getParentDescr(), accumDescr, null, "Duplicate declaration for variable '" + fc.getBind() + "' in the rule '" + context.getRule().getName() + "'"));
            } else {
                Declaration inner = context.getDeclarationResolver().getDeclaration(fc.getBind());
                Constraint c = new MVELConstraint(Collections.singletonList(context.getPkg().getName()), index >= 0 ? "this[ " + index + " ] == " + fc.getBind() : "this == " + fc.getBind(), new Declaration[] { inner }, null, null, IndexUtil.ConstraintType.EQUAL, context.getDeclarationResolver().getDeclaration(fc.getBind()), index >= 0 ? new ArrayElementReader(readAccessor, index, resultType) : readAccessor, true);
                ((MutableTypeConstraint) c).setType(Constraint.ConstraintType.BETA);
                pattern.addConstraint(c);
            }
        } else {
            Declaration declr = pattern.addDeclaration(fc.getBind());
            declr.setReadAccessor(readAccessor);
        }
    }
}
Also used : DescrBuildError(org.drools.compiler.compiler.DescrBuildError) MVELConstraint(org.drools.mvel.MVELConstraint) MutableTypeConstraint(org.drools.core.rule.MutableTypeConstraint) MVELConstraint(org.drools.mvel.MVELConstraint) Constraint(org.drools.core.spi.Constraint) MutableTypeConstraint(org.drools.core.rule.MutableTypeConstraint) ArrayElementReader(org.drools.core.base.extractors.ArrayElementReader) Declaration(org.drools.core.rule.Declaration)

Example 4 with MutableTypeConstraint

use of org.drools.core.rule.MutableTypeConstraint in project drools by kiegroup.

the class DefaultBetaConstraints method cloneIfInUse.

public DefaultBetaConstraints cloneIfInUse() {
    if (constraints[0] instanceof MutableTypeConstraint && ((MutableTypeConstraint) constraints[0]).setInUse()) {
        BetaNodeFieldConstraint[] clonedConstraints = new BetaNodeFieldConstraint[constraints.length];
        for (int i = 0; i < constraints.length; i++) {
            clonedConstraints[i] = constraints[i].cloneIfInUse();
        }
        DefaultBetaConstraints clone = new DefaultBetaConstraints();
        clone.constraints = clonedConstraints;
        clone.disableIndexing = disableIndexing;
        clone.indexPrecedenceOption = indexPrecedenceOption;
        clone.indexed = indexed;
        return clone;
    }
    return this;
}
Also used : MutableTypeConstraint(org.drools.core.rule.MutableTypeConstraint) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) MutableTypeConstraint(org.drools.core.rule.MutableTypeConstraint) IndexableConstraint(org.drools.core.rule.IndexableConstraint)

Example 5 with MutableTypeConstraint

use of org.drools.core.rule.MutableTypeConstraint in project drools by kiegroup.

the class JavaAccumulateBuilder method bindReaderToDeclaration.

private void bindReaderToDeclaration(RuleBuildContext context, AccumulateDescr accumDescr, Pattern pattern, AccumulateFunctionCallDescr fc, InternalReadAccessor readAccessor, Class<?> resultType, int index) {
    if (fc.getBind() != null) {
        if (context.getDeclarationResolver().isDuplicated(context.getRule(), fc.getBind(), resultType.getName())) {
            if (!fc.isUnification()) {
                context.addError(new DescrBuildError(context.getParentDescr(), accumDescr, null, "Duplicate declaration for variable '" + fc.getBind() + "' in the rule '" + context.getRule().getName() + "'"));
            } else {
                Declaration inner = context.getDeclarationResolver().getDeclaration(fc.getBind());
                Constraint c = new MvelConstraint(Collections.singletonList(context.getPkg().getName()), index >= 0 ? "this[ " + index + " ] == " + fc.getBind() : "this == " + fc.getBind(), new Declaration[] { inner }, null, null, IndexUtil.ConstraintType.EQUAL, context.getDeclarationResolver().getDeclaration(fc.getBind()), index >= 0 ? new ArrayElementReader(readAccessor, index, resultType) : readAccessor, true);
                ((MutableTypeConstraint) c).setType(Constraint.ConstraintType.BETA);
                pattern.addConstraint(c);
            }
        } else {
            Declaration declr = pattern.addDeclaration(fc.getBind());
            declr.setReadAccessor(readAccessor);
        }
    }
}
Also used : DescrBuildError(org.drools.compiler.compiler.DescrBuildError) MutableTypeConstraint(org.drools.core.rule.MutableTypeConstraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) Constraint(org.drools.core.spi.Constraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) MutableTypeConstraint(org.drools.core.rule.MutableTypeConstraint) ArrayElementReader(org.drools.core.base.extractors.ArrayElementReader) Declaration(org.drools.core.rule.Declaration)

Aggregations

MutableTypeConstraint (org.drools.core.rule.MutableTypeConstraint)12 BetaNodeFieldConstraint (org.drools.core.spi.BetaNodeFieldConstraint)8 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)4 ArrayElementReader (org.drools.core.base.extractors.ArrayElementReader)4 Declaration (org.drools.core.rule.Declaration)4 Constraint (org.drools.core.spi.Constraint)4 AnalysisResult (org.drools.compiler.compiler.AnalysisResult)2 SelfReferenceClassFieldReader (org.drools.core.base.extractors.SelfReferenceClassFieldReader)2 Pattern (org.drools.core.rule.Pattern)2 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)2 Accumulator (org.drools.core.spi.Accumulator)2 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)2 MvelAccumulator (org.drools.core.spi.MvelAccumulator)2 MVELConstraint (org.drools.mvel.MVELConstraint)2 AccumulateFunction (org.kie.api.runtime.rule.AccumulateFunction)2 AccumulateFunctionCallDescr (org.drools.compiler.lang.descr.AccumulateDescr.AccumulateFunctionCallDescr)1 MVELAccumulatorFunctionExecutor (org.drools.core.base.accumulators.MVELAccumulatorFunctionExecutor)1 MVELAccumulator (org.drools.core.base.mvel.MVELAccumulator)1 MVELCompilationUnit (org.drools.core.base.mvel.MVELCompilationUnit)1 IndexableConstraint (org.drools.core.rule.IndexableConstraint)1