Yep. See attached project. It actually simplifies the java a bit, as now there
doesn't need to be any trickery in the contains method for Query. The rule now uses a
from clause to get the Criteria directly from the Query:
rule "Match"
when
d : Data()
q : Query( size <= d.size )
$total : Number( intValue == q.size )
from accumulate( Criteria( this memberOf d ) from q, count(1) )
then
System.out.println("Match: " + d + " and " + q) ;
end
Try the other methods, though. (The eval 'hack' I included before, and
Wolfgang's method.) All of these approaches should have different performance
behavior, and I honestly don't know which one is fastest.
--- On Sun, 11/8/09, Wishing Carebear <wishing.carebear(a)gmail.com> wrote:
From: Wishing Carebear <wishing.carebear(a)gmail.com>
Subject: Re: [rules-users] some pointers for solution
To: "Rules Users List" <rules-users(a)lists.jboss.org>
Date: Sunday, November 8, 2009, 8:02 PM
Yes Edson, I tried with count and sum.
Both seems to be working fine.
But with the test case, the Criteria for query also
needs to be inserted in addition to the query and data
objects like shown below:
Query query = new Query(1);
query.add(new
Criteria(query, "c2", "bas"));
ksession.insert(query);
for(Criteria c :
query) {
ksession.insert(c);
}
Is it possible to avoid them.
Thanks,
cabear
2009/11/8 Edson Tirelli <ed.tirelli(a)gmail.com>
Why not use count() accumulate function? ;)
from accumulate( Criteria( this memberOf d, this memberOf q
),
count(1) )
Edson
2009/11/8 Greg Barton <greg_barton(a)yahoo.com>
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:
# Find Criteria that are contained in both the Data and
Query
from accumulate( Criteria( this memberOf d, this
memberOf q ),
# Initialize the counter to 0
init( int total = 0; ),
# Increment when the above condition is matched
action( total ++; ),
# Decrement if a matched Criteria now fails to match
reverse( total --; ),
# return the total when all known Criteria are matched
result( total ) )
--- On Sun, 11/8/09, Wishing Carebear <wishing.carebear(a)gmail.com>
wrote:
> From: Wishing Carebear <wishing.carebear(a)gmail.com>
> Subject: Re: [rules-users] some pointers for
solution
> To: "Rules Users List" <rules-users(a)lists.jboss.org>
> Date: Sunday, November 8, 2009, 11:39 AM
> Hi Greg:
> I'm trying to understand your first
> solution.
>
> Ran the project and it works fine. If possible could
> you explain me little bit on :
>
> from accumulate( Criteria( this memberOf d,
> this memberOf q ),
> init( int total = 0; ),
> action( total ++; ),
> reverse( total --; ),
> result( total ) )
>
>
> Thanks,
> cabear
>
>
> 2009/11/8 Greg Barton <greg_barton(a)yahoo.com>
>
> 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(a)gmail.com>
> wrote:
>
> > From: Wishing Carebear <wishing.carebear(a)gmail.com>
>
> > Subject: [rules-users] some pointers for
solution
> > To: rules-users(a)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(a)lists.jboss.org
> >
https://lists.jboss.org/mailman/listinfo/rules-users
>
> >
>
>
>
> _______________________________________________
> rules-users mailing list
> rules-users(a)lists.jboss.org
>
https://lists.jboss.org/mailman/listinfo/rules-users
>
>
>
>
>
> -----Inline Attachment Follows-----
>
> _______________________________________________
> rules-users mailing list
> rules-users(a)lists.jboss.org
>
https://lists.jboss.org/mailman/listinfo/rules-users
>
_______________________________________________
rules-users mailing list
rules-users(a)lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users
--
Edson Tirelli
JBoss Drools Core Development
JBoss by Red Hat @
www.jboss.com
_______________________________________________
rules-users mailing list
rules-users(a)lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users
-----Inline Attachment Follows-----
_______________________________________________
rules-users mailing list
rules-users(a)lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users