Page 1 of 1

Infinite loop problem in SegmentedTimeline's addBaseTimeline

Posted: Mon Jul 26, 2010 2:36 pm
by Junaid
Hi,

I am using JFreeChart version 1.0.13. I am trying to combine two segmented timelines, as shown in the code below:

Code: Select all

SegmentedTimeline baseTimeLine = new SegmentedTimeline(SegmentedTimeline.DAY_SEGMENT_SIZE, 5, 2);
baseTimeLine.setStartTime(startTime);

SegmentedTimeline twentyFourHourSegTimeline = new SegmentedTimeline(SegmentedTimeline.DAY_SEGMENT_SIZE, 7, 0);
twentyFourHourSegTimeline.setStartTime(startTime);
twentyFourHourSegTimeline.setBaseTimeline(baseTimeLine);

SegmentedTimeline oneHourSegTimeLine = new SegmentedTimeline(SegmentedTimeline.HOUR_SEGMENT_SIZE, 8 , 16);
oneHourSegTimeLine.setStartTime(startTime);
oneHourSegTimeLine.setBaseTimeline(twentyFourHourSegTimeline);

oneHourSegTimeLine.addBaseTimelineException(exceptionDate);
The last line of the code "oneHourSegTimeLine.addBaseTimelineException(exceptionDate)" enters into infinite loop.

Has anyone come across the same issue. Any inputs are appreciated.

Rgds,
Junaid

Re: Infinite loop problem in SegmentedTimeline's addBaseTimeline

Posted: Mon Jul 26, 2010 2:42 pm
by skunk

Re: Infinite loop problem in SegmentedTimeline's addBaseTimeline

Posted: Mon Aug 02, 2010 9:41 am
by Junaid
Thanks skunk for your reply. The issue at http://www.jfree.org/phpBB2/viewtopic.php?f=3&t=26926 is different.

My problem was due to programming error.

In the source code of SegmentedTimeLine in 1.0.13, given below

Code: Select all

    public void addBaseTimelineException(long domainValue) {

        Segment baseSegment = this.baseTimeline.getSegment(domainValue);
        if (baseSegment.inIncludeSegments()) {

            // cycle through all the segments contained in the BaseTimeline
            // exception segment
            Segment segment = getSegment(baseSegment.getSegmentStart());
            while (segment.getSegmentStart() <= baseSegment.getSegmentEnd()) {
                if (segment.inIncludeSegments()) {

                    // find all consecutive included segments
                    long fromDomainValue = segment.getSegmentStart();
                    long toDomainValue;
                    do {
                        toDomainValue = segment.getSegmentEnd();
                        segment.inc();
                    }
                    while (segment.inIncludeSegments());

                    // add the interval as an exception
                    addException(fromDomainValue, toDomainValue);

                }
                else {
                    // this is not one of our included segment, skip it
                    segment.inc();
                }
            }
        }
    }
The infinite loop happened in the inner while loop, shown below

Code: Select all

                    do {
                        toDomainValue = segment.getSegmentEnd();
                        segment.inc();
                    }
                    while (segment.inIncludeSegments());
It was my coding mistake, I had made a SegmentedTimeLine with segment size of 1 day, and I was trying to add the exception segments that were some hours in a particular day. But since all the hourly segments fall within 24 hours, the while loop (while (segment.inIncludeSegments()) would not never end. As all the segments will always be included segment.

Thanks guys for your help.

Rgds,
Junaid