public static Tnum IntervalsSince(Tdate startDate, Tdate endDate, IntervalType interval, int? startAt)
        {
            // Handle unknowns
            Hstate top = PrecedingState(startDate.FirstValue, endDate.FirstValue);
            if (top != Hstate.Known) return new Tnum(new Hval(null,top));

            DateTime start = startDate.ToDateTime;
            DateTime end = endDate.ToDateTime;

            Tnum result = new Tnum();

            if (start != Time.DawnOf)
            {
                result.AddState(Time.DawnOf,0);
            }

            DateTime indexDate = start;
            int? indexNumber = startAt;

            while (indexDate < end)
            {
                result.AddState(indexDate,Convert.ToDecimal(indexNumber));
                indexNumber++;
                indexDate = indexDate.AddInterval(interval, 1);
            }

            if (end < Time.EndOf) result.AddState(end, 0);
            return result;
        }
Exemple #2
0
        public static Tnum IntervalsUntil(Tdate startDate, Tdate endDate, IntervalType interval, int startAt)
        {
            // Handle unknowns
            Hstate top = PrecedingState(startDate.FirstValue, endDate.FirstValue);

            if (top != Hstate.Known)
            {
                return(new Tnum(new Hval(null, top)));
            }

            DateTime start = startDate.ToDateTime;
            DateTime end   = endDate.ToDateTime;

            Tnum result = new Tnum();

            DateTime indexDate   = end;
            int      indexNumber = startAt - 1;

            while (indexDate > start)
            {
                if (indexNumber != startAt - 1)
                {
                    result.AddState(indexDate, Convert.ToString(indexNumber));
                }
                indexNumber++;
                indexDate = indexDate.SubtractInterval(interval);
            }

            result.AddState(Time.DawnOf, 0);
            result.AddState(start, Convert.ToString(indexNumber));
            result.AddState(end, 0);

            return(result);
        }
Exemple #3
0
        /// <summary>
        /// Determines whether this instance is ever true during a specified time period.
        /// </summary>
        public Tbool IsEverTrue(Tdate start, Tdate end)
        {
            Tbool equalsVal = this == true;

            Tbool isDuringInterval = Time.IsBetween(start, end);

            Tbool isOverlap = equalsVal & isDuringInterval;

            return(isOverlap.IsEverTrue());
        }
Exemple #4
0
        /// <summary>
        /// Determines whether this instance is always true during a specified interval.
        /// </summary>
        public Tbool IsAlwaysTrue(Tdate start, Tdate end)
        {
            Tbool equalsVal = this == true;

            Tbool isDuringInterval = Time.IsBetween(start, end);

            Tbool isOverlap = equalsVal & isDuringInterval;

            Tbool overlapAndIntervalAreCoterminous = isOverlap == isDuringInterval;

            return(!overlapAndIntervalAreCoterminous.IsEver(false));
        }
Exemple #5
0
        /// <summary>
        /// Returns a Tbool that's true up to a specified DateTime, and false
        /// at and after it.
        /// </summary>
        public static Tbool IsBefore(Tdate dt)
        {
            // Handle unknowns
            if (!dt.FirstValue.IsKnown)
            {
                return(new Tbool(dt.FirstValue));
            }

            // Create boolean
            Tbool result = new Tbool();

            if (dt == DawnOf)
            {
                result.AddState(DawnOf, false);
            }
            else
            {
                result.AddState(DawnOf, true);
                result.AddState(dt.ToDateTime, false);
            }
            return(result);
        }
Exemple #6
0
        //*********************************************************************
        // TEMPORAL "RECURRENCE" FUNCTIONS
        //*********************************************************************

        /// <summary>
        /// Loops (cycles) through numbers over time (e.g. 1 2 3 4 1 2 3 4 ...)
        /// </summary>
        public static Tnum Recurrence(Tdate startDate, Tdate endDate, IntervalType interval, int min, int max)
        {
            // Handle unknowns
            Hstate top = PrecedingState(startDate.FirstValue, endDate.FirstValue);

            if (top != Hstate.Known)
            {
                return(new Tnum(new Hval(null, top)));
            }

            DateTime start = startDate.ToDateTime;
            DateTime end   = endDate.ToDateTime;

            Tnum result = new Tnum();

            if (start != Time.DawnOf)
            {
                result.AddState(Time.DawnOf, 0);
            }

            DateTime indexDate   = start;
            int      indexNumber = min;

            while (indexDate < end)
            {
                result.AddState(indexDate, Convert.ToString(indexNumber));
                indexDate = indexDate.AddInterval(interval, 1);

                // Reset sequence
                indexNumber++;
                if (indexNumber > max)
                {
                    indexNumber = min;
                }
            }

            result.AddState(end, 0);
            return(result);
        }
