<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="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<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>