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 
14 #include "libfaudes.h"
15 #include "tp_include.h"
16 
17 
18 // make the faudes namespace available to our program
19 using namespace faudes;
20 
21 /*
22 
23 General Description of the TimeConstraints Class.
24 
25 TimeConstraints provides a container of ElemConstraints, the latter modeling Alur's
26 elementary clock contraints. Each ElemConstraint is a simple inequality that restricts
27 a clock e.g. "clock7 less than 120". In this library, clocks are required to have
28 symbolic names that are resolved via a SymbolTable reference that is member of TimeConstraints.
29 By default, the reference is set to a global SymbolTable. This is the same behaviour
30 as with events.
31 
32 
33 */
34 
35 
36 
37 /////////////////
38 // main program
39 /////////////////
40 
41 int main() {
42 
43  ////////////////////////////////////////////////////
44  // constraints file io and inspection
45  ////////////////////////////////////////////////////
46 
47  // Create a timeconstraint from file
48  TimeConstraint tc1("data/timeconstraint.txt");
49 
50  // Create an empty timeconstraint
51  TimeConstraint tc2;
52 
53  // Inspect on console
54  std::cout << "######################################\n";
55  std::cout << "# a timeconstraint \n";
56  tc1.DWrite();
57  std::cout << "######################################\n";
58 
59  // Write the constraint to a file
60  tc1.Write("tmp_timeconstraint.txt");
61 
62 
63  // Get set of active clocks
64  ClockSet clocks1=tc1.ActiveClocks();
65 
66  // Iterate over all clocks, inspect and convert to time interval
67  std::cout << "######################################\n";
68  std::cout << "# inspection ... \n";
69  ClockSet::Iterator cit;
70  for(cit = clocks1.Begin(); cit != clocks1.End(); cit++) {
71  std::cout << "clock: " << clocks1.SymbolicName(*cit) << "\n";
73  for(tit = tc1.Begin(*cit); tit != tc1.End(*cit); tit++) {
74  std::cout << "elem constraint: " << tc1.EStr(*tit) << "\n";
75  }
76  TimeInterval tint=tc1.Interval(*cit);
77  std::cout << "time interval: " << tint.Str() << "\n";
78  }
79  std::cout << "######################################\n";
80 
81 
82  ////////////////////////////////////////////////////
83  // edit constraint
84  ////////////////////////////////////////////////////
85 
86  // Some interaction
87  tc1.Insert("clockC", ElemConstraint::LessThan, 125);
88  tc1.Insert("clockC", ElemConstraint::GreaterThan, 10);
89  tc1.Insert("clockC", ElemConstraint::GreaterEqual, 10); // redundent, but still added
90  tc1.Insert("clockB", ElemConstraint::GreaterThan, 110);
91  tc1.Insert("clockB", ElemConstraint::GreaterThan, 110); // double, not added to set
92  tc1.Insert("clockB", ElemConstraint::LessEqual, 200);
93  tc1.Insert("clockB", ElemConstraint::LessEqual, 210); // redundent, but still added
94  tc1.Insert("clockB", ElemConstraint::LessThan, 220); // redundent, but still added
95 
96  // Report
97  std::cout << "######################################\n";
98  std::cout << "# after various insertions \n";
99  tc1.DWrite();
100  std::cout << "######################################\n";
101 
102  // Erase an elementary constraint
103  tc1.Erase("clockB", ElemConstraint::LessEqual, 200);
104 
105  // Report
106  std::cout << "######################################\n";
107  std::cout << "# after erase \n";
108  tc1.DWrite();
109  std::cout << "######################################\n";
110 
111  // Introduce constraints to represent a valid interval
112  TimeInterval tint; // to become "(150,300]"
113  tint.LB(150);
114  tint.UB(300);
115  tint.UBincl(true);
116  tc2.Interval("clockB",tint);
117  tc2.Interval("clockD",tint);
118 
119  // Report
120  std::cout << "######################################\n";
121  std::cout << "# another constraint \n";
122  tc2.DWrite();
123  std::cout << "######################################\n";
124 
125 
126  // Merge two time constraints (aka intersection)
127  tc1.Insert(tc2);
128 
129  // Remove redundant elem. constraints
130  tc1.Minimize();
131 
132  // Report
133  std::cout << "######################################\n";
134  std::cout << "# minimal version \n";
135  tc1.DWrite();
136  std::cout << "######################################\n";
137 
138  // Test protocol
139  FAUDES_TEST_DUMP("final constraint",tc1.ToString());
140 
141 
142  return 0;
143 }
144 
145 
146 

libFAUDES 2.26g --- 2015.08.17 --- c++ api documentaion by doxygen