Search in sources :

Example 1 with GroupByPatternImpl

use of org.drools.model.patterns.GroupByPatternImpl in project drools by kiegroup.

the class KiePackagesBuilder method buildAccumulate.

private Accumulate buildAccumulate(RuleContext ctx, AccumulatePattern accPattern, RuleConditionElement source, Pattern pattern, Set<String> usedVariableName, Collection<Binding> bindings) {
    boolean isGroupBy = accPattern instanceof GroupByPattern;
    AccumulateFunction[] accFunctions = accPattern.getAccumulateFunctions();
    Declaration groupByDeclaration = null;
    Class selfType = (isGroupBy || accFunctions.length > 1) ? Object[].class : accFunctions[0].getResult().getType();
    InternalReadAccessor selfReader = new SelfReferenceClassFieldReader(selfType);
    int arrayIndexOffset = 0;
    if (isGroupBy) {
        if (accFunctions.length == 0) {
            // In this situation the result is anonymous, but it still uses element position 0.
            // For this reason the i used to populate hte array index must be offset by 1.
            accFunctions = new AccumulateFunction[] { new AccumulateFunction(null, CountAccumulateFunction::new) };
            arrayIndexOffset = 1;
        }
        // GroupBy  key is always the last element in the result array
        Variable groupVar = ((GroupByPattern<?, ?>) accPattern).getVarKey();
        groupByDeclaration = new Declaration(groupVar.getName(), new ArrayElementReader(selfReader, accFunctions.length, groupVar.getType()), pattern, true);
        pattern.addDeclaration(groupByDeclaration);
    }
    Accumulate accumulate;
    Accumulator[] accumulators = new Accumulator[accFunctions.length];
    List<Declaration> requiredDeclarationList = new ArrayList<>();
    for (int i = 0; i < accFunctions.length; i++) {
        Variable boundVar = processFunctions(ctx, accPattern, source, pattern, usedVariableName, bindings, isGroupBy, accFunctions[i], selfReader, accumulators, requiredDeclarationList, arrayIndexOffset, i);
        if (isGroupBy) {
            ctx.addGroupByDeclaration(((GroupByPattern) accPattern).getVarKey(), groupByDeclaration);
        }
    }
    if (accFunctions.length == 1) {
        accumulate = new SingleAccumulate(source, requiredDeclarationList.toArray(new Declaration[requiredDeclarationList.size()]), accumulators[0]);
    } else {
        if (source instanceof Pattern) {
            requiredDeclarationList.forEach(((Pattern) source)::addDeclaration);
        }
        accumulate = new MultiAccumulate(source, new Declaration[0], accumulators, // if this is a groupby +1 for the key
        accumulators.length + ((isGroupBy) ? 1 : 0));
    }
    if (isGroupBy) {
        GroupByPatternImpl groupBy = (GroupByPatternImpl) accPattern;
        Declaration[] groupingDeclarations = new Declaration[groupBy.getVars().length];
        for (int i = 0; i < groupBy.getVars().length; i++) {
            groupingDeclarations[i] = ctx.getDeclaration(groupBy.getVars()[i]);
        }
        accumulate = new LambdaGroupByAccumulate(accumulate, groupingDeclarations, groupBy.getGroupingFunction());
    }
    for (Variable boundVar : accPattern.getBoundVariables()) {
        ctx.addAccumulateSource(boundVar, accumulate);
    }
    return accumulate;
}
Also used : LambdaAccumulator(org.drools.modelcompiler.constraints.LambdaAccumulator) Accumulator(org.drools.core.spi.Accumulator) QueryCallPattern(org.drools.model.patterns.QueryCallPattern) AccumulatePattern(org.drools.model.AccumulatePattern) Pattern(org.drools.core.rule.Pattern) GroupByPattern(org.drools.model.GroupByPattern) PrototypeVariable(org.drools.model.PrototypeVariable) Variable(org.drools.model.Variable) MultiAccumulate(org.drools.core.rule.MultiAccumulate) ArrayList(java.util.ArrayList) SingleAccumulate(org.drools.core.rule.SingleAccumulate) 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) LambdaGroupByAccumulate(org.drools.modelcompiler.constraints.LambdaGroupByAccumulate) MultiAccumulate(org.drools.core.rule.MultiAccumulate) SingleAccumulate(org.drools.core.rule.SingleAccumulate) Accumulate(org.drools.core.rule.Accumulate) GroupByPattern(org.drools.model.GroupByPattern) LambdaGroupByAccumulate(org.drools.modelcompiler.constraints.LambdaGroupByAccumulate) SelfReferenceClassFieldReader(org.drools.core.base.extractors.SelfReferenceClassFieldReader) GroupByPatternImpl(org.drools.model.patterns.GroupByPatternImpl) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) 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) CountAccumulateFunction(org.drools.core.base.accumulators.CountAccumulateFunction) AccumulateFunction(org.drools.model.functions.accumulate.AccumulateFunction)

