Search in sources :

Example 1 with BindingEvaluator

use of org.drools.modelcompiler.constraints.BindingEvaluator in project drools by kiegroup.

the class KiePackagesBuilder method processFunctions.

private Variable processFunctions(RuleContext ctx, AccumulatePattern accPattern, RuleConditionElement source, Pattern pattern, Set<String> usedVariableName, Collection<Binding> bindings, boolean isGroupBy, AccumulateFunction accFunction, InternalReadAccessor selfReader, Accumulator[] accumulators, List<Declaration> requiredDeclarationList, int arrayIndexOffset, int i) {
    Binding binding = findBindingForAccumulate(bindings, accFunction);
    if (binding != null) {
        for (Variable var : binding.getInputVariables()) {
            usedVariableName.add(var.getName());
        }
    }
    final BindingEvaluator bindingEvaluator = createBindingEvaluator(ctx, binding);
    final Accumulator accumulator = createAccumulator(usedVariableName, bindingEvaluator, accFunction);
    Variable boundVar = accPattern.getBoundVariables()[i];
    Declaration declaration;
    if (!isGroupBy && accumulators.length == 1) {
        declaration = new Declaration(boundVar.getName(), new PatternExtractor(new ClassObjectType(boundVar.getType())), pattern, true);
    } else {
        // GroupBy or multi-accumulate always return an array
        // If GroupBy has no bound function, it uses an anonymous one.
        // The result is still in element 0 so must be offset using arrayIndexOffset
        declaration = new Declaration(boundVar.getName(), new ArrayElementReader(selfReader, i + arrayIndexOffset, boundVar.getType()), pattern, true);
    }
    pattern.addDeclaration(declaration);
    ctx.addDeclaration(boundVar, declaration);
    accumulators[i] = accumulator;
    Declaration[] requiredDeclarations = getRequiredDeclarationsForAccumulate(ctx, source, accFunction, binding, bindingEvaluator);
    requiredDeclarationList.addAll(Arrays.asList(requiredDeclarations));
    return boundVar;
}
Also used : Binding(org.drools.model.Binding) LambdaAccumulator(org.drools.modelcompiler.constraints.LambdaAccumulator) Accumulator(org.drools.core.spi.Accumulator) PrototypeVariable(org.drools.model.PrototypeVariable) Variable(org.drools.model.Variable) ClassObjectType(org.drools.core.base.ClassObjectType) PatternExtractor(org.drools.core.spi.PatternExtractor) ArrayElementReader(org.drools.core.base.extractors.ArrayElementReader) Declaration(org.drools.core.rule.Declaration) WindowDeclaration(org.drools.core.rule.WindowDeclaration) TypeDeclarationUtil.createTypeDeclaration(org.drools.modelcompiler.util.TypeDeclarationUtil.createTypeDeclaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration) BindingEvaluator(org.drools.modelcompiler.constraints.BindingEvaluator)

Example 2 with BindingEvaluator

use of org.drools.modelcompiler.constraints.BindingEvaluator in project drools by kiegroup.

the class KiePackagesBuilder method createBindingEvaluator.

private BindingEvaluator createBindingEvaluator(RuleContext ctx, Binding binding) {
    if (binding == null) {
        return null;
    }
    Variable[] inputs = binding.getInputVariables();
    if (inputs.length == 1) {
        return new BindingInnerObjectEvaluator(binding);
    }
    Declaration[] declarations = new Declaration[inputs.length];
    for (int i = 0; i < inputs.length; i++) {
        declarations[i] = ctx.getDeclaration(inputs[i]);
    }
    return new BindingEvaluator(declarations, binding);
}
Also used : PrototypeVariable(org.drools.model.PrototypeVariable) Variable(org.drools.model.Variable) Declaration(org.drools.core.rule.Declaration) WindowDeclaration(org.drools.core.rule.WindowDeclaration) TypeDeclarationUtil.createTypeDeclaration(org.drools.modelcompiler.util.TypeDeclarationUtil.createTypeDeclaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration) BindingInnerObjectEvaluator(org.drools.modelcompiler.constraints.BindingInnerObjectEvaluator) BindingEvaluator(org.drools.modelcompiler.constraints.BindingEvaluator) SingleConstraint(org.drools.model.SingleConstraint) QueryNameConstraint(org.drools.core.rule.constraint.QueryNameConstraint) LambdaConstraint(org.drools.modelcompiler.constraints.LambdaConstraint) UnificationConstraint(org.drools.modelcompiler.constraints.UnificationConstraint) EntryPoint(org.drools.model.EntryPoint) AbstractConstraint(org.drools.modelcompiler.constraints.AbstractConstraint) Constraint(org.drools.model.Constraint) CombinedConstraint(org.drools.modelcompiler.constraints.CombinedConstraint) AbstractSingleConstraint(org.drools.model.constraints.AbstractSingleConstraint) DSL.entryPoint(org.drools.model.DSL.entryPoint)

Aggregations

Declaration (org.drools.core.rule.Declaration)2 TypeDeclaration (org.drools.core.rule.TypeDeclaration)2 WindowDeclaration (org.drools.core.rule.WindowDeclaration)2 PrototypeVariable (org.drools.model.PrototypeVariable)2 Variable (org.drools.model.Variable)2 BindingEvaluator (org.drools.modelcompiler.constraints.BindingEvaluator)2 TypeDeclarationUtil.createTypeDeclaration (org.drools.modelcompiler.util.TypeDeclarationUtil.createTypeDeclaration)2 ClassObjectType (org.drools.core.base.ClassObjectType)1 ArrayElementReader (org.drools.core.base.extractors.ArrayElementReader)1 QueryNameConstraint (org.drools.core.rule.constraint.QueryNameConstraint)1 Accumulator (org.drools.core.spi.Accumulator)1 PatternExtractor (org.drools.core.spi.PatternExtractor)1 Binding (org.drools.model.Binding)1 Constraint (org.drools.model.Constraint)1 DSL.entryPoint (org.drools.model.DSL.entryPoint)1 EntryPoint (org.drools.model.EntryPoint)1 SingleConstraint (org.drools.model.SingleConstraint)1 AbstractSingleConstraint (org.drools.model.constraints.AbstractSingleConstraint)1 AbstractConstraint (org.drools.modelcompiler.constraints.AbstractConstraint)1 BindingInnerObjectEvaluator (org.drools.modelcompiler.constraints.BindingInnerObjectEvaluator)1