Exemple #7
0
        /// <summary>
        /// Returns the value of a Tvar at a specified point in time.
        /// </summary>
        /// <remarks>
        /// If the Tdate varies over time, only the first value is used.
        /// </remarks>
        public T AsOf <T>(Tdate date) where T : Tvar
        {
            Hval result;

            // If base Tvar has eternal, known value, return that.
            // (In these cases, the Tdate is irrelevant.)
            if (this.IsEternal && !this.IsEternallyUnknown)
            {
                result = this.FirstValue;
            }
            // If either the base Tvar or Tdate are unknown...
            else if (!date.FirstValue.IsKnown || this.IsEternallyUnknown)
            {
                Hstate top = PrecedingState(this.FirstValue, date.FirstValue);
                result = new Hval(null, top);
            }
            else
            {
                result = this.ObjectAsOf(date.ToDateTime);
            }

            return((T)Util.ReturnProperTvar <T>(result));
        }
Exemple #8
0
        public static Tnum IntervalsSince(Tdate startDate, Tdate endDate, IntervalType interval, int?startAt)
        {
            // Handle unknowns
            Hstate top = PrecedingState(startDate.FirstValue, endDate.FirstValue);

            if (top != Hstate.Known)
            {
                return(new Tnum(new Hval(null, top)));
            }

            DateTime start = startDate.ToDateTime;
            DateTime end   = endDate.ToDateTime;

            Tnum result = new Tnum();

            if (start != Time.DawnOf)
            {
                result.AddState(Time.DawnOf, 0);
            }

            DateTime indexDate   = start;
            int?     indexNumber = startAt;

            while (indexDate < end)
            {
                result.AddState(indexDate, Convert.ToDecimal(indexNumber));
                indexNumber++;
                indexDate = indexDate.AddInterval(interval, 1);
            }

            if (end < Time.EndOf)
            {
                result.AddState(end, 0);
            }
            return(result);
        }
 //*********************************************************************
 // TEMPORAL "STEP" FUNCTIONS
 //*********************************************************************
 /// <summary>
 /// Returns the number of intervals since a given date (step up function)
 /// (e.g. 1 2 3 4 5 6 7 8 9 ...)
 /// </summary>
 public static Tnum IntervalsSince(Tdate start, Tdate end, IntervalType interval)
 {
     return IntervalsSince(start, end, interval, 0);
 }
        //*********************************************************************
        // TEMPORAL "RECURRENCE" FUNCTIONS
        //*********************************************************************
        /// <summary>
        /// Loops (cycles) through numbers over time (e.g. 1 2 3 4 1 2 3 4 ...)
        /// </summary>
        public static Tnum Recurrence(Tdate startDate, Tdate endDate, IntervalType interval, int min, int max)
        {
            // Handle unknowns
            Hstate top = PrecedingState(startDate.FirstValue, endDate.FirstValue);
            if (top != Hstate.Known) return new Tnum(new Hval(null,top));

            DateTime start = startDate.ToDateTime;
            DateTime end = endDate.ToDateTime;

            Tnum result = new Tnum();

            if (start != Time.DawnOf)
            {
                result.AddState(Time.DawnOf, 0);
            }

            DateTime indexDate = start;
            int indexNumber = min;

            while (indexDate < end)
            {
                result.AddState(indexDate,Convert.ToString(indexNumber));
                indexDate = indexDate.AddInterval(interval, 1);

                // Reset sequence
                indexNumber++;
                if (indexNumber > max)
                {
                    indexNumber = min;
                }
            }

            result.AddState(end, 0);
            return result;
        }
Exemple #11
0
 /// <summary>
 /// Returns the value of a Tbool at a specified point in time. 
 /// </summary>
 public Tbool AsOf(Tdate dt)
 {
     return this.AsOf<Tbool>(dt);
 }
Exemple #12
0
        /// <summary>
        /// Determines whether this instance is always true during a specified interval.
        /// </summary>
        public Tbool IsAlwaysTrue(Tdate start, Tdate end)
        {
            Tbool equalsVal = this == true; 

            Tbool isDuringInterval = Time.IsBetween(start, end);
            
            Tbool isOverlap = equalsVal & isDuringInterval;

            Tbool overlapAndIntervalAreCoterminous = isOverlap == isDuringInterval;

            return !overlapAndIntervalAreCoterminous.IsEver(false);
        }