Example 2 with GroupByPatternImpl

use of org.drools.model.patterns.GroupByPatternImpl in project drools by kiegroup.

the class ViewPatternBuilder method ruleItem2Condition.

public static Condition ruleItem2Condition(RuleItem ruleItem) {
    if (ruleItem instanceof PatternDefImpl) {
        PatternDefImpl<?> patternDef = (PatternDefImpl) ruleItem;
        Variable<?> patternVariable = patternDef.getFirstVariable();
        PatternImpl pattern = new PatternImpl(patternVariable, patternVariable instanceof Exchange ? Condition.Type.RECEIVER : Condition.Type.PATTERN);
        for (PatternItem patternItem : patternDef.getItems()) {
            if (patternItem instanceof PatternExprImpl) {
                pattern.addConstraint(((PatternExprImpl) patternItem).asConstraint(patternDef));
            } else if (patternItem instanceof PatternBindingImpl) {
                pattern.addBinding(((PatternBindingImpl) patternItem).asBinding(patternDef));
            } else {
                throw new UnsupportedOperationException("Unknown pattern item type: " + patternItem);
            }
        }
        pattern.addWatchedProps(patternDef.getWatch());
        pattern.setPassive(patternDef.isPassive());
        return pattern;
    }
    if (ruleItem instanceof FixedValueItem) {
        return new EvalImpl(((FixedValueItem) ruleItem).isValue());
    }
    if (ruleItem instanceof QueryCallViewItem) {
        return new QueryCallPattern((QueryCallViewItem) ruleItem);
    }
    if (ruleItem instanceof CombinedExprViewItem) {
        CombinedExprViewItem combined = (CombinedExprViewItem) ruleItem;
        List<Condition> conditions = new ArrayList<>();
        for (ViewItem expr : combined.getExpressions()) {
            conditions.add(ruleItem2Condition(expr));
        }
        return new CompositePatterns(combined.getType(), conditions);
    }
    if (ruleItem instanceof ExistentialExprViewItem) {
        ExistentialExprViewItem existential = (ExistentialExprViewItem) ruleItem;
        return new ExistentialPatternImpl(ruleItem2Condition(existential.getExpression()), existential.getType());
    }
    if (ruleItem instanceof GroupByExprViewItem) {
        GroupByExprViewItem groupBy = (GroupByExprViewItem) ruleItem;
        return new GroupByPatternImpl(ruleItem2Condition(groupBy.getExpr()), groupBy.getVars(), groupBy.getVarKey(), groupBy.getGroupingFunction(), groupBy.getAccumulateFunctions());
    }
    if (ruleItem instanceof AccumulateExprViewItem) {
        AccumulateExprViewItem acc = (AccumulateExprViewItem) ruleItem;
        return new AccumulatePatternImpl(ruleItem2Condition(acc.getExpr()), null, acc.getAccumulateFunctions());
    }
    if (ruleItem instanceof ExprViewItem) {
        return new EvalImpl(createConstraint((ExprViewItem) ruleItem));
    }
    if (ruleItem instanceof ExchangeDefImpl) {
        ExchangeDefImpl<?> exchangeDef = (ExchangeDefImpl) ruleItem;
        return new PatternImpl(exchangeDef.getFirstVariable(), Condition.Type.SENDER);
    }
    throw new UnsupportedOperationException("Unknown " + ruleItem);
}
Also used : AccumulatePatternImpl(org.drools.model.patterns.AccumulatePatternImpl) GroupByExprViewItem(org.drools.model.view.GroupByExprViewItem) AccumulateExprViewItem(org.drools.model.view.AccumulateExprViewItem) ExistentialExprViewItem(org.drools.model.view.ExistentialExprViewItem) CombinedExprViewItem(org.drools.model.view.CombinedExprViewItem) ExprViewItem(org.drools.model.view.ExprViewItem) CombinedExprViewItem(org.drools.model.view.CombinedExprViewItem) ExistentialPatternImpl(org.drools.model.patterns.ExistentialPatternImpl) ExchangeDefImpl(org.drools.model.PatternDSL.ExchangeDefImpl) PatternItem(org.drools.model.PatternDSL.PatternItem) ArrayList(java.util.ArrayList) CompositePatterns(org.drools.model.patterns.CompositePatterns) GroupByExprViewItem(org.drools.model.view.GroupByExprViewItem) GroupByPatternImpl(org.drools.model.patterns.GroupByPatternImpl) PatternImpl(org.drools.model.patterns.PatternImpl) ExistentialPatternImpl(org.drools.model.patterns.ExistentialPatternImpl) AccumulatePatternImpl(org.drools.model.patterns.AccumulatePatternImpl) EvalImpl(org.drools.model.patterns.EvalImpl) QueryCallViewItem(org.drools.model.view.QueryCallViewItem) QueryCallPattern(org.drools.model.patterns.QueryCallPattern) GroupByPatternImpl(org.drools.model.patterns.GroupByPatternImpl) ExistentialExprViewItem(org.drools.model.view.ExistentialExprViewItem) Condition(org.drools.model.Condition) GroupByExprViewItem(org.drools.model.view.GroupByExprViewItem) AccumulateExprViewItem(org.drools.model.view.AccumulateExprViewItem) ExistentialExprViewItem(org.drools.model.view.ExistentialExprViewItem) QueryCallViewItem(org.drools.model.view.QueryCallViewItem) ViewItem(org.drools.model.view.ViewItem) CombinedExprViewItem(org.drools.model.view.CombinedExprViewItem) ExprViewItem(org.drools.model.view.ExprViewItem) FixedValueItem(org.drools.model.view.FixedValueItem) PatternExprImpl(org.drools.model.PatternDSL.PatternExprImpl) AccumulateExprViewItem(org.drools.model.view.AccumulateExprViewItem) PatternBindingImpl(org.drools.model.PatternDSL.PatternBindingImpl) PatternDefImpl(org.drools.model.PatternDSL.PatternDefImpl)

