Search in sources :

Example 1 with EntryPoint

use of org.drools.model.EntryPoint in project drools by kiegroup.

the class KiePackagesBuilder method addPatternForVariable.

private Pattern addPatternForVariable(RuleContext ctx, GroupElement group, Variable patternVariable) {
    Pattern pattern = new Pattern(ctx.getNextPatternIndex(), // offset will be set by ReteooBuilder
    0, getObjectType(patternVariable), patternVariable.getName(), true);
    if (patternVariable instanceof org.drools.model.Declaration) {
        org.drools.model.Declaration decl = (org.drools.model.Declaration) patternVariable;
        if (decl.getSource() != null) {
            if (decl.getSource() instanceof EntryPoint) {
                pattern.setSource(new EntryPointId(((EntryPoint) decl.getSource()).getName()));
            } else if (decl.getSource() instanceof WindowReference) {
                WindowReference<?> window = (WindowReference) decl.getSource();
                if (!ctx.getPkg().getWindowDeclarations().containsKey(window.getName())) {
                    createWindowReference(ctx, window);
                }
                pattern.setSource(new org.drools.core.rule.WindowReference(window.getName()));
            } else if (decl.getSource() instanceof From) {
                From<?> from = (From) decl.getSource();
                DataProvider provider = new LambdaDataProvider(ctx.getDeclaration(from.getVariable()), from.getProvider(), from.isReactive());
                org.drools.core.rule.From fromSource = new org.drools.core.rule.From(provider);
                fromSource.setResultPattern(pattern);
                pattern.setSource(fromSource);
            } else if (decl.getSource() instanceof UnitData) {
                UnitData unitData = (UnitData) decl.getSource();
                pattern.setSource(new EntryPointId(ctx.getRule().getRuleUnitClassName() + "." + unitData.getName()));
            } else {
                throw new UnsupportedOperationException("Unknown source: " + decl.getSource());
            }
        } else {
            Accumulate accSource = ctx.getAccumulateSource(patternVariable);
            if (accSource != null) {
                for (RuleConditionElement element : group.getChildren()) {
                    if (element instanceof Pattern && ((Pattern) element).getSource() == accSource) {
                        if (accSource instanceof MultiAccumulate) {
                            ((Pattern) element).getConstraints().forEach(pattern::addConstraint);
                            ((Pattern) element).getDeclarations().values().forEach(d -> {
                                pattern.addDeclaration(d);
                                d.setPattern(pattern);
                            });
                        }
                        group.getChildren().remove(element);
                        break;
                    }
                }
                pattern.setSource(accSource);
            }
        }
        if (decl.getWindow() != null) {
            pattern.addBehavior(createWindow(decl.getWindow()));
        }
    }
    ctx.registerPattern(patternVariable, pattern);
    return pattern;
}
Also used : QueryCallPattern(org.drools.model.patterns.QueryCallPattern) AccumulatePattern(org.drools.model.AccumulatePattern) Pattern(org.drools.core.rule.Pattern) MultiAccumulate(org.drools.core.rule.MultiAccumulate) EntryPoint(org.drools.model.EntryPoint) From(org.drools.model.From) RuleConditionElement(org.drools.core.rule.RuleConditionElement) WindowReference(org.drools.model.WindowReference) UnitData(org.drools.model.UnitData) MultiAccumulate(org.drools.core.rule.MultiAccumulate) SingleAccumulate(org.drools.core.rule.SingleAccumulate) Accumulate(org.drools.core.rule.Accumulate) LambdaDataProvider(org.drools.modelcompiler.constraints.LambdaDataProvider) DataProvider(org.drools.core.spi.DataProvider) EntryPointId(org.drools.core.rule.EntryPointId) LambdaDataProvider(org.drools.modelcompiler.constraints.LambdaDataProvider) 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)

Example 2 with EntryPoint

use of org.drools.model.EntryPoint in project drools by kiegroup.

the class KiePackagesBuilder method addPatternForVariable.

