Quantcast

use of enum with "in" on LHS

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

use of enum with "in" on LHS

dsykes
Hi there,

I have a rule where I would like human-readable rule to have a comma separated list of enum constants in it, however I can't find a way to get this working.

DSLR file:

import com.test.Report;
import com.test.RulesResult;
import com.test.State;

rule "Test rule"
    when
        the received object
        - state is NEW, MODIFY
    then
        #unimportant
end

DSL file:

[*][when]the received object=result : RulesResult();$stm : Report()
[*][when]- state is {stateList}=state in (State.{stateList})

Class Report has a property called state which is an enum of type State which has a number of constants including NEW and MODIFY.

When I run this I get:

org.drools.RuntimeDroolsException: Exception executing predicate ( state == State.NEW || state == MODIFY)

As you can see only the first constant is being qualified with the enum class.  Can anyone help me with how to do this properly?

Thanks,
Derek
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: use of enum with "in" on LHS

FrankVhh
Hi,

Unless there is another workaround, I think you would have to do it something like this:

[*][when]the received object=result : RulesResult();$stm : Report()
[*][keyword]NEW=State.NEW
[*][keyword]OLD=State.OLD
[*][when]- state is {stateList}=state in ({stateList})

Regards,
Frank
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [rules-users] use of enum with "in" on LHS

laune
In reply to this post by dsykes
Regrettably, this is just a string substitution, not caring what it
is, or what comes before or after.
-W

On 16/05/2012, dsykes <[hidden email]> wrote:

> Hi there,
>
> I have a rule where I would like human-readable rule to have a comma
> separated list of enum constants in it, however I can't find a way to get
> this working.
>
> DSLR file:
>
> import com.test.Report;
> import com.test.RulesResult;
> import com.test.State;
>
> rule "Test rule"
>     when
>         the received object
>         - state is NEW, MODIFY
>     then
>         #unimportant
> end
>
> DSL file:
>
> [*][when]the received object=result : RulesResult();$stm : Report()
> [*][when]- state is {stateList}=state in (State.{stateList})
>
> Class Report has a property called state which is an enum of type State
> which has a number of constants including NEW and MODIFY.
>
> When I run this I get:
>
> org.drools.RuntimeDroolsException: Exception executing predicate ( state ==
> State.NEW || state == MODIFY)
>
> As you can see only the first constant is being qualified with the enum
> class.  Can anyone help me with how to do this properly?
>
> Thanks,
> Derek
>
> --
> View this message in context:
> http://drools.46999.n3.nabble.com/use-of-enum-with-in-on-LHS-tp3997081.html
> Sent from the Drools: User forum mailing list archive at Nabble.com.
> _______________________________________________
> rules-users mailing list
> [hidden email]
> https://lists.jboss.org/mailman/listinfo/rules-users
>
_______________________________________________
rules-users mailing list
[hidden email]
https://lists.jboss.org/mailman/listinfo/rules-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [rules-users] use of enum with "in" on LHS

laune
In reply to this post by FrankVhh
But take care, this replaces *all* occurrences of the words NEW and OLD.
-W

On 16/05/2012, FrankVhh <[hidden email]> wrote:

> Hi,
>
> Unless there is another workaround, I think you would have to do it
> something like this:
>
> [*][when]the received object=result : RulesResult();$stm : Report()
> [*][keyword]NEW=State.NEW
> [*][keyword]OLD=State.OLD
> [*][when]- state is {stateList}=state in ({stateList})
>
> Regards,
> Frank
>
> --
> View this message in context:
> http://drools.46999.n3.nabble.com/use-of-enum-with-in-on-LHS-tp3997081p3997136.html
> Sent from the Drools: User forum mailing list archive at Nabble.com.
> _______________________________________________
> rules-users mailing list
> [hidden email]
> https://lists.jboss.org/mailman/listinfo/rules-users
>
_______________________________________________
rules-users mailing list
[hidden email]
https://lists.jboss.org/mailman/listinfo/rules-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: use of enum with "in" on LHS

dsykes
In reply to this post by FrankVhh
Hey Frank,

It's not perfect but it should serve the purpose and is far preferable to the alternatives I was considering.  Thanks very much!

Cheers,
Derek
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [rules-users] use of enum with "in" on LHS

Vincent Legendre
In reply to this post by dsykes
Another potentiel trick :

In State enum, add a method to build a State list from a list of string
enum State {
    ...
    static List<State> buildList(String listAsStr) {
        // well, don't need to detail that ...
        // ... but think to keep a cache somewhere to avoid unecessary iterations at each rule's evaluation ...
    }
}

and in your DSL :
[*][when]- state is {stateList}=state in (State.buildList({stateList}))


Don't know if it really works, just an idea that could be better than replacing all NEW/OLD strings ...
If ot works, it can even be better to handle weird items list in a big string ...
_______________________________________________
rules-users mailing list
[hidden email]
https://lists.jboss.org/mailman/listinfo/rules-users
Loading...