Search in sources :

Example 1 with AccumulateExprViewItem

use of org.drools.model.view.AccumulateExprViewItem 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 AccumulateExprViewItem

use of org.drools.model.view.AccumulateExprViewItem in project drools by kiegroup.

the class ViewFlowBuilder method viewItems2Condition.

private static CompositePatterns viewItems2Condition(BuildContext ctx, Condition.Type type, boolean topLevel) {
    List<Condition> conditions = new ArrayList<>();
    Map<Variable<?>, Condition> conditionMap = new HashMap<>();
    Map<String, Consequence> consequences = topLevel ? new LinkedHashMap<>() : null;
    Iterator<RuleItem> ruleItemIterator = ctx.ruleItems.iterator();
    while (ruleItemIterator.hasNext()) {
        Map<Variable<?>, InputViewItemImpl<?>> scopedInputs = type.createsScope() ? new LinkedHashMap<>(ctx.inputs) : ctx.inputs;
        RuleItem ruleItem = ruleItemIterator.next();
        if (ruleItem instanceof FixedValueItem) {
            conditions.add(new EvalImpl(((FixedValueItem) ruleItem).isValue()));
            continue;
        }
        if (ruleItem instanceof Consequence) {
            if (!topLevel) {
                throw new IllegalStateException("A consequence can be only a top level item");
            }
            Consequence consequence = (Consequence) ruleItem;
            String name = ruleItemIterator.hasNext() ? generateName("consequence") : RuleImpl.DEFAULT_CONSEQUENCE_NAME;
            consequences.put(name, consequence);
            conditions.add(new NamedConsequenceImpl(name, consequence.isBreaking()));
            continue;
        }
        if (ruleItem instanceof ConditionalConsequence) {
            if (!topLevel) {
                throw new IllegalStateException("A consequence can be only a top level item");
            }
            conditions.add(createConditionalNamedConsequence(consequences, (ConditionalConsequence) ruleItem));
            continue;
        }
        ViewItem viewItem = (ViewItem) ruleItem;
        if (viewItem instanceof CombinedExprViewItem) {
            CombinedExprViewItem combined = (CombinedExprViewItem) viewItem;
            conditions.add(viewItems2Condition(new BuildContext(ctx, combined.getExpressions(), scopedInputs), combined.getType(), false));
            continue;
        }
        if (viewItem instanceof QueryCallViewItem) {
            QueryCallViewItem query = ((QueryCallViewItem) viewItem);
            for (Argument arg : query.getArguments()) {
                if (arg instanceof Variable) {
                    ctx.usedVars.add(((Variable) arg));
                }
            }
            conditions.add(new QueryCallPattern(query));
            continue;
        }
        if (viewItem instanceof Binding) {
            Binding bindViewItem = (Binding) viewItem;
            PatternImpl pattern = (PatternImpl) conditionMap.get(bindViewItem.getInputVariable());
            if (pattern == null) {
                // This should probably be the bindViewItem.getBoundVariable() instead of the input
                // as the input variables can be many
                pattern = new PatternImpl(bindViewItem.getInputVariable());
                pattern.addWatchedProps(bindViewItem.getWatchedProps());
                ctx.usedVars.add(bindViewItem.getInputVariable());
                conditions.add(pattern);
                conditionMap.put(bindViewItem.getInputVariable(), pattern);
            }
            pattern.addBinding(bindViewItem);
            ctx.usedVars.add(viewItem.getFirstVariable());
            ctx.addBinding(bindViewItem);
            scopedInputs.putIfAbsent(viewItem.getFirstVariable(), (InputViewItemImpl) input(viewItem.getFirstVariable()));
            continue;
        }
        Variable<?> patterVariable = findPatterVariable(viewItem, scopedInputs.keySet());
        if (viewItem instanceof InputViewItemImpl) {
            scopedInputs.put(patterVariable, (InputViewItemImpl) viewItem);
            PatternImpl condition = new PatternImpl(patterVariable);
            condition.addWatchedProps(((InputViewItemImpl) viewItem).getWatchedProps());
            conditions.add(condition);
            conditionMap.put(patterVariable, condition);
            ctx.usedVars.add(patterVariable);
            continue;
        }
        if (viewItem instanceof ExistentialExprViewItem) {
            ExistentialExprViewItem existential = ((ExistentialExprViewItem) viewItem);
            if (patterVariable != null && !existential.isQueryExpression()) {
                registerInputsFromViewItem(existential.getExpression(), conditionMap, scopedInputs, patterVariable);
            }
            Condition condition = new PatternImpl(patterVariable, SingleConstraint.TRUE, ctx.bindings.get(patterVariable));
            conditions.add(new ExistentialPatternImpl(viewItem2Condition(existential.getExpression(), condition, new BuildContext(ctx, new LinkedHashMap<>())), existential.getType()));
            continue;
        }
        if (ruleItem instanceof ExprViewItem && ctx.boundVars.contains(patterVariable)) {
            conditions.add(new EvalImpl(createConstraint((ExprViewItem) ruleItem)));
            continue;
        }
        ctx.usedVars.add(patterVariable);
        Condition condition;
        if (type == Type.AND) {
            condition = conditionMap.get(patterVariable);
            if (condition == null) {
                condition = new PatternImpl(patterVariable, SingleConstraint.TRUE, ctx.bindings.get(patterVariable));
                conditions.add(condition);
                if (!(viewItem instanceof AccumulateExprViewItem)) {
                    conditionMap.put(patterVariable, condition);
                }
                scopedInputs.putIfAbsent(patterVariable, (InputViewItemImpl) input(patterVariable));
            }
        } else {
            condition = new PatternImpl(patterVariable);
            conditions.add(condition);
        }
        addInputFromVariableSource(scopedInputs, patterVariable);
        registerInputsFromViewItem(viewItem, conditionMap, scopedInputs, null);
        Condition modifiedPattern = viewItem2Condition(viewItem, condition, new BuildContext(ctx, scopedInputs));
        conditions.set(conditions.indexOf(condition), modifiedPattern);
        if (type == Type.AND && !(viewItem instanceof AccumulateExprViewItem)) {
            conditionMap.put(patterVariable, modifiedPattern);
        }
    }
    return new CompositePatterns(type, conditions, ctx.usedVars, consequences);
}
Also used : AccumulateExprViewItem(org.drools.model.view.AccumulateExprViewItem) ExistentialExprViewItem(org.drools.model.view.ExistentialExprViewItem) CombinedExprViewItem(org.drools.model.view.CombinedExprViewItem) TemporalExprViewItem(org.drools.model.view.TemporalExprViewItem) AbstractExprViewItem(org.drools.model.view.AbstractExprViewItem) ExprViewItem(org.drools.model.view.ExprViewItem) Variable(org.drools.model.Variable) InputViewItemImpl(org.drools.model.view.InputViewItemImpl) Argument(org.drools.model.Argument) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) CombinedExprViewItem(org.drools.model.view.CombinedExprViewItem) ExistentialPatternImpl(org.drools.model.patterns.ExistentialPatternImpl) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) CompositePatterns(org.drools.model.patterns.CompositePatterns) NamedConsequenceImpl(org.drools.model.consequences.NamedConsequenceImpl) ConditionalNamedConsequenceImpl(org.drools.model.consequences.ConditionalNamedConsequenceImpl) 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) RuleItem(org.drools.model.RuleItem) ExistentialExprViewItem(org.drools.model.view.ExistentialExprViewItem) Condition(org.drools.model.Condition) Binding(org.drools.model.Binding) ConditionalConsequence(org.drools.model.ConditionalConsequence) ExprNViewItem(org.drools.model.view.ExprNViewItem) AccumulateExprViewItem(org.drools.model.view.AccumulateExprViewItem) ExistentialExprViewItem(org.drools.model.view.ExistentialExprViewItem) InputViewItem(org.drools.model.view.InputViewItem) QueryCallViewItem(org.drools.model.view.QueryCallViewItem) ViewItem(org.drools.model.view.ViewItem) CombinedExprViewItem(org.drools.model.view.CombinedExprViewItem) TemporalExprViewItem(org.drools.model.view.TemporalExprViewItem) AbstractExprViewItem(org.drools.model.view.AbstractExprViewItem) ExprViewItem(org.drools.model.view.ExprViewItem) Consequence(org.drools.model.Consequence) ConditionalConsequence(org.drools.model.ConditionalConsequence) FixedValueItem(org.drools.model.view.FixedValueItem) AccumulateExprViewItem(org.drools.model.view.AccumulateExprViewItem)

