简体   繁体   中英

Else Condition in Drools

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.

There are variations to the design pattern I call "classification". See my paper on Design Patterns, https://engage.redhat.com/forms/rule-design-patterns

Since the rules - as you have shown them - are syntactically absolutely incorrect, I don't want to furnish a recommendation without knowing what really needs to be done - I can imagine several options, even down to a couple of rules.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM