tp_timeinterval.h
Go to the documentation of this file.
1 /** @file tp_timeinterval.h Class TimeInterval */
2 
3 
4 /*
5  Time plug-in for FAU Discrete Event Systems Library (libfaudes)
6 
7  Copyright (C) 2007 Ruediger Berndt
8  Copyright (C) 2007 Thomas Moor
9  Exclusive copyright is granted to Klaus Schmidt
10 
11 */
12 
13 #ifndef FAUDES_TP_TIMEINTERVAL_H
14 #define FAUDES_TP_TIMEINTERVAL_H
15 
16 #include "corefaudes.h"
17 #include <cstdlib>
18 #include <cstring>
19 #include <cfloat>
20 #include <limits>
21 #include <iostream>
22 
23 
24 namespace faudes {
25 
26 /**
27  * Type to represent time.
28  *
29  * The time plugin will compile with integer or float time type. However,
30  * we insist in discrete time in the sense that there is a smallest
31  * time step. So float time would be cosmetic only.
32  *
33  * Note that timed generators do not explicitely refer to a physical time unit like e.g.
34  * hours or seconds. However, it is assumed, that a fixed conversion factor
35  * from values of type Time to some physical unit exists. Thus, we say Time
36  * variables hold time in faudes-time units. We feel that this is consistent with
37  * Alur's timed automata.
38  *
39  * @ingroup TimedPlugin
40  */
42  public:
43  /** Datatype for point on time axis */
44  typedef Int Type;
45  /** Maximum time, associated with infinitiy */
46  static Type Max(void) {return std::numeric_limits<Int>::max();};
47  /** Minimum time, associated with minus infinitiy */
48  static Type Min(void) {return std::numeric_limits<Int>::min()+1;};
49  /** Undefined time value */
50  static Type UnDef(void) {return std::numeric_limits<Int>::min();};
51  /** Smallest representable time step */
52  static Type Step(void) {return 1;};
53  /** Discretize (when using cosmetic float time: "step*(round(t/step))" */
54  static Type Quantize(Float x) {return (Type)(x+0.5);};
55  /** Convert From float (for convenient token-reading only) */
56  static Float FromFloat(Float x) {
57  if(x<=std::numeric_limits<Float>::min()) return Min();
58  if(x>=std::numeric_limits<Float>::max()) return Max();
59  return (Int) (x+0.5);
60  }
61  /** convert to string */
62  std::string static Str(Type time) {
63  if(time == UnDef()) return "undef";
64  if(time < Min()) return "err";
65  if(time == Min()) return "-inf";
66  if(time > Max()) return "err";
67  if(time == Max()) return "inf";
68  return ToStringInteger(time);
69  };
70 };
71 
72 
73 
74 
75 /**
76  * Model of a time interval. An interval consisits of a lower and upper bound,
77  * plus flags to indicate whether the bounds are inclusive or not.
78  * The maximum and minimum values of the time data tyle are interpreted as infinity.
79  *
80  *
81  * @ingroup TimedPlugin
82  */
84 
85  public:
86 
87  /**
88  * Default constructor - sets time interval to (-inf; +inf)
89  */
90  TimeInterval(void) {SetFull();};
91 
92  /**
93  * Default destructor
94  */
95  ~TimeInterval(void) {};
96 
97  /**
98  * Set the lower bound to a given value.
99  * If the value is minus infinity, it is set exclusive. Otherwise,
100  * the exclusive/inclusive flag is kept.
101  *
102  * @param time
103  * The new value of lower bound
104  */
105  void LB(Time::Type time) {
106  if(time <= Time::Min()) time=Time::Min();
107  if(time >= Time::Max()) time=Time::Max();
108  mstLB=time;
109  if(mstLB==Time::Min()) mbLBincl=false;
110  }
111 
112 
113  /**
114  * Set the upper bound to a given value.
115  * If the value is infinity, it is set exclusive. Otherwise,
116  * the exclusive/inclusive flag is kept.
117  *
118  * @param time
119  * The new value of upper bound
120  */
121  void UB(Time::Type time) {
122  if(time <= Time::Min()) time=Time::Min();
123  if(time >= Time::Max()) time=Time::Max();
124  mstUB=time;
125  if(mstUB==Time::Max()) mbUBincl=false;
126  }
127 
128 
129  /**
130  * Configures the upper bound to be inclusive.
131  * If the upper bound is infinity, it stays exclusive.
132  *
133  * @param incl
134  */
135  void UBincl(bool incl) {
136  mbUBincl=incl;
137  if(mstUB>=Time::Max()) mbUBincl=false;
138  }
139 
140 
141  /**
142  * Configures the lower bound to be inclusive.
143  * If the lower bound is minus infinity, it stays exclusive.
144  *
145  * @param incl
146  */
147  void LBincl(bool incl) {
148  mbLBincl=incl;
149  if(mstLB<=Time::Min()) mbLBincl=false;
150  }
151 
152 
153  /**
154  * Set upper bound to infinity
155  */
156  void setUBinf(void) {
157  mstUB=Time::Max(); mbUBincl=false; }
158 
159  /**
160  * Set lower bound to infinity
161  */
162  void setLBinf(void) {
163  mstLB=Time::Min(); mbLBincl=false; }
164 
165  /**
166  * Return upper bound.
167  *
168  * @return upper bound
169  */
170  Time::Type UB(void) const {return mstUB;}
171 
172  /**
173  * Return lower bound.
174  *
175  * @return lower bound
176  */
177  Time::Type LB(void) const {return mstLB;}
178 
179  /**
180  * Test for lower bound inclusive
181  *
182  * @return true for inclusive
183  */
184  bool LBincl(void) const {return mbLBincl;}
185 
186  /**
187  * Test for upper bound inclusive
188  *
189  * @return true for inclusive
190  */
191  bool UBincl(void) const {return mbUBincl;}
192 
193  /**
194  * Test for lower bound infinity
195  *
196  * @return true for infinity
197  */
198  bool LBinf(void) const {return mstLB == Time::Min(); }
199 
200  /**
201  * Test for upper bound infinity
202  *
203  * @return true for infinity
204  */
205  bool UBinf(void) const {return mstUB == Time::Max();}
206 
207 
208  /**
209  * Convert to canonical representation.
210  *
211  * Uses Time::Step to convert to aleft closed / right opened
212  * representation
213  */
214  void Canonical(void);
215 
216  /**
217  * Set to full (-inf, +inf)
218  */
219  void SetFull(void) {
220  mstUB=Time::Max(); mstLB=Time::Min(); mbUBincl=false; mbLBincl=false; };
221 
222  /**
223  * Set to empty (1, -1)
224  */
225  void SetEmpty(void) {
226  mstUB=-1; mstLB=1; mbUBincl=false; mbLBincl=false; };
227 
228  /**
229  * Set to positive [0, inf)
230  */
231  void SetPositive(void) {
232  mstUB=Time::Max(); mstLB=0; mbUBincl=false; mbLBincl=true; };
233 
234 
235  /**
236  * Set to negative (-inf, 0]
237  */
238  void SetNegative(void) {
239  mstLB=Time::Min(); mstUB=0; mbUBincl=false; mbLBincl=true; };
240 
241  /**
242  * Test whether interval is full
243  *
244  * @return
245  * True if interval is (-inf,+inf)
246  */
247  bool Full(void) const { return (mstUB==Time::Max()) && (mstLB==Time::Min()) ;};
248 
249  /**
250  * Test interval for empty set
251  *
252  * @return
253  * True if interval is empty
254  */
255  bool Empty(void) const;
256 
257  /**
258  * Test whether interval includes [0,inf)
259  *
260  * @return
261  * True if interval includes [0,inf)
262  */
263  bool IncludesPositive(void) const {
264  return ( (mstLB<0) || ((mstLB ==0) && mbLBincl) ) && (mstUB==Time::Max());};
265 
266  /**
267  * Test whether interval includes (-inf,0]
268  *
269  * @return
270  * True if interval includes (-inf,0]
271  */
272  bool IncludesNegative(void) const {
273  return ( (mstUB>0) || ((mstUB ==0) && mbUBincl) ) && (mstLB==Time::Min());};
274 
275  /**
276  * Test whether a point satisfies interval
277  * @return
278  * True if interval includes the point
279  */
280  bool In(Time::Type time) const;
281 
282 
283  /**
284  * Test whether two intervals are equal.
285  *
286  * Note that the test is strictly based on the
287  * internal representation, ie [0,10] is not equal to [0,11). This
288  * may change in future implementations to consider the Time::TypeStep
289  *
290  * @return
291  * True on equality
292  */
293  bool operator == (const TimeInterval& rOtherInterval) const {
294  if(mstUB!=rOtherInterval.mstUB) return false;
295  if(mstLB!=rOtherInterval.mstLB) return false;
296  if(mbUBincl!=rOtherInterval.mbUBincl) return false;
297  if(mbLBincl!=rOtherInterval.mbLBincl) return false;
298  return true;
299  }
300 
301  /**
302  * Test whether two intervals not equal.
303  *
304  * Note that the test is strictly based on the
305  * internal representation, ie [0,10] is not equal to [0,11). This
306  * may change in future implementations to consider the Time::TypeStep
307  *
308  * @return
309  * False on equality
310  */
311  bool operator != (const TimeInterval& rOtherInterval) const {
312  return ! (*this == rOtherInterval) ;
313  }
314 
315  /**
316  * Transform by left shift and intersection with [0, inf)
317  * @param time
318  * Amount to shift left
319  *
320  */
321  void PositiveLeftShift(Time::Type time);
322 
323  /**
324  * Intersect this interval with other interval
325  *
326  * @param rOtherInterval
327  *
328  */
329  void Intersect(const TimeInterval& rOtherInterval);
330 
331  /**
332  * Intersection of two time intervals.
333  *
334  * @param rInterval1
335  * Reference of first time interval
336  * @param rInterval2
337  * Reference of second time interval
338  *
339  * @return Intersection of both intervals.
340  */
341  static TimeInterval Intersect(const TimeInterval& rInterval1, const TimeInterval& rInterval2);
342 
343 
344  /**
345  * Merge this interval with other interval.
346  * I.e. find smallest superset of the union of the two intervals.
347  *
348  * @param rOtherInterval
349  * Reference to other time interval
350  *
351  */
352  void Merge(const TimeInterval& rOtherInterval);
353 
354  /**
355  * Merge this interval with other interval.
356  * I.e. find smallest superset of the union of the two intervals.
357  *
358  * @param rInterval1
359  * Reference of first time interval
360  * @param rInterval2
361  * Reference of second time interval
362  *
363  * @return Superset of both intervals.
364  */
365  static TimeInterval Merge(const TimeInterval& rInterval1, const TimeInterval& rInterval2);
366 
367  /**
368  * Pretty printable string.
369  *
370  * @return printable string.
371  */
372  std::string Str(void) const;
373 
374 
375  private:
376 
377  /** Upper bound */
379 
380  /** Lower bound */
382 
383  /** Flag to indicate that the upper bound is part of the interval */
384  bool mbUBincl;
385 
386  /** Flag to indicate that lower boundary is part of the interval. */
387  bool mbLBincl;
388 
389 };
390 
391 
392 } // end namespace faudes
393 
394 
395 #endif
#define FAUDES_API
Interface export/import symbols: windows.
Definition: cfl_platform.h:81
Model of a time interval.
bool IncludesNegative(void) const
Test whether interval includes (-inf,0].
void LB(Time::Type time)
Set the lower bound to a given value.
void SetPositive(void)
Set to positive [0, inf)
Time::Type LB(void) const
Return lower bound.
Time::Type mstLB
Lower bound.
bool LBincl(void) const
Test for lower bound inclusive.
Time::Type UB(void) const
Return upper bound.
bool IncludesPositive(void) const
Test whether interval includes [0,inf)
bool UBinf(void) const
Test for upper bound infinity.
void SetNegative(void)
Set to negative (-inf, 0].
Time::Type mstUB
Upper bound.
void setLBinf(void)
Set lower bound to infinity.
bool UBincl(void) const
Test for upper bound inclusive.
void setUBinf(void)
Set upper bound to infinity.
bool Full(void) const
Test whether interval is full.
void UBincl(bool incl)
Configures the upper bound to be inclusive.
~TimeInterval(void)
Default destructor.
void LBincl(bool incl)
Configures the lower bound to be inclusive.
void UB(Time::Type time)
Set the upper bound to a given value.
bool mbUBincl
Flag to indicate that the upper bound is part of the interval.
void SetEmpty(void)
Set to empty (1, -1)
void SetFull(void)
Set to full (-inf, +inf)
bool LBinf(void) const
Test for lower bound infinity.
bool mbLBincl
Flag to indicate that lower boundary is part of the interval.
TimeInterval(void)
Default constructor - sets time interval to (-inf; +inf)
Type to represent time.
static Type Min(void)
Minimum time, associated with minus infinitiy.
static Type UnDef(void)
Undefined time value.
Int Type
Datatype for point on time axis.
static Type Quantize(Float x)
Discretize (when using cosmetic float time: "step*(round(t/step))".
static std::string Str(Type time)
convert to string
static Type Step(void)
Smallest representable time step.
static Type Max(void)
Maximum time, associated with infinitiy.
static Float FromFloat(Float x)
Convert From float (for convenient token-reading only)
Includes all libFAUDES headers, no plugins.
libFAUDES resides within the namespace faudes.
double Float
Type definition for real type.
std::string ToStringInteger(Int number)
integer to string
Definition: cfl_helper.cpp:43
long int Int
Type definition for integer type (let target system decide, minimum 32bit)

libFAUDES 2.32b --- 2024.03.01 --- c++ api documentaion by doxygen