Search code examples
javajbossdrools

Drool Condition doesn't match


I am writing currently trying out an example on Drools in which i am trying to check a perticular condition based on less than and equal to operator.

My drools .drl file is :

//created on: May 7, 2015
package inlife.rules.controller

//list any import classes here.
import inlife.rules.model.StudentRuleBean;

//declare any global variables here

rule "Check if Student age is below limit"

    when
        //conditions
        o: StudentRuleBean (studentName == "Peter" && age < 5 && age > 18)
    then
        //actions
        o.setStudentEligible("false");

end

rule "Check for Range"

when
        //conditions
        o: StudentRuleBean (studentName == "Peter" && age > 5 && age <= 18)
    then
        //actions
        o.setStudentEligible("true");

end

Now in the above .drl fle i am trying to check if the student name is Peter and its age is greater than five and less than 18 than he is eligible to goto school else he is not..

But my condition is not getting satisfied. I guess i am doing something wrong in my drool file.

Below is my Java Bean Class

public class StudentRuleBean {

    private String studentName;
    private double age;
    private String studentEligible;

    public String getStudentName() {
        return studentName;
    }
    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }
    public double getAge() {
        return age;
    }
    public void setAge(double age) {
        this.age = age;
    }
    public String getStudentEligible() {
        return studentEligible;
    }
    public void setStudentEligible(String studentEligible) {
        this.studentEligible = studentEligible;
    }

}

Badly stuck.. Need help on this.

Looking forward to ypur answers. Thanks in advance.


Solution

  • As I know, all the items in the predicate are logical AND. Then you don't need to type && for each conditions. Separate them by coma instead.

            o: StudentRuleBean (studentName == "Peter" , age < 5 && age > 18)
    

    The documentations says :

    Although the && and , operators have the same semantics, they are resolved with different priorities: The && operator precedes the || operator. Both the && and || operator precede the , operator. See the operator precedence list below.

    The comma operator should be preferred at the top level constraint, as it makes constraints easier to read and the engine will often be able to optimize them better.

    copy paste from https://docs.jboss.org/drools/release/5.2.0.Final/drools-expert-docs/html/ch05.html#d0e3962 (5.8.3.3.4. Comma separated AND)