Infinite loop problem in SegmentedTimeline's addBaseTimeline

Discussion about JFreeChart related to stockmarket charts.
Locked
Junaid
Posts: 13
Joined: Tue Jul 20, 2010 2:25 pm
antibot: No, of course not.
Contact:

Infinite loop problem in SegmentedTimeline's addBaseTimeline

Post by Junaid » Mon Jul 26, 2010 2:36 pm

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

skunk
Posts: 1087
Joined: Thu Jun 02, 2005 10:14 pm
Location: Brisbane, Australia

Re: Infinite loop problem in SegmentedTimeline's addBaseTimeline

Post by skunk » Mon Jul 26, 2010 2:42 pm


Junaid
Posts: 13
Joined: Tue Jul 20, 2010 2:25 pm
antibot: No, of course not.
Contact:

Re: Infinite loop problem in SegmentedTimeline's addBaseTimeline

Post by Junaid » Mon Aug 02, 2010 9:41 am

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

Locked