tp_timeinterval.h

Go to the documentation of this file.
00001 /** @file tp_timeinterval.h  Class TimeInterval */
00002 
00003 
00004 /* 
00005    Time plug-in for FAU Discrete Event Systems Library (libfaudes)
00006 
00007    Copyright (C) 2007  Ruediger Berndt
00008    Copyright (C) 2007  Thomas Moor
00009    Exclusive copyright is granted to Klaus Schmidt
00010 
00011 */
00012 
00013 #ifndef FAUDES_TP_TIMEINTERVAL_H
00014 #define FAUDES_TP_TIMEINTERVAL_H
00015 
00016 #include "corefaudes.h"
00017 #include <cstdlib>
00018 #include <cstring>
00019 #include <cfloat>
00020 #include <climits>
00021 #include <iostream>
00022 
00023 
00024 namespace faudes {
00025 
00026 /** 
00027  * Type to represent time. 
00028  *
00029  * The time plugin will compile with integer or float time type. However,
00030  * we insist in discrete time in the sense that there is a smallest
00031  * time step. So float time would be cosmetic only. 
00032  * 
00033  * Note that timed generators do not explicitely refer to a physical time unit like e.g.
00034  * hours or seconds. However, it is assumed, that a fixed conversion factor
00035  * from values of type tpTime to some physical unit exists. Thus, we say tpTime
00036  * variables hold time in faudes-time units. We feel that this is consistent with
00037  * Alur's timed automata.
00038  *
00039  * @ingroup TimedPlugin 
00040  */
00041 class tpTime {
00042  public:
00043    /** Datatype for point on time axis */
00044    typedef Int Type;
00045    /** Maximum time, associated with infinitiy */
00046    const static Type Max=FAUDES_INT_MAX;
00047    /** Minimum time, associated with minus infinitiy */
00048    const static Type Min=FAUDES_INT_MIN+1;
00049    /** Undefined time value */
00050    const static Type UnDef=FAUDES_INT_MIN;
00051    /** Smallest representable time step */
00052    const static Type Step=1;
00053    /** convert to string */
00054    std::string static Str(Type time) { 
00055      if(time == UnDef) return "undef"; 
00056      if(time < Min) return "err"; 
00057      if(time == Min) return "-inf"; 
00058      if(time > Max) return "err"; 
00059      if(time == Max) return "inf"; 
00060      return ToStringInteger(time); 
00061    };
00062 };
00063 
00064 /*
00065 Alternative time representation: cosmetic float
00066 class tpTime {
00067  public:
00068    typedef double Type;
00069    const static Type Max=FLT_MAX;
00070    const static Type Min=FLT_MIN*0.5;
00071    const static Type UnDef=FLT_MIN;
00072    const static Type Step=0.0001;
00073 };
00074 */
00075 
00076 
00077 
00078 /** 
00079  * Model of a time interval. An interval consisits of a lower and upper bound,
00080  * plus flags to indicate whether the bounds are inclusive or not.
00081  * The maximum and minimum values of the time data tyle are interpreted as infinity.
00082  * 
00083  *
00084  * @ingroup TimedPlugin 
00085  */
00086  class TimeInterval {
00087   
00088  public:
00089     
00090    /** 
00091     * Default constructor - sets time interval to (-inf; +inf)
00092     */
00093    TimeInterval(void) {SetFull();};
00094 
00095    /**
00096     * Default destructor
00097     */
00098    ~TimeInterval(void) {};
00099 
00100    /** 
00101     * Set the lower bound to a given value.
00102     * If the value is minus infinity, it is set exclusive. Otherwise,
00103     * the exclusive/inclusive flag is kept.
00104     *
00105     * @param time
00106     *   The new value of lower bound
00107     */
00108    void LB(tpTime::Type time) {
00109      if(time <= tpTime::Min) time=tpTime::Min;
00110      if(time >= tpTime::Max) time=tpTime::Max;
00111      mstLB=time;
00112      if(mstLB==tpTime::Min)  mbLBincl=false; 
00113    }
00114 
00115 
00116    /** 
00117     * Set the upper bound to a given value.
00118     * If the value is infinity, it is set exclusive. Otherwise,
00119     * the exclusive/inclusive flag is kept.
00120     *
00121     * @param time
00122     *   The new value of upper bound
00123     */
00124    void UB(tpTime::Type time) {
00125      if(time <= tpTime::Min) time=tpTime::Min;
00126      if(time >= tpTime::Max) time=tpTime::Max;
00127      mstUB=time;
00128      if(mstUB==tpTime::Max)  mbUBincl=false;                                  
00129    }
00130 
00131 
00132    /** 
00133     * Configures the upper bound to be inclusive.
00134     * If the upper bound is infinity, it stays exclusive.
00135     *
00136     * @param incl
00137     */
00138    void UBincl(bool incl) {
00139      mbUBincl=incl;
00140      if(mstUB>=tpTime::Max) mbUBincl=false;
00141    }
00142 
00143 
00144    /** 
00145     * Configures the lower bound to be inclusive.
00146     * If the lower bound is minus infinity, it stays exclusive.
00147     *
00148     * @param incl
00149     */
00150    void LBincl(bool incl) {
00151      mbLBincl=incl;
00152      if(mstLB<=tpTime::Min) mbLBincl=false;
00153    }
00154 
00155 
00156    /** 
00157     * Set upper bound to infinity
00158     */
00159    void setUBinf(void) {
00160      mstUB=tpTime::Max; mbUBincl=false; }
00161 
00162    /** 
00163     * Set lower bound to infinity
00164     */
00165    void setLBinf(void) {
00166      mstLB=tpTime::Min; mbLBincl=false; }
00167 
00168    /** 
00169     * Return upper bound.
00170     *
00171     * @return upper bound
00172     */
00173    tpTime::Type UB(void) const {return mstUB;}
00174 
00175    /** 
00176     * Return lower bound.
00177     *
00178     * @return lower bound
00179     */
00180    tpTime::Type LB(void) const {return mstLB;}
00181 
00182    /** 
00183     * Test for lower bound inclusive
00184     *
00185     * @return true for inclusive
00186     */
00187    bool LBincl(void) const {return mbLBincl;}
00188 
00189    /** 
00190     * Test for upper bound inclusive
00191     *
00192     * @return true for inclusive
00193     */
00194    bool UBincl(void) const {return mbUBincl;}
00195 
00196    /** 
00197     * Test for lower bound infinity
00198     *
00199     * @return true for infinity
00200     */
00201    bool LBinf(void) const {return mstLB == tpTime::Min; }
00202 
00203    /** 
00204     * Test for upper bound infinity
00205     *
00206     * @return true for infinity
00207     */
00208    bool UBinf(void) const {return mstUB == tpTime::Max;}
00209 
00210 
00211    /** 
00212     * Convert to canonical representation.
00213     *
00214     * Uses tpTime::Step to convert to aleft closed / right opened
00215     * representation
00216     */
00217    void Canonical(void);
00218 
00219    /** 
00220     * Set to full  (-inf, +inf)
00221     */
00222    void SetFull(void) {
00223      mstUB=tpTime::Max; mstLB=tpTime::Min; mbUBincl=false; mbLBincl=false; };
00224 
00225    /** 
00226     * Set to empty  (1, -1)
00227     */
00228    void SetEmpty(void) {
00229      mstUB=-1; mstLB=1; mbUBincl=false; mbLBincl=false; };
00230 
00231    /** 
00232     * Set to positive  [0, inf)
00233     */
00234    void SetPositive(void) {
00235      mstUB=tpTime::Max; mstLB=0; mbUBincl=false; mbLBincl=true; };
00236 
00237 
00238    /** 
00239     * Set to negative  (-inf, 0]
00240     */
00241    void SetNegative(void) {
00242      mstLB=tpTime::Min; mstUB=0; mbUBincl=false; mbLBincl=true; };
00243 
00244    /** 
00245     *  Test whether interval is full
00246     *
00247     *  @return
00248     *    True if interval is (-inf,+inf)
00249     */    
00250    bool Full(void) const { return (mstUB==tpTime::Max) && (mstLB==tpTime::Min) ;};
00251       
00252    /** 
00253     *  Test interval for empty set
00254     *
00255     *  @return
00256     *    True if interval is empty
00257     */    
00258    bool Empty(void) const;
00259       
00260    /**
00261     * Test whether interval includes [0,inf)
00262     *
00263     * @return
00264     *    True if interval includes [0,inf)
00265     */
00266    bool IncludesPositive(void) const {  
00267       return ( (mstLB<0) || ((mstLB ==0) && mbLBincl) ) && (mstUB==tpTime::Max);};
00268 
00269    /**
00270     * Test whether interval includes (-inf,0]
00271     *
00272     * @return
00273     *    True if interval includes (-inf,0]
00274     */
00275    bool IncludesNegative(void) const {  
00276       return ( (mstUB>0) || ((mstUB ==0) && mbUBincl) ) && (mstLB==tpTime::Min);};
00277 
00278    /**
00279     * Test whether a point satisfies interval
00280     * @return
00281     *    True if interval includes the point
00282     */
00283    bool In(tpTime::Type time) const;
00284 
00285 
00286    /**
00287     * Test whether two intervals are equal.
00288     *
00289     * Note that the test is strictly based on the
00290     * internal representation, ie [0,10] is not equal to [0,11). This
00291     * may change in future implementations to consider the tpTime::TypeStep
00292     *
00293     * @return
00294     *    True on equality
00295     */
00296    bool operator == (const TimeInterval& rOtherInterval) const {
00297      if(mstUB!=rOtherInterval.mstUB) return false;
00298      if(mstLB!=rOtherInterval.mstLB) return false;
00299      if(mbUBincl!=rOtherInterval.mbUBincl) return false;
00300      if(mbLBincl!=rOtherInterval.mbLBincl) return false;
00301      return true;
00302    }
00303 
00304    /**
00305     * Test whether two intervals not equal.
00306     *
00307     * Note that the test is strictly based on the
00308     * internal representation, ie [0,10] is not equal to [0,11). This
00309     * may change in future implementations to consider the tpTime::TypeStep
00310     *
00311     * @return
00312     *    False on equality
00313     */
00314    bool operator != (const TimeInterval& rOtherInterval) const {
00315      return ! (*this == rOtherInterval) ;
00316    }
00317 
00318    /**
00319     * Transform by left shift and intersection with [0, inf)
00320     * @param time
00321     *    Amount to shift left
00322     *
00323     */
00324    void PositiveLeftShift(tpTime::Type time);
00325 
00326    /** 
00327     * Intersect this interval with other interval
00328     *
00329     * @param rOtherInterval
00330     *   
00331     */
00332    void Intersect(const TimeInterval& rOtherInterval);
00333       
00334    /** 
00335     * Intersection of two time intervals.
00336     *
00337     * @param rInterval1
00338     *   Reference of first time interval
00339     * @param rInterval2
00340     *   Reference of second time interval
00341     *   
00342     * @return Intersection of both intervals.
00343     */
00344    static TimeInterval Intersect(const TimeInterval& rInterval1, const TimeInterval& rInterval2);
00345       
00346 
00347    /** 
00348     * Merge this interval with other interval.
00349     * I.e. find smallest superset of the union of the two intervals.
00350     *
00351     * @param rOtherInterval
00352     *   Reference to other time interval
00353     *   
00354     */
00355    void Merge(const TimeInterval& rOtherInterval);
00356 
00357    /** 
00358     * Merge this interval with other interval.
00359     * I.e. find smallest superset of the union of the two intervals.
00360     *
00361     * @param rInterval1
00362     *   Reference of first time interval
00363     * @param rInterval2
00364     *   Reference of second time interval
00365     *   
00366     *   @return Superset of both intervals. 
00367     */
00368    static TimeInterval Merge(const TimeInterval& rInterval1, const TimeInterval& rInterval2);
00369             
00370    /** 
00371     * Pretty printable string.
00372     *
00373     * @return printable string.
00374     */
00375    std::string Str(void) const;
00376       
00377     
00378  private:
00379 
00380    /** Upper bound */    
00381    tpTime::Type mstUB;
00382 
00383    /** Lower bound */
00384    tpTime::Type mstLB;
00385 
00386    /** Flag to indicate that the upper bound is part of the interval */
00387    bool mbUBincl;
00388 
00389    /** Flag to indicate that lower boundary is part of the interval. */
00390    bool mbLBincl;
00391 
00392 };
00393 
00394 
00395 } // end namespace faudes
00396 
00397 
00398 #endif

libFAUDES 2.23h --- 2014.04.03 --- c++ api documentaion by doxygen