Aggregations

ArrayList (java.util.ArrayList)2 GroupByPatternImpl (org.drools.model.patterns.GroupByPatternImpl)2 QueryCallPattern (org.drools.model.patterns.QueryCallPattern)2 CountAccumulateFunction (org.drools.core.base.accumulators.CountAccumulateFunction)1 ArrayElementReader (org.drools.core.base.extractors.ArrayElementReader)1 SelfReferenceClassFieldReader (org.drools.core.base.extractors.SelfReferenceClassFieldReader)1 Accumulate (org.drools.core.rule.Accumulate)1 Declaration (org.drools.core.rule.Declaration)1 MultiAccumulate (org.drools.core.rule.MultiAccumulate)1 Pattern (org.drools.core.rule.Pattern)1 SingleAccumulate (org.drools.core.rule.SingleAccumulate)1 TypeDeclaration (org.drools.core.rule.TypeDeclaration)1 WindowDeclaration (org.drools.core.rule.WindowDeclaration)1 QueryNameConstraint (org.drools.core.rule.constraint.QueryNameConstraint)1 Accumulator (org.drools.core.spi.Accumulator)1 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)1 AccumulatePattern (org.drools.model.AccumulatePattern)1 Condition (org.drools.model.Condition)1 Constraint (org.drools.model.Constraint)1 DSL.entryPoint (org.drools.model.DSL.entryPoint)1