Exemple #13
0
 /// <summary>
 /// Returns the number of weeks (accurate to three decimal places) between the DateTimes in two Tdates.
 /// </summary>
 public static Tnum WeekDiff(Tdate td1, Tdate td2)
 {
     return(Tdate.WeekDifference(td1, td2));
 }
Exemple #14
0
        /// <summary>
        /// Asserts a given fact (of the proper Tvar type)
        /// </summary>
        private static void AssertFact(Factoid f)
        {
            // Instantiate relevant Things
            Thing t1 = f.Arg1.ToString() != "" ? Facts.AddThing(f.Arg1.ToString()) : null;
            Thing t2 = f.Arg2.ToString() != "" ? Facts.AddThing(f.Arg2.ToString()) : null;
            Thing t3 = f.Arg3.ToString() != "" ? Facts.AddThing(f.Arg3.ToString()) : null;

            // Sometimes I have my doubts about static typing...
            if (f.FactType == "Tbool")
            {
                Tbool val = new Tbool();
                foreach (TemporalValue v in f.Timeline)
                {
                    val.AddState(v.Date, new Hval(v.Value));
                }
                Facts.Assert(t1, f.Relationship, t2, t3, val);
            }
            else if (f.FactType == "Tnum")
            {
                Tnum val = new Tnum();
                foreach (TemporalValue v in f.Timeline)
                {
                    val.AddState(v.Date, new Hval(v.Value));
                }
                Facts.Assert(t1, f.Relationship, t2, t3, val);
            }
            else if (f.FactType == "Tstr")
            {
                Tstr val = new Tstr();
                foreach (TemporalValue v in f.Timeline)
                {
                    val.AddState(v.Date, new Hval(v.Value));
                }
                Facts.Assert(t1, f.Relationship, t2, t3, val);
            }
            else if (f.FactType == "Tdate")
            {
                Tdate val = new Tdate();
                foreach (TemporalValue v in f.Timeline)
                {
                    val.AddState(v.Date, new Hval(v.Value));
                }
                Facts.Assert(t1, f.Relationship, t2, t3, val);
            }
            else if (f.FactType == "Tset")
            {
                Tset val = new Tset();
                foreach (TemporalValue v in f.Timeline)
                {
                    val.AddState(v.Date, new Hval(v.Value));
                }
                Facts.Assert(t1, f.Relationship, t2, t3, val);
            }
        }
Exemple #15
0
 /// <summary>
 /// Returns the value of the Tdate at a specified point in time.
 /// </summary>
 public Tdate AsOf(Tdate dt)
 {
     return this.AsOf<Tdate>(dt);
 }
Exemple #16
0
 /// <summary>
 /// Returns the value of the Tnum at a specified point in time.
 /// </summary>
 public Tnum AsOf(Tdate dt)
 {
     return(this.AsOf <Tnum>(dt));
 }
Exemple #17
0
 /// <summary>
 /// Returns the value of a Tstr at a specified point in time.
 /// </summary>
 public Tstr AsOf(Tdate dt)
 {
     return(this.AsOf <Tstr>(dt));
 }
Exemple #18
0
        // TODO: Add MonthDiff and QuarterDiff functions

        /// <summary>
        /// Returns the number of years between the DateTimes in two Tdates.
        /// </summary>
        public static Tnum YearDifference(Tdate td1, Tdate td2)
        {
            return(ApplyFcnToTimeline <Tnum>(x => YearDiffRec(x), td1, td2));;
        }
Exemple #19
0
 /// <summary>
 /// Returns the number of weeks between the DateTimes in two Tdates.
 /// </summary>
 public static Tnum WeekDifference(Tdate td1, Tdate td2)
 {
     return((DayDifference(td1, td2) / 7).RoundToNearest(0.001));
 }
Exemple #20
0
        // ********************************************************************
        // TimeDiff
        // ********************************************************************

        /// <summary>
        /// Returns the number of days between the DateTimes in two Tdates.
        /// </summary>
        public static Tnum DayDifference(Tdate td1, Tdate td2)
        {
            return(ApplyFcnToTimeline <Tnum>(x => CoreDayDiff(x), td1, td2));
        }
Exemple #21
0
 /// <summary>
 /// Returns the value of the Tdate at a specified point in time.
 /// </summary>
 public Tdate AsOf(Tdate dt)
 {
     return(this.AsOf <Tdate>(dt));
 }
Exemple #22
0
 /// <summary>
 /// Returns the number of weeks (accurate to three decimal places) between the DateTimes in two Tdates.
 /// </summary>
 public static Tnum WeekDiff(Tdate td1, Tdate td2)
 {
     return Tdate.WeekDifference(td1,td2);
 }
