mtc_2_functions.cpp
Go to the documentation of this file.
1 /** @file mtc_2_functions.cpp
2 
3 Tutorial, MtcSystem functions eg parallel, project etc.
4 
5 
6 @ingroup Tutorials
7 
8 @include mtc_2_functions.cpp
9 */
10 
11 
12 #include "libfaudes.h"
13 
14 
15 // Make the faudes namespace available to our program
16 using namespace faudes;
17 
18 
19 /////////////////
20 // main program
21 /////////////////
22 
23 int main() {
24  {
25  // create generator which is not accessible and not strongly, but weakly coaccessible
26  MtcSystem original, accNotStrCoac, notAccStrCoac, strTrim;
27 
28  // the implementation below allows the user to easily change the generators
29  // and to find out what the algorithms effect when being applied on them
30  Idx st1 = original.InsState("1");
31  Idx st2 = original.InsState("2");
32  Idx st3 = original.InsState("3");
33  Idx st4 = original.InsState("4");
34  Idx st5 = original.InsState("5");
35  Idx st6 = original.InsColoredState("6", "c6a");
36 
37  Idx eva = original.InsEvent("a");
38  Idx evb = original.InsEvent("b");
39  Idx evc = original.InsEvent("c");
40  Idx evd = original.InsEvent("d");
41 
42  original.InsColor(st3, "c3");
43  original.InsColor(st4, "c4");
44  original.InsColor(st6, "c6b");
45 
46  original.SetTransition(st1, eva, st2);
47  original.SetTransition(st2, evb, st3);
48  original.SetTransition(st2, evc, st4);
49  original.SetTransition(st4, evc, st6);
50  original.SetTransition(st5, eva, st4);
51  original.SetTransition(st6, evd, st1);
52  original.SetTransition(st3, evd, st3);
53 
54  original.SetInitState(st1);
55 
56  original.Write("tmp_mtc_functions_1a_not_trim.gen");
57  original.GraphWrite("tmp_mtc_functions_1a_not_trim.png");
58 
59  ////////////////////////////////////////////////////
60  // analyze a generator's accessibility properties
61  ////////////////////////////////////////////////////
62  if (!original.IsAccessible())
63  std::cout << std::endl << "Original generator is not accessible" << std::endl;
64  if (!original.IsStronglyCoaccessible())
65  std::cout << "Original generator is not strongly coaccessible" << std::endl;
66  if (!original.IsStronglyTrim())
67  std::cout << "Original generator is not strongly trim" << std::endl << std::endl;
68 
69  ////////////////////////////////////////////////////
70  // generate accessible version of original generator
71  ////////////////////////////////////////////////////
72  accNotStrCoac = original;
73  accNotStrCoac.Accessible();
74 
75  accNotStrCoac.Write("tmp_mtc_functions_1b_acc.gen");
76  accNotStrCoac.GraphWrite("tmp_mtc_functions_1b_acc.png");
77 
78  if (accNotStrCoac.IsAccessible())
79  std::cout << "accNotStrCoac is accessible" << std::endl;
80  if (!accNotStrCoac.IsStronglyTrim())
81  std::cout << "accNotStrCoac is not strongly trim" << std::endl << std::endl;
82 
83  // generate strongly coaccessible version of original generator
84  notAccStrCoac = original;
85  notAccStrCoac.StronglyCoaccessible();
86 
87  notAccStrCoac.Write("tmp_mtc_functions_1c_str_trim.gen");
88  notAccStrCoac.GraphWrite("tmp_mtc_functions_1c_str_trim.png");
89 
90  if (notAccStrCoac.IsStronglyCoaccessible())
91  std::cout << "notAccStrCoac is strongly coaccessible" << std::endl;
92  if (!notAccStrCoac.IsStronglyTrim())
93  std::cout << "notAccStrCoac is not strongly trim" << std::endl << std::endl;
94 
95  // generate trim version of original generator
96  strTrim = original;
97  strTrim.StronglyTrim();
98 
99  strTrim.Write("tmp_mtc_functions_1d_str_trim.gen");
100  strTrim.GraphWrite("tmp_mtc_functions_1d_str_trim.png");
101 
102  if (strTrim.IsAccessible())
103  std::cout << "strTrim is accessible" << std::endl;
104  if (strTrim.IsStronglyCoaccessible())
105  std::cout << "strTrim is strongly coaccessible" << std::endl;
106  if (strTrim.IsStronglyTrim())
107  std::cout << "strTrim is strongly trim" << std::endl << std::endl;
108  }
109 
110  {
111  // create nondeterministic generator nondet, deterministic version to compute shall be saved as det
112  MtcSystem nondet, det;
113 
114  // the implementation below allows the user to easily change the generators
115  // and to find out what the algorithms effect when being applied on them
116  Idx st1 = nondet.InsState("1");
117  Idx st2 = nondet.InsState("2");
118  Idx st3 = nondet.InsState("3");
119  Idx st4 = nondet.InsState("4");
120  Idx st5 = nondet.InsState("5");
121  Idx st6 = nondet.InsColoredState("6", "c6a");
122 
123  Idx eva = nondet.InsEvent("a");
124  Idx evb = nondet.InsEvent("b");
125  Idx evc = nondet.InsEvent("c");
126 
127  nondet.InsColor(st3, "c3");
128  nondet.InsColor(st4, "c4");
129  nondet.InsColor(st5, "c5");
130  nondet.InsColor(st6, "c6b");
131 
132  nondet.SetTransition(st1, eva, st2);
133  nondet.SetTransition(st2, evb, st3);
134  nondet.SetTransition(st2, evb, st4);
135  nondet.SetTransition(st4, evc, st6);
136  nondet.SetTransition(st5, eva, st4);
137 
138  nondet.SetInitState(st1);
139  nondet.SetInitState(st5);
140 
141  nondet.Write("tmp_mtc_functions_2a_nondet.gen");
142  nondet.GraphWrite("tmp_mtc_functions_2a_nondet.png");
143 
144  //////////////////////////////////////////////////
145  // make nondeterministic generator deterministic
146  //////////////////////////////////////////////////
147  mtcDeterministic(nondet, det);
148 
149  det.Write("tmp_mtc_functions_2b_det.gen");
150  det.GraphWrite("tmp_mtc_functions_2b_det.png");
151  }
152 
153  {
154  EventSet evset;
155  evset.Insert("a");
156  evset.Insert("b");
157  evset.Insert("d");
158 
159  MtcSystem original, projected;
160 
161  Idx st1 = original.InsState("1");
162  Idx st2 = original.InsState("2");
163  Idx st3 = original.InsState("3");
164  Idx st4 = original.InsState("4");
165  Idx st5 = original.InsState("5");
166 
167  Idx eva = original.InsEvent("a");
168  Idx evb = original.InsEvent("b");
169  Idx evc = original.InsEvent("c");
170  Idx evd = original.InsEvent("d");
171 
172  original.InsColor(st2, "color2");
173  original.InsColor(st3, "color3");
174 
175  original.SetTransition(st1, eva, st2);
176  original.SetTransition(st2, evb, st3);
177  original.SetTransition(st2, evc, st4);
178  original.SetTransition(st3, evc, st5);
179  original.SetTransition(st4, evb, st5);
180  original.SetTransition(st5, evd, st1);
181 
182  original.SetInitState(st1);
183 
184  original.StateNamesEnabled(false);
185  original.Write("tmp_mtc_functions_3a_system.gen");
186  original.GraphWrite("tmp_mtc_functions_3a_system.png");
187 
188  // Write state sets for colored and uncolored states to console
189  // original.ColoredStates().Write();
190  // original.UncoloredStates().Write();
191 
192  //////////////////////////////////////////////////////////////
193  // compute projection for alphabet evset and the original
194  // generator result is deterministic and saved in projected
195  //////////////////////////////////////////////////////////////
196  mtcProject(original, evset, projected);
197 
198  projected.Write("tmp_mtc_functions_3b_projected.gen");
199  projected.GraphWrite("tmp_mtc_functions_3b_projected.png");
200 
201  //////////////////////////////////////////////////////////////
202  // compute original generator to its projected version for the
203  // alphabet evset result is nondeterministic
204  //////////////////////////////////////////////////////////////
205  mtcProjectNonDet(original, evset);
206 
207  original.Write("tmp_mtc_functions_3c_projected_nondet.gen");
208  original.GraphWrite("tmp_mtc_functions_3c_projected_nondet.png");
209  }
210 
211  {
212  // create two generators for parallel composition and compose them to gen_ab
213  MtcSystem gen_a, gen_b, gen_ab;
214 
215  // generator a
216  Idx sta1 = gen_a.InsInitState("1");
217  Idx sta2 = gen_a.InsState("2");
218 
219  gen_a.InsColor(sta2, "Color_1");
220 
221  Idx evaa = gen_a.InsEvent("a");
222  Idx evab = gen_a.InsEvent("b");
223 
224  gen_a.SetTransition(sta1, evaa, sta2);
225  gen_a.SetTransition(sta2, evab, sta1);
226 
227  gen_a.Write("tmp_mtc_functions_4a_system.gen");
228  gen_a.GraphWrite("tmp_mtc_functions_4a_system.png");
229 
230  // generator b
231  Idx stb1 = gen_b.InsInitState("1");
232  Idx stb2 = gen_b.InsState("2");
233 
234  gen_b.InsColor(stb1, "Color_1");
235  gen_b.InsColor(stb2, "Color_2");
236 
237  Idx evba = gen_b.InsEvent("a");
238  Idx evbc = gen_b.InsEvent("c");
239 
240  gen_b.SetTransition(sta1, evba, sta2);
241  gen_b.SetTransition(sta2, evbc, sta1);
242 
243  gen_b.Write("tmp_mtc_functions_4b_system.gen");
244  gen_b.GraphWrite("tmp_mtc_functions_4b_system.png");
245 
246  /////////////////////////////////////////////
247  // Compute the parallel composition gen_ab of
248  // these two colored marking generators
249  /////////////////////////////////////////////
250  mtcParallel(gen_a, gen_b, gen_ab);
251 
252  gen_ab.Write("tmp_mtc_functions_4c_parallel.gen");
253  gen_ab.GraphWrite("tmp_mtc_functions_4c_parallel.png");
254  }
255 
256 
257  {
258  // create model model and specification spec for
259  // computing a strongly nonblocking supervisor sup
260  MtcSystem model, spec, sup, sup_nb;
261 
262  // generator model
263  Idx stm1 = model.InsInitState("1");
264  Idx stm2 = model.InsState("2");
265  Idx stm3 = model.InsState("3");
266  Idx stm4 = model.InsState("4");
267  Idx stm5 = model.InsState("5");
268  Idx stm6 = model.InsState("6");
269 
270  model.InsColor(stm1, "Color_1");
271  model.InsColor(stm6, "Color_2");
272 
273  Idx evma = model.InsControllableEvent("a");
274  Idx evmb = model.InsControllableEvent("b");
275  Idx evmc = model.InsControllableEvent("c");
276 
277  model.SetTransition(stm1, evmb, stm2);
278  model.SetTransition(stm2, evma, stm3);
279  model.SetTransition(stm2, evmc, stm4);
280  model.SetTransition(stm4, evma, stm6);
281  model.SetTransition(stm1, evmc, stm5);
282  model.SetTransition(stm5, evmb, stm6);
283  model.SetTransition(stm6, evma, stm1);
284 
285  model.Write("tmp_mtc_functions_5_plant.gen");
286  model.GraphWrite("tmp_mtc_functions_5_plant.png");
287 
288  // generator spec
289  Idx sts1 = spec.InsInitState("1");
290  Idx sts2 = spec.InsState("2");
291 
292  Idx evsb = spec.InsControllableEvent("b");
293  Idx evsc = spec.InsControllableEvent("c");
294 
295  spec.SetTransition(sts1, evsb, sts2);
296  spec.SetTransition(sts2, evsc, sts1);
297 
298  spec.Write("tmp_mtc_functions_5_spec.gen");
299  spec.GraphWrite("tmp_mtc_functions_5_spec.png");
300 
301  /////////////////////////////////////////////////////////
302  // Inverse projection adds missing events by inserting
303  // self-loops into all automaton's states
304  /////////////////////////////////////////////////////////
305  mtcInvProject(spec, model.Alphabet());
306 
307  spec.Write("tmp_mtc_functions_5_spec_invpro.gen");
308  spec.GraphWrite("tmp_mtc_functions_5_spec_invpro.png");
309 
310  /////////////////////////////////////////////////////////
311  // Supervisor computation
312  /////////////////////////////////////////////////////////
313  mtcSupConClosed(model, spec, sup);
314 
315  sup.Write("tmp_mtc_functions_5_super.gen");
316  sup.GraphWrite("tmp_mtc_functions_5_super.png");
317 
318  /////////////////////////////////////////////
319  // Nonblocking supervisor computation
320  /////////////////////////////////////////////
321  mtcSupConNB(model, spec, sup_nb);
322 
323  sup_nb.Write("tmp_mtc_functions_5_supernb.gen");
324  sup_nb.GraphWrite("tmp_mtc_functions_5_supernb.png");
325  }
326 
327 }
328 
Set of indices with symbolic names.
Definition: cfl_nameset.h:69
bool Insert(const Idx &rIndex)
Add an element by index.
Idx InsState(void)
Add new anonymous state to generator.
bool InsEvent(Idx index)
Add an existing event to alphabet by index.
const TaEventSet< EventAttr > & Alphabet(void) const
Return const reference to alphabet.
bool SetTransition(Idx x1, Idx ev, Idx x2)
Add a transition to generator by indices.
void InsControllableEvent(Idx index)
Add an existing controllable event to generator.
Allows to create colored marking generators (CMGs) as the common five tupel consisting of alphabet,...
Definition: mtc_generator.h:53
bool StronglyTrim(void)
Make generator strongly trim.
Idx InsColoredState(const std::string &rStateName, const std::string &rColorName)
Create a new named state and set the color rColorName.
bool IsStronglyTrim(void) const
Check if the MtcSystem is strongly trim.
Idx InsColor(Idx stateIndex, const std::string &rColorName)
Insert a color by name into an existing state.
bool StronglyCoaccessible(void)
Make generator strongly coaccessible.
bool IsStronglyCoaccessible(void) const
Check if MtcSystem is strongly coaccessible.
void Write(const Type *pContext=0) const
Write configuration data to console.
Definition: cfl_types.cpp:139
bool Accessible(void)
Make generator accessible.
bool IsAccessible(void) const
Check if generator is accessible.
void SetInitState(Idx index)
Set an existing state as initial 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 mtcSupConNB(const MtcSystem &rPlantGen, const MtcSystem &rSpecGen, MtcSystem &rResGen)
Nonblocking Supremal Controllable Sublanguage (wrapper function)
Definition: mtc_supcon.cpp:41
void mtcProject(const MtcSystem &rGen, const EventSet &rProjectAlphabet, MtcSystem &rResGen)
Minimized Deterministic projection.
void mtcSupConClosed(const MtcSystem &rPlantGen, const MtcSystem &rSpecGen, MtcSystem &rResGen)
Supremal Controllable Sublanguage (wrapper function)
Definition: mtc_supcon.cpp:130
void mtcInvProject(MtcSystem &rGen, const EventSet &rProjectAlphabet)
Inverse projection.
void mtcProjectNonDet(MtcSystem &rGen, const EventSet &rProjectAlphabet)
Project generator to alphabet rProjectAlphabet.
void mtcDeterministic(const MtcSystem &rGen, MtcSystem &rResGen)
Make generator deterministic.
Definition: mtc_project.cpp:73
void mtcParallel(const MtcSystem &rGen1, const MtcSystem &rGen2, MtcSystem &rResGen)
Parallel composition of two colored marking generators, controllability status is observed.
Includes all libFAUDES headers, incl plugings
int main()
libFAUDES resides within the namespace faudes.
uint32_t Idx
Type definition for index type (allways 32bit)

libFAUDES 2.32f --- 2024.12.22 --- c++ api documentaion by doxygen