Search in sources :

Example 1 with AccumulatePatternImpl

use of org.drools.model.patterns.AccumulatePatternImpl 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 AccumulatePatternImpl

use of org.drools.model.patterns.AccumulatePatternImpl 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

Condition (org.drools.model.Condition)2 AccumulatePatternImpl (org.drools.model.patterns.AccumulatePatternImpl)2 CompositePatterns (org.drools.model.patterns.CompositePatterns)2 ExistentialPatternImpl (org.drools.model.patterns.ExistentialPatternImpl)2 PatternImpl (org.drools.model.patterns.PatternImpl)2 AccumulateExprViewItem (org.drools.model.view.AccumulateExprViewItem)2 CombinedExprViewItem (org.drools.model.view.CombinedExprViewItem)2 ArrayList (java.util.ArrayList)1 ExchangeDefImpl (org.drools.model.PatternDSL.ExchangeDefImpl)1 PatternBindingImpl (org.drools.model.PatternDSL.PatternBindingImpl)1 PatternDefImpl (org.drools.model.PatternDSL.PatternDefImpl)1 PatternExprImpl (org.drools.model.PatternDSL.PatternExprImpl)1 PatternItem (org.drools.model.PatternDSL.PatternItem)1 SingleConstraint1 (org.drools.model.constraints.SingleConstraint1)1 TemporalConstraint (org.drools.model.constraints.TemporalConstraint)1 AccumulateFunction (org.drools.model.functions.accumulate.AccumulateFunction)1 EvalImpl (org.drools.model.patterns.EvalImpl)1 GroupByPatternImpl (org.drools.model.patterns.GroupByPatternImpl)1 QueryCallPattern (org.drools.model.patterns.QueryCallPattern)1 AbstractExprViewItem (org.drools.model.view.AbstractExprViewItem)1