private Pattern addPatternForVariable(RuleContext ctx, GroupElement group, Variable patternVariable, Condition.Type type) {
    Pattern pattern = null;
    // If the variable is already bound to the result of previous accumulate result pattern, then find it.
    if (patternVariable instanceof org.drools.model.Declaration) {
        org.drools.model.Declaration decl = (org.drools.model.Declaration) patternVariable;
        if (decl.getSource() == null) {
            Accumulate accSource = ctx.getAccumulateSource(patternVariable);
            if (accSource != null) {
                for (RuleConditionElement element : group.getChildren()) {
                    if (element instanceof Pattern && ((Pattern) element).getSource() == accSource) {
                        pattern = (Pattern) element;
                        break;
                    }
                }
            }
        }
    }
    PatternSource priorSource = null;
    if (pattern != null && type == Condition.Type.ACCUMULATE) {
        // so it would be potentially tricky if this was a multi var, with multiple bindings.
        if (pattern.getSource() instanceof SingleAccumulate) {
            group.getChildren().remove(pattern);
            priorSource = pattern.getSource();
            pattern = null;
        }
    }
    if (pattern == null) {
        pattern = new Pattern(ctx.getNextPatternIndex(), // tupleIndex will be set by ReteooBuilder
        0, // tupleIndex will be set by ReteooBuilder
        0, getObjectType(patternVariable), patternVariable.getName(), true);
        pattern.setSource(priorSource);
    }
    if (patternVariable instanceof org.drools.model.Declaration) {
        org.drools.model.Declaration decl = (org.drools.model.Declaration) patternVariable;
        if (decl.getSource() != null) {
            if (decl.getSource() instanceof EntryPoint) {
                pattern.setSource(new EntryPointId(((EntryPoint) decl.getSource()).getName()));
            } else if (decl.getSource() instanceof WindowReference) {
                WindowReference<?> window = (WindowReference) decl.getSource();
                if (!ctx.getPkg().getWindowDeclarations().containsKey(window.getName())) {
                    createWindowReference(ctx, window);
                }
                pattern.setSource(new org.drools.core.rule.WindowReference(window.getName()));
            } else if (decl.getSource() instanceof From) {
                pattern.setSource(buildFrom(ctx, pattern, (From) decl.getSource()));
            } else if (decl.getSource() instanceof UnitData) {
                UnitData unitData = (UnitData) decl.getSource();
                pattern.setSource(new EntryPointId(ctx.getRule().getRuleUnitClassName() + "." + unitData.getName()));
            } else {
                throw new UnsupportedOperationException("Unknown source: " + decl.getSource());
            }
        }
        if (decl.getWindow() != null) {
            pattern.addBehavior(createWindow(decl.getWindow()));
            ctx.setNeedStreamMode();
        }
    } else if (patternVariable instanceof Exchange) {
        if (type == Condition.Type.SENDER) {
            Function0 supplier = ((Exchange) patternVariable).getMessageSupplier();
            DataProvider provider = new LambdaDataProvider(x -> supplier.apply(), false);
            pattern.setSource(new AsyncSend(pattern, patternVariable.getName(), provider));
        } else if (type == Condition.Type.RECEIVER) {
            pattern.setSource(new AsyncReceive(pattern, patternVariable.getName()));
        } else {
            throw new UnsupportedOperationException();
        }
    }
    ctx.registerPattern(patternVariable, pattern);
    return pattern;
}
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) QueryCallPattern(org.drools.model.patterns.QueryCallPattern) AccumulatePattern(org.drools.model.AccumulatePattern) Pattern(org.drools.core.rule.Pattern) GroupByPattern(org.drools.model.GroupByPattern) AsyncReceive(org.drools.core.rule.AsyncReceive) EntryPoint(org.drools.model.EntryPoint) Function0(org.drools.model.functions.Function0) RuleConditionElement(org.drools.core.rule.RuleConditionElement) From(org.drools.model.From) AsyncSend(org.drools.core.rule.AsyncSend) SingleAccumulate(org.drools.core.rule.SingleAccumulate) LambdaGroupByAccumulate(org.drools.modelcompiler.constraints.LambdaGroupByAccumulate) MultiAccumulate(org.drools.core.rule.MultiAccumulate) SingleAccumulate(org.drools.core.rule.SingleAccumulate) Accumulate(org.drools.core.rule.Accumulate) WindowReference(org.drools.model.WindowReference) UnitData(org.drools.model.UnitData) Exchange(org.drools.model.impl.Exchange) LambdaDataProvider(org.drools.modelcompiler.constraints.LambdaDataProvider) DataProvider(org.drools.core.spi.DataProvider) EntryPointId(org.drools.core.rule.EntryPointId) LambdaDataProvider(org.drools.modelcompiler.constraints.LambdaDataProvider) PatternSource(org.drools.core.rule.PatternSource) 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)

