Search in sources :

Example 6 with CompositePatterns

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

the class ViewPatternBuilder method apply.

public CompositePatterns apply(RuleItemBuilder<?>[] viewItemBuilders) {
    List<RuleItem> ruleItems = Stream.of(viewItemBuilders).map(RuleItemBuilder::get).collect(toList());
    Iterator<RuleItem> ruleItemIterator = ruleItems.iterator();
    List<Condition> conditions = new ArrayList<>();
    Map<String, Consequence> consequences = new LinkedHashMap<>();
    while (ruleItemIterator.hasNext()) {
        RuleItem ruleItem = ruleItemIterator.next();
        if (ruleItem instanceof Consequence) {
            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) {
            conditions.add(createConditionalNamedConsequence(consequences, (ConditionalConsequence) ruleItem));
            continue;
        }
        conditions.add(ruleItem2Condition(ruleItem));
    }
    return new CompositePatterns(Condition.Type.AND, conditions, consequences);
}
Also used : Condition(org.drools.model.Condition) ConditionalConsequence(org.drools.model.ConditionalConsequence) ArrayList(java.util.ArrayList) Consequence(org.drools.model.Consequence) ConditionalConsequence(org.drools.model.ConditionalConsequence) LinkedHashMap(java.util.LinkedHashMap) CompositePatterns(org.drools.model.patterns.CompositePatterns) NamedConsequenceImpl(org.drools.model.consequences.NamedConsequenceImpl) ConditionalNamedConsequenceImpl(org.drools.model.consequences.ConditionalNamedConsequenceImpl) RuleItem(org.drools.model.RuleItem)

Example 7 with CompositePatterns

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

the class KiePackagesBuilder method recursivelyAddConditions.

private void recursivelyAddConditions(RuleContext ctx, GroupElement group, GroupElement allSubConditions, Condition c) {
    if (c instanceof CompositePatterns) {
        c.getSubConditions().forEach(sc -> recursivelyAddConditions(ctx, group, allSubConditions, sc));
    } else if (c instanceof ExistentialPatternImpl) {
        if (c.getType() == Condition.Type.FORALL) {
            allSubConditions.addChild(buildForAll(ctx, allSubConditions, c));
        } else {
            GroupElement existGroupElement = new GroupElement(conditionToGroupElementType(c.getType()));
            allSubConditions.addChild(existGroupElement);
            recursivelyAddConditions(ctx, existGroupElement, existGroupElement, c.getSubConditions().iterator().next());
        }
    } else if (c instanceof PatternImpl) {
        org.drools.model.Pattern pattern = (org.drools.model.Pattern<?>) c;
        RuleConditionElement rce = buildPattern(ctx, allSubConditions, pattern);
        if (ctx.getAccumulateSource(pattern.getPatternVariable()) == null) {
            allSubConditions.addChild(rce);
        }
    } else if (c instanceof AccumulatePattern) {
        RuleConditionElement rce = buildAccumulate(ctx, group, (AccumulatePattern) c);
        if (rce != null) {
            allSubConditions.addChild(rce);
        }
    } else if (c instanceof EvalImpl) {
        allSubConditions.addChild(buildEval(ctx, (EvalImpl) c));
    }
}
Also used : QueryCallPattern(org.drools.model.patterns.QueryCallPattern) AccumulatePattern(org.drools.model.AccumulatePattern) Pattern(org.drools.core.rule.Pattern) GroupByPattern(org.drools.model.GroupByPattern) CompositePatterns(org.drools.model.patterns.CompositePatterns) ExistentialPatternImpl(org.drools.model.patterns.ExistentialPatternImpl) GroupByPatternImpl(org.drools.model.patterns.GroupByPatternImpl) PatternImpl(org.drools.model.patterns.PatternImpl) ExistentialPatternImpl(org.drools.model.patterns.ExistentialPatternImpl) GroupElement(org.drools.core.rule.GroupElement) EvalImpl(org.drools.model.patterns.EvalImpl) RuleConditionElement(org.drools.core.rule.RuleConditionElement) AccumulatePattern(org.drools.model.AccumulatePattern)

Aggregations

CompositePatterns (org.drools.model.patterns.CompositePatterns)7 Condition (org.drools.model.Condition)6 ExistentialPatternImpl (org.drools.model.patterns.ExistentialPatternImpl)6 PatternImpl (org.drools.model.patterns.PatternImpl)6 ArrayList (java.util.ArrayList)5 QueryCallPattern (org.drools.model.patterns.QueryCallPattern)5 EvalImpl (org.drools.model.patterns.EvalImpl)4 GroupByPatternImpl (org.drools.model.patterns.GroupByPatternImpl)4 GroupElement (org.drools.core.rule.GroupElement)3 Pattern (org.drools.core.rule.Pattern)3 RuleConditionElement (org.drools.core.rule.RuleConditionElement)3 AccumulatePattern (org.drools.model.AccumulatePattern)3 Binding (org.drools.model.Binding)3 Consequence (org.drools.model.Consequence)3 GroupByPattern (org.drools.model.GroupByPattern)3 Variable (org.drools.model.Variable)3 ConditionalNamedConsequenceImpl (org.drools.model.consequences.ConditionalNamedConsequenceImpl)3 NamedConsequenceImpl (org.drools.model.consequences.NamedConsequenceImpl)3 AccumulatePatternImpl (org.drools.model.patterns.AccumulatePatternImpl)3 AccumulateExprViewItem (org.drools.model.view.AccumulateExprViewItem)3