Search in sources :

Example 1 with AccumulateFunction

use of org.drools.model.functions.accumulate.AccumulateFunction in project drools by kiegroup.

the class ViewFlowBuilder method viewItem2Condition.

private static Condition viewItem2Condition(ViewItem viewItem, Condition condition, BuildContext ctx) {
    if (viewItem instanceof AbstractExprViewItem) {
        ((PatternImpl) condition).addWatchedProps(((AbstractExprViewItem) viewItem).getWatchedProps());
    }
    if (viewItem instanceof Expr1ViewItemImpl) {
        Expr1ViewItemImpl expr = (Expr1ViewItemImpl) viewItem;
        if (expr.getPredicate() != null) {
            ((PatternImpl) condition).addConstraint(new SingleConstraint1(expr));
        }
        return condition;
    }
    if (viewItem instanceof ExprNViewItem) {
        ((PatternImpl) condition).addConstraint(SingleConstraint.createConstraint((ExprNViewItem) viewItem));
        return condition;
    }
    if (viewItem instanceof TemporalExprViewItem) {
        TemporalExprViewItem expr = (TemporalExprViewItem) viewItem;
        ((PatternImpl) condition).addConstraint(new TemporalConstraint(expr));
        return condition;
    }
    if (viewItem instanceof AccumulateExprViewItem) {
        AccumulateExprViewItem acc = (AccumulateExprViewItem) viewItem;
        for (AccumulateFunction accFunc : acc.getAccumulateFunctions()) {
            ctx.usedVars.add(accFunc.getVariable());
        }
        final Condition newCondition = acc.getExpr() instanceof InputViewItem ? condition : viewItem2Condition(acc.getExpr(), condition, ctx);
        return new AccumulatePatternImpl(newCondition, acc.getAccumulateFunctions());
    }
    if (viewItem instanceof CombinedExprViewItem) {
        CombinedExprViewItem combined = (CombinedExprViewItem) viewItem;
        CompositePatterns patterns = viewItems2Condition(new BuildContext(ctx, combined.getExpressions()), combined.getType(), false);
        return patterns.getSubConditions().size() == 1 ? patterns.getSubConditions().get(0) : patterns;
    }
    throw new UnsupportedOperationException("Unknown ViewItem: " + viewItem);
}
Also used : Condition(org.drools.model.Condition) AccumulatePatternImpl(org.drools.model.patterns.AccumulatePatternImpl) SingleConstraint1(org.drools.model.constraints.SingleConstraint1) CombinedExprViewItem(org.drools.model.view.CombinedExprViewItem) InputViewItem(org.drools.model.view.InputViewItem) CompositePatterns(org.drools.model.patterns.CompositePatterns) AbstractExprViewItem(org.drools.model.view.AbstractExprViewItem) PatternImpl(org.drools.model.patterns.PatternImpl) ExistentialPatternImpl(org.drools.model.patterns.ExistentialPatternImpl) AccumulatePatternImpl(org.drools.model.patterns.AccumulatePatternImpl) AccumulateExprViewItem(org.drools.model.view.AccumulateExprViewItem) TemporalConstraint(org.drools.model.constraints.TemporalConstraint) Expr1ViewItemImpl(org.drools.model.view.Expr1ViewItemImpl) AccumulateFunction(org.drools.model.functions.accumulate.AccumulateFunction) TemporalExprViewItem(org.drools.model.view.TemporalExprViewItem) ExprNViewItem(org.drools.model.view.ExprNViewItem)

Example 2 with AccumulateFunction

use of org.drools.model.functions.accumulate.AccumulateFunction in project drools by kiegroup.

the class KiePackagesBuilder method buildAccumulate.

private Accumulate buildAccumulate(RuleContext ctx, AccumulatePattern accPattern, RuleConditionElement source, Pattern pattern, List<String> usedVariableName, Binding binding) {
    AccumulateFunction[] accFunctions = accPattern.getAccumulateFunctions();
    Accumulate accumulate;
    if (accFunctions.length == 1) {
        final Class<?> functionClass = accFunctions[0].getFunctionClass();
        final Accumulator accumulator = createAccumulator(usedVariableName, binding, functionClass);
        final Variable boundVar = accPattern.getBoundVariables()[0];
        final Declaration declaration = new Declaration(boundVar.getName(), getReadAcessor(JAVA_CLASS_OBJECT_TYPE), pattern, true);
        pattern.addDeclaration(declaration);
        Declaration[] bindingDeclaration = binding != null ? new Declaration[0] : new Declaration[] { ctx.getPattern(accFunctions[0].getSource()).getDeclaration() };
        accumulate = new SingleAccumulate(source, bindingDeclaration, accumulator);
    } else {
        InternalReadAccessor reader = new SelfReferenceClassFieldReader(Object[].class);
        Accumulator[] accumulators = new Accumulator[accFunctions.length];
        for (int i = 0; i < accFunctions.length; i++) {
            final Class<?> functionClass = accFunctions[i].getFunctionClass();
            final Accumulator accumulator = createAccumulator(usedVariableName, binding, functionClass);
            Variable boundVar = accPattern.getBoundVariables()[i];
            pattern.addDeclaration(new Declaration(boundVar.getName(), new ArrayElementReader(reader, i, boundVar.getType()), pattern, true));
            accumulators[i] = accumulator;
        }
        accumulate = new MultiAccumulate(source, new Declaration[0], accumulators);
    }
    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) PrototypeVariable(org.drools.model.PrototypeVariable) Variable(org.drools.model.Variable) MultiAccumulate(org.drools.core.rule.MultiAccumulate) SingleAccumulate(org.drools.core.rule.SingleAccumulate) SingleConstraint(org.drools.model.SingleConstraint) QueryNameConstraint(org.drools.core.rule.constraint.QueryNameConstraint) LambdaConstraint(org.drools.modelcompiler.constraints.LambdaConstraint) FlowDSL.entryPoint(org.drools.model.FlowDSL.entryPoint) UnificationConstraint(org.drools.modelcompiler.constraints.UnificationConstraint) EntryPoint(org.drools.model.EntryPoint) Constraint(org.drools.model.Constraint) MultiAccumulate(org.drools.core.rule.MultiAccumulate) SingleAccumulate(org.drools.core.rule.SingleAccumulate) Accumulate(org.drools.core.rule.Accumulate) SelfReferenceClassFieldReader(org.drools.core.base.extractors.SelfReferenceClassFieldReader) 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) AccumulateFunction(org.drools.model.functions.accumulate.AccumulateFunction)

Aggregations

AccumulateFunction (org.drools.model.functions.accumulate.AccumulateFunction)2 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 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 Condition (org.drools.model.Condition)1 Constraint (org.drools.model.Constraint)1 EntryPoint (org.drools.model.EntryPoint)1 FlowDSL.entryPoint (org.drools.model.FlowDSL.entryPoint)1 PrototypeVariable (org.drools.model.PrototypeVariable)1 SingleConstraint (org.drools.model.SingleConstraint)1 Variable (org.drools.model.Variable)1 SingleConstraint1 (org.drools.model.constraints.SingleConstraint1)1