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

libFAUDES 2.33k --- 2025.09.16 --- c++ api documentaion by doxygen