2
votes

Trying to implement Drools rules into my project and I am trying to convert lot of if-else into drools rules, but I am not sure how to put else condition in drools. My scenario is like this:

if(value.equals("java.lang.Integer")) {
   //Put value into pojo
} else if(value.equals("java.lang.Float")) {

} 
// Similar if-else for other data type
else {
  //Do some action (if it doesn't satisfy all other condition)
}

My current content of drl file is like this:

rule "When Method Parameter is java.lang.Integer"
   when
       object.getValue().eqals("java.lang.Integer")
   then
       #Set value to another pojo
end

rule "When Method Parameter is java.lang.Float"
   when
       object.getValue().eqals("java.lang.Float")
   then
       #Set value to another pojo
end

rule "Rules if all of the above condition fails"
    # is it possible to have rule just like else, if all
    # of above condition fails, this rule should execute
end

Now I want an conditon, which should be executed if all of the above condition fails. Is this possible to do this using drools? That else part should execute if all of the above condition fails, I tried search and found out currently drools doesn't support else conditon. If this is the case how do I implement this type of scenario? Can anyone please guide me since I am beginner to drools.

1

1 Answers

0
votes

The closest way to model it is probably an activation group. With an activation group, only one rule within that group will fire. So by creating an additional rule with lower salience, which doesn't constrain on the value, it will fire if none of the other rules in the group fire.

You can also create a rule which matches on the negation of the patterns used by the other rules, or you could just have one rule with an if-then-else on the RHS. You shouldn't rule that last one out just because it doesn't feel 'pure'. Depending on your circumstances, it may just be the cleanest way to do it.