Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drools: How to use an enum in the lhs of a rule?

Tags:

java

drools

I am having difficulties in writing a rule which match with an enum value in its lhs.

For example, if I have the following enum:

public enum EStatus {
  OK,
  NOT_OK
}

I would like to use it in something like this:

rule "my rule"
dialect "java"
    when        
        status : EStatus()                      // --> this works, but I want to be more specific
        // status : EStatus(this == EStatus.OK) // --> doesn't work. How can I make it work?
    then
        // ...
end

Is this even possible in Drools? I use version 5.1.1.

like image 647
Calin Avatar asked Nov 21 '11 12:11

Calin


People also ask

How do you use enums correctly?

You should always use enums when a variable (especially a method parameter) can only take one out of a small set of possible values. Examples would be things like type constants (contract status: “permanent”, “temp”, “apprentice”), or flags (“execute now”, “defer execution”).

Can we sort enum?

If you just create a list of the enum values (instead of strings) via parsing, then sort that list using Collections. sort , it should sort the way you want. If you need a list of strings again, you can just convert back by calling name() on each element.


1 Answers

This works for me:

rule "my rule"
when
    Ticket(status == EStatus.OK)
then
    ...
end

so that should work too:

rule "my rule"
when
    EStatus(this == EStatus.OK)
then
    ...
end

Verify if it still occurs in Drools 5.3 and file a bug if it does in jira

like image 93
Geoffrey De Smet Avatar answered Oct 05 '22 23:10

Geoffrey De Smet