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

<div><div></div><div class="Wj3C7c">Sent from the drools - user mailing list archive at Nabble.com.<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>