Search in sources :

Example 1 with CompositePatterns

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

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

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

the class KiePackagesBuilder method addInnerBindings.

private void addInnerBindings(Collection<Binding> bindings, AccumulateFunction[] accumulateFunctions, Condition condition) {
    List<org.drools.model.Declaration> functionArgList = Arrays.stream(accumulateFunctions).map(function -> function.getSource()).filter(org.drools.model.Declaration.class::isInstance).map(org.drools.model.Declaration.class::cast).collect(Collectors.toList());
    if (condition instanceof CompositePatterns) {
        CompositePatterns compositePatterns = (CompositePatterns) condition;
        for (Condition c : compositePatterns.getSubConditions()) {
            Variable<?>[] boundVariables = c.getBoundVariables();
            Arrays.stream(boundVariables).filter(org.drools.model.Declaration.class::isInstance).map(org.drools.model.Declaration.class::cast).filter(decl -> functionArgList.contains(decl)).forEach(decl -> bindings.add(new SelfPatternBiding<>((org.drools.model.Declaration) decl)));
        }
    }
}
Also used : GroupByPatternImpl(org.drools.model.patterns.GroupByPatternImpl) Arrays(java.util.Arrays) AsyncReceive(org.drools.core.rule.AsyncReceive) FunctionUtils.toFunctionN(org.drools.model.functions.FunctionUtils.toFunctionN) Binding(org.drools.model.Binding) CoreComponentFactory(org.drools.core.reteoo.CoreComponentFactory) SingleConstraint(org.drools.model.SingleConstraint) QueryCallPattern(org.drools.model.patterns.QueryCallPattern) PatternImpl(org.drools.model.patterns.PatternImpl) PatternSource(org.drools.core.rule.PatternSource) Map(java.util.Map) Declaration(org.drools.core.rule.Declaration) NamesGenerator.generateName(org.drools.model.impl.NamesGenerator.generateName) LambdaGroupByAccumulate(org.drools.modelcompiler.constraints.LambdaGroupByAccumulate) QueryNameConstraint(org.drools.core.rule.constraint.QueryNameConstraint) ConditionalElement(org.drools.core.rule.ConditionalElement) QueryImpl(org.drools.core.rule.QueryImpl) LambdaConstraint(org.drools.modelcompiler.constraints.LambdaConstraint) Direct(org.kie.api.definition.rule.Direct) EvaluationUtil.adaptBitMask(org.drools.modelcompiler.util.EvaluationUtil.adaptBitMask) TimerUtil.buildTimerExpression(org.drools.modelcompiler.util.TimerUtil.buildTimerExpression) CompositePatterns(org.drools.model.patterns.CompositePatterns) Set(java.util.Set) Predicate1(org.drools.model.functions.Predicate1) Exchange(org.drools.model.impl.Exchange) Query(org.drools.model.Query) Stream(java.util.stream.Stream) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) FactTemplateObjectType(org.drools.core.facttemplates.FactTemplateObjectType) LambdaAccumulator(org.drools.modelcompiler.constraints.LambdaAccumulator) CountAccumulateFunction(org.drools.core.base.accumulators.CountAccumulateFunction) AccumulatePattern(org.drools.model.AccumulatePattern) DomainClassMetadata(org.drools.model.DomainClassMetadata) LambdaDataProvider(org.drools.modelcompiler.constraints.LambdaDataProvider) SelfPatternBiding(org.drools.model.view.SelfPatternBiding) Enabled(org.drools.core.spi.Enabled) ObjectType(org.drools.core.spi.ObjectType) EntryPointId(org.drools.core.rule.EntryPointId) All(org.kie.api.definition.rule.All) TypeMetaData(org.drools.model.TypeMetaData) ArrayList(java.util.ArrayList) UnificationConstraint(org.drools.modelcompiler.constraints.UnificationConstraint) BitMaskUtil.calculatePatternMask(org.drools.model.bitmask.BitMaskUtil.calculatePatternMask) RuleUnitUtil(org.kie.internal.ruleunit.RuleUnitUtil) LinkedHashSet(java.util.LinkedHashSet) EntryPoint(org.drools.model.EntryPoint) QueryElement(org.drools.core.rule.QueryElement) RuleConditionElement(org.drools.core.rule.RuleConditionElement) WindowDeclaration(org.drools.core.rule.WindowDeclaration) SelfReferenceClassFieldReader(org.drools.core.base.extractors.SelfReferenceClassFieldReader) LambdaConsequence(org.drools.modelcompiler.consequence.LambdaConsequence) PatternExtractor(org.drools.core.spi.PatternExtractor) NamedConsequenceImpl(org.drools.model.consequences.NamedConsequenceImpl) TemporalConstraintEvaluator(org.drools.modelcompiler.constraints.TemporalConstraintEvaluator) Rule(org.drools.model.Rule) DataProvider(org.drools.core.spi.DataProvider) PropertySpecificOption(org.kie.internal.builder.conf.PropertySpecificOption) BindingEvaluator(org.drools.modelcompiler.constraints.BindingEvaluator) EvalCondition(org.drools.core.rule.EvalCondition) TypeDeclarationUtil.createTypeDeclaration(org.drools.modelcompiler.util.TypeDeclarationUtil.createTypeDeclaration) EnabledBoolean(org.drools.core.base.EnabledBoolean) TypeDeclaration(org.drools.core.rule.TypeDeclaration) Accumulator(org.drools.core.spi.Accumulator) PrototypeVariable(org.drools.model.PrototypeVariable) Consequence(org.drools.model.Consequence) Global(org.drools.model.Global) WindowReference(org.drools.model.WindowReference) DroolsQuery(org.drools.core.base.DroolsQuery) UnitData(org.drools.model.UnitData) DynamicValueSupplier(org.drools.model.DynamicValueSupplier) ExistentialPatternImpl(org.drools.model.patterns.ExistentialPatternImpl) NamedConsequence(org.drools.core.rule.NamedConsequence) AbstractConstraint(org.drools.modelcompiler.constraints.AbstractConstraint) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Value(org.drools.model.Value) FactFactory.prototypeToFactTemplate(org.drools.modelcompiler.facttemplate.FactFactory.prototypeToFactTemplate) Condition(org.drools.model.Condition) Constraint(org.drools.model.Constraint) Collection(java.util.Collection) Forall(org.drools.core.rule.Forall) SlidingTimeWindow(org.drools.core.rule.SlidingTimeWindow) Index(org.drools.model.Index) LambdaEvalExpression(org.drools.modelcompiler.constraints.LambdaEvalExpression) Collectors(java.util.stream.Collectors) RuleBuilder.buildTimer(org.drools.compiler.rule.builder.RuleBuilder.buildTimer) List(java.util.List) SingleConstraint1(org.drools.model.constraints.SingleConstraint1) Entry(java.util.Map.Entry) Optional(java.util.Optional) BindingInnerObjectEvaluator(org.drools.modelcompiler.constraints.BindingInnerObjectEvaluator) SlidingLengthWindow(org.drools.core.rule.SlidingLengthWindow) Role(org.kie.api.definition.type.Role) ConstraintEvaluator(org.drools.modelcompiler.constraints.ConstraintEvaluator) OR(org.drools.core.rule.GroupElement.OR) LambdaSalience(org.drools.modelcompiler.attributes.LambdaSalience) CombinedConstraint(org.drools.modelcompiler.constraints.CombinedConstraint) KieBaseConfiguration(org.kie.api.KieBaseConfiguration) ClassObjectType(org.drools.core.base.ClassObjectType) Prototype(org.drools.model.Prototype) ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) Pattern(org.drools.core.rule.Pattern) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) HashMap(java.util.HashMap) AsyncSend(org.drools.core.rule.AsyncSend) Behavior(org.drools.core.rule.Behavior) AbstractSingleConstraint(org.drools.model.constraints.AbstractSingleConstraint) AND(org.drools.core.rule.GroupElement.AND) GroupByPattern(org.drools.model.GroupByPattern) View(org.drools.model.View) Function0(org.drools.model.functions.Function0) EvalImpl(org.drools.model.patterns.EvalImpl) LambdaReadAccessor(org.drools.modelcompiler.constraints.LambdaReadAccessor) Function1(org.drools.model.functions.Function1) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) WindowDefinition(org.drools.model.WindowDefinition) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) From0(org.drools.model.From0) From1(org.drools.model.From1) EvalExpression(org.drools.core.spi.EvalExpression) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) From(org.drools.model.From) From4(org.drools.model.From4) From2(org.drools.model.From2) Model(org.drools.model.Model) From3(org.drools.model.From3) MultiAccumulate(org.drools.core.rule.MultiAccumulate) Salience(org.drools.core.spi.Salience) Variable(org.drools.model.Variable) ArrayElementReader(org.drools.core.base.extractors.ArrayElementReader) KnowledgeBuilderConfiguration(org.kie.internal.builder.KnowledgeBuilderConfiguration) Propagation(org.kie.api.definition.rule.Propagation) Consumer(java.util.function.Consumer) DSL.entryPoint(org.drools.model.DSL.entryPoint) QueryArgument(org.drools.core.rule.QueryArgument) Collectors.toList(java.util.stream.Collectors.toList) SingleAccumulate(org.drools.core.rule.SingleAccumulate) SalienceInteger(org.drools.core.base.SalienceInteger) Accumulate(org.drools.core.rule.Accumulate) ConditionalNamedConsequenceImpl(org.drools.model.consequences.ConditionalNamedConsequenceImpl) Collections(java.util.Collections) DSL.declarationOf(org.drools.model.DSL.declarationOf) ConditionalBranch(org.drools.core.rule.ConditionalBranch) AccumulateFunction(org.drools.model.functions.accumulate.AccumulateFunction) LambdaEnabled(org.drools.modelcompiler.attributes.LambdaEnabled) GroupElement(org.drools.core.rule.GroupElement) Argument(org.drools.model.Argument) DeclarationImpl(org.drools.model.impl.DeclarationImpl) EvalCondition(org.drools.core.rule.EvalCondition) Condition(org.drools.model.Condition) CompositePatterns(org.drools.model.patterns.CompositePatterns) PrototypeVariable(org.drools.model.PrototypeVariable) Variable(org.drools.model.Variable) 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) SelfPatternBiding(org.drools.model.view.SelfPatternBiding)

