Search in sources :

Example 1 with UnificationConstraint

use of org.drools.modelcompiler.constraints.UnificationConstraint 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 2 with UnificationConstraint

use of org.drools.modelcompiler.constraints.UnificationConstraint in project drools by kiegroup.

the class KiePackagesBuilder method addConstraintsToPattern.

private void addConstraintsToPattern(RuleContext ctx, Pattern pattern, org.drools.model.Pattern modelPattern, Constraint constraint) {
    if (constraint.getType() == Constraint.Type.SINGLE) {
        SingleConstraint singleConstraint = (SingleConstraint) constraint;
        boolean isEqual = singleConstraint.getIndex() != null && singleConstraint.getIndex().getConstraintType() == Index.ConstraintType.EQUAL;
        if (singleConstraint.getVariables().length > 0) {
            Variable[] vars = singleConstraint.getVariables();
            Declaration[] declarations = new Declaration[vars.length];
            Declaration unificationDeclaration = null;
            for (int i = 0; i < vars.length; i++) {
                declarations[i] = ctx.getDeclaration(vars[i]);
                if (isEqual && declarations[i].getPattern().getObjectType().equals(ClassObjectType.DroolsQuery_ObjectType)) {
                    unificationDeclaration = declarations[i];
                } else if (pattern.getSource() instanceof MultiAccumulate) {
                    Declaration accDeclaration = pattern.getDeclarations().get(declarations[i].getBindingName());
                    if (accDeclaration != null) {
                        declarations[i].setReadAccessor(accDeclaration.getExtractor());
                    }
                }
            }
            ConstraintEvaluator constraintEvaluator = singleConstraint.isTemporal() ? new TemporalConstraintEvaluator(declarations, pattern, singleConstraint) : new ConstraintEvaluator(declarations, pattern, singleConstraint);
            org.drools.core.spi.Constraint droolsConstraint = unificationDeclaration != null ? new UnificationConstraint(unificationDeclaration, constraintEvaluator) : new LambdaConstraint(constraintEvaluator);
            pattern.addConstraint(droolsConstraint);
        }
    } else if (modelPattern.getConstraint().getType() == Constraint.Type.AND) {
        for (Constraint child : constraint.getChildren()) {
            addConstraintsToPattern(ctx, pattern, modelPattern, child);
        }
    }
}
Also used : PrototypeVariable(org.drools.model.PrototypeVariable) Variable(org.drools.model.Variable) SingleConstraint(org.drools.model.SingleConstraint) QueryNameConstraint(org.drools.core.rule.constraint.QueryNameConstraint) LambdaConstraint(org.drools.modelcompiler.constraints.LambdaConstraint) UnificationConstraint(org.drools.modelcompiler.constraints.UnificationConstraint) Constraint(org.drools.model.Constraint) MultiAccumulate(org.drools.core.rule.MultiAccumulate) LambdaConstraint(org.drools.modelcompiler.constraints.LambdaConstraint) TemporalConstraintEvaluator(org.drools.modelcompiler.constraints.TemporalConstraintEvaluator) ConstraintEvaluator(org.drools.modelcompiler.constraints.ConstraintEvaluator) UnificationConstraint(org.drools.modelcompiler.constraints.UnificationConstraint) 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) TemporalConstraintEvaluator(org.drools.modelcompiler.constraints.TemporalConstraintEvaluator) SingleConstraint(org.drools.model.SingleConstraint) 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 UnificationConstraint

use of org.drools.modelcompiler.constraints.UnificationConstraint in project drools by kiegroup.

the class KiePackagesBuilder method createSingleConstraint.

