Search in sources :

Example 1 with SingleConstraint

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

the class BruteForceEngine method match.

private boolean match(Constraint constraint, TupleHandle tuple) {
    switch(constraint.getType()) {
        case SINGLE:
            SingleConstraint singleCon = (SingleConstraint) constraint;
            Variable[] vars = singleCon.getVariables();
            switch(vars.length) {
                case 0:
                    try {
                        return singleCon.getPredicate().test();
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                case 1:
                    Object obj = tuple.get(vars[0]);
                    try {
                        return singleCon.getPredicate().test(obj);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                case 2:
                    Object obj1 = tuple.get(vars[0]);
                    Object obj2 = tuple.get(vars[1]);
                    try {
                        return singleCon.getPredicate().test(obj1, obj2);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
            }
        case AND:
            AndConstraints andCon = (AndConstraints) constraint;
            return andCon.getChildren().stream().allMatch(con -> match(con, tuple));
        case OR:
            OrConstraints orCon = (OrConstraints) constraint;
            return orCon.getChildren().stream().anyMatch(con -> match(con, tuple));
    }
    return false;
}
Also used : Variable(org.drools.model.Variable) SingleConstraint(org.drools.model.SingleConstraint) AndConstraints(org.drools.model.constraints.AndConstraints) OrConstraints(org.drools.model.constraints.OrConstraints)

Example 2 with SingleConstraint

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

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

the class KiePackagesBuilder method createWindowReference.

private <T> void createWindowReference(RuleContext ctx, WindowReference<T> window) {
    WindowDeclaration windowDeclaration = new WindowDeclaration(window.getName(), ctx.getPkg().getName());
    Variable<T> variable = declarationOf(window.getPatternType());
    Pattern windowPattern = new Pattern(0, getClassObjectType(window.getPatternType()), variable.getName());
    windowDeclaration.setPattern(windowPattern);
    for (Predicate1<T> predicate : window.getPredicates()) {
        SingleConstraint singleConstraint = new SingleConstraint1<>(generateName("expr"), variable, predicate);
        ConstraintEvaluator constraintEvaluator = new ConstraintEvaluator(windowPattern, singleConstraint);
        windowPattern.addConstraint(new LambdaConstraint(constraintEvaluator));
    }
    windowPattern.addBehavior(createWindow(window));
    ctx.getPkg().addWindowDeclaration(windowDeclaration);
}
Also used : QueryCallPattern(org.drools.model.patterns.QueryCallPattern) AccumulatePattern(org.drools.model.AccumulatePattern) Pattern(org.drools.core.rule.Pattern) SingleConstraint1(org.drools.model.constraints.SingleConstraint1) SingleConstraint(org.drools.model.SingleConstraint) WindowDeclaration(org.drools.core.rule.WindowDeclaration) LambdaConstraint(org.drools.modelcompiler.constraints.LambdaConstraint) TemporalConstraintEvaluator(org.drools.modelcompiler.constraints.TemporalConstraintEvaluator) ConstraintEvaluator(org.drools.modelcompiler.constraints.ConstraintEvaluator)

Aggregations

SingleConstraint (org.drools.model.SingleConstraint)3 WindowDeclaration (org.drools.core.rule.WindowDeclaration)2 Variable (org.drools.model.Variable)2 ConstraintEvaluator (org.drools.modelcompiler.constraints.ConstraintEvaluator)2 LambdaConstraint (org.drools.modelcompiler.constraints.LambdaConstraint)2 TemporalConstraintEvaluator (org.drools.modelcompiler.constraints.TemporalConstraintEvaluator)2 Declaration (org.drools.core.rule.Declaration)1 MultiAccumulate (org.drools.core.rule.MultiAccumulate)1 Pattern (org.drools.core.rule.Pattern)1 TypeDeclaration (org.drools.core.rule.TypeDeclaration)1 QueryNameConstraint (org.drools.core.rule.constraint.QueryNameConstraint)1 AccumulatePattern (org.drools.model.AccumulatePattern)1 Constraint (org.drools.model.Constraint)1 EntryPoint (org.drools.model.EntryPoint)1 FlowDSL.entryPoint (org.drools.model.FlowDSL.entryPoint)1 PrototypeVariable (org.drools.model.PrototypeVariable)1 AndConstraints (org.drools.model.constraints.AndConstraints)1 OrConstraints (org.drools.model.constraints.OrConstraints)1 SingleConstraint1 (org.drools.model.constraints.SingleConstraint1)1 QueryCallPattern (org.drools.model.patterns.QueryCallPattern)1