tp_2_constraints.cpp
Go to the documentation of this file.
1 /** @file tp_2_constraints.cpp
2 
3 Tutorial, class faudes::TimeConstraint. This tutorial demonstrates
4 basic usage of faudes::TimeConstraint objects
5 
6 @ingroup Tutorials
7 
8 @include tp_2_constraints.cpp
9 
10 */
11 
12 
13 #include "libfaudes.h"
14 
15 
16 // make the faudes namespace available to our program
17 using namespace faudes;
18 
19 /*
20 
21 General Description of the TimeConstraints Class.
22 
23 TimeConstraints provides a container of ElemConstraints, the latter modeling Alur's
24 elementary clock contraints. Each ElemConstraint is a simple inequality that restricts
25 a clock e.g. "clock7 less than 120". In this library, clocks are required to have
26 symbolic names that are resolved via a SymbolTable reference that is member of TimeConstraints.
27 By default, the reference is set to a global SymbolTable. This is the same behaviour
28 as with events.
29 
30 
31 */
32 
33 
34 
35 /////////////////
36 // main program
37 /////////////////
38 
39 int main() {
40 
41  ////////////////////////////////////////////////////
42  // constraints file io and inspection
43  ////////////////////////////////////////////////////
44 
45  // Create a timeconstraint from file
46  TimeConstraint tc1("data/timeconstraint.txt");
47 
48  // Create an empty timeconstraint
49  TimeConstraint tc2;
50 
51  // Inspect on console
52  std::cout << "######################################\n";
53  std::cout << "# a timeconstraint \n";
54  tc1.DWrite();
55  std::cout << "######################################\n";
56 
57  // Write the constraint to a file
58  tc1.Write("tmp_timeconstraint.txt");
59 
60 
61  // Get set of active clocks
62  ClockSet clocks1=tc1.ActiveClocks();
63 
64  // Iterate over all clocks, inspect and convert to time interval
65  std::cout << "######################################\n";
66  std::cout << "# inspection ... \n";
67  ClockSet::Iterator cit;
68  for(cit = clocks1.Begin(); cit != clocks1.End(); cit++) {
69  std::cout << "clock: " << clocks1.SymbolicName(*cit) << "\n";
71  for(tit = tc1.Begin(*cit); tit != tc1.End(*cit); tit++) {
72  std::cout << "elem constraint: " << tc1.EStr(*tit) << "\n";
73  }
74  TimeInterval tint=tc1.Interval(*cit);
75  std::cout << "time interval: " << tint.Str() << "\n";
76  }
77  std::cout << "######################################\n";
78 
79 
80  ////////////////////////////////////////////////////
81  // edit constraint
82  ////////////////////////////////////////////////////
83 
84  // Some interaction
85  tc1.Insert("clockC", ElemConstraint::LessThan, 125);
86  tc1.Insert("clockC", ElemConstraint::GreaterThan, 10);
87  tc1.Insert("clockC", ElemConstraint::GreaterEqual, 10); // redundent, but still added
88  tc1.Insert("clockB", ElemConstraint::GreaterThan, 110);
89  tc1.Insert("clockB", ElemConstraint::GreaterThan, 110); // double, not added to set
90  tc1.Insert("clockB", ElemConstraint::LessEqual, 200);
91  tc1.Insert("clockB", ElemConstraint::LessEqual, 210); // redundent, but still added
92  tc1.Insert("clockB", ElemConstraint::LessThan, 220); // redundent, but still added
93 
94  // Report
95  std::cout << "######################################\n";
96  std::cout << "# after various insertions \n";
97  tc1.DWrite();
98  std::cout << "######################################\n";
99 
100  // Erase an elementary constraint
101  tc1.Erase("clockB", ElemConstraint::LessEqual, 200);
102 
103  // Report
104  std::cout << "######################################\n";
105  std::cout << "# after erase \n";
106  tc1.DWrite();
107  std::cout << "######################################\n";
108 
109  // Introduce constraints to represent a valid interval
110  TimeInterval tint; // to become "(150,300]"
111  tint.LB(150);
112  tint.UB(300);
113  tint.UBincl(true);
114  tc2.Interval("clockB",tint);
115  tc2.Interval("clockD",tint);
116 
117  // Report
118  std::cout << "######################################\n";
119  std::cout << "# another constraint \n";
120  tc2.DWrite();
121  std::cout << "######################################\n";
122 
123 
124  // Merge two time constraints (aka intersection)
125  tc1.Insert(tc2);
126 
127  // Remove redundant elem. constraints
128  tc1.Minimize();
129 
130  // Report
131  std::cout << "######################################\n";
132  std::cout << "# minimal version \n";
133  tc1.DWrite();
134  std::cout << "######################################\n";
135 
136  // Test protocol
137  FAUDES_TEST_DUMP("final constraint",tc1.ToString());
138 
139 
140  return 0;
141 }
142 
143 
144 
#define FAUDES_TEST_DUMP(mes, dat)
Test protocol record macro ("mangle" filename for platform independance)
Definition: cfl_helper.h:483
Container class to model a set of clocks.
void SymbolicName(Idx index, const std::string &rName)
Set new name for existing index.
A TimeConstraint is a set of elementary clock constraints.
std::set< ElemConstraint >::const_iterator Iterator
Iterator to access ElemConstraints.
Iterator Erase(Iterator it)
Calls std::set::erase(iterator).
std::string ToString(void) const
Write to a std::string.
Iterator End(void) const
Iterator to end of set.
std::string EStr(const ElemConstraint &rElemConstr) const
Pretty printable string of elem.
void DWrite(void) const
Write NameSet to console, debug version.
TimeInterval Interval(Idx clockindex) const
Given a clock, compute the timeinterval in which the constraint is satisfied.
void Minimize(void)
Minimize by eliminating redundant elementary constraints.
Iterator Begin(void) const
Iterator to begin of set.
Iterator Insert(const ElemConstraint &rElemConstr)
Adds an elementary clock constraint to the time constraint.
ClockSet ActiveClocks(void) const
Returns a Clockset containing all clocks used by the TimeConstraint.
void Write(void) const
Write to console.
Model of a time interval.
void LB(Time::Type time)
Set the lower bound to a given value.
std::string Str(void) const
Pretty printable string.
void UBincl(bool incl)
Configures the upper bound to be inclusive.
void UB(Time::Type time)
Set the upper bound to a given value.
Iterator End(void) const
Iterator to the end of set.
Definition: cfl_baseset.h:1896
Iterator Begin(void) const
Iterator to the begin of set.
Definition: cfl_baseset.h:1891
Includes all libFAUDES headers, incl plugings
libFAUDES resides within the namespace faudes.
int main()

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