private org.drools.core.spi.Constraint createSingleConstraint(RuleContext ctx, Pattern pattern, SingleConstraint singleConstraint) {
    Variable[] vars = singleConstraint.getVariables();
    Declaration[] declarations = new Declaration[vars.length];
    Declaration unificationDeclaration = collectConstraintDeclarations(ctx, pattern, singleConstraint, vars, declarations);
    ConstraintEvaluator constraintEvaluator = singleConstraint.isTemporal() ? new TemporalConstraintEvaluator(declarations, pattern, singleConstraint) : new ConstraintEvaluator(declarations, pattern, singleConstraint);
    return unificationDeclaration != null ? new UnificationConstraint(unificationDeclaration, constraintEvaluator) : new LambdaConstraint(constraintEvaluator, singleConstraint.predicateInformation());
}
Also used : PrototypeVariable(org.drools.model.PrototypeVariable) Variable(org.drools.model.Variable) LambdaConstraint(org.drools.modelcompiler.constraints.LambdaConstraint) 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) TemporalConstraintEvaluator(org.drools.modelcompiler.constraints.TemporalConstraintEvaluator) ConstraintEvaluator(org.drools.modelcompiler.constraints.ConstraintEvaluator) UnificationConstraint(org.drools.modelcompiler.constraints.UnificationConstraint) TemporalConstraintEvaluator(org.drools.modelcompiler.constraints.TemporalConstraintEvaluator)

Example 4 with UnificationConstraint

use of org.drools.modelcompiler.constraints.UnificationConstraint in project drools by kiegroup.

the class KiePackagesBuilder method buildPattern.

private RuleConditionElement buildPattern(RuleContext ctx, GroupElement group, org.drools.model.Pattern<?> modelPattern) {
    Variable patternVariable = modelPattern.getPatternVariable();
    Pattern pattern = addPatternForVariable(ctx, group, patternVariable, modelPattern.getType());
    Arrays.stream(modelPattern.getWatchedProps()).forEach(pattern::addWatchedProperty);
    pattern.setPassive(modelPattern.isPassive());
    for (Binding binding : modelPattern.getBindings()) {
        // FIXME this is returning null for BindViewItem2, BindViewItem3 etc (mdp)
        Function1 f1 = getBindingFunction(ctx, patternVariable, binding);
        Declaration declaration = new Declaration(binding.getBoundVariable().getName(), new LambdaReadAccessor(binding.getBoundVariable().getType(), f1), pattern, true);
        pattern.addDeclaration(declaration);
        if (binding.getReactOn() != null) {
            Arrays.stream(binding.getReactOn()).forEach(pattern::addBoundProperty);
        }
        ctx.addDeclaration(binding.getBoundVariable(), declaration);
    }
    Declaration queryArgDecl = ctx.getQueryDeclaration(patternVariable);
    if (queryArgDecl != null) {
        pattern.addConstraint(new UnificationConstraint(queryArgDecl));
    }
    addConstraintsToPattern(ctx, pattern, modelPattern.getConstraint());
    addReactiveMasksToPattern(pattern, modelPattern.getPatternClassMetadata());
    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) GroupByPattern(org.drools.model.GroupByPattern) PrototypeVariable(org.drools.model.PrototypeVariable) Variable(org.drools.model.Variable) Function1(org.drools.model.functions.Function1) 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)

Aggregations

Declaration (org.drools.core.rule.Declaration)4 TypeDeclaration (org.drools.core.rule.TypeDeclaration)4 WindowDeclaration (org.drools.core.rule.WindowDeclaration)4 UnificationConstraint (org.drools.modelcompiler.constraints.UnificationConstraint)4 TypeDeclarationUtil.createTypeDeclaration (org.drools.modelcompiler.util.TypeDeclarationUtil.createTypeDeclaration)4 PrototypeVariable (org.drools.model.PrototypeVariable)3 Variable (org.drools.model.Variable)3 Pattern (org.drools.core.rule.Pattern)2 AccumulatePattern (org.drools.model.AccumulatePattern)2 Binding (org.drools.model.Binding)2 QueryCallPattern (org.drools.model.patterns.QueryCallPattern)2 ConstraintEvaluator (org.drools.modelcompiler.constraints.ConstraintEvaluator)2 LambdaConstraint (org.drools.modelcompiler.constraints.LambdaConstraint)2 LambdaReadAccessor (org.drools.modelcompiler.constraints.LambdaReadAccessor)2 TemporalConstraintEvaluator (org.drools.modelcompiler.constraints.TemporalConstraintEvaluator)2 MultiAccumulate (org.drools.core.rule.MultiAccumulate)1 QueryNameConstraint (org.drools.core.rule.constraint.QueryNameConstraint)1 Constraint (org.drools.model.Constraint)1 EntryPoint (org.drools.model.EntryPoint)1 FlowDSL.entryPoint (org.drools.model.FlowDSL.entryPoint)1