Exemple #23
0
 /// <summary>
 /// Returns the number of days between the DateTimes in two Tdates.
 /// </summary>
 public static Tnum DayDiff(Tdate td1, Tdate td2)
 {
     return Tdate.DayDifference(td1,td2);
 }
Exemple #24
0
 // ********************************************************************
 // TimeDiff
 // ********************************************************************
 
 /// <summary>
 /// Returns the number of days between the DateTimes in two Tdates.
 /// </summary>
 public static Tnum DayDifference(Tdate td1, Tdate td2)
 {
     return ApplyFcnToTimeline<Tnum>(x => CoreDayDiff(x), td1, td2);
 }    
Exemple #25
0
        //*********************************************************************
        // TEMPORAL "STEP" FUNCTIONS
        //*********************************************************************

        /// <summary>
        /// Returns the number of intervals since a given date (step up function)
        /// (e.g. 1 2 3 4 5 6 7 8 9 ...)
        /// </summary>
        public static Tnum IntervalsSince(Tdate start, Tdate end, IntervalType interval)
        {
            return(IntervalsSince(start, end, interval, 0));
        }
Exemple #26
0
 /// <summary>
 /// Returns the number of weeks between the DateTimes in two Tdates.
 /// </summary>
 public static Tnum WeekDifference(Tdate td1, Tdate td2)
 {
     return (DayDifference(td1, td2) / 7).RoundToNearest(0.001);
 }
Exemple #27
0
 /// <summary>
 /// Returns the number of days between the DateTimes in two Tdates.
 /// </summary>
 public static Tnum DayDiff(Tdate td1, Tdate td2)
 {
     return(Tdate.DayDifference(td1, td2));
 }
Exemple #28
0
 // TODO: Add MonthDiff and QuarterDiff functions
 
 /// <summary>
 /// Returns the number of years between the DateTimes in two Tdates.
 /// </summary>
 public static Tnum YearDifference(Tdate td1, Tdate td2)
 {
     return ApplyFcnToTimeline<Tnum>(x => YearDiffRec(x), td1, td2);;
 }
Exemple #29
0
 /// <summary>
 /// Returns the number of years (accurate to three decimal places) between the DateTimes in two Tdates.
 /// </summary>
 public static Tnum YearDiff(Tdate td1, Tdate td2)
 {
     return(Tdate.YearDifference(td1, td2));
 }
Exemple #30
0
 /// <summary>
 /// Returns the members of the set at a specified point in time.
 /// </summary>
 public Tset AsOf(Tdate dt)
 {
     return(this.AsOf <Tset>(dt));
 }
Exemple #31
0
 /// <summary>
 /// Determines whether this instance is ever true during a specified time period.
 /// </summary>
 public Tbool IsEverTrue(Tdate start, Tdate end)
 {
     Tbool equalsVal = this == true; 
     
     Tbool isDuringInterval = Time.IsBetween(start, end);
     
     Tbool isOverlap = equalsVal & isDuringInterval;
     
     return isOverlap.IsEverTrue();
 }
Exemple #32
0
        /// <summary>
        /// Returns the total number of elapsed intervals between two dates.
        /// </summary>
        public Tnum TotalElapsedIntervals(Tnum interval, Tdate start, Tdate end)
        {
            Tnum rei = RunningElapsedIntervals(interval);

            return(rei.AsOf(end) - rei.AsOf(start));
        }
        public static Tnum IntervalsUntil(Tdate startDate, Tdate endDate, IntervalType interval, int startAt)
        {
            // Handle unknowns
            Hstate top = PrecedingState(startDate.FirstValue, endDate.FirstValue);
            if (top != Hstate.Known) return new Tnum(new Hval(null,top));

            DateTime start = startDate.ToDateTime;
            DateTime end = endDate.ToDateTime;

            Tnum result = new Tnum();

            DateTime indexDate = end;
            int indexNumber = startAt-1;

            while (indexDate > start)
            {
                if (indexNumber != startAt-1)
                {
                    result.AddState(indexDate,Convert.ToString(indexNumber));
                }
                indexNumber++;
                indexDate = indexDate.SubtractInterval(interval);
            }

            result.AddState(Time.DawnOf, 0);
            result.AddState(start,Convert.ToString(indexNumber));
            result.AddState(end, 0);

            return result;
        }
