Thank you! You are right, I should not try this at home ;)
To explain the rule, I have to explain the relevant business objects first:
A Journey contains consecutive Segments. If you interrupt your Journey to
change transportation type (on foot, train, car, bus, ship, etc.) or vehicle
(e.g. another train) you change between Segments. A Segment takes you
through one or more Country(ies).
The rule is something like this:
Find a Segment in a Journey which is of a special type T1 and is completely
in Country C1. If this Segment connects to another Segment which is of type
T2 and is at least partly or completely not in C1 then the rule fires.
Thanks everyone for your help. I think I have found the solution in the our
model. We have segment ids unique to each journey which form a natural
order. So I can check for id-1 and id+1. Did not notice this before. These
ids were never used in Java based rules implementation. So it's a workaround
without solving the actual problem ;)
Regards,
Christian
Edson Tirelli-3 wrote:
Christian,
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.
If you can share a mock of what your business rule is, maybe we can
help
more.
Having said that, about your questions:
* 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.
* 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:
rule "Children: do not do this at home, unless your parents says it is ok"
when
$cf : ControlFact( $pos : position < $journey.segmentList.size )
$s : Segment( countries contains Country.Switzerland, ... ) from
$journey.segmentList[$pos]
$ps : Segment( countries not contains Country.Switzerland, ... ) from
$journey.segmentList[$pos-1]
then
// do something
// and also update control fact
modify( $cf ) { setPosition( $pos + 1 ) }
end
Initialize control fact with position number 1 and it should be fine, but
I
did not tested the above rule.
[]s
Edson
2008/11/4 CSchr <christian.schrinner(a)sbb.ch>
>
> Thanks Greg for your fast reply. Unfortunately I do not get this to work:
>
> $preSegment : Segment (
> $journey.segmentList.indexOf(this) ==
> ($journey.segmentList.indexOf($segment) - 1),
> countries not contains Country.Switzerland && ...
> ) from $journey.segmentList
>
> Compiler throws:
>
> [36,48]: unknown:36:48 Unexpected token 'this'[36,98]: unknown:36:98
> Unexpected token '$segment'
>
> Is it possible to use eval() inside Segment ()?
>
> With eval the compiler throws something like this:
>
> $journey.segmentList cannot be resolved to a type
> Cannot use this in a static context
>
> I don't know how matching works in drools, but wouldn't this also be very
> expensive? As for every match of Segment ( ) the segmentList has to be
> iterated two times. These lists can get very long. A cheap approach would
> be
> if the from statement would "know" the index of the Segment () matched. I
> was hoping this would be possible.
>
>
> Regards,
>
> Chris
>
>
> Greg Barton wrote:
> >
> > You can't do $journey.segmentList.indexOf(this) and
> > $journey.segmentList.indexOf($segment), even in an eval?
> >
> > --- On Mon, 11/3/08, CSchr <christian.schrinner(a)sbb.ch> wrote:
> >
> >> From: CSchr <christian.schrinner(a)sbb.ch>
> >> Subject: [rules-users] Newbie question on accessing predecessor and
> >> successor in a list
> >> To: rules-users(a)lists.jboss.org
> >> Date: Monday, November 3, 2008, 9:56 AM
> >> Hi everyone!
> >>
> >> I'm relatively new to Drools and rule languages.
> >>
> >> I'm wondering if it is possible to access the
> >> predecessor and successor in a
> >> match using from operator on an (Array)List. I have
> >> something like this
> >> (simplified):
> >>
> >> ...
> >> $segment : Segment( countries contains Country.Switzerland
> >> && ... ) from
> >> $journey.segmentList
> >>
> >> $preSegment : Segment ( indexOf(this) == indexOf($segment)
> >> - 1,
> >> countries not contains
> Country.Switzerland
> >> && ...
> >> ) from $journey.segmentList
> >>
> >> I need something like indexOf() for the exact predecessor
> >> of the matched
> >> segment.
> >>
> >> If the matched business object was an element of a double
> >> linked list it
> >> would be easy. Unfortunately I don't have the luxury to
> >> change the business
> >> object into knowing it's predecessor or successor. I
> >> have to prove that we
> >> could write our java coded business rules in Drools rule
> >> language without
> >> changing the business object model.
> >>
> >> Thanks in advance,
> >>
> >> Chris
> >>
> >>
> >> --
> >> View this message in context:
> >>
>
http://www.nabble.com/Newbie-question-on-accessing-predecessor-and-succes...
> >> Sent from the drools - user mailing list archive at
> >>
Nabble.com.
> >>
> >> _______________________________________________
> >> rules-users mailing list
> >> rules-users(a)lists.jboss.org
> >>
https://lists.jboss.org/mailman/listinfo/rules-users
> >
> >
> >
> > _______________________________________________
> > rules-users mailing list
> > rules-users(a)lists.jboss.org
> >
https://lists.jboss.org/mailman/listinfo/rules-users
> >
> >
>
> --
> View this message in context:
>
http://www.nabble.com/Newbie-question-on-accessing-predecessor-and-succes...
> Sent from the drools - user mailing list archive at
Nabble.com.
>
> _______________________________________________
> rules-users mailing list
> rules-users(a)lists.jboss.org
>
https://lists.jboss.org/mailman/listinfo/rules-users
>
--
Edson Tirelli
JBoss Drools Core Development
JBoss, a division of Red Hat @
www.jboss.com
_______________________________________________
rules-users mailing list
rules-users(a)lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users