<br>&nbsp;&nbsp; Good point Greg, thanks!<br>&nbsp;&nbsp; Either that or move the actual update on the control fact to a rule with lower salience.<br><br>&nbsp;&nbsp; []s<br>&nbsp;&nbsp; Edson<br><br><br><br><div class="gmail_quote">2008/11/4 Greg Barton <span dir="ltr">&lt;<a href="mailto:greg_barton@yahoo.com">greg_barton@yahoo.com</a>&gt;</span><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">I think you&#39;d have to toss in ControlFact instances with positions from 1 to journey.segmentList.size-1 for this to work. &nbsp;Otherwise the evaluation of the journey.segmentList would halt on the first failure of the rule to match. &nbsp;Then the modify block wouldn&#39;t be needed in the action.<br>

<br>
Also, CSchr, you mentioned that the business model could not be altered. &nbsp;Does that mean you can&#39;t use additional objects like control facts?<br>
<br>
--- On Tue, 11/4/08, Edson Tirelli &lt;<a href="mailto:tirelli@post.com">tirelli@post.com</a>&gt; wrote:<br>
<br>
&gt; From: Edson Tirelli &lt;<a href="mailto:tirelli@post.com">tirelli@post.com</a>&gt;<br>
&gt; Subject: Re: [rules-users] Newbie question on accessing predecessor and successor in a list<br>
&gt; To: &quot;Rules Users List&quot; &lt;<a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a>&gt;<br>
&gt; Date: Tuesday, November 4, 2008, 8:56 AM<br>
<div><div></div><div class="Wj3C7c">&gt; Christian,<br>
&gt;<br>
&gt; &nbsp; &nbsp;My guess is that there are better approaches to do what<br>
&gt; you want (i.e.,<br>
&gt; write your business rules) without using specific indexes<br>
&gt; in a list, since<br>
&gt; that is usually a sign of trying to use a imperative<br>
&gt; algorithm in a<br>
&gt; declarative engine.<br>
&gt; &nbsp; &nbsp;If you can share a mock of what your business rule is,<br>
&gt; maybe we can help<br>
&gt; more.<br>
&gt;<br>
&gt; &nbsp; &nbsp;Having said that, about your questions:<br>
&gt;<br>
&gt; * You can use eval inside patterns. Just remember that<br>
&gt; eval() uses a code<br>
&gt; block in the same dialect you are using to write your rules<br>
&gt; (default is<br>
&gt; java). So, $journey.segmentList is probably not valid in<br>
&gt; java (since<br>
&gt; segmentList is probably private attribute), but is valid in<br>
&gt; MVEL that<br>
&gt; transparently call getSegmentList(). So, either change your<br>
&gt; expression to a<br>
&gt; correct java expression or change the rule dialect to MVEL.<br>
&gt;<br>
&gt; * DISCLAIMER: THIS IS REALLY BAD PRACTICE: if you really<br>
&gt; want to emulate<br>
&gt; &quot;imperative programming&quot; for this rule, you need<br>
&gt; a control fact to keep<br>
&gt; track of the index you are testing:<br>
&gt;<br>
&gt; rule &quot;Children: do not do this at home, unless your<br>
&gt; parents says it is ok&quot;<br>
&gt; when<br>
&gt; &nbsp; &nbsp; $cf : ControlFact( $pos : position &lt;<br>
&gt; $journey.segmentList.size )<br>
&gt; &nbsp; &nbsp; $s : Segment( countries contains Country.Switzerland,<br>
&gt; ... ) from<br>
&gt; $journey.segmentList[$pos]<br>
&gt; &nbsp; &nbsp; $ps : Segment( countries not contains<br>
&gt; Country.Switzerland, ... ) from<br>
&gt; $journey.segmentList[$pos-1]<br>
&gt; then<br>
&gt; &nbsp; &nbsp; // do something<br>
&gt; &nbsp; &nbsp; // and also update control fact<br>
&gt; &nbsp; &nbsp; modify( $cf ) { setPosition( $pos + 1 ) }<br>
&gt; end<br>
&gt;<br>
&gt; Initialize control fact with position number 1 and it<br>
&gt; should be fine, but I<br>
&gt; did not tested the above rule.<br>
&gt;<br>
&gt; &nbsp; &nbsp;[]s<br>
&gt; &nbsp; &nbsp;Edson<br>
&gt;<br>
&gt;<br>
&gt; 2008/11/4 CSchr &lt;christian.schrinner@sbb.ch&gt;<br>
&gt;<br>
&gt; &gt;<br>
&gt; &gt; Thanks Greg for your fast reply. Unfortunately I do<br>
&gt; not get this to work:<br>
&gt; &gt;<br>
&gt; &gt; $preSegment : Segment (<br>
&gt; &gt;<br>
&gt; $journey.segmentList.indexOf(this) ==<br>
&gt; &gt; ($journey.segmentList.indexOf($segment) - 1),<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;countries not contains<br>
&gt; Country.Switzerland &amp;&amp; ...<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ) from $journey.segmentList<br>
&gt; &gt;<br>
&gt; &gt; Compiler throws:<br>
&gt; &gt;<br>
&gt; &gt; [36,48]: unknown:36:48 Unexpected token<br>
&gt; &#39;this&#39;[36,98]: unknown:36:98<br>
&gt; &gt; Unexpected token &#39;$segment&#39;<br>
&gt; &gt;<br>
&gt; &gt; Is it possible to use eval() inside Segment ()?<br>
&gt; &gt;<br>
&gt; &gt; With eval the compiler throws something like this:<br>
&gt; &gt;<br>
&gt; &gt; $journey.segmentList cannot be resolved to a type<br>
&gt; &gt; Cannot use this in a static context<br>
&gt; &gt;<br>
&gt; &gt; I don&#39;t know how matching works in drools, but<br>
&gt; wouldn&#39;t this also be very<br>
&gt; &gt; expensive? As for every match of Segment ( ) the<br>
&gt; segmentList has to be<br>
&gt; &gt; iterated two times. These lists can get very long. A<br>
&gt; cheap approach would<br>
&gt; &gt; be<br>
&gt; &gt; if the from statement would &quot;know&quot; the index<br>
&gt; of the Segment () matched. I<br>
&gt; &gt; was hoping this would be possible.<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt; Regards,<br>
&gt; &gt;<br>
&gt; &gt; Chris<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt; Greg Barton wrote:<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt; You can&#39;t do<br>
&gt; $journey.segmentList.indexOf(this) and<br>
&gt; &gt; &gt; $journey.segmentList.indexOf($segment), even in<br>
&gt; an eval?<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt; --- On Mon, 11/3/08, CSchr<br>
&gt; &lt;christian.schrinner@sbb.ch&gt; wrote:<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt;&gt; From: CSchr<br>
&gt; &lt;christian.schrinner@sbb.ch&gt;<br>
&gt; &gt; &gt;&gt; Subject: [rules-users] Newbie question on<br>
&gt; accessing predecessor and<br>
&gt; &gt; &gt;&gt; successor in a list<br>
&gt; &gt; &gt;&gt; To: <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
&gt; &gt; &gt;&gt; Date: Monday, November 3, 2008, 9:56 AM<br>
&gt; &gt; &gt;&gt; Hi everyone!<br>
&gt; &gt; &gt;&gt;<br>
&gt; &gt; &gt;&gt; I&#39;m relatively new to Drools and rule<br>
&gt; languages.<br>
&gt; &gt; &gt;&gt;<br>
&gt; &gt; &gt;&gt; I&#39;m wondering if it is possible to access<br>
&gt; the<br>
&gt; &gt; &gt;&gt; predecessor and successor in a<br>
&gt; &gt; &gt;&gt; match using from operator on an (Array)List.<br>
&gt; I have<br>
&gt; &gt; &gt;&gt; something like this<br>
&gt; &gt; &gt;&gt; (simplified):<br>
&gt; &gt; &gt;&gt;<br>
&gt; &gt; &gt;&gt; ...<br>
&gt; &gt; &gt;&gt; $segment : Segment( countries contains<br>
&gt; Country.Switzerland<br>
&gt; &gt; &gt;&gt; &amp;&amp; ... ) from<br>
&gt; &gt; &gt;&gt; $journey.segmentList<br>
&gt; &gt; &gt;&gt;<br>
&gt; &gt; &gt;&gt; $preSegment : Segment ( indexOf(this) ==<br>
&gt; indexOf($segment)<br>
&gt; &gt; &gt;&gt; - 1,<br>
&gt; &gt; &gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; countries not<br>
&gt; contains<br>
&gt; &gt; Country.Switzerland<br>
&gt; &gt; &gt;&gt; &amp;&amp; ...<br>
&gt; &gt; &gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;) from $journey.segmentList<br>
&gt; &gt; &gt;&gt;<br>
&gt; &gt; &gt;&gt; I need something like indexOf() for the exact<br>
&gt; predecessor<br>
&gt; &gt; &gt;&gt; of the matched<br>
&gt; &gt; &gt;&gt; segment.<br>
&gt; &gt; &gt;&gt;<br>
&gt; &gt; &gt;&gt; If the matched business object was an element<br>
&gt; of a double<br>
&gt; &gt; &gt;&gt; linked list it<br>
&gt; &gt; &gt;&gt; would be easy. Unfortunately I don&#39;t have<br>
&gt; the luxury to<br>
&gt; &gt; &gt;&gt; change the business<br>
&gt; &gt; &gt;&gt; object into knowing it&#39;s predecessor or<br>
&gt; successor. I<br>
&gt; &gt; &gt;&gt; have to prove that we<br>
&gt; &gt; &gt;&gt; could write our java coded business rules in<br>
&gt; Drools rule<br>
&gt; &gt; &gt;&gt; language without<br>
&gt; &gt; &gt;&gt; changing the business object model.<br>
&gt; &gt; &gt;&gt;<br>
&gt; &gt; &gt;&gt; Thanks in advance,<br>
&gt; &gt; &gt;&gt;<br>
&gt; &gt; &gt;&gt; Chris<br>
&gt; &gt; &gt;&gt;<br>
&gt; &gt; &gt;&gt;<br>
&gt; &gt; &gt;&gt; --<br>
&gt; &gt; &gt;&gt; View this message in context:<br>
&gt; &gt; &gt;&gt;<br>
&gt; &gt;<br>
&gt; <a href="http://www.nabble.com/Newbie-question-on-accessing-predecessor-and-successor-in-a-list-tp20302694p20302694.html" target="_blank">http://www.nabble.com/Newbie-question-on-accessing-predecessor-and-successor-in-a-list-tp20302694p20302694.html</a><br>

