hio_3_hiofunctions.cpp
Go to the documentation of this file.
1 /** @file hio_3_hiofunctions.cpp
2 
3 Tutorial, functions provided by hiosys plugin.
4 
5 @ingroup Tutorials
6 
7 @include hio_3_hiofunctions.cpp
8 
9 */
10 
11 #include "libfaudes.h"
12 
13 // make the faudes namespace available to our program
14 using namespace faudes;
15 using namespace std;
16 
17 /** simple machine example to demonstrate I/O controller synthesis */
18 void simpleMachine() {
19 
20  /*
21  % Simple machine example for hiosys-tutorial.
22  % Machine sends YP-event "A_wait" to operator.
23  % Does nothing after UP-event "A_stp", or executes
24  % the process "A_do", which requires a shared
25  % resource from the environment, modelled by
26  % "A_req". The environment may provide or
27  % deny access to the resource ("A_pack" or
28  % "A_nack", respectively).
29  */
30 
31  // read plant A model
32  HioPlant plantA;
33  plantA.Read("data/3_hiofunctions/hio_simplemachine_A.gen");
34  plantA.GraphWrite("tmp_hio_simplemachine_A.png");
35  // must be in I/O plant form
36  if(!IsHioPlantForm(plantA)){
37  std::stringstream errstr;
38  errstr << "plant A not in HioPlantForm.\n";
39  throw Exception("simpleMachine", errstr.str(), 0);
40  }
41 
42  /*
43  % Environment constraint:
44  % Resources are always provided as requested.
45  */
46  // read plant A environment constraint
47  HioConstraint SeA;
48  SeA.Read("data/3_hiofunctions/hio_simpleEnvConstr_A.gen");
49  //should be in I/O constraint form:
50  if(!IsHioConstraintForm(SeA)) {
51  std::cout<<"Warning: environment constraint A not in I/O constraint form.";
52  }
53  SeA.GraphWrite("tmp_hio_simpleEnvConstr_A.png");
54 
55  /*
56  % Operator constraint:
57  % simple machine is complete and YP-life wrt. to the
58  % environment constraint and a minimal operator
59  % constraint, which - for convenience - can be
60  % modelled by an epsilon language HioConstraint.
61  */
62  // construct epsilon language operator constraint
63  HioConstraint SpA;
64  Idx init=SpA.InsInitState();
65  SpA.SetMarkedState(init);
66 
67  // first, we synthesise a controller for the single plant A:
68 /*============================================================================
69 ================ CONTROLLER SYNTHESIS FOR MONOLITHIC PLANT =============
70 ==============================================================================*/
71 
72  /*
73  % Specification:
74  % Send YC-event "A_READY" as feedback to operator.
75  % Accept UC-events "A_STANDBY" or "A_OPERATE" as
76  % operator inputs.
77  % Command "A_STANDBY": do nothing.
78  % Command "A_OPERATE": Process two resources.
79  */
80  HioPlant specA;
81  specA.Read("data/3_hiofunctions/hio_simplespec_A.gen");
82  specA.GraphWrite("tmp_hio_simplespec_A.png");
83 
84  /*
85  % external operator constraint:
86  % also specification A is complete and YP-life wrt. to the
87  % environment constraint and a minimal operator
88  % constraint.
89  */
90  // construct epsilon language external operator constraint
91  HioConstraint ScA;
92  init=ScA.InsInitState();
93  ScA.SetMarkedState(init);
94 
95  // run controller synthesis algorithm
96  HioController controllerA;
97  HioSynthMonolithic(plantA,specA,ScA,SpA,SeA,controllerA);
98  FAUDES_TEST_DUMP("HioSynthMonolithic",controllerA);
99  controllerA.Write("tmp_hio_simpleController_A.gen");
100  controllerA.GraphWrite("tmp_hio_simpleController_A.png");
101 
102  // now, we synthesise a controller for two plants A and B:
103 /*============================================================================
104 ===================== HIERARCHICAL CONTROLLER SYNTHESIS =================
105 ==============================================================================*/
106 
107  /*
108  % Machine B Sends YP-event "B_wait" to operator.
109  % Does nothing after UP-event "B_stp", or executes
110  % the process "B_do", which produces a shared
111  % resource provided to the environment, modelled
112  % by "B_prov". The environment may or may not
113  % accept the resource ("B_pack" or "B_nack",
114  % respectively).
115  */
116 
117  // read plant B model
118  HioPlant plantB;
119  plantB.Read("data/3_hiofunctions/hio_simplemachine_B.gen");
120  plantB.GraphWrite("tmp_hio_simplemachine_B.png");
121  // must be in I/O plant form
122  if(!IsHioPlantForm(plantB)){
123  std::stringstream errstr;
124  errstr << "plant B not in HioPlantForm.\n";
125  throw Exception("simpleMachine", errstr.str(), 0);
126  }
127 
128  /*
129  % Environment constraint:
130  % Resources are always provided as requested.
131  */
132  // read plant A environment constraint
133  HioConstraint SeB;
134  SeB.Read("data/3_hiofunctions/hio_simpleEnvConstr_B.gen");
135  if(!IsHioConstraintForm(SeB)) {
136  std::cout<<"Warning: environment constraint B not in I/O constraint form.";
137  }
138  SeB.GraphWrite("tmp_hio_simpleEnvConstr_B.png");
139 
140  /*
141  % Operator constraint:
142  % simple machine B is complete and YP-life wrt. to the
143  % environment constraint and a minimal operator
144  % constraint, which - for convenience - can be
145  % modelled by an epsilon language HioConstraint.
146  */
147  // construct epsilon language operator constraint
148  HioConstraint SpB;
149  init=SpB.InsInitState();
150  SpB.SetMarkedState(init);
151 
152  /*===============================================
153  I/O SHUFFLE OF PLANT A AND PLANT B
154  =================================================*/
155 
156  HioPlant ioShuffleAB;
157  HioShuffle(plantA,plantB,ioShuffleAB);
158  FAUDES_TEST_DUMP("HioShuffle",ioShuffleAB);
159  ioShuffleAB.Write("tmp_hio_simpleHioShuffle_AB.gen");
160  ioShuffleAB.GraphWrite("tmp_hio_simpleHioShuffle_AB.png");
161 
162  // compose constraints of plant A and B_do
163  HioConstraint intConstrAB;
164  Parallel(SpA,SpB,intConstrAB);
165  Parallel(intConstrAB,SeA,intConstrAB);
166  Parallel(intConstrAB,SeB,intConstrAB);
167  intConstrAB.StateNamesEnabled(false);
168 
169  /*===============================================
170  ENVIRONMENT MODEL FOR PLANT AB
171  =================================================*/
172  /*
173  % Simple machine example for hiosys-tutorial.
174  % Environment:
175  % A request of machine A (A_req) for a resource is
176  % denied (A_nack) until machine B provides it
177  % (B_prov, B_pack). Then, machine A has to request
178  % the resource until machine B can provide the next
179  % one. A resource provided by machine B and
180  % requested by machine A is readily processed
181  % and can be provided to the external
182  % environment (AB_prov), which may or may not
183  % accept the processed resource (AB_pack,
184  % AB_nack).
185  */
186  HioEnvironment envAB("data/3_hiofunctions/hio_simpleenvironment_AB.gen");
187  envAB.GraphWrite("tmp_hio_simpleenvironment_AB.png");
188 
189  /*
190  % Environment constraint:
191  % Processed resources are always accepted as provided.
192  */
193  // read plant A environment constraint
194  HioConstraint SlAB;
195  SlAB.Read("data/3_hiofunctions/hio_simpleEnvConstr_AB.gen");
196  if(!IsHioConstraintForm(SlAB)) {
197  std::cout<<"Warning: environment constraint AB not in I/O constraint form.";
198  }
199  SlAB.GraphWrite("tmp_hio_simpleEnvConstr_AB.png");
200 
201  /*====================================================
202  SPECIFICATION AND EXTERNAL OPERATOR CONSTRAINT
203  ======================================================*/
204  /*
205  % Simple machine example for hiosys-tutorial.
206  % Specification:
207  % Send YC-event "AB_READY" as feedback to operator.
208  % Accept UC-events "AB_STANDBY" or "AB_OPERATE" as
209  % operator inputs.
210  % Command "AB_STANDBY": do nothing.
211  % Command "AB_OPERATE": Provide processed resource (AB_prov).
212  */
213  HioPlant specAB;
214  specAB.Read("data/3_hiofunctions/hio_simplespec_AB.gen");
215  specAB.GraphWrite("tmp_hio_simplespec_AB.png");
216 
217  /*
218  % external operator constraint:
219  % also specification AB is complete and YP-life wrt. to the
220  % environment constraint and a minimal operator
221  % constraint.
222  */
223  // construct epsilon language external operator constraint
224  HioConstraint ScAB;
225  init=ScAB.InsInitState();
226  ScAB.SetMarkedState(init);
227 
228  // run controller synthesis algorithm
229  HioController controllerAB;
230  HioSynthHierarchical(ioShuffleAB,envAB,specAB,intConstrAB,ScAB,SlAB,controllerAB);
231  FAUDES_TEST_DUMP("HioSynthHierarchical",controllerAB);
232  controllerAB.Write("tmp_hio_simpleController_AB.gen");
233  controllerAB.GraphWrite("tmp_hio_simpleController_AB.png");
234 
235  return;
236 }
237 
238 
239 /** Run the tutorial */
240 int main() {
241  // call simple machine example
242  try {
243  simpleMachine();
244  }
245  catch (Exception& e) {
246  std::cout << "function: " << e.Where() << std::endl;
247  std::cout << "exception description: " << e.What() << std::endl;
248  std::cout << "exception id: " << e.Id() << std::endl;
249 }
250  return 0;
251 }
#define FAUDES_TEST_DUMP(mes, dat)
Test protocol record macro ("mangle" filename for platform independance)
Definition: cfl_helper.h:483
Faudes exception class.
virtual const char * What() const
Returns error description.
virtual unsigned int Id() const
Returns error id.
virtual const char * Where() const
Returns Function.
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
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.
bool StateNamesEnabled(void) const
Whether libFAUEDS functions are requested to generate state names.
void GraphWrite(const std::string &rFileName, const std::string &rOutFormat="", const std::string &rDotExec="dot") const
Produce graphical representation of this generator.
void Parallel(const Generator &rGen1, const Generator &rGen2, Generator &rResGen)
Parallel composition.
void HioSynthMonolithic(const HioPlant &rPlant, const HioPlant &rSpec, const HioConstraint &rSc, const HioConstraint &rSp, const HioConstraint &rSe, HioController &rController)
HioSynthMonolithic: I/O controller synthesis procedure for monolithic plant.
void HioSynthHierarchical(const HioPlant &rHioShuffle, const HioEnvironment &rEnvironment, const HioPlant &rSpec, const Generator &rIntConstr, const HioConstraint &rSc, const HioConstraint &rSl, HioController &rController)
HioSynthHierarchical: I/O controller synthesis procedure for I/O-shuffle of i plants and their intera...
void HioShuffle(const Generator &rPlantA, const Generator &rPlantB, const EventSet &rYp, const EventSet &rUp, const EventSet &rYe, const EventSet &rUe, Generator &rIOShuffAB)
HioShuffle: IO-shuffle of rPlantA and rPlantB according to definition.
void simpleMachine()
simple machine example to demonstrate I/O controller synthesis
int main()
Run the tutorial.
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 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