Example 4 with CompositePatterns

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

the class KiePackagesBuilder method buildAccumulate.

private RuleConditionElement buildAccumulate(RuleContext ctx, GroupElement group, AccumulatePattern accumulatePattern) {
    Pattern pattern = null;
    boolean isGroupBy = accumulatePattern instanceof GroupByPattern;
    if (accumulatePattern.getAccumulateFunctions() != null) {
        if (!isGroupBy && accumulatePattern.getAccumulateFunctions().length == 1) {
            // non groupby with single accumulates can be optimized to directly return the result, rather than place in an array of 1
            pattern = ctx.getPattern(accumulatePattern.getAccumulateFunctions()[0].getResult());
        } else if (accumulatePattern.getAccumulateFunctions().length > 0 && ctx.getPattern(accumulatePattern.getAccumulateFunctions()[0].getResult()) != null) {
            // Illegal executable model. Cannot have groupby or multi accumulate mapped to a single result object.
            throw new RuntimeException("Only single accumulate functions, with no group by can optimize the result pattern to be the function return value");
        }
    }
    boolean existingPattern = pattern != null;
    if (!existingPattern) {
        ObjectType type = !isGroupBy && accumulatePattern.getAccumulateFunctions().length == 1 ? new ClassObjectType(accumulatePattern.getAccumulateFunctions()[0].getResult().getType()) : // groupby or multi function accumulate
        JAVA_CLASS_ARRAY_TYPE;
        pattern = new Pattern(ctx.getNextPatternIndex(), type);
    }
    PatternImpl<?> sourcePattern = (PatternImpl<?>) accumulatePattern.getPattern();
    Set<String> usedVariableName = new LinkedHashSet<>();
    if (sourcePattern != null) {
        for (Variable v : sourcePattern.getInputVariables()) {
            usedVariableName.add(v.getName());
        }
    }
    RuleConditionElement source;
    if (accumulatePattern.isQuerySource()) {
        source = buildQueryPattern(ctx, ((QueryCallPattern) accumulatePattern.getCondition()));
    } else if (accumulatePattern.isCompositePatterns()) {
        CompositePatterns compositePatterns = (CompositePatterns) accumulatePattern.getCondition();
        GroupElement allSubConditions = new GroupElement(conditionToGroupElementType(compositePatterns.getType()));
        for (Condition c : compositePatterns.getSubConditions()) {
            recursivelyAddConditions(ctx, group, allSubConditions, c);
        }
        source = allSubConditions.getChildren().size() == 1 ? allSubConditions.getChildren().get(0) : allSubConditions;
    } else {
        source = buildPattern(ctx, group, accumulatePattern);
    }
    Collection<Binding> bindings = new ArrayList<>();
    if (sourcePattern != null) {
        bindings.addAll(sourcePattern.getBindings());
        bindings.add(new SelfPatternBiding<>(sourcePattern.getPatternVariable()));
    } else {
        // No pattern is associated. It likely uses inner bindings
        addInnerBindings(bindings, accumulatePattern.getAccumulateFunctions(), accumulatePattern.getCondition());
    }
    pattern.setSource(buildAccumulate(ctx, accumulatePattern, source, pattern, usedVariableName, bindings));
    return existingPattern ? null : pattern;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) EvalCondition(org.drools.core.rule.EvalCondition) Condition(org.drools.model.Condition) Binding(org.drools.model.Binding) 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) ClassObjectType(org.drools.core.base.ClassObjectType) GroupElement(org.drools.core.rule.GroupElement) ArrayList(java.util.ArrayList) RuleConditionElement(org.drools.core.rule.RuleConditionElement) FactTemplateObjectType(org.drools.core.facttemplates.FactTemplateObjectType) ObjectType(org.drools.core.spi.ObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) CompositePatterns(org.drools.model.patterns.CompositePatterns) GroupByPattern(org.drools.model.GroupByPattern) GroupByPatternImpl(org.drools.model.patterns.GroupByPatternImpl) PatternImpl(org.drools.model.patterns.PatternImpl) ExistentialPatternImpl(org.drools.model.patterns.ExistentialPatternImpl)

Example 5 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