Exemple #34
0
 /// <summary>
 /// Returns the value of a Tstr at a specified point in time. 
 /// </summary>
 public Tstr AsOf(Tdate dt)
 {
     return this.AsOf<Tstr>(dt);
 }
 /// <summary>
 /// Maps a function to a timeline, starting on a given date.
 /// </summary>
 //  TODO: Get rid of Time.IntervalType
 public static Tnum TemporalMap(Func<Tnum,Tnum> fcn, Tdate startDate, Tnum intervalCount, Time.IntervalType intervalType)
 {
     return fcn(Time.IntervalsSince(startDate, startDate.AddYears(intervalCount), intervalType));
 }
Exemple #36
0
 /// <summary>
 /// Returns the members of the set at a specified point in time. 
 /// </summary>
 public Tset AsOf(Tdate dt)
 {
     return this.AsOf<Tset>(dt);
 }
Exemple #37
0
 /// <summary>
 /// Returns a Tbool that's true during a specified time interval (including
 /// at the "start"), and otherwise false (including at the moment represented
 /// by the "end").
 /// </summary>
 public static Tbool IsBetween(Tdate start, Tdate end)
 {
     return(IsAtOrAfter(start) && IsBefore(end));
 }
Exemple #38
0
        /// <summary>
        /// Returns a Tbool that's true up to a specified DateTime, and false
        /// at and after it.
        /// </summary>
        public static Tbool IsBefore(Tdate dt)
        {
            // Handle unknowns
            if (!dt.FirstValue.IsKnown)
            {
                return new Tbool(dt.FirstValue);
            }

            // Create boolean
            Tbool result = new Tbool();

            if (dt == DawnOf)
            {
                result.AddState(DawnOf, false);
            }
            else
            {
                result.AddState(DawnOf, true);
                result.AddState(dt.ToDateTime, false);
            }
            return result;
        }
Exemple #39
0
 /// <summary>
 /// Returns the number of years (accurate to three decimal places) between the DateTimes in two Tdates.
 /// </summary>
 public static Tnum YearDiff(Tdate td1, Tdate td2)
 {
     return Tdate.YearDifference(td1,td2);
 }
Exemple #40
0
        /// <summary>
        /// Returns the total number of elapsed intervals between two dates.
        /// </summary>
        public Tnum TotalElapsedIntervals(Tnum interval, Tdate start, Tdate end)
        {
            Tnum rei = RunningElapsedIntervals(interval);

            return rei.AsOf(end) - rei.AsOf(start);
        }
        /// <summary>
        /// Finds the total of a Tnum, summed over the given intervals between two 
        /// dates (start and end).
        /// </summary>
        /// <example>
        ///   AnnualIncome2014 = MonthlyIncome.TotalSummedIntervals(TheMonth, 2014-01-01, 2014-12-31)
        /// </example>
        public Tnum TotalSummedIntervals(Tnum interval, Tdate start, Tdate end)
        {
            Tnum rsi = RunningSummedIntervals(interval);

            return rsi.AsOf(end) - rsi.AsOf(start);
        }
Exemple #42
0
 /// <summary>
 /// Returns the value of the Tnum at a specified point in time.
 /// </summary>
 public Tnum AsOf(Tdate dt)
 {
     return this.AsOf<Tnum>(dt);
 }
Exemple #43
0
 /// <summary>
 /// Maps a function to a timeline, starting on a given date.
 /// </summary>
 //  TODO: Get rid of Time.IntervalType
 public static Tnum TemporalMap(Func <Tnum, Tnum> fcn, Tdate startDate, Tnum intervalCount, Time.IntervalType intervalType)
 {
     return(fcn(Time.IntervalsSince(startDate, startDate.AddYears(intervalCount), intervalType)));
 }
Exemple #44
0
 /// <summary>
 /// Returns a Tbool that's true during a specified time interval (including
 /// at the "start"), and otherwise false (including at the moment represented 
 /// by the "end").
 /// </summary>
 public static Tbool IsBetween(Tdate start, Tdate end)
 {
     return IsAtOrAfter(start) && IsBefore(end);
 }
Exemple #45
0
 /// <summary>
 /// Returns the value of a Tbool at a specified point in time.
 /// </summary>
 public Tbool AsOf(Tdate dt)
 {
     return(this.AsOf <Tbool>(dt));
 }
Exemple #46
0
        /// <summary>
        /// Finds the total of a Tnum, summed over the given intervals between two
        /// dates (start and end).
        /// </summary>
        /// <example>
        ///   AnnualIncome2014 = MonthlyIncome.TotalSummedIntervals(TheMonth, 2014-01-01, 2014-12-31)
        /// </example>
        public Tnum TotalSummedIntervals(Tnum interval, Tdate start, Tdate end)
        {
            Tnum rsi = RunningSummedIntervals(interval);

            return(rsi.AsOf(end) - rsi.AsOf(start));
        }