Search in sources :

Example 1 with AndConstraints

use of org.drools.model.constraints.AndConstraints 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)

Aggregations

SingleConstraint (org.drools.model.SingleConstraint)1 Variable (org.drools.model.Variable)1 AndConstraints (org.drools.model.constraints.AndConstraints)1 OrConstraints (org.drools.model.constraints.OrConstraints)1