Search in sources :

Example 46 with Declaration

use of org.drools.core.rule.Declaration in project drools by kiegroup.

the class ConstraintEvaluator method findPatternAndRequiredDeclaration.

private Declaration findPatternAndRequiredDeclaration() {
    Declaration patternDeclaration = null;
    List<Declaration> requiredDeclarationsList = new ArrayList<>();
    for (int i = 0; i < declarations.length; i++) {
        if (pattern.getDeclaration().getIdentifier().equals(declarations[i].getIdentifier())) {
            patternDeclaration = declarations[i];
        } else {
            requiredDeclarationsList.add(declarations[i]);
        }
    }
    this.requiredDeclarations = requiredDeclarationsList.toArray(new Declaration[requiredDeclarationsList.size()]);
    return patternDeclaration;
}
Also used : ArrayList(java.util.ArrayList) Declaration(org.drools.core.rule.Declaration) SingleConstraint(org.drools.model.SingleConstraint)

Example 47 with Declaration

use of org.drools.core.rule.Declaration in project drools by kiegroup.

the class KiePackagesBuilder method buildEval.

private EvalCondition buildEval(RuleContext ctx, EvalImpl eval) {
    Declaration[] declarations = Stream.of(eval.getExpr().getVariables()).map(ctx::getDeclaration).toArray(Declaration[]::new);
    EvalExpression evalExpr = new LambdaEvalExpression(declarations, eval.getExpr());
    return new EvalCondition(evalExpr, declarations);
}
Also used : LambdaEvalExpression(org.drools.modelcompiler.constraints.LambdaEvalExpression) EvalExpression(org.drools.core.spi.EvalExpression) LambdaEvalExpression(org.drools.modelcompiler.constraints.LambdaEvalExpression) 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) EvalCondition(org.drools.core.rule.EvalCondition)

Example 48 with Declaration

use of org.drools.core.rule.Declaration in project drools by kiegroup.

the class KiePackagesBuilder method addQueryPattern.

private void addQueryPattern(Query query, QueryImpl queryImpl, RuleContext ctx) {
    Pattern pattern = new Pattern(ctx.getNextPatternIndex(), // offset is 0 by default
    0, ClassObjectType.DroolsQuery_ObjectType, null);
    InternalReadAccessor extractor = new LambdaReadAccessor(DroolsQuery.class, q -> ((DroolsQuery) q).getName());
    QueryNameConstraint constraint = new QueryNameConstraint(extractor, query.getName());
    pattern.addConstraint(constraint);
    queryImpl.getLhs().addChild(pattern);
    Variable<?>[] args = query.getArguments();
    Declaration[] declarations = new Declaration[args.length];
    for (int i = 0; i < args.length; i++) {
        int index = i;
        LambdaReadAccessor accessor = new LambdaReadAccessor(index, args[index].getType(), obj -> ((DroolsQuery) obj).getElements()[index]);
        declarations[i] = new Declaration(args[i].getName(), accessor, pattern, true);
        pattern.addDeclaration(declarations[i]);
        ctx.addQueryDeclaration(args[i], declarations[i]);
    }
    queryImpl.setParameters(declarations);
}
Also used : QueryCallPattern(org.drools.model.patterns.QueryCallPattern) AccumulatePattern(org.drools.model.AccumulatePattern) Pattern(org.drools.core.rule.Pattern) QueryNameConstraint(org.drools.core.rule.constraint.QueryNameConstraint) PrototypeVariable(org.drools.model.PrototypeVariable) Variable(org.drools.model.Variable) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) 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) LambdaReadAccessor(org.drools.modelcompiler.constraints.LambdaReadAccessor) SingleConstraint(org.drools.model.SingleConstraint) QueryNameConstraint(org.drools.core.rule.constraint.QueryNameConstraint) LambdaConstraint(org.drools.modelcompiler.constraints.LambdaConstraint) FlowDSL.entryPoint(org.drools.model.FlowDSL.entryPoint) UnificationConstraint(org.drools.modelcompiler.constraints.UnificationConstraint) EntryPoint(org.drools.model.EntryPoint) Constraint(org.drools.model.Constraint) DroolsQuery(org.drools.core.base.DroolsQuery)

Example 49 with Declaration

use of org.drools.core.rule.Declaration in project drools by kiegroup.

the class KiePackagesBuilder method buildPattern.