&gt; &gt; &gt;&gt; Sent from the drools - user mailing list<br>
&gt; archive at<br>
&gt; &gt; &gt;&gt; Nabble.com.<br>
&gt; &gt; &gt;&gt;<br>
&gt; &gt; &gt;&gt;<br>
&gt; _______________________________________________<br>
&gt; &gt; &gt;&gt; rules-users mailing list<br>
&gt; &gt; &gt;&gt; <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
&gt; &gt; &gt;&gt;<br>
&gt; <a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt; _______________________________________________<br>
&gt; &gt; &gt; rules-users mailing list<br>
&gt; &gt; &gt; <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
&gt; &gt; &gt;<br>
&gt; <a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt; --<br>
&gt; &gt; View this message in context:<br>
&gt; &gt;<br>
&gt; <a href="http://www.nabble.com/Newbie-question-on-accessing-predecessor-and-successor-in-a-list-tp20302694p20317549.html" target="_blank">http://www.nabble.com/Newbie-question-on-accessing-predecessor-and-successor-in-a-list-tp20302694p20317549.html</a><br>

&gt; &gt; Sent from the drools - user mailing list archive at<br>
&gt; Nabble.com.<br>
&gt; &gt;<br>
&gt; &gt; _______________________________________________<br>
&gt; &gt; rules-users mailing list<br>
&gt; &gt; <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
&gt; &gt; <a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
&gt; &gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; --<br>
&gt; &nbsp;Edson Tirelli<br>
&gt; &nbsp;JBoss Drools Core Development<br>
&gt; &nbsp;JBoss, a division of Red Hat @ <a href="http://www.jboss.com" target="_blank">www.jboss.com</a><br>
&gt; _______________________________________________<br>
&gt; rules-users mailing list<br>
&gt; <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
&gt; <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>
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> &nbsp;Edson Tirelli<br> &nbsp;JBoss Drools Core Development<br> &nbsp;JBoss, a division of Red Hat @ <a href="http://www.jboss.com">www.jboss.com</a><br>