tp_1_tgenerator.cpp
Go to the documentation of this file.
1 /** @file tp_1_tgenerator.cpp
2 
3 Tutorial, class faudes::TimedGenerator. This tutorial demonstrates
4 basic maintenance of TimedGenerator objects
5 
6 @ingroup Tutorials
7 
8 @include tp_1_tgenerator.cpp
9 
10 */
11 
12 #include "libfaudes.h"
13 #include "tp_include.h"
14 
15 
16 // make the faudes namespace available to our program
17 using namespace faudes;
18 
19 
20 
21 /////////////////
22 // main program
23 /////////////////
24 
25 int main() {
26 
27  ////////////////////////////////////////////////////
28  // constructor and file io
29  // EventSets
30  ////////////////////////////////////////////////////
31 
32  // Create an empty TimedGenerator object
33  TimedGenerator g1;
34 
35  // Create a TimedGenerator by reading a TimedGenerator file
36  TimedGenerator g2("data/tsimplemachine.gen");
37 
38  // Create a TimedGenerator by reading a Generator file (no timing, no control)
39  TimedGenerator g3("data/simplemachine.gen");
40 
41  // Read a TimedGenerator from a System file (no timing)
42  g1.Read("data/csimplemachine.gen");
43 
44  // Copy a Generator to a TimedGenerator (no timing, no control)
45  TimedGenerator g4;
46  Generator g5("data/simplemachine.gen");
47  g4.Assign(g5);
48 
49  // Write the TimedGenerator to a files
50  g1.Write("tmp_tsimplemachine1.gen");
51  g2.Write("tmp_tsimplemachine2.gen");
52  g3.Write("tmp_tsimplemachine3.gen");
53  g4.Write("tmp_tsimplemachine4.gen");
54 
55  // See result on console
56  std::cout << "######################################\n";
57  std::cout << "# tc simple machine (\n";
58  g2.DWrite();
59  std::cout << "######################################\n";
60 
61  // Test protocol
62  FAUDES_TEST_DUMP("construct g1",g1);
63  FAUDES_TEST_DUMP("construct g2",g2);
64  FAUDES_TEST_DUMP("construct g3",g3);
65  FAUDES_TEST_DUMP("construct g4",g4);
66 
67 
68  ////////////////////////////////////////////////////
69  // access to controllability status of events
70  //
71  // (using System methods)
72  ////////////////////////////////////////////////////
73 
74  // Retrieve an EventSet containing all the controllabe events
75 
76  EventSet cevents = g2.ControllableEvents();
77 
78  // See result on console
79  std::cout << "######################################\n";
80  std::cout << "# controllabel events\n";
81  cevents.DWrite();
82  std::cout << "######################################\n";
83 
84  // Test protocol
85  FAUDES_TEST_DUMP("contr attrib",cevents);
86 
87 
88  ////////////////////////////////////////////////////
89  // access/edit timing
90  ////////////////////////////////////////////////////
91 
92  // Insert a clock to tcsimplemachine
93  g2.InsClock("cRepair");
94 
95  // Read clocks from tcsimplemachine and set them for csimplemachine
96  ClockSet clocks = g2.Clocks();
97 
98  // Report
99  std::cout << "######################################\n";
100  std::cout << "# tcsimple machine clocks \n";
101  clocks.DWrite();
102  std::cout << "######################################\n";
103 
104 
105  // Add constraints to invariant
106  TimeConstraint inv;
107  inv.Insert("cRepair",ElemConstraint::LessEqual,20);
108  g2.InsInvariant("down", inv);
109 
110  // Add constraints to guard and a reset
111  TransSet::Iterator tit;
112  tit=g2.FindTransition("down","lambda","idle");
113  if(tit==g2.TransRelEnd()) {}; // not found
114  TimeConstraint guard;
115  guard.Insert("cRepair",ElemConstraint::GreaterEqual,10);
116  g2.InsGuard(*tit, guard);
117  tit=g2.FindTransition("busy","mue","down");
118  if(tit==g2.TransRelEnd()) {}; // not found
119  ClockSet resets;
120  resets.Insert("cRepair");
121  g2.InsResets(*tit, resets);
122 
123  // Fix name
124  g2.Name("tc simple machine with timed repair");
125 
126  // Report
127  std::cout << "######################################\n";
128  std::cout << "# tcsimple machine with timed repair \n";
129  g2.DWrite();
130  g2.ActiveClocks().DWrite();
131  std::cout << "######################################\n";
132 
133  // Copy to g1 and remove a clock
134  g1=g2;
135  g1.DelClock("cBusy");
136 
137  // Report
138  std::cout << "######################################\n";
139  std::cout << "# tcsimple machine with untimed busy \n";
140  g1.DWrite();
141  g1.ActiveClocks().DWrite();
142  std::cout << "######################################\n";
143 
144 
145  // Test protocol
146  FAUDES_TEST_DUMP("edit timing 1",g1);
147  FAUDES_TEST_DUMP("edit timing 2",g2);
148 
149 
150  // Read clocks from tcsimplemachine and set them for csimplemachine
151  std::cout << "######################################\n";
152  std::cout << "# csimple machine (active clocks) \n";
153  g3.InsClocks(g2.ActiveClocks());
154  std::cout << g3.Clocks().ToString() << "\n";
155 
156  // Read invriants from tcsimplemachine and set them for csimplemachine
157  std::cout << "######################################\n";
158  std::cout << "# csimple machine (invariants) \n";
159  StateSet::Iterator sit;
160  for(sit=g2.StatesBegin(); sit!=g2.StatesEnd(); sit++) {
161  TimeConstraint invariant = g2.Invariant(*sit);
162  std::string state = g2.StateName(*sit);
163  std::cout << state << " inv " << invariant.ToString() << "\n";
164  g3.Invariant(state, invariant);
165  }
166 
167  // Read guards and resets from tcsimplemachine and set them for csimplemachine
168  std::cout << "######################################\n";
169  std::cout << "# csimple machine (guards and resets) \n";
170  TransSet::Iterator stit, dtit;
171  for(stit=g2.TransRelBegin(); stit!= g2.TransRelEnd(); stit++) {
172  std::cout << "src transition: " << g2.TStr(*stit) << "\n";
173  std::cout << "guard: " << g2.Guard(*stit).ToString() << "\n";
174  std::cout << "resets: " << g2.Resets(*stit).ToString() << "\n";
175  dtit=g3.FindTransition(g2.StateName(stit->X1),g2.EventName(stit->Ev),g2.StateName(stit->X2));
176  g3.Guard(*dtit,g2.Guard(*stit));
177  g3.Resets(*dtit,g2.Resets(*stit));
178  }
179 
180  // Report
181  std::cout << "######################################\n";
182  std::cout << "# csimple machine with timing \n";
183  g3.DWrite();
184  std::cout << "######################################\n";
185 
186 
187  // Check validiy (internal names and symboltables)
188  if(g3.Valid())
189  std::cout << "############# valid #################\n";
190 
191  // Test protocol
192  FAUDES_TEST_DUMP("edit timing 3",g3);
193 
194  return 0;
195 }
196 
197 
198 

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