tp_timeconstraint.h
Go to the documentation of this file.
1 /** @file tp_timeconstraint.h Classes ClockSet, ElemConstraint and TimeConstraint */
2 
3 
4 
5 /* Timeplugin for FAU Discrete Event Systems Library (libfaudes)
6 
7  Copyright (C) 2006 B Schlein
8  Copyright (C) 2007 Thomas Moor
9  Exclusive copyright is granted to Klaus Schmidt
10 
11 */
12 
13 #ifndef FAUDES_TP_TIMECONSTRAINT_H
14 #define FAUDES_TP_TIMECONSTRAINT_H
15 
16 #include "corefaudes.h"
17 #include "tp_timeinterval.h"
18 #include <list>
19 
20 
21 namespace faudes {
22 
23 
24 
25 /**
26  * Container class to model a set of clocks.
27  * Technically, this is a NameSet with a static SymbolTable to map
28  * symbolic clock names to clock indices. Thus, clock names are
29  * global similar to event names. Note that clocksets of individual
30  * TtGenerators are assumed to be disjoint.
31  *
32  * Todo: explicitely inherit other constructors (!)
33  *
34  * @ingroup TimedPlugin
35  *
36  */
37 
38 class FAUDES_API ClockSet : public NameSet {
39 
41 
42  public:
43 
44  /**
45  * Constructor
46  */
47  ClockSet(void);
48 
49  /**
50  * Copy-constructor.
51  *
52  * @param rOtherSet
53  * Set to copy
54  */
55  ClockSet(const ClockSet& rOtherSet);
56 
57  /**
58  * Construct from file.
59  * Uses the NameSet's Read() function to scan a file for a
60  * specified clockset.
61  *
62  * @param rFilename
63  * File to read
64  * @param rLabel
65  * Section label for the clocks in the file; default value "Clocks"
66  */
67  ClockSet(const std::string& rFilename, const std::string& rLabel = "Clocks");
68 
69  /**
70  * Get pointer to static clock SymbolTable
71  * (init on first use pattern)
72  * @return
73  * Pointer to static clock SymbolTable
74  */
75  static SymbolTable* GlobalClockSymbolTablep(void);
76 
77  protected:
78 
79  /**
80  * Assign from other clock set.
81  *
82  * @param rSourceSet
83  * Destination to copy from
84  * @return
85  * ref to this set
86  */
87  virtual void DoAssign(const ClockSet& rSourceSet);
88 
89  /**
90  * Test equality with other clock set.
91  *
92  * @param rOtherSet
93  * Set to compare with
94  * @return
95  * True/false
96  */
97  virtual bool DoEqual(const ClockSet& rOtherSet) const;
98 
99 }; // end class ClockSet
100 
101 
102 
103 /**
104  * Model of an elementary clock constraint formula. The constraint consists of an index
105  * of a clock, a comperative operator and a constant time value.
106  * Semantically, an elementary contraint is satisfied at a time t if at that time
107  * the clock value compares true with the constant time.
108  * The clock index 0 is used to indicate an invalid contraint. It is only the
109  * context of the more general TimeContraint that provides
110  * a reference to a clock SymbolTable.
111  *
112  * @param mClock
113  * Clock index
114  *
115  * @param mCompOperator
116  * Symbolic integer value for comperative operator
117  *
118  * @param mTimeConstant
119  * Time constant for comparison
120  *
121  * @ingroup TimedPlugin
122  *
123  */
124 
126 
127  public:
128 
129  /**
130  * Typedef for comparison operators in elementary clock constraints
131  *
132  */
133  typedef enum {GreaterEqual, GreaterThan, LessThan, LessEqual} Operator;
134 
135  /**
136  * Conversion from symbolic operator to string
137  */
138  static std::string OperatorName(Operator op);
139 
140  /**
141  * Construct an (invalid) elementary clock constraint (clockindex 0)
142  *
143  */
144  ElemConstraint(void);
145 
146  /**
147  * Construct an elementary clock constraint from values
148  *
149  * @param clockindex
150  * Clock by index.
151  * @param op
152  * Symbolic value for operator
153  * @param timeconst
154  * Value for time constant
155  */
156  ElemConstraint(Idx clockindex, Operator op, Time::Type timeconst);
157 
158  /**
159  * Set all values
160  *
161  * @param clockindex
162  * Clock by index.
163  * @param op
164  * Symbolic value for operator
165  * @param timeconst
166  * Value for time constant
167  */
168  void Set(Idx clockindex, Operator op, Time::Type timeconst);
169 
170  /**
171  * Set clock by index
172  */
173  Idx Clock(Idx newClock);
174 
175  /**
176  * Get clock by index
177  *
178  * @return clock index
179  */
180  Idx Clock(void) const;
181 
182  /**
183  * Set operator.
184  *
185  * @param newOp
186  * Symbolic value of new operator
187  */
188  void CompOperator(Operator newOp);
189 
190  /**
191  * Get operator
192  * @return Operator
193  */
194  Operator CompOperator(void) const;
195 
196  /**
197  * Set time constant.
198  *
199  * @param newTimeConst
200  * Value of new time constant
201  */
202  void TimeConstant(Time::Type newTimeConst);
203 
204  /**
205  * Get time constant
206  * @return Time constant
207  */
208  Time::Type TimeConstant(void) const;
209 
210  /**
211  * Writes ElemConstraint to std::string
212  *
213  * @return std::string
214  */
215  std::string Str(void) const;
216 
217  /**
218  * Test for equality.
219  *
220  * @param otherElemConstraint
221  * Other elementary constraint
222  * @return
223  * True if mClockIndex, mTimeConstant and mCompOperator are equal. Else false.
224  */
225  bool operator== (const ElemConstraint & otherElemConstraint) const;
226 
227  /**
228  * Test for equality.
229  *
230  * @param otherElemConstraint
231  * Other elementary constraint
232  * @return
233  * True if not equal; see operator==.
234  */
235  bool operator!= (const ElemConstraint & otherElemConstraint) const;
236 
237  /**
238  * Less operator for ordering in container classes
239  *
240  * @param otherElemConstraint
241  * Other ElemConstraint
242  *
243  * @return True when clock index of left constraint is less
244  * than the one of right constraint. Else false.
245  */
246  bool operator < (const ElemConstraint& otherElemConstraint) const;
247 
248 
249  protected:
250 
251  /** Index of clock. */
253 
254  /** Comparative operator */
256 
257  /** Time constant to compare with clock value */
259 
260 }; // ElemConstraint
261 
262 
263 
264 /**
265  * A TimeConstraint is a set of elementary clock constraints. Semantically,
266  * the elementary constraints are combibed by conjunction, ie the TimeConstraint is
267  * satisfied if all ElemConstraint s are satisfied. This implementation also maintains
268  * the clock symboltable to may cloc indices to symbolic names.
269  *
270  * @param ClockConstraints
271  * Set of elementary clock constraints
272  *
273  * @ingroup TimedPlugin
274  */
275 
277 
278  public:
279 
280  /** Iterator to access ElemConstraints */
281  typedef std::set<ElemConstraint>::const_iterator Iterator;
282 
283  /** Reverse iterator to access ElemConstraints */
284  typedef std::set<ElemConstraint>::const_reverse_iterator RIterator;
285 
286  /** Convenience typedef for operators */
288 
289  /**
290  * Construct an empty TimeConstraint (allways satisfied)
291  */
292  TimeConstraint(void);
293 
294  /**
295  * Copy constructor
296  *
297  * @param rOtherTimeConstraint
298  * Time constraint to copy
299  */
300  TimeConstraint(const TimeConstraint& rOtherTimeConstraint);
301 
302  /**
303  * Constructor from file.
304  *
305  * Uses Read() to scan a file for specified label to read the constraint.
306  *
307  * @param rFilename
308  * File to read
309  * @param rLabel
310  * Section label for the set in the file.
311  */
312  TimeConstraint(const std::string& rFilename, const std::string& rLabel = "TimeConstraint");
313 
314  /**
315  * Destructor
316  */
317  ~TimeConstraint(void);
318 
319 
320  /**
321  * Get Pointer to mpClockSymbolTable.
322  *
323  * @return
324  * Pointer to mpClockSymbolTable
325  */
326  SymbolTable* ClockSymbolTablep(void) const;
327 
328 
329  /**
330  * Set Pointer to mpClockSymbolTable.
331  *
332  */
333  void ClockSymbolTablep(SymbolTable* pSymTab);
334 
335  /**
336  * Write to console
337  */
338  void Write(void) const;
339 
340  /**
341  * Write to file with label (default: "TimeConstraint") and
342  * openmode (default: truncate file).
343  *
344  * @param rFileName
345  * Filename
346  * @param rLabel
347  * Label for set in file
348  * @param openmode
349  * std::ios::openmode
350  *
351  * @exception Exception
352  * If opening/writing fails an Exception object is thrown (id 2)
353  */
354  void Write(const std::string& rFileName, const std::string& rLabel = "TimeConstraint",
355  std::ios::openmode openmode = std::ios::out|std::ios::trunc) const;
356 
357 
358  /**
359  * Write to TokenWriter. The name of the constraint is used
360  * as the label in the file.
361  *
362  * @param tw
363  * Reference to TokenWriter
364  *
365  * @exception std::ios::failure
366  * Thrown on i/o error.
367  */
368  void Write(TokenWriter& tw) const;
369 
370  /**
371  * Write to TokenWriter with a given label
372  *
373  * @param tw
374  * Reference to TokenWriter
375  * @param rLabel
376  * Label for set in file
377  *
378  * @exception std::ios::failure
379  * Thrown on i/o error.
380  */
381  void Write(TokenWriter& tw, const std::string& rLabel) const;
382 
383  /**
384  * Write to a std::string
385  *
386  * @return
387  * String containing all elements
388  */
389  std::string ToString(void) const;
390 
391  /**
392  * Write NameSet to console, debug version
393  */
394  void DWrite(void) const;
395 
396  /** Write to TokenWriter, debug version
397  *
398  * @param tw
399  * Reference to TokenWriter
400  *
401  * @exception std::ios::failure
402  * Thrown on i/o error.
403  */
404  void DWrite(TokenWriter& tw) const;
405 
406  /**
407  * Read from file. Reads specified label by creating a tokenreader and
408  * calling read(tr)
409  *
410  * @param rFileName
411  * Filename
412  * @param rLabel
413  * token label to read from file
414  *
415  * @exception Exception
416  * If opening/reading fails an Exception object is thrown (id 1, 50, 51, 56)
417  */
418  void Read(const std::string& rFileName, const std::string& rLabel = "TimeConstraint");
419 
420 
421  /**
422  * Read from tokenreader. Clears before. It is an error
423  * if the file contains an index
424  *
425  * @param tr
426  * Reference to tokenreader
427  * @param rLabel
428  * Label to read
429  *
430  * @exception std::ios::failure
431  * Thrown on i/o error.
432  */
433  void Read(TokenReader& tr, const std::string& rLabel = "TimeConstraint");
434 
435 
436  /**
437  * Return name of Constraint
438  *
439  * @return
440  * Name
441  */
442  std::string Name(void) const { return mName;};
443 
444  /**
445  * Set name of Constraint
446  *
447  * @param rName
448  * BaseSet name
449  */
450  void Name(const std::string& rName) { mName=rName; };
451 
452 
453  /**
454  * Checks if TimeConstraint containts no ElemConstraints
455  *
456  * @return
457  * True if TimeConstraint is empty. Else false.
458  */
459  bool Empty(void) const;
460 
461  /**
462  * Returns number of ElemConstraint s.
463  *
464  * @return Number of ElemConstraint s.
465  */
466  Idx Size(void) const;
467 
468  /**
469  * Adds an elementary clock constraint to the time constraint.
470  *
471  * @param rElemConstr
472  * Elementary clock constraint that is to be added to time constraint
473  * @return
474  * iterator to new constraint
475  */
476  Iterator Insert(const ElemConstraint& rElemConstr);
477 
478  /**
479  * Adds an elementary clock constraint to the time constraint.
480  *
481  * @param clockname
482  * Clock for new ElemConstraint
483  * @param op
484  * Operator for new ElemConstraint
485  * @param timeconst
486  * Time constant for new ElemConstraint
487  * @return
488  * iterator to new constraint
489  */
490  Iterator Insert(const std::string clockname, Operator op, const Time::Type timeconst);
491 
492  /**
493  * Adds an elementary clock constraint to the time constraint.
494  *
495  * @param clockindex
496  * Clock for new ElemConstraint
497  * @param op
498  * Operator for new ElemConstraint
499  * @param timeconst
500  * Time constant for new ElemConstraint
501  * @return
502  * iterator to new constraint
503  */
504  Iterator Insert(Idx clockindex, Operator op, const Time::Type timeconst);
505 
506  /**
507  * Adds a list of elementary clock constraints to the time constraint.
508  *
509  * @param rNewConstraints
510  * Set of elementary clock constraints that is to be added to time constraint.
511  */
512  void Insert(const std::list<ElemConstraint>& rNewConstraints);
513 
514  /**
515  * Adds elementary clock constraints from other TimeConstant to the time constraint.
516  *
517  * @param rOtherTimeConstraint
518  * Time constraint whos clock constraints are to be added to time constraint.
519  */
520  void Insert(const TimeConstraint& rOtherTimeConstraint);
521 
522  /**
523  * Convenience operator to combine a TimeConstraint with another TimeConstraint.
524  *
525  * @param rOtherTimeConstraint
526  * other TimeConstraint
527  *
528  * @return TimeConstraint object containing all ElemConstraints of both TimeConstraints.
529  */
530  TimeConstraint& operator << (const TimeConstraint& rOtherTimeConstraint) {
531  this->Insert(rOtherTimeConstraint); return *this; };
532 
533  /**
534  * Convenience operator to combines a TimeConstraint with an elementary TimeConstraint.
535  *
536  * @param rElemConstr
537  * extra elemtary constraint
538  *
539  * @return TimeConstraint object containing all ElemConstraints of both TimeConstraints.
540  */
541  TimeConstraint& operator << (const ElemConstraint& rElemConstr) {
542  this->Insert(rElemConstr); return *this; };
543 
544  /**
545  * Returns copy of ClockConstraints
546  */
547  std::set<ElemConstraint> ClockConstraints(void) const;
548 
549  /**
550  * Advertise clock to ClockSymbolTable and retrive index
551  *
552  * @param rClockName
553  * symbolic name of clock
554  *
555  * @return
556  * index of clock
557  */
558  Idx InsClock(const std::string& rClockName) const;
559 
560  /**
561  * Lookup clock name
562  *
563  * @param clockindex
564  * index of clock
565  *
566  * @return
567  * name of clock
568  */
569  std::string ClockName(Idx clockindex) const;
570 
571  /**
572  * Lookup clock index
573  *
574  * @param rClockName
575  * Symbolic name of clock
576  *
577  * @return
578  * Index of clock
579  */
580  Idx ClockIndex(const std::string& rClockName) const;
581 
582  /**
583  * Pretty printable string of elem. constraint
584  *
585  * @param rElemConstr
586  * ref to elem constraint
587  *
588  * @return
589  * output string
590  */
591  std::string EStr(const ElemConstraint& rElemConstr) const;
592 
593  /**
594  * Removes all elementary clock constraints refering to a specified clock.
595  *
596  * @param clock
597  * Clock whos constraints are to be removed.
598  * @return
599  * True, if constraints(s) have been erased
600  */
601  bool EraseByClock(Idx clock);
602 
603  /**
604  * Calls std::set::erase(iterator). ElemConstraint refered by it is removed from constraint.
605  *
606  * @param it
607  * TimeConstraint::Iterator pointing to ElemConstraint that is removed.
608  *
609  * @return
610  * Iterator pointing to next ElemConstraint.
611  */
612  Iterator Erase(Iterator it);
613 
614  /**
615  * Removes elementary clock constraint.
616  *
617  * @param rElemConstr
618  * Constraint to be removed.
619  * @return
620  * True, if constraint has been removed
621  */
622  bool Erase(const ElemConstraint& rElemConstr);
623 
624  /**
625  * Removes elementary clock constraint.
626  *
627  * @param rClockName
628  * Clock
629  * @param op
630  * Operator
631  * @param timeconst
632  * Time constant
633  * @return
634  * True, if constraint has been removed
635  */
636  bool Erase(const std::string& rClockName, Operator op, const Time::Type timeconst);
637 
638  /**
639  * Removes elementary clock constraint.
640  *
641  * @param clockindex
642  * Clock
643  * @param op
644  * Operator
645  * @param timeconst
646  * Time constant
647  * @return
648  * True, if constraint has been removed
649  */
650  bool Erase(Idx clockindex, Operator op, const Time::Type timeconst);
651 
652  /**
653  * Clear all
654  */
655  void Clear(void);
656 
657 
658  /**
659  * Checks if elementary clock constraint is contained in constraint
660  *
661  * @param rElemConstr
662  * elementary constraint
663  */
664  bool Exists(const ElemConstraint& rElemConstr) const;
665 
666  /**
667  * Iterator to begin of set
668  *
669  * @return
670  * TimeConstraint::Iterator
671  */
672  Iterator Begin(void) const;
673 
674  /**
675  * Iterator to end of set
676  *
677  * @return
678  * TimeConstraint::Iterator
679  */
680  Iterator End(void) const;
681 
682  /**
683  * Reverse iterator that yields the ElemConstraints in reverse order starting at
684  * the last element and ending after the first. See set<...>::rbegin(void).
685  * Returns the "End".
686  *
687  * @return
688  * TimeConstraint::RIterator
689  */
690  RIterator RBegin(void) const;
691 
692  /**
693  * Reverse iterator that yields the ElemConstraints in reverse order starting at
694  * the last element and ending after the first. See set<...>::rend(void).
695  * Returns the "Begin".
696  *
697  * @return
698  * TimeConstraint::RIterator
699  */
700  RIterator REnd(void) const;
701 
702  /**
703  * Iterator to first constraint with specified clock
704  *
705  * @return
706  * TimeConstraint::iterator
707  */
708  Iterator Begin(Idx clock) const;
709 
710  /**
711  * Iterator to first constraint just behind specified clock
712  *
713  * @return
714  * TimeConstraint::Iterator
715  */
716  Iterator End(Idx clock) const;
717 
718  /**
719  * Returns a Clockset containing all clocks used
720  * by the TimeConstraint.
721  *
722  * @return Clockset containing all clocks used by the TimeConstraint.
723  */
724  ClockSet ActiveClocks(void) const;
725 
726  /**
727  * Given a clock, compute the timeinterval in which
728  * the constraint is satisfied
729  *
730  * @return TimeInterval
731  */
732  TimeInterval Interval(Idx clockindex) const;
733 
734  /**
735  * Given a clock, compute the timeinterval in which
736  * the constraint is satisfied
737  *
738  * @return TimeInterval
739  */
740  TimeInterval Interval(const std::string& clockname) const;
741 
742  /**
743  * Given a clock and an interval, set up the constraint such
744  * that it is valid in the given interval.
745  *
746  * @param clockindex
747  * specifies the clock
748  * @param rInterval
749  * reference to time interval
750  */
751  void Interval(Idx clockindex, const TimeInterval& rInterval);
752 
753 
754  /**
755  * Given a clock and an interval, set up the constraint such
756  * that it is valid in the given interval.
757  *
758  * @param rClockName
759  * specifies the clock
760  * @param rInterval
761  * reference to time interval
762  */
763  void Interval(const std::string& rClockName, const TimeInterval& rInterval);
764 
765  /**
766  * Minimize by eliminating redundant elementary constraints. The current implemantation
767  * retrieves the time constraints as intervals per clock and then converts back to
768  * a time constraint.
769  */
770  void Minimize(void);
771 
772 
773  /**
774  * Test for equality.
775  * The implementation converts both constraints to intervals and then
776  * performs the comparison.
777  *
778  * @param rOther
779  * Constraint to compare with.
780  * @return
781  * True if constraints are semantically identical.
782  */
783  bool operator== (const TimeConstraint& rOther) const;
784 
785  /**
786  * Test for equality.
787  *
788  * @param rOther
789  * Constraint to compare with.
790  * @return
791  * True if not equal; see operator==.
792  */
793  bool operator!= (const TimeConstraint & rOther) const;
794 
795  protected:
796 
797  /** My name */
798  std::string mName;
799 
800  /** Set of elementary clock constraints */
801  std::set<ElemConstraint> mClockConstraints;
802 
803  /** SymbolTable for clock names */
805 
806  /** nonconst iterator to access ElemConstraints */
807  typedef std::set<ElemConstraint>::iterator iterator;
808 
809 }; //TimeConstraint
810 
811 
812 } // namespace faudes
813 
814 
815 #endif
816 
#define FAUDES_API
Interface export/import symbols: windows.
Definition: cfl_platform.h:81
#define FAUDES_TYPE_DECLARATION(ftype, ctype, cbase)
faudes type declaration macro
Definition: cfl_types.h:867
Container class to model a set of clocks.
Model of an elementary clock constraint formula.
Operator
Typedef for comparison operators in elementary clock constraints.
Time::Type mTimeConstant
Time constant to compare with clock value.
Operator mCompOperator
Comparative operator.
Idx mClockIndex
Index of clock.
Set of indices with symbolic names.
Definition: cfl_nameset.h:69
A SymbolTable associates sybolic names with indices.
A TimeConstraint is a set of elementary clock constraints.
void Name(const std::string &rName)
Set name of Constraint.
std::set< ElemConstraint >::const_iterator Iterator
Iterator to access ElemConstraints.
std::set< ElemConstraint > mClockConstraints
Set of elementary clock constraints.
ElemConstraint::Operator Operator
Convenience typedef for operators.
std::set< ElemConstraint >::const_reverse_iterator RIterator
Reverse iterator to access ElemConstraints.
std::string Name(void) const
Return name of Constraint.
SymbolTable * mpClockSymbolTable
SymbolTable for clock names.
std::set< ElemConstraint >::iterator iterator
nonconst iterator to access ElemConstraints
std::string mName
My name.
Model of a time interval.
Int Type
Datatype for point on time axis.
A TokenReader reads sequential tokens from a file or string.
A TokenWriter writes sequential tokens to a file, a string or stdout.
Includes all libFAUDES headers, no plugins.
libFAUDES resides within the namespace faudes.
uint32_t Idx
Type definition for index type (allways 32bit)
Class TimeInterval.

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