Example 3 with EntryPoint

use of org.drools.model.EntryPoint in project drools by kiegroup.

the class KiePackagesBuilder method build.

public CanonicalKiePackages build() {
    for (Model model : models) {
        for (EntryPoint entryPoint : model.getEntryPoints()) {
            InternalKnowledgePackage pkg = packages.computeIfAbsent(model.getName(), this::createKiePackage);
            pkg.addEntryPointId(entryPoint.getName());
        }
        for (TypeMetaData metaType : model.getTypeMetaDatas()) {
            InternalKnowledgePackage pkg = packages.computeIfAbsent(metaType.getPackage(), this::createKiePackage);
            pkg.addTypeDeclaration(createTypeDeclaration(metaType, getPropertySpecificOption(), pkg.getTypeResolver()));
        }
        for (Global global : model.getGlobals()) {
            InternalKnowledgePackage pkg = packages.computeIfAbsent(global.getPackage(), this::createKiePackage);
            pkg.addGlobal(global.getName(), global.getType());
        }
        for (Query query : model.getQueries()) {
            InternalKnowledgePackage pkg = packages.computeIfAbsent(query.getPackage(), this::createKiePackage);
            pkg.addRule(compileQuery(pkg, query));
        }
        int ruleCounter = 0;
        for (Rule rule : model.getRules()) {
            InternalKnowledgePackage pkg = packages.computeIfAbsent(rule.getPackage(), this::createKiePackage);
            for (RuleImpl ruleImpl : compileRule(pkg, rule)) {
                ruleImpl.setLoadOrder(ruleCounter++);
                pkg.addRule(ruleImpl);
            }
        }
    }
    return new CanonicalKiePackages(packages);
}
Also used : TypeMetaData(org.drools.model.TypeMetaData) Query(org.drools.model.Query) DroolsQuery(org.drools.core.base.DroolsQuery) Model(org.drools.model.Model) EntryPoint(org.drools.model.EntryPoint) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Rule(org.drools.model.Rule) Global(org.drools.model.Global) SingleConstraint(org.drools.model.SingleConstraint) QueryNameConstraint(org.drools.core.rule.constraint.QueryNameConstraint) LambdaConstraint(org.drools.modelcompiler.constraints.LambdaConstraint) UnificationConstraint(org.drools.modelcompiler.constraints.UnificationConstraint) EntryPoint(org.drools.model.EntryPoint) AbstractConstraint(org.drools.modelcompiler.constraints.AbstractConstraint) Constraint(org.drools.model.Constraint) CombinedConstraint(org.drools.modelcompiler.constraints.CombinedConstraint) AbstractSingleConstraint(org.drools.model.constraints.AbstractSingleConstraint) DSL.entryPoint(org.drools.model.DSL.entryPoint) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Aggregations

EntryPoint (org.drools.model.EntryPoint)3 DroolsQuery (org.drools.core.base.DroolsQuery)2 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)2 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)2 QueryNameConstraint (org.drools.core.rule.constraint.QueryNameConstraint)2 Constraint (org.drools.model.Constraint)2 DSL.entryPoint (org.drools.model.DSL.entryPoint)2 Global (org.drools.model.Global)2 Model (org.drools.model.Model)2 Query (org.drools.model.Query)2 Rule (org.drools.model.Rule)2 SingleConstraint (org.drools.model.SingleConstraint)2 TypeMetaData (org.drools.model.TypeMetaData)2 AbstractSingleConstraint (org.drools.model.constraints.AbstractSingleConstraint)2 AbstractConstraint (org.drools.modelcompiler.constraints.AbstractConstraint)2 CombinedConstraint (org.drools.modelcompiler.constraints.CombinedConstraint)2 LambdaConstraint (org.drools.modelcompiler.constraints.LambdaConstraint)2 UnificationConstraint (org.drools.modelcompiler.constraints.UnificationConstraint)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1