Example 3 with AccumulateExprViewItem

use of org.drools.model.view.AccumulateExprViewItem in project drools by kiegroup.

the class ViewPatternBuilder method ruleItem2Condition.

private static Condition ruleItem2Condition(RuleItem ruleItem) {
    if (ruleItem instanceof PatternDefImpl) {
        PatternDefImpl<?> patternDef = (PatternDefImpl) ruleItem;
        PatternImpl pattern = new PatternImpl(patternDef.getFirstVariable());
        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));
            }
        }
        pattern.addWatchedProps(patternDef.getWatch());
        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 AccumulateExprViewItem) {
        AccumulateExprViewItem acc = (AccumulateExprViewItem) ruleItem;
        return new AccumulatePatternImpl(ruleItem2Condition(acc.getExpr()), acc.getAccumulateFunctions());
    }
    if (ruleItem instanceof ExprViewItem) {
        return new EvalImpl(createConstraint((ExprViewItem) ruleItem));
    }
    throw new UnsupportedOperationException("Unknown " + ruleItem);
}
Also used : Condition(org.drools.model.Condition) AccumulatePatternImpl(org.drools.model.patterns.AccumulatePatternImpl) ExistentialExprViewItem(org.drools.model.view.ExistentialExprViewItem) AccumulateExprViewItem(org.drools.model.view.AccumulateExprViewItem) CombinedExprViewItem(org.drools.model.view.CombinedExprViewItem) ExprViewItem(org.drools.model.view.ExprViewItem) ExistentialExprViewItem(org.drools.model.view.ExistentialExprViewItem) QueryCallViewItem(org.drools.model.view.QueryCallViewItem) ViewItem(org.drools.model.view.ViewItem) AccumulateExprViewItem(org.drools.model.view.AccumulateExprViewItem) CombinedExprViewItem(org.drools.model.view.CombinedExprViewItem) ExprViewItem(org.drools.model.view.ExprViewItem) CombinedExprViewItem(org.drools.model.view.CombinedExprViewItem) ExistentialPatternImpl(org.drools.model.patterns.ExistentialPatternImpl) PatternItem(org.drools.model.PatternDSL.PatternItem) ArrayList(java.util.ArrayList) FixedValueItem(org.drools.model.view.FixedValueItem) PatternExprImpl(org.drools.model.PatternDSL.PatternExprImpl) CompositePatterns(org.drools.model.patterns.CompositePatterns) PatternImpl(org.drools.model.patterns.PatternImpl) ExistentialPatternImpl(org.drools.model.patterns.ExistentialPatternImpl) AccumulatePatternImpl(org.drools.model.patterns.AccumulatePatternImpl) AccumulateExprViewItem(org.drools.model.view.AccumulateExprViewItem) EvalImpl(org.drools.model.patterns.EvalImpl) QueryCallViewItem(org.drools.model.view.QueryCallViewItem) QueryCallPattern(org.drools.model.patterns.QueryCallPattern) ExistentialExprViewItem(org.drools.model.view.ExistentialExprViewItem) PatternBindingImpl(org.drools.model.PatternDSL.PatternBindingImpl) PatternDefImpl(org.drools.model.PatternDSL.PatternDefImpl)

