A-HA!
Thank you!
I figured I was making things overly complicated.
:-)
-Allen
________________________________
From: rules-users-bounces(a)lists.jboss.org [mailto:rules-users-bounces@lists.jboss.org] On
Behalf Of Edson Tirelli
Sent: Friday, July 18, 2008 8:55 AM
To: Rules Users List
Subject: Re: [rules-users] Clarification of using collections as facts
Hi all, sorry for the long absence. I'm back, but I know no one noticed, anyway...
;)
Allen, not really. Collections usually are not asserted as standalone facts because
they lack semantic meaning. They are abstractions, if you know what I mean.
So, in your case, you can write patterns like this:
Foo( names contains "abc" )
Foo( names not contains "abc" )
Now, a regexp will act over Strings, not collections. So if you want to write a rule
that acts on each name that matches a regexp, you must "iterate" over your
collections elements using the "from" CE:
Foo( $names : names )
String( this matches "error.*" ) from $names
Now, if you want the rule to re-create a new list containing only the names matching a
pattern, THEN you may use the collect CE to create a collection:
$foo : Foo( $names : names )
$foundThese : ArrayList( ) from collect( String( this matches "error.*" ) from
$names )
You may also filter your resulting collection to only fire the rule when there are
actual elements. Just for the sake of examples, since you are already binding $foo, you
don't need to bind $names too:
$foo : Foo( )
$foundThese : ArrayList( size > 0 ) from collect( String( this matches
"error.*" ) from $foo.names )
Happy drooling,
Edson
2008/7/15 Bagwell, Allen F <afbagwe@sandia.gov<mailto:afbagwe@sandia.gov>>:
I'm going through the Drools documentation regarding the use of lists and other
collections. I want to make sure my understanding is correct.
So if I have a class:
public class Foo {
private int x;
private int y;
private List<String> names = ArrayList<String>();
public List<String> getNames() {return names;}
// appropriate getters/setters for the int fields ...
}
With this I can insert a Foo object into working memory. I can even make the int fields
dynamic facts with the appropriate addition of "bean-ifying" code.
For the List, however, I need to insert it separately into working memory in order to make
use of Drools' rule language ('contains' ,etc.)? I'm assuming that
doesn't come free because I have to insert my own custom class objects if they are
included via composition in a larger fact!
Also, I'm guessing the Collection type classes cannot be dynamic facts? Meaning if I
modify my List with add, remove, clear, etc., I must explicitly call update in my code on
the List fact in order to alert Drools to a change in the List, correct?
So then the question also comes up as to how I would write a rule that looks for all
instances of Strings in the List that match against a given regex expression. I've not
been successful at figuring this out...
Rule "when a Foo List is modified, get all Strings in it that start with
'error'"
when
$foo : Foo($names : names)
$foundThese : ArrayList() from collect(??? matches "error.*" from $names)
then
// act on $foo and $foundThese
I know that rule is NOT correct, but I'm not sure how I'm supposed to access an
immutable object inside a collection. The examples in the documentation show how to get at
mutable class objects.
Thanks!
-Allen
_______________________________________________
rules-users mailing list
rules-users@lists.jboss.org<mailto:rules-users@lists.jboss.org>
https://lists.jboss.org/mailman/listinfo/rules-users
--
Edson Tirelli
JBoss Drools Core Development
JBoss, a division of Red Hat @
www.jboss.com<http://www.jboss.com>