[rules-users] some pointers for solution

Greg Barton greg_barton at yahoo.com
Sun Nov 8 04:08:22 EST 2009


There are a couple of ways to do this.  I'm sure there's a bit more clean way than the example I'm providing, but this should get you in the right direction.  It's not 100% rules, because it involves a bit of java collections trickery. (See attached project, collection_DroolsCriteriaMatch.tar.gz)

The heart of it is a single rule:

rule "Match"
  when
    d : Data()
    q : Query( size <= d.size )
    Number( intValue == q.size ) 
    from accumulate( 
      Criteria( this memberOf d, this memberOf q ),
      init( int total = 0; ),
      action( total ++; ),
      reverse( total --; ),
      result( total ) 
    )
  then
    System.out.println("Match: " + d + " and " + q) ;
end

The Data object holds data to be queried, Query objects are asserted to match the Data, and Criteria objects can be contained in either. (With the aforementioned collections trickery that if a Criteria is contained in a Query it can be found in a Data object, but the reverse isn't true.  See the Query.contains(Object) method for how that's implemented.)

So the rule above basically says "There's a Data object, and all of the Query objects Criteria are contained in the Data object."

There's an alternate way of doing this using eval and a bit more java fu.  See the eval_DroolsCriteriaMatch.tar.gz project attached.  This one's probably not optimal, though, as it's basically a brute force check of all Data objects against the asserted Query.

I tried for a while to get a solution working with different criteria types from both Data and Query objects being asserted into working memory, but I couldn't get the accumulate syntax right.  Anyone know of a way to do that? (I figure that would get a "pure rules" solution.)

--- On Sat, 11/7/09, Wishing Carebear <wishing.carebear at gmail.com> wrote:

> From: Wishing Carebear <wishing.carebear at gmail.com>
> Subject: [rules-users] some pointers for solution
> To: rules-users at lists.jboss.org
> Date: Saturday, November 7, 2009, 10:19 PM
> Hello:
> There are n selection criteria from s1 .. sn for each
> item i1.. in. Each item can have a subset of criteria which
> applies to them.
>  
> The end user, can choose a subset of criteria like c1
> and c5 and only the item that has c1 and c5 valid should be
> returned. For example: if item i1 and i2 have criterias
> valid for c1, c2, c5, c6, c8 since the request is only for
> criteria c1 and c5, i1 and i2 must be returned.
> 
>  
> Is it possible to write a rule using drools for this
> requirement. 
>  
> Thanks for your help and time,
> cabear
> 
> -----Inline Attachment Follows-----
> 
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>


      
-------------- next part --------------
A non-text attachment was scrubbed...
Name: collection_DroolsCriteriaMatch.tar.gz
Type: application/x-gzip
Size: 5760 bytes
Desc: not available
Url : http://lists.jboss.org/pipermail/rules-users/attachments/20091108/f76ced18/attachment.gz 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: eval_DroolsCriteriaMatch.tar.gz
Type: application/x-gzip
Size: 4837 bytes
Desc: not available
Url : http://lists.jboss.org/pipermail/rules-users/attachments/20091108/f76ced18/attachment-0001.gz 


More information about the rules-users mailing list