Inheritance: ITimeLineMomentCollection
        // ----------------------------------------------------------------------
        protected virtual void StartEvaluation()
        {
            if ( periods.Count > 0 )
            {
                TimeLineMomentCollection timeLineMoments = new TimeLineMomentCollection();
                timeLineMoments.AddAll( periods );
                if ( timeLineMoments.Count > 1 )
                {
                    int periodCount = 0;
                    for ( int i = 0; i < timeLineMoments.Count - 1; i++ )
                    {
                        ITimeLineMoment start = timeLineMoments[ i ];
                        ITimeLineMoment end = timeLineMoments[ i + 1 ];

                        if ( i == 0 )
                        {
                            periodCount += start.StartCount;
                            periodCount -= start.EndCount;
                        }

                        ITimePeriod period = new TimeRange( MapPeriodStart( start.Moment ), MapPeriodEnd( end.Moment ) );
                        if ( !( IgnoreEmptyPeriods && period.IsMoment ) )
                        {
                            if ( EvaluatePeriod( period, periodCount ) == false )
                            {
                                break;
                            }
                        }

                        periodCount += end.StartCount;
                        periodCount -= end.EndCount;
                    }
                }
            }
        }
        }         // IgnoreEmptyPeriods

        // ----------------------------------------------------------------------
        protected virtual void StartEvaluation()
        {
            if (periods.Count > 0)
            {
                TimeLineMomentCollection timeLineMoments = new TimeLineMomentCollection();
                timeLineMoments.AddAll(periods);
                if (timeLineMoments.Count > 1)
                {
                    int periodCount = 0;
                    for (int i = 0; i < timeLineMoments.Count - 1; i++)
                    {
                        ITimeLineMoment start = timeLineMoments[i];
                        ITimeLineMoment end   = timeLineMoments[i + 1];

                        if (i == 0)
                        {
                            periodCount += start.StartCount;
                            periodCount -= start.EndCount;
                        }

                        ITimePeriod period = new TimeRange(MapPeriodStart(start.Moment), MapPeriodEnd(end.Moment));
                        if (!(IgnoreEmptyPeriods && period.IsMoment))
                        {
                            if (EvaluatePeriod(period, periodCount) == false)
                            {
                                break;
                            }
                        }

                        periodCount += end.StartCount;
                        periodCount -= end.EndCount;
                    }
                }
            }
        }         // StartEvaluation
Exemple #3
0
        }         // HasOverlaps

        // ----------------------------------------------------------------------
        public virtual bool HasGaps()
        {
            bool hasGaps = false;

            if (Count > 1)
            {
                hasGaps = new TimeLineMomentCollection(this).HasGaps();
            }

            return(hasGaps);
        }         // HasGaps
Exemple #4
0
        }         // InsidePeriods

        // ----------------------------------------------------------------------
        public virtual bool HasOverlaps()
        {
            bool hasOverlaps = false;

            if (Count == 2)
            {
                hasOverlaps = this[0].OverlapsWith(this[1]);
            }
            else if (Count > 2)
            {
                hasOverlaps = new TimeLineMomentCollection(this).HasOverlaps();
            }

            return(hasOverlaps);
        }         // HasOverlaps
Exemple #5
0
        }         // GetTimeLineMoments

        // ----------------------------------------------------------------------
        private ITimeLineMomentCollection GetTimeLineMoments(ICollection <ITimePeriod> momentPeriods)
        {
            TimeLineMomentCollection timeLineMoments = new TimeLineMomentCollection();

            if (momentPeriods.Count == 0)
            {
                return(timeLineMoments);
            }

            // setup gap set with all start/end points
            ITimePeriodCollection intersections = new TimePeriodCollection();

            foreach (ITimePeriod momentPeriod in momentPeriods)
            {
                if (momentPeriod.IsMoment)
                {
                    continue;
                }

                // calculate the intersection between the periods
                ITimeRange intersection = limits.GetIntersection(momentPeriod);
                if (intersection == null || intersection.IsMoment)
                {
                    continue;
                }

                if (periodMapper != null)
                {
                    intersection = new TimeRange(MapPeriodStart(intersection.Start), MapPeriodEnd(intersection.End));
                }

                intersections.Add(intersection);
            }

            timeLineMoments.AddAll(intersections);
            return(timeLineMoments);
        }         // GetTimeLineMoments
        public void TimeLinePeriodsSample()
        {
            TimePeriodCollection periods = new TimePeriodCollection();
            periods.Add( new IdTimeRange( 1, new DateTime( 2000, 1, 1 ), new DateTime( 2019, 12, 31 ) ) );
            periods.Add( new IdTimeRange( 2, new DateTime( 2000, 1, 1 ), new DateTime( 2019, 12, 31 ) ) );
            periods.Add( new IdTimeRange( 3, new DateTime( 2000, 1, 1 ), new DateTime( 2009, 12, 31 ) ) );
            periods.Add( new IdTimeRange( 3, new DateTime( 2010, 1, 1 ), new DateTime( 2019, 12, 31 ) ) );
            periods.Add( new IdTimeRange( 4, new DateTime( 2000, 1, 1 ), new DateTime( 2019, 12, 31 ) ) );
            periods.Add( new IdTimeRange( 5, new DateTime( 2000, 1, 1 ), new DateTime( 2014, 12, 31 ) ) );
            periods.Add( new IdTimeRange( 5, new DateTime( 2015, 1, 1 ), new DateTime( 2019, 12, 31 ) ) );
            foreach ( ITimePeriod period in periods )
            {
                Console.WriteLine( "Period: " + period );
            }

            // time line with all period start and end moments
            ITimeLineMomentCollection moments = new TimeLineMomentCollection();
            moments.AddAll( periods );
            DateTime start = periods.Start;
            foreach ( ITimeLineMoment moment in moments )
            {
                if ( moment.EndCount <= 0 ) // search the next period end
                {
                    continue;
                }
                DateTime end = moment.Moment;
                TimeRange timeRange = new TimeRange( start, end );
                Console.WriteLine( "Period: {0}", timeRange );
                ITimePeriodCollection intersections = periods.IntersectionPeriods( timeRange );
                foreach ( ITimePeriod intersection in intersections )
                {
                    Console.WriteLine( "  Intersection: {0}", intersection );
                }
                start = moment.Moment;
            }
        }
        // ----------------------------------------------------------------------
        private static DateTime? CalcTargetMoment2( ICollection<ITimePeriod> periods, TimeSpan duration )
        {
            DateTime? targetMoment = null;
            if ( periods.Count > 0 && duration.Ticks > 0 )
            {
                TimeLineMomentCollection timeLineMoments = new TimeLineMomentCollection();
                timeLineMoments.AddAll( periods );
                if ( timeLineMoments.Count > 2 )
                {
                    int balance = 0;
                    for ( int i = 0; i < timeLineMoments.Count - 1; i++ )
                    {
                        ITimeLineMoment start = timeLineMoments[ i ];
                        ITimeLineMoment end = timeLineMoments[ i + 1 ];

                        if ( i == 0 )
                        {
                            balance += start.StartCount;
                            balance -= start.EndCount;
                        }
                        TimeRange slot = new TimeRange( start.Moment, end.Moment );
                        TimeSpan slotDuration = new TimeSpan( slot.Duration.Ticks * balance );

                        if ( slotDuration >= duration )
                        {
                            targetMoment = start.Moment.Add( new TimeSpan( duration.Ticks / balance ) );
                            break;
                        }

                        duration = duration.Subtract( slotDuration );
                        balance += end.StartCount;
                        balance -= end.EndCount;
                    }
                }
            }

            return targetMoment;
        }