hio_2_hiogenerators.cpp
Go to the documentation of this file.
1 /** @file hio_2_hiogenerators.cpp
2 
3 Tutorial, i/o system generator classes.
4 
5 This tutorial demonstrates basic maintenance of HioPlant, HioController
6 and HioEnvironment objects.
7 
8 @ingroup Tutorials
9 
10 @include hio_2_hiogenerators.cpp
11 
12 */
13 
14 #include "libfaudes.h"
15 
16 // make the faudes namespace available to our program
17 using namespace faudes;
18 
19 /////////////////
20 // main program
21 /////////////////
22 
23 int main() {
24 
25  // for intermediate results
26  bool tmpresult;
27 
28  /**************************************************
29  * HIO PLANT
30  ***************************************************/
31 
32  /******************
33  * constructor and file io
34  ********************/
35 
36  // Create an empty HioPlant object
37  HioPlant g1;
38 
39  // Create a HioPlant by reading a HioPlant file
40  HioPlant g2("data/2_hiogenerators/hio_simplemachine_A.gen");
41  FAUDES_TEST_DUMP("HioPlant from genfile",g2);
42 
43  // Create a HioPlant by reading a plain Generator file (no event attributes)
44  g1.Read("data/2_hiogenerators/simplemachine_A.gen");
45 
46  // Assign/copy a plain Generator to a HioPlant (no event attributes)
47  Generator g4("data/2_hiogenerators/simplemachine_A.gen");
48  HioPlant g3=g4;
49  HioPlant g5(g4);
50 
51  // Assign/copy a HioPlant to a HioPlant (no event attributes)
52  HioPlant g6=g2;
53  FAUDES_TEST_DUMP("HioPlant = operator",g6);
54  HioPlant g7(g2);
55  FAUDES_TEST_DUMP("HioPlant construct from HioPlant",g7);
56  HioPlant g8;
57  g8.Assign(g2);
58  FAUDES_TEST_DUMP("HioPlant::Assign()",g8);
59 
60  // Construct HioPlant from plain generator and event sets
61  EventSet ypEvents,upEvents,yeEvents,ueEvents;
62  ypEvents=g2.YpEvents();
63  FAUDES_TEST_DUMP("get Yp events",ypEvents);
64  upEvents=g2.UpEvents();
65  yeEvents=g2.YeEvents();
66  ueEvents=g2.UeEvents();
67 
68  HioPlant g9(g4,ypEvents,upEvents,yeEvents,ueEvents);
69  FAUDES_TEST_DUMP("HioPlant(vGen,EventSets)",g9);
70 
71  // Write the HioPlant to files
72  g1.Write("tmp_hiosimplemachine_A1.gen");
73  g2.Write("tmp_hiosimplemachine_A2.gen");
74  g3.Write("tmp_hiosimplemachine_A3.gen");
75  g5.Write("tmp_hiosimplemachine_A5.gen");
76  g6.Write("tmp_hiosimplemachine_A6.gen");
77  g9.Write("tmp_hiosimplemachine_A9.gen");
78 
79  g1.Write("tmp_hiosimplemachine_A.gen");
80 
81 
82  // Report to console
83  std::cout << "######################################\n";
84  std::cout << "# hio simple machine\n";
85  g2.Write();
86  std::cout << "######################################\n";
87 
88  /*****************************
89  * Test whether HioPlant meets IO-Plant form
90  ******************************/
91 
92  std::cout << "######################################\n";
93 
94  // Fails for g1 due to missing event attributes
95  std::cout << "# IsHioPlantForm A: expect to fail for missing attributes\n";
96  std::string report;
97  tmpresult=IsHioPlantForm(g1,report);
98  FAUDES_TEST_DUMP("IsHioPlant fail",tmpresult);
99  std::cout<<report;
100 
101  // Fulfilled for g2:
102  std::cout << "# IsHioPlantForm B: expect to pass\n";
103  tmpresult=IsHioPlantForm(g2,report);
104  FAUDES_TEST_DUMP("IsHioPlant pass",tmpresult);
105  std::cout<<report;
106 
107  // Remove some transitions with input-event
108  TransSet::Iterator tit;
109  for (tit = g2.TransRelBegin(); tit != g2.TransRelEnd(); ++tit) {
110  //if(g2.IsUp(tit->Ev)) {
111  if(g2.EventName(tit->Ev)=="A_stp" || g2.EventName(tit->Ev)=="A_nack") {
112  g2.ClrTransition(*tit);
113  }
114  }
115  g2.Write("tmp_hiosimplemachine_A_broken.gen");
116  g2.GraphWrite("tmp_hiosimplemachine_A_broken.png");
117  // Now, the input is no longer free in g2
118  std::cout << "# IsHioPlantForm C: expect to fail for missing input transitions\n";
119  IsHioPlantForm(g2,report);
120  std::cout<<report;
121  // Repair
122  HioFreeInput(g2,g2);
123  FAUDES_TEST_DUMP("HioFreeInput",g2);
124  g2.Write("tmp_hiosimplemachine_A_fixed.gen");
125  // HioPlantForm is retrieved for g2:
126  std::cout << "# IsHioPlantForm D: expect to pass\n";
127  IsHioPlantForm(g2,report);
128  std::cout<<report;
129 
130  FAUDES_TEST_DUMP("HioPlant construction",g2);
131  g2.Write();
132  g2.Write("tmp_hiosimplemachine_A_repaired.gen");
133 
134  std::cout << "######################################\n";
135 
136  /*******************************************
137  * Access to hio-property of states
138  *
139  * Note: hio state properties need not be up to date
140  * - always run IsHioPlantForm() to set state attributes
141  **************************************/
142 
143  // Get states of g2 sorted according to their active event sets
144  StateSet QYpYe,QUp,QUe;
145  QYpYe=g2.QYpYeStates();
146  QUp=g2.QUpStates();
147  FAUDES_TEST_DUMP("get Up states",QUp);
148  QUe=g2.QUeStates();
149  std::cout<<std::endl;
150 
151  // Show on console
152  std::cout << "######################################\n";
153  std::cout << "# QYpYe,QUp and QUe of simple machine:\n";
154  QYpYe.Write();
155  QUp.Write();
156  QUe.Write();
157  std::cout << "######################################\n";
158 
159 
160  /*************************
161  * Access to hio-property of events
162  **************************/
163 
164  // Retrieve EventSets containing all YP-, UP-, YE-, UE-events from g2
165  ypEvents=g2.YpEvents();
166  upEvents=g2.UpEvents();
167  yeEvents=g2.YeEvents();
168  ueEvents=g2.UeEvents();
169 
170  // Set YP-, UP-, YE-, UE-events in g1
171  g1.SetYp(ypEvents);
172  g1.SetUp(upEvents);
173  g1.SetYe(yeEvents);
174  g1.SetUe(ueEvents);
175 
176  // Now, also g1 is in HioPlantForm
177  std::cout << "######################################\n";
178  std::cout << "# IsHioPlantForm:\n";
179  IsHioPlantForm(g1,report);
180  std::cout<<report;
181  std::cout << "######################################\n";
182 
183  // File i/o and access to attributes are analogous for HioConstraint, HioController and HioEnvironment.
184  std::cout<<std::endl<<std::endl;
185  /**************************************************
186  * HIO CONTROLLER
187  ***************************************************/
188 
189  // Construct simple HioController structure
190  HioController c1;
191  c1.Name("HioController");
192  Idx yp1=c1.InsYpEvent("yp1");
193  Idx yp2=c1.InsYpEvent("yp2");
194  Idx yc=c1.InsYcEvent("yc");
195  Idx uc1=c1.InsUcEvent("uc1");
196  Idx uc2=c1.InsUcEvent("uc2");
197  Idx up=c1.InsUpEvent("up");
198 
199  Idx st1=c1.InsInitState();
200  c1.SetMarkedState(st1);
201  Idx st2=c1.InsMarkedState();
202  Idx st3=c1.InsMarkedState();
203  Idx st4=c1.InsMarkedState();
204 
205  c1.SetTransition(st1,yp1,st2);
206  c1.SetTransition(st2,yc,st3);
207  c1.SetTransition(st3,uc1,st4);
208  c1.SetTransition(st4,up,st1);
209 
210  // Up to now, no I/O-controller form as inputs yp2 and uc2 are not accepted:
211  std::cout<<std::endl<<"######################################\n";
212  std::cout<<"######################################\n";
213  std::cout<<"####### I/O CONTROLLER: #########\n";
214  tmpresult=IsHioControllerForm(c1,report);
215  FAUDES_TEST_DUMP("IsHioControllerForm fail",tmpresult);
216 
217  // Test correct file I/O
218  c1.Write();
219  c1.Write("tmp_hiocontroller_incomplete.gen");
220  c1.Read("tmp_hiocontroller_incomplete.gen");
221  c1.Write();
222  std::cout<<report;
223  c1.GraphWrite("tmp_hiocontroller_incomplete.png");
224 
225  // Repair HioControllerForm using HioFreeInput
226  HioFreeInput(c1,c1);
227  tmpresult=IsHioControllerForm(c1,report);
228  FAUDES_TEST_DUMP("IsHioControllerForm pass",tmpresult);
229  std::cout<<report;
230  FAUDES_TEST_DUMP("HioController construction",c1);
231  c1.Write("tmp_hiocontroller_repaired.gen");
232  c1.GraphWrite("tmp_hiocontroller_repaired.png");
233  std::cout<<"######################################\n";
234  std::cout<<std::endl<<std::endl;
235 
236  /**************************************************
237  * HIO ENVIRONMENT
238  ***************************************************/
239 
240  // Construct simple HioEnvironment structure
241  HioEnvironment e1;
242  e1.Name("HioEnvironment");
243  Idx ye1=e1.InsYeEvent("ye1");
244  Idx ye2=e1.InsYeEvent("ye2");
245  Idx yl=e1.InsYlEvent("yl");
246  Idx ul1=e1.InsUlEvent("ul1");
247  Idx ul2=e1.InsUlEvent("ul2");
248  Idx ue=e1.InsUeEvent("ue");
249 
250  st1=e1.InsInitState();
251  e1.SetMarkedState(st1);
252  st2=e1.InsMarkedState();
253  st3=e1.InsMarkedState();
254  st4=e1.InsMarkedState();
255 
256  e1.SetTransition(st1,ye1,st2);
257  e1.SetTransition(st2,yl,st3);
258  e1.SetTransition(st3,ul1,st4);
259  e1.SetTransition(st4,ue,st1);
260 
261  // Up to now, no I/O-environment form as inputs ye2 and ul2 are not accepted:
262  std::cout<<std::endl<<"######################################\n";
263  std::cout<<"######################################\n";
264  std::cout<<"####### I/O ENVIRONMENT: #########\n";
265  tmpresult=IsHioEnvironmentForm(e1,report);
266  FAUDES_TEST_DUMP("IsHioEnvironmentForm fail",tmpresult);
267  // Test correct file I/O
268  e1.Write();
269  e1.Write("tmp_hioenvironment_incomplete.gen");
270  e1.Read("tmp_hioenvironment_incomplete.gen");
271  e1.Write();
272  std::cout<<report;
273  e1.GraphWrite("tmp_hioenvironment_incomplete.png");
274 
275  // Repair HioEnvironmentForm using HioFreeInput
276  HioFreeInput(e1,e1);
277  tmpresult=IsHioEnvironmentForm(e1,report);
278  FAUDES_TEST_DUMP("IsHioEnvironmentForm pass",tmpresult);
279  e1.Write();
280  std::cout<<report;
281  FAUDES_TEST_DUMP("HioEnvironment construction",e1);
282  e1.Write("tmp_hioenvironment_repaired.gen");
283  e1.GraphWrite("tmp_hioenvironment_repaired.png");
284  std::cout<<"######################################\n";
285  std::cout<<std::endl<<std::endl;
286  /**************************************************
287  * HIO CONSTRAINT
288  ***************************************************/
289  // Construct simple HioConstraint structure
290  HioConstraint cnstr1;
291  cnstr1.Name("HioConstraint");
292  Idx y1=cnstr1.InsYEvent("y1");
293  Idx y2=cnstr1.InsYEvent("y2");
294  Idx u=cnstr1.InsUEvent("u");
295 
296  st1=cnstr1.InsInitState();
297  cnstr1.SetMarkedState(st1);
298  st2=cnstr1.InsMarkedState();
299 
300  cnstr1.SetTransition(st1,y1,st2);
301  cnstr1.SetTransition(st2,u,st1);
302 
303  // Up to now, no I/O-constraint form as input u2 is not accepted:
304  std::cout<<std::endl<<"######################################\n";
305  std::cout<<"######################################\n";
306  std::cout<<"####### I/O CONSTRAINT: #########\n";
307  std::cout<<"#######\n";
308  // Access to event properties:
309  EventSet yEvents=cnstr1.YEvents();
310  yEvents.Name("####### HioConstraint: Y-Events");
311  yEvents.Write();
312  tmpresult=IsHioConstraintForm(cnstr1,report);
313  FAUDES_TEST_DUMP("IsHioConstraintForm fail",tmpresult);
314  // Test correct file I/O
315  cnstr1.Write();
316  cnstr1.Write("tmp_hioconstraint_incomplete.gen");
317  cnstr1.Read("tmp_hioconstraint_incomplete.gen");
318  cnstr1.Write();
319  std::cout<<report;
320  cnstr1.GraphWrite("tmp_hioconstraint_incomplete.png");
321 
322  // Repair HioEnvironmentForm using HioFreeInput
323  HioFreeInput(cnstr1,cnstr1);
324  tmpresult=IsHioConstraintForm(cnstr1,report);
325  FAUDES_TEST_DUMP("IsHioConstraintForm pass",tmpresult);
326  FAUDES_TEST_DUMP("HioController construction",cnstr1);
327  cnstr1.Write();
328  std::cout<<report;
329  cnstr1.Write("tmp_hioconstraint_repaired.gen");
330  cnstr1.GraphWrite("tmp_hioconstraint_repaired.png");
331  std::cout<<"######################################\n";
332 
333  return 0;
334 }
335 
336 
337 

libFAUDES 2.24g --- 2014.09.15 --- c++ api documentaion by doxygen