<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=us-ascii">
<META content="MSHTML 6.00.2900.3354" name=GENERATOR></HEAD>
<BODY>
<DIV dir=ltr align=left><SPAN class=622012218-21072008><FONT face=Arial
color=#0000ff size=2>A-HA!</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=622012218-21072008><FONT face=Arial
color=#0000ff size=2></FONT></SPAN> </DIV>
<DIV dir=ltr align=left><SPAN class=622012218-21072008><FONT face=Arial
color=#0000ff size=2>Thank you!</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=622012218-21072008><FONT face=Arial
color=#0000ff size=2></FONT></SPAN> </DIV>
<DIV dir=ltr align=left><SPAN class=622012218-21072008><FONT face=Arial
color=#0000ff size=2>I figured I was making things overly
complicated.</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=622012218-21072008><FONT face=Arial
color=#0000ff size=2></FONT></SPAN> </DIV>
<DIV dir=ltr align=left><SPAN class=622012218-21072008><FONT face=Arial
color=#0000ff size=2>:-)</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=622012218-21072008><FONT face=Arial
color=#0000ff size=2>-Allen</FONT></SPAN></DIV><BR>
<DIV class=OutlookMessageHeader lang=en-us dir=ltr align=left>
<HR tabIndex=-1>
<FONT face=Tahoma size=2><B>From:</B> rules-users-bounces@lists.jboss.org
[mailto:rules-users-bounces@lists.jboss.org] <B>On Behalf Of </B>Edson
Tirelli<BR><B>Sent:</B> Friday, July 18, 2008 8:55 AM<BR><B>To:</B> Rules Users
List<BR><B>Subject:</B> Re: [rules-users] Clarification of using collections as
facts<BR></FONT><BR></DIV>
<DIV></DIV>
<DIV dir=ltr><BR> Hi all, sorry for the long absence. I'm back, but
I know no one noticed, anyway... ;)<BR><BR> 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.<BR><BR> So, in your case, you can write patterns like
this:<BR><BR>Foo( names contains "abc" )<BR>Foo( names not contains "abc"
)<BR><BR> 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:<BR><BR>Foo( $names : names )<BR>String( this matches "error.*" ) from
$names<BR><BR> 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:<BR><BR><FONT face="Arial, sans-serif" size=3><FONT
face="Courier New, monospace" size=2>$foo : </FONT></FONT>Foo( $names : names
)<BR><FONT face="Arial, sans-serif" size=3><FONT face="Courier New, monospace"
size=2>$foundThese : </FONT></FONT>ArrayList( ) from collect( String( this
matches "error.*" ) from $names )<BR><BR> 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:<BR><BR><FONT face="Arial, sans-serif" size=3><FONT
face="Courier New, monospace" size=2>$foo : </FONT></FONT>Foo( )<BR><FONT
face="Arial, sans-serif" size=3><FONT face="Courier New, monospace"
size=2>$foundThese : </FONT></FONT>ArrayList( size > 0 ) from collect(
String( this matches "error.*" ) from $foo.names )<BR><BR>
Happy drooling,<BR>
Edson<BR><BR><BR>
<DIV class=gmail_quote>2008/7/15 Bagwell, Allen F <<A
href="mailto:afbagwe@sandia.gov">afbagwe@sandia.gov</A>>:<BR>
<BLOCKQUOTE class=gmail_quote
style="PADDING-LEFT: 1ex; MARGIN: 0pt 0pt 0pt 0.8ex; BORDER-LEFT: rgb(204,204,204) 1px solid">
<DIV><FONT face="Arial, sans-serif" size=3>
<DIV> </DIV>
<DIV><FONT size=2>I'm going through the Drools documentation regarding the use
of lists and other collections. I want to make sure my understanding is
correct.</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>So if I have a class:</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT face="Courier New, monospace" size=2>public class Foo
{</FONT></DIV>
<DIV><FONT face="Courier New, monospace" size=2></FONT> </DIV>
<DIV><FONT face="Courier New, monospace" size=2> private int
x;</FONT></DIV>
<DIV><FONT face="Courier New, monospace" size=2> private int
y;</FONT></DIV>
<DIV><FONT face="Courier New, monospace" size=2> private
List<String> names = ArrayList<String>();</FONT></DIV>
<DIV><FONT face="Courier New, monospace" size=2></FONT> </DIV>
<DIV><FONT face="Courier New, monospace" size=2> public
List<String> getNames() {return names;}</FONT></DIV>
<DIV><FONT face="Courier New, monospace" size=2></FONT> </DIV>
<DIV><FONT face="Courier New, monospace" size=2> // appropriate
getters/setters for the int fields ...</FONT></DIV>
<DIV><FONT face="Courier New, monospace" size=2>}</FONT></DIV>
<DIV><FONT face="Courier New, monospace" size=2></FONT> </DIV>
<DIV><FONT size=2>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.</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>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!</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>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? </FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>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...</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT face="Courier New, monospace" size=2>Rule "when a Foo List is
modified, get all Strings in it that start with 'error'"</FONT></DIV>
<DIV><FONT face="Courier New, monospace" size=2>when</FONT></DIV>
<DIV><FONT face="Courier New, monospace" size=2> $foo : Foo($names
: names)</FONT></DIV>
<DIV><FONT face="Courier New, monospace" size=2> $foundThese :
ArrayList() from collect(??? matches "error.*" from $names)</FONT></DIV>
<DIV><FONT face="Courier New, monospace" size=2>then</FONT></DIV>
<DIV><FONT face="Courier New, monospace" size=2> // act on $foo
and $foundThese</FONT></DIV>
<DIV><FONT face="Courier New, monospace" size=2></FONT> </DIV>
<DIV><FONT size=2>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.</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>Thanks!</FONT></DIV>
<DIV><FONT size=2>-Allen</FONT></DIV>
<DIV style="MARGIN-TOP: 5pt; MARGIN-BOTTOM: 5pt"><FONT
size=2></FONT> </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT
size=2></FONT> </DIV></FONT></DIV><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></BLOCKQUOTE></DIV><BR><BR
clear=all><BR>-- <BR>Edson Tirelli<BR>JBoss Drools Core Development<BR>JBoss, a
division of Red Hat @ <A href="http://www.jboss.com">www.jboss.com</A>
</DIV></BODY></HTML>