[rules-users] How to use type-safe enums in rule condition

Wolfgang Laun wolfgang.laun at gmail.com
Tue Nov 12 07:23:44 EST 2013


Using the same name for a fact class Country and an enum Country
will most certainly not be possible unless you use full class names for
disambiguation.

And, of course, the name property of fact class Country would have to
have the enum type.

-W


On 12/11/2013, Stephen Masters <stephen.masters at me.com> wrote:
> That rule would work if you added a “name” property to Country.
>
> As a general tip on working with countries, always work with their ISO
> codes, not their names.
>
> http://en.wikipedia.org/wiki/ISO_3166-1
>
> … ideally the 2-char codes, which are much more commonly used than the
> 3-char codes.
>
> Feel free to steal this:
> https://github.com/gratiartis/sctrcd-payment-validation-web/blob/master/src/main/java/com/sctrcd/payments/enums/CountryEnum.java
>
> … which sets up all countries with ISO codes and full-text names as enums.
> Or at least all countries which were included in the ISO spec at the time of
> writing.
>
> Grab it while you can - I’m in the middle of refactoring it into a JPA
> entity. :)
>
> Steve
>
>
> On 12 Nov 2013, at 10:20, droolster <quant.coder at gmail.com> wrote:
>
>> Hello,
>>
>> I would like to use type-safe enums in my rules. At the moment I have
>> situation like this:
>>
>> public class Country {
>>    private String name;
>>
>>    // getter/setters
>> }
>>
>> rule "Country"
>>    when
>>        Country ( name == "USA" )
>>    then
>>        DO SOMETHING
>> end
>>
>> I would like to have something like this:
>>
>> public enum Country {
>>    USA ()
>>    // list of all countries in the world
>> }
>>
>> and in the rule I would like to do this:
>>
>> rule "Country"
>>    when
>>        Country ( name == Country.USA )
>>    then
>>        DO SOMETHING
>> end
>>
>> Is that possible?
>>
>> Thanks in advance for your help.
>>
>>
>>
>>
>>
>>
>> --
>> View this message in context:
>> http://drools.46999.n3.nabble.com/How-to-use-type-safe-enums-in-rule-condition-tp4026739.html
>> Sent from the Drools: User forum mailing list archive at Nabble.com.
>> _______________________________________________
>> rules-users mailing list
>> rules-users at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/rules-users
>
>



More information about the rules-users mailing list