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 
#define FAUDES_TEST_DUMP(mes, dat)
Test protocol record macro ("mangle" filename for platform independance)
Definition: cfl_helper.h:483
Set of indices.
Definition: cfl_indexset.h:78
Set of indices with symbolic names.
Definition: cfl_nameset.h:69
void InsUEvent(Idx index)
Add an existing U-event to generator.
void InsYEvent(Idx index)
Add an existing Y-event to generator.
EventSet YEvents(void) const
Get EventSet with Y-events.
void InsYcEvent(Idx index)
Add an existing Yc-event to generator.
void InsYpEvent(Idx index)
Add an existing Yp-event to generator.
void InsUcEvent(Idx index)
Add an existing Uc-event to generator.
void InsUpEvent(Idx index)
Add an existing Up-event to generator.
void InsYeEvent(Idx index)
Add an existing Ye-event to generator.
void InsUeEvent(Idx index)
Add an existing Ue-event to generator.
void InsUlEvent(Idx index)
Add an existing Ul-event to generator.
void InsYlEvent(Idx index)
Add an existing Yl-event to generator.
void SetYp(Idx index)
Mark event as Yp-event (by index)
Definition: hio_plant.h:1033
void SetYe(Idx index)
Mark event as Ye-event (by index)
Definition: hio_plant.h:1167
EventSet UeEvents(void) const
Get EventSet with Ue-events.
Definition: hio_plant.h:1252
StateSet QUpStates(void) const
Get StateSet with QUp-states.
Definition: hio_plant.h:1466
EventSet UpEvents(void) const
Get EventSet with Up-events.
Definition: hio_plant.h:1118
StateSet QUeStates(void) const
Get StateSet with QUe-states.
Definition: hio_plant.h:1548
void SetUp(Idx index)
Mark event Up-event(by index)
Definition: hio_plant.h:1058
StateSet QYpYeStates(void) const
Get StateSet with QYpYe-states.
Definition: hio_plant.h:1386
EventSet YpEvents(void) const
Get EventSet with Yp-events.
Definition: hio_plant.h:1107
EventSet YeEvents(void) const
Get EventSet with Ye-events.
Definition: hio_plant.h:1241
void SetUe(Idx index)
Mark event as Ue-event (by index)
Definition: hio_plant.h:1192
TBaseSet< Transition, TransSort::X1EvX2 >::Iterator Iterator
Iterator on transition.
Definition: cfl_transset.h:269
virtual TaGenerator & Assign(const Type &rSrc)
Copy from other faudes Type (try to cast to agenerator or pass to base)
bool SetTransition(Idx x1, Idx ev, Idx x2)
Add a transition to generator by indices.
void Read(const std::string &rFileName, const std::string &rLabel="", const Type *pContext=0)
Read configuration data from file with label specified.
Definition: cfl_types.cpp:261
void Write(const Type *pContext=0) const
Write configuration data to console.
Definition: cfl_types.cpp:139
Base class of all FAUDES generators.
Idx InsMarkedState(void)
Create new anonymous state and set as marked state.
TransSet::Iterator TransRelBegin(void) const
Iterator to Begin() of transition relation.
void ClrTransition(Idx x1, Idx ev, Idx x2)
Remove a transition by indices.
void Name(const std::string &rName)
Set the generator's name.
TransSet::Iterator TransRelEnd(void) const
Iterator to End() of transition relation.
void SetMarkedState(Idx index)
Set an existing state as marked state by index.
Idx InsInitState(void)
Create new anonymous state and set as initial state.
void GraphWrite(const std::string &rFileName, const std::string &rOutFormat="", const std::string &rDotExec="dot") const
Produce graphical representation of this generator.
std::string EventName(Idx index) const
Event name lookup.
const std::string & Name(void) const
Return name of TBaseSet.
Definition: cfl_baseset.h:1755
void HioFreeInput(const Generator &rGen, const EventSet &rInput, const EventSet &rOutput, Generator &rResGen, const std::string &rErrState1, const std::string &rErrState2, Idx &rErrState1Idx, Idx &rErrState2Idx)
HioFreeInput: extend generator by obviously missing input transitions.
int main()
Includes all libFAUDES headers, incl plugings
libFAUDES resides within the namespace faudes.
uint32_t Idx
Type definition for index type (allways 32bit)
bool IsHioConstraintForm(HioConstraint &rHioConstraint, StateSet &rQY, StateSet &rQU, EventSet &rErrEvSet, TransSet &rErrTrSet, StateSet &rErrStSet, std::string &rReportStr)
IsHioConstraintForm: check if rHioConstraint is in I/O-constraint form and assign state attributes.
bool IsHioEnvironmentForm(HioEnvironment &rHioEnvironment, StateSet &rQYe, StateSet &rQUe, StateSet &rQUl, StateSet &rQYlUe, EventSet &rErrEvSet, TransSet &rErrTrSet, StateSet &rErrStSet, std::string &rReportStr)
IsHioEnvironmentForm: check if rHioEnvironment is in I/O-environment form and assign state attributes...
bool IsHioControllerForm(HioController &rHioController, StateSet &rQUc, StateSet &rQYP, StateSet &rQUp, StateSet &rQYcUp, EventSet &rErrEvSet, TransSet &rErrTrSet, StateSet &rErrStSet, std::string &rReportStr)
IsHioControllerForm: check if rHioController is in I/O-controller form and assign state attributes.
bool IsHioPlantForm(HioPlant &rHioPlant, StateSet &rQYpYe, StateSet &rQUp, StateSet &rQUe, EventSet &rErrEvSet, TransSet &rErrTrSet, StateSet &rErrStSet, std::string &rReportStr)
IsHioPlantForm: check if rHioPlant is in I/O-plant form and assign state attributes.
Definition: hio_plant.cpp:16

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