<br> Christian,<br><br> 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>
If you can share a mock of what your business rule is, maybe we can help more.<br><br> 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 "imperative programming" for this rule, you need a control fact to keep track of the index you are testing:<br><br>rule "Children: do not do this at home, unless your parents says it is ok"<br>
when<br> $cf : ControlFact( $pos : position < $journey.segmentList.size )<br> $s : Segment( countries contains Country.Switzerland, ... ) from $journey.segmentList[$pos]<br> $ps : Segment( countries not contains Country.Switzerland, ... ) from $journey.segmentList[$pos-1]<br>
then<br> // do something<br> // and also update control fact<br> 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> []s<br> Edson<br> <br><br><div class="gmail_quote">2008/11/4 CSchr <span dir="ltr"><christian.schrinner@sbb.ch></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>
$journey.segmentList.indexOf(this) ==<br>
($journey.segmentList.indexOf($segment) - 1),<br>
<div class="Ih2E3d"> countries not contains Country.Switzerland && ...<br>
) from $journey.segmentList<br>
<br>
</div>Compiler throws:<br>
<br>
[36,48]: unknown:36:48 Unexpected token 'this'[36,98]: unknown:36:98<br>
Unexpected token '$segment'<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't know how matching works in drools, but wouldn'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 "know" 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>
><br>
> You can't do $journey.segmentList.indexOf(this) and<br>
> $journey.segmentList.indexOf($segment), even in an eval?<br>
><br>
> --- On Mon, 11/3/08, CSchr <christian.schrinner@sbb.ch> wrote:<br>
><br>
>> From: CSchr <christian.schrinner@sbb.ch><br>
>> Subject: [rules-users] Newbie question on accessing predecessor and<br>
>> successor in a list<br>
>> To: <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
>> Date: Monday, November 3, 2008, 9:56 AM<br>
>> Hi everyone!<br>
>><br>
>> I'm relatively new to Drools and rule languages.<br>
>><br>
>> I'm wondering if it is possible to access the<br>
>> predecessor and successor in a<br>
>> match using from operator on an (Array)List. I have<br>
>> something like this<br>
>> (simplified):<br>
>><br>
>> ...<br>
>> $segment : Segment( countries contains Country.Switzerland<br>
>> && ... ) from<br>
>> $journey.segmentList<br>
>><br>
>> $preSegment : Segment ( indexOf(this) == indexOf($segment)<br>
>> - 1,<br>
>> countries not contains Country.Switzerland<br>
>> && ...<br>
>> ) from $journey.segmentList<br>
>><br>
>> I need something like indexOf() for the exact predecessor<br>
>> of the matched<br>
>> segment.<br>
>><br>
>> If the matched business object was an element of a double<br>
>> linked list it<br>
>> would be easy. Unfortunately I don't have the luxury to<br>
>> change the business<br>
>> object into knowing it's predecessor or successor. I<br>
>> have to prove that we<br>
>> could write our java coded business rules in Drools rule<br>
>> language without<br>
>> changing the business object model.<br>
>><br>
>> Thanks in advance,<br>
>><br>
>> Chris<br>
>><br>
>><br>
>> --<br>
>> View this message in context:<br>
>> <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>
>> Sent from the drools - user mailing list archive at<br>
>> 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>
><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>
><br>
><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> Edson Tirelli<br> JBoss Drools Core Development<br> JBoss, a division of Red Hat @ <a href="http://www.jboss.com">www.jboss.com</a><br>