<br> Why not use count() accumulate function? ;)<br><br>from accumulate( Criteria( this memberOf d, this memberOf q ),<br> count(1) )<br><br> Edson<br><br><div class="gmail_quote">2009/11/8 Greg Barton <span dir="ltr"><<a href="mailto:greg_barton@yahoo.com">greg_barton@yahoo.com</a>></span><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">In this case the accumulate clause is maintaining a counter (total) that's incremented whenever a Criteria is detected that is contained in both the Data and Query object matched in the rule. So:<br>
<br>
# Find Criteria that are contained in both the Data and Query<br>
<div class="im">from accumulate( Criteria( this memberOf d, this memberOf q ),<br>
</div># Initialize the counter to 0<br>
init( int total = 0; ),<br>
# Increment when the above condition is matched<br>
action( total ++; ),<br>
# Decrement if a matched Criteria now fails to match<br>
reverse( total --; ),<br>
# return the total when all known Criteria are matched<br>
result( total ) )<br>
<div class="im"><br>
--- On Sun, 11/8/09, Wishing Carebear <<a href="mailto:wishing.carebear@gmail.com">wishing.carebear@gmail.com</a>> wrote:<br>
<br>
> From: Wishing Carebear <<a href="mailto:wishing.carebear@gmail.com">wishing.carebear@gmail.com</a>><br>
</div>> Subject: Re: [rules-users] some pointers for solution<br>
> To: "Rules Users List" <<a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a>><br>
> Date: Sunday, November 8, 2009, 11:39 AM<br>
<div><div></div><div class="h5">> Hi Greg:<br>
> I'm trying to understand your first<br>
> solution.<br>
> <br>
> Ran the project and it works fine. If possible could<br>
> you explain me little bit on :<br>
><br>
> from accumulate( Criteria( this memberOf d,<br>
> this memberOf q ),<br>
> init( int total = 0; ),<br>
> action( total ++; ),<br>
> reverse( total --; ),<br>
> result( total ) )<br>
> <br>
> <br>
> Thanks,<br>
> cabear<br>
><br>
><br>
> 2009/11/8 Greg Barton <<a href="mailto:greg_barton@yahoo.com">greg_barton@yahoo.com</a>><br>
><br>
> There are a couple of<br>
> ways to do this. I'm sure there's a bit more clean<br>
> way than the example I'm providing, but this should get<br>
> you in the right direction. It's not 100% rules,<br>
> because it involves a bit of java collections trickery. (See<br>
> attached project, collection_DroolsCriteriaMatch.tar.gz)<br>
><br>
><br>
> The heart of it is a single rule:<br>
><br>
> rule "Match"<br>
> when<br>
> d : Data()<br>
> q : Query( size <= d.size )<br>
> Number( intValue == q.size )<br>
> from accumulate(<br>
> Criteria( this memberOf d, this memberOf q ),<br>
><br>
> init( int total = 0; ),<br>
> action( total ++; ),<br>
> reverse( total --; ),<br>
> result( total )<br>
> )<br>
> then<br>
> System.out.println("Match: " + d + "<br>
> and " + q) ;<br>
> end<br>
><br>
> The Data object holds data to be queried, Query objects are<br>
> asserted to match the Data, and Criteria objects can be<br>
> contained in either. (With the aforementioned collections<br>
> trickery that if a Criteria is contained in a Query it can<br>
> be found in a Data object, but the reverse isn't true.<br>
> See the Query.contains(Object) method for how that's<br>
> implemented.)<br>
><br>
><br>
> So the rule above basically says "There's a Data<br>
> object, and all of the Query objects Criteria are contained<br>
> in the Data object."<br>
><br>
> There's an alternate way of doing this using eval and a<br>
> bit more java fu. See the eval_DroolsCriteriaMatch.tar.gz<br>
> project attached. This one's probably not optimal,<br>
> though, as it's basically a brute force check of all<br>
> Data objects against the asserted Query.<br>
><br>
><br>
> I tried for a while to get a solution working with<br>
> different criteria types from both Data and Query objects<br>
> being asserted into working memory, but I couldn't get<br>
> the accumulate syntax right. Anyone know of a way to do<br>
> that? (I figure that would get a "pure rules"<br>
> solution.)<br>
><br>
><br>
> --- On Sat, 11/7/09, Wishing Carebear <<a href="mailto:wishing.carebear@gmail.com">wishing.carebear@gmail.com</a>><br>
> wrote:<br>
><br>
> > From: Wishing Carebear <<a href="mailto:wishing.carebear@gmail.com">wishing.carebear@gmail.com</a>><br>
><br>
> > Subject: [rules-users] some pointers for solution<br>
> > To: <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
> > Date: Saturday, November 7, 2009, 10:19 PM<br>
><br>
><br>
><br>
> > Hello:<br>
> > There are n selection criteria from s1 .. sn for each<br>
> > item i1.. in. Each item can have a subset of criteria<br>
> which<br>
> > applies to them.<br>
> > <br>
> > The end user, can choose a subset of criteria like c1<br>
><br>
> > and c5 and only the item that has c1 and c5 valid<br>
> should be<br>
> > returned. For example: if item i1 and i2 have<br>
> criterias<br>
> > valid for c1, c2, c5, c6, c8 since the request is only<br>
> for<br>
> > criteria c1 and c5, i1 and i2 must be returned.<br>
><br>
> ><br>
> > <br>
> > Is it possible to write a rule using drools for this<br>
> > requirement.<br>
> > <br>
> > Thanks for your help and time,<br>
> > cabear<br>
> ><br>
> > -----Inline Attachment Follows-----<br>
><br>
> ><br>
> > _______________________________________________<br>
> > rules-users mailing list<br>
> > <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
> > <a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
><br>
> ><br>
><br>
><br>
> <br>
> _______________________________________________<br>
> rules-users mailing list<br>
> <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
> <a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
><br>
><br>
><br>
><br>
><br>
> -----Inline Attachment Follows-----<br>
><br>
> _______________________________________________<br>
> rules-users mailing list<br>
> <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
> <a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
><br>
<br>
<br>
<br>
<br>
_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br> Edson Tirelli<br> JBoss Drools Core Development<br> JBoss by Red Hat @ <a href="http://www.jboss.com">www.jboss.com</a><br>