Aggregations

Condition (org.drools.model.Condition)3 AccumulatePatternImpl (org.drools.model.patterns.AccumulatePatternImpl)3 CompositePatterns (org.drools.model.patterns.CompositePatterns)3 ExistentialPatternImpl (org.drools.model.patterns.ExistentialPatternImpl)3 PatternImpl (org.drools.model.patterns.PatternImpl)3 AccumulateExprViewItem (org.drools.model.view.AccumulateExprViewItem)3 CombinedExprViewItem (org.drools.model.view.CombinedExprViewItem)3 ArrayList (java.util.ArrayList)2 EvalImpl (org.drools.model.patterns.EvalImpl)2 QueryCallPattern (org.drools.model.patterns.QueryCallPattern)2 AbstractExprViewItem (org.drools.model.view.AbstractExprViewItem)2 ExistentialExprViewItem (org.drools.model.view.ExistentialExprViewItem)2 ExprNViewItem (org.drools.model.view.ExprNViewItem)2 ExprViewItem (org.drools.model.view.ExprViewItem)2 FixedValueItem (org.drools.model.view.FixedValueItem)2 InputViewItem (org.drools.model.view.InputViewItem)2 QueryCallViewItem (org.drools.model.view.QueryCallViewItem)2 TemporalExprViewItem (org.drools.model.view.TemporalExprViewItem)2 ViewItem (org.drools.model.view.ViewItem)2 HashMap (java.util.HashMap)1