[rules-users] How to traverse the Collection by index

Swindells, Thomas TSwindells at nds.com
Mon Mar 8 03:16:31 EST 2010


One of the big challenges of using drools is thinking declaratively and not the iterative method of normal programming.  Your first paragraph actually pretty much sums up your rules nicely.  One thing to comment on is that working on collections (using the from keyword) isn't a particularly efficient approach as whenever the collection (or fact the collection is in) is updated everything using that collection is re-evaluated.  Putting your Segments straight into the knowledge base is more efficient and fits the drools model better, however there are cases where this isn't really an option so you may have to adapt the pseudo code below.

>From your summary you'd probably want rules similar to the pseudo code here, I've added a index field into your segment so that your can reason over the order easily as well:

//will match segment with index 0
rule "first segment doesn't start at start of year"
when
        Segment(index == 0, startDate.month != 1 || startDate.day != 1)
then
        System.out.println("First segment doesn't start on 1/1");
End

//Will match every pair of segments except the last one (as there isn't a last one +1)
rule "segments aren't consecutive"
when
        first : Segment()
        second : Segment(index == first.index+1, startDate != first.endDate+1)
then
        System.out.println("Segments aren't consecutive");
end

//if there is a segment without a following segment and whose end date isn't 31/12
rule "last segment doesn't end at end of year"
when
        last : Segment(endDate.month != 12 || endDate.day != 31)
        not exist Segment(index == last.index+1)
then
        System.out.println("Last segment doesn't end at end of year")
end


These probably won't compile but will hopefully give you a clue on how you could define your rules, if necessary you could liberally sprinkle from's across them to read the segments out of a particular collection.

Thomas

> -----Original Message-----
> From: rules-users-bounces at lists.jboss.org [mailto:rules-users-
> bounces at lists.jboss.org] On Behalf Of dhari
> Sent: 08 March 2010 06:38
> To: rules-users at lists.jboss.org
> Subject: [rules-users] How to traverse the Collection by index
>
>
> Hi all,
> I want to compare all elements in the collection with other element of the
> same collection. In my case I have collection of Segment, a Segment contains
> startDate and endDate. The whole collection of Segment should consists on
> complete calendar year and every segment in the list must start 1+ from the
> endDate of last element. Valid fact should be:
>
> MemberShip
>      Segment 1: StartDate: 01-01-2010  EndDate: 30-05-2010
>      Segment 2: StartDate: 31-05-2010 EndDate: 08-08-2010
>      Segment 3: StartDate 09-08-2010 EndDate: 31-12-2010
>
> Data structure
> class Segment{
>       java.util.Date startDate;
>       java.util.Date endDate;
> }
> class Membership{
>      java.util.ArrayList<Segment> segments;
> }
>
> I know the date functions. I just want to know how can I traverse the List
> from get element on Index and Index+1?
> --
> View this message in context: http://n3.nabble.com/How-to-traverse-the-
> Collection-by-index-tp435000p435000.html
> Sent from the Drools - User mailing list archive at Nabble.com.
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users


**************************************************************************************
This message is confidential and intended only for the addressee. If you have received this message in error, please immediately notify the postmaster at nds.com and delete it from your system as well as any copies. The content of e-mails as well as traffic data may be monitored by NDS for employment and security purposes. To protect the environment please do not print this e-mail unless necessary.

NDS Limited. Registered Office: One London Road, Staines, Middlesex, TW18 4EX, United Kingdom. A company registered in England and Wales. Registered no. 3080780. VAT no. GB 603 8808 40-00
**************************************************************************************

This message is confidential and intended only for the addressee. If you have received this message in error, please immediately notify the postmaster at nds.com and delete it from your system as well as any copies. The content of e-mails as well as traffic data may be monitored by NDS for employment and security purposes.
To protect the environment please do not print this e-mail unless necessary.

An NDS Group Limited company. www.nds.com




More information about the rules-users mailing list