private Pattern buildPattern(RuleContext ctx, GroupElement group, Condition condition) {
    org.drools.model.Pattern<?> modelPattern = (org.drools.model.Pattern) condition;
    Pattern pattern = addPatternForVariable(ctx, group, modelPattern.getPatternVariable());
    for (Binding binding : modelPattern.getBindings()) {
        Declaration declaration = new Declaration(binding.getBoundVariable().getName(), new LambdaReadAccessor(binding.getBoundVariable().getType(), binding.getBindingFunction()), pattern, true);
        pattern.addDeclaration(declaration);
        if (binding.getReactOn() != null) {
            addFieldsToPatternWatchlist(pattern, binding.getReactOn());
        }
        ctx.addInnerDeclaration(binding.getBoundVariable(), declaration);
    }
    Declaration queryArgDecl = ctx.getQueryDeclaration(modelPattern.getPatternVariable());
    if (queryArgDecl != null) {
        pattern.addConstraint(new UnificationConstraint(queryArgDecl));
    }
    addConstraintsToPattern(ctx, pattern, modelPattern, modelPattern.getConstraint());
    addFieldsToPatternWatchlist(pattern, modelPattern.getWatchedProps());
    return pattern;
}
Also used : Binding(org.drools.model.Binding) QueryCallPattern(org.drools.model.patterns.QueryCallPattern) AccumulatePattern(org.drools.model.AccumulatePattern) Pattern(org.drools.core.rule.Pattern) 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) LambdaReadAccessor(org.drools.modelcompiler.constraints.LambdaReadAccessor) UnificationConstraint(org.drools.modelcompiler.constraints.UnificationConstraint)

Example 50 with Declaration

use of org.drools.core.rule.Declaration in project drools by kiegroup.

the class KiePackagesBuilder method buildAccumulate.

private Accumulate buildAccumulate(RuleContext ctx, AccumulatePattern accPattern, RuleConditionElement source, Pattern pattern, List<String> usedVariableName, Binding binding) {
    AccumulateFunction[] accFunctions = accPattern.getAccumulateFunctions();
    Accumulate accumulate;
    if (accFunctions.length == 1) {
        final Class<?> functionClass = accFunctions[0].getFunctionClass();
        final Accumulator accumulator = createAccumulator(usedVariableName, binding, functionClass);
        final Variable boundVar = accPattern.getBoundVariables()[0];
        final Declaration declaration = new Declaration(boundVar.getName(), getReadAcessor(JAVA_CLASS_OBJECT_TYPE), pattern, true);
        pattern.addDeclaration(declaration);
        Declaration[] bindingDeclaration = binding != null ? new Declaration[0] : new Declaration[] { ctx.getPattern(accFunctions[0].getSource()).getDeclaration() };
        accumulate = new SingleAccumulate(source, bindingDeclaration, accumulator);
    } else {
        InternalReadAccessor reader = new SelfReferenceClassFieldReader(Object[].class);
        Accumulator[] accumulators = new Accumulator[accFunctions.length];
        for (int i = 0; i < accFunctions.length; i++) {
            final Class<?> functionClass = accFunctions[i].getFunctionClass();
            final Accumulator accumulator = createAccumulator(usedVariableName, binding, functionClass);
            Variable boundVar = accPattern.getBoundVariables()[i];
            pattern.addDeclaration(new Declaration(boundVar.getName(), new ArrayElementReader(reader, i, boundVar.getType()), pattern, true));
            accumulators[i] = accumulator;
        }
        accumulate = new MultiAccumulate(source, new Declaration[0], accumulators);
    }
    for (Variable boundVar : accPattern.getBoundVariables()) {
        ctx.addAccumulateSource(boundVar, accumulate);
    }
    return accumulate;
}
Also used : LambdaAccumulator(org.drools.modelcompiler.constraints.LambdaAccumulator) Accumulator(org.drools.core.spi.Accumulator) PrototypeVariable(org.drools.model.PrototypeVariable) Variable(org.drools.model.Variable) MultiAccumulate(org.drools.core.rule.MultiAccumulate) SingleAccumulate(org.drools.core.rule.SingleAccumulate) SingleConstraint(org.drools.model.SingleConstraint) QueryNameConstraint(org.drools.core.rule.constraint.QueryNameConstraint) LambdaConstraint(org.drools.modelcompiler.constraints.LambdaConstraint) FlowDSL.entryPoint(org.drools.model.FlowDSL.entryPoint) UnificationConstraint(org.drools.modelcompiler.constraints.UnificationConstraint) EntryPoint(org.drools.model.EntryPoint) Constraint(org.drools.model.Constraint) MultiAccumulate(org.drools.core.rule.MultiAccumulate) SingleAccumulate(org.drools.core.rule.SingleAccumulate) Accumulate(org.drools.core.rule.Accumulate) SelfReferenceClassFieldReader(org.drools.core.base.extractors.SelfReferenceClassFieldReader) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) ArrayElementReader(org.drools.core.base.extractors.ArrayElementReader) 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) AccumulateFunction(org.drools.model.functions.accumulate.AccumulateFunction)

Aggregations

Declaration (org.drools.core.rule.Declaration)115 Pattern (org.drools.core.rule.Pattern)42 ClassObjectType (org.drools.core.base.ClassObjectType)29 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)27 TypeDeclaration (org.drools.core.rule.TypeDeclaration)24 InternalFactHandle (org.drools.core.common.InternalFactHandle)22 ArrayList (java.util.ArrayList)19 HashMap (java.util.HashMap)17 BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)17 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)16 Test (org.junit.Test)16 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)14 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)12 WorkingMemory (org.drools.core.WorkingMemory)11 MVELCompilationUnit (org.drools.core.base.mvel.MVELCompilationUnit)11 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)11 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)10 Consequence (org.drools.core.spi.Consequence)10 Constraint (org.drools.core.spi.Constraint)10 AnalysisResult (org.drools.compiler.compiler.AnalysisResult)9