[rules-users] Filtering inside the LHS

Mark Proctor mproctor at codehaus.org
Thu Feb 23 01:30:05 EST 2012


On 23/02/2012 03:38, Hezi Stern wrote:
>
> Hi All,
>
> I am seeking some kind of filtering (if statement) inside the LHS.
>
> I have the following example rule (note: $attributeFilteredSentences 
> and $cmdescriptor are defined in the extends rule which is not presented):
>
> *rule*" choose message sentence example"extends " 
> attributeFilteredSentences  collection"
>
> *when*
>
>                               SContextState(state == SContextState.ONE)
>
>             $sentences:       ArrayList()
>
> *from**collect*(Sentences.Sentence(
>
>                                                                   
>    descriptor.meanings.meaning == $cmdescriptor.meanings.meaning
>
>                                                                      ) 
> *from*  $attributeFilteredSentences)
>
> *then*
>
>             logger.info("RULE: entered rule: 
> "+drools.getRule().getName());
>
> //some logic...
>
> *end*
>
> **
>
> as can be seen I am filtering sentences based on the meanings, and the 
> meaning collection must be identical as in $cmdescriptor (this is the 
> case when SContextState.ONE)
>
> Now to my question: I have cases ((this is the case when 
> SContextState.TWO) where I do not need exact validation but rather 
> superset of (for this I have a supersetOf operator implemented)
>
> I would like to avoid duplicating  this rule (as I have similar 
> condition in many other rules) and being able to filter once with "==" 
> and once with "supersetOf" depending on the SContextState.
>
I have found that query's can be used to provide re-usable rule 
fragments. In the example below I could have inlined the intentory LHS 
in the updateInventory rule, but instead I separated it out into a query 
so that multiple rules can re-use it. There is a small performance 
overhead from using queries, compared to normal patterns, but in most 
cases this is compensated by more maintainable and readable code.

query inventory(Character $char, List $inventory)
     $char := Character()
     $inventory := List() from accumulate( Holding( $char, $thing; ),
                                           collectList( $thing ) )
end

rule updateInventory salience 5 when
     SessionCharacter( $char : character, $us : session )
     inventory( $char, $things; )
then
     $us.channels["inventory"].send( $things );
end


Mark
>
> Any ideas how to do this?
>
> Thanks,
>
> Hezi
>
>
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20120223/273ebb73/attachment.html 


More information about the rules-users mailing list