mtc_1_generators.cpp
Go to the documentation of this file.
1 /** @file mtc_1_generators.cpp
2 
3 Tutorial, MtcSystem methods.
4 
5 @ingroup Tutorials
6 
7 @include mtc_1_generators.cpp
8 */
9 
10 
11 #include "libfaudes.h"
12 
13 
14 // make faudes namespace available to our program
15 using namespace faudes;
16 
17 
18 /////////////////
19 // main program
20 /////////////////
21 
22 int main() {
23 
24  /***************************************************************
25  * Constructor and file io
26  *
27  ***************************************************************/
28 
29  // At first we create an empty MtcSystem object
30  MtcSystem g1;
31 
32  // Insert states
33  Idx s1 = g1.InsState("up");
34  Idx s2 = g1.InsInitState("middle");
35 
36  // Insert events
37  Idx e1, e2;
38  e1 = g1.InsEvent("go_up");
39  e2 = g1.InsEvent("go_down");
40 
41  // Set transitions - by name or index
42  g1.SetTransition("middle", "go_up", "up");
43  g1.SetTransition(s1, e2, s2);
44 
45  // Generate further MtcSystem objects by reading a generator from a file...
46  MtcSystem g2("data/mtc_generator_1.gen");
47 
48  MtcSystem g3;
49  g3.Read("data/mtc_generator_1.gen");
50 
51  // ...or by copying a generator
52  MtcSystem g4=g3;
53 
54  // Output of MtcSystem to file
55  g4.Write("tmp_mtc_generator_1.gen");
56  g1.Write("tmp_mtc_generator_2.gen");
57 
58  // Write MtcSystem to console (for debugging)
59  std::cout << "##########################################" << std::endl;
60  std::cout << "# mtcgenerators - constructors and file io" << std::endl;
61  std::cout << std::endl << "# MtcSystem without colors:" << std::endl << std::endl;
62  g1.DWrite();
63  std::cout << std::endl << "# MtcSystem with colors:" << std::endl << std::endl;
64  g2.DWrite();
65  std::cout << std::endl << "# same MtcSystem, but read from file using the Read() method:" << std::endl << std::endl;
66  g3.DWrite();
67  std::cout << "##########################################" << std::endl << std::endl;
68 
69 
70  /***************************************************************
71  * Output to .dot-file for Graphviz visualization
72  *
73  ***************************************************************/
74 
75  // Generator .dot-file for Graphviz dot
76 
77  // Output of the MtcSystem to the dot format, where states are colored
78  g1.DotWrite("tmp_mtc_generator_1.dot");
79 
80  // Graphical output of the MtcSystem, invokes Graphviz dot
81  g2.GraphWrite("tmp_mtc_generator_2.png");
82 
83 
84  /***************************************************************
85  * Editing state attributes - colored states
86  *
87  ***************************************************************/
88 
89  // Insert a new colored state
90  Idx s3 = g1.InsColoredState("down", "low");
91 
92  // Set further transitions
93  g1.SetTransition(s2, e2, s3);
94  g1.SetTransition(s3, e1, s2);
95 
96  // Set a color for an existing state
97  Idx c1 = g1.InsColor(s1, "high");
98  g1.InsColor(s2, c1);
99 
100  // test output
101  g1.Write("tmp_mtc_generator_3.gen");
102  g1.GraphWrite("tmp_mtc_generator_3.png");
103 
104  // The color label of the second state is wrong
105  // ==> delete it
106  g1.DelColor(s2, "high");
107 
108  // Test output
109  g1.Write("tmp_mtc_generator_4.gen");
110  g1.GraphWrite("tmp_mtc_generator_4.png");
111 
112  // Find out index of a color
113  Idx c3 = g1.ColorIndex("high");
114  std::cout << "Index of color \"high\": " << c3 << std::endl;
115 
116  // Find out name of color
117  std::string color3 = g1.ColorName(c3);
118  std::cout << "Color name for index " << c3 << ": " << color3 << std::endl;
119 
120  // Delete one color from all states
121  g1.DelColor(c3);
122 
123  // Test output
124  g1.Write("tmp_mtc_generator_5.gen");
125  g1.GraphWrite("tmp_mtc_generator_5.png");
126 
127  // Delete all colors from all states
129 
130  // Test output
131  g1.Write("tmp_mtc_generator_6.gen");
132  g1.GraphWrite("tmp_mtc_generator_6.png");
133 
134  // Reinsert color "high"
135  g1.InsColor(s1, c3);
136 
137  // Test output
138  g1.Write("tmp_mtc_generator_7.gen");
139  g1.GraphWrite("tmp_mtc_generator_7.png");
140 
141  // delete all colors from a particular state
142  g1.ClrStateAttribute(s1);
143 
144  // test output
145  g1.Write("tmp_mtc_generator_8.gen");
146  g1.GraphWrite("tmp_mtc_generator_8.png");
147 
148  // reinsert color
149  g1.InsColor(s1, c1);
150 
151  // test output
152  g1.Write("tmp_mtc_generator_9.gen");
153  g1.GraphWrite("tmp_mtc_generator_9.png");
154 
155  // Lookup name of reinserted color
156  std::string name = g1.ColorName(c1);
157 
158  // Color names are not deleted from symbol table as long as the global
159  // color symbol table is used.
160  std::cout << "Color name for index " << c1 << ": " << name << std::endl;
161 
162  // Check if color exists in generator
163  // (therefore iterate over all states -> expensive method)
164  if(!g1.ExistsColor(c1))
165  std::cout << "Color c1 does not exist in g1 anymore" << std::endl;
166  else
167  std::cout << "Color c1 exists in g1" << std::endl;
168 
169  // Check if a color exists in a particular state
170  if(!g1.ExistsColor(s2, c1))
171  std::cout << "State s2 is not colored by color c1" << std::endl;
172  if(g1.ExistsColor(s1, c1))
173  std::cout << "State s1 is colored by color " << g1.CStr(c1) << std::endl;
174 
175  // Collect all colors of the generator
176  // Directly inserts colors into the specified reference of a color set
177  ColorSet allColors1;
178  g1.Colors(allColors1);
179 
180  // Creates temporary color set, inserts all colors,
181  // and copies the set to allcolors2 afterwards
182  ColorSet allColors2 = g1.Colors();
183 
184  // Get all colors of a particular state
185  const ColorSet& allStateColors =g1.Colors(s1);
186 
187  // Print color to console
188  allStateColors.DWrite();
189 
190  // Clear MtcSystem g1
191  g1.Clear();
192 
193 
194  /////////////////////////////////////////////////////
195  // Local color symbol table
196  //
197  // In the current implementation, a symbol table is treated local
198  // whenever it is different to the global one. The implementation
199  // then tries to keep book and remove symbols that are not referenced.
200  // However, this feature in its current form is inefficient and not
201  // very useful. It will disapper in a future version.
202  //
203  /////////////////////////////////////////////////////
204 
205  {
206  MtcSystem gen, copygen;
207 
208  Idx st1 = gen.InsInitState("1");
209  Idx st2 = gen.InsState("2");
210 
211  Idx eva = gen.InsEvent("a");
212  Idx evb = gen.InsEvent("b");
213  Idx evc = gen.InsEvent("c");
214 
215  Idx c1 = gen.InsColor(st1, "first");
216  Idx c2 = gen.InsColor(st2, "second");
217 
218  gen.SetTransition(st1, eva, st2);
219  gen.SetTransition(st2, evb, st2);
220  gen.SetTransition(st2, evc, st1);
221 
222  std::cout << "gen: Color name of c1 = " << gen.ColorName(c1) << std::endl;
223  std::cout << "gen: Color index of first = " << gen.ColorIndex("first") << std::endl;
224 
225  // Generate new color symbol table for gen,
226  // all color labels already contained in gen are copied
227  gen.NewColorSymbolTable();
228 
229  std::cout << "gen: Color name of c1 = " << gen.ColorName(c1) << std::endl;
230  std::cout << "gen: Color index of first = " << gen.ColorIndex("first") << std::endl;
231 
232  copygen.Assign(gen);
233 
234  gen.DelColor(c1);
235 
236  std::cout << "copygen: Color name of c1 = " << copygen.ColorName(c1) << std::endl;
237  std::cout << "copygen: Color index of first = " << copygen.ColorIndex("first") << std::endl;
238 
239  Idx c3 = copygen.InsColor(st1, "New_Color");
240 
241  if (!gen.ExistsColor(c1)) std::cout << "gen: Index c1 does not exist!" << std::endl;
242  else std::cout << "gen: Index c1 exists!" << std::endl;
243  try {
244  std::cout << "gen: Color name of c1 = " << gen.ColorName(c1) << std::endl;
245  }
246  catch (faudes::Exception& exception){
247  std::cout << "gen: Color name of c1 does not exist any more" << std::endl;
248  }
249 
250  if (!gen.ExistsColor(c3)) std::cout << "gen: Index c3 does not exist!" << std::endl;
251  else std::cout << "gen: Index c3 exists!" << std::endl;
252  try {
253  std::cout << "gen: Color name of c3 = " << gen.ColorName(c1) << std::endl;
254  }
255  catch (faudes::Exception& exception){
256  std::cout << "gen: Color name of c3 does not exist any more" << std::endl;
257  }
258 
259  if (!copygen.ExistsColor(c1)) std::cout << "copygen: Index c1 does not exist!" << std::endl;
260  else std::cout << "copygen: Index c1 exists!" << std::endl;
261  try {
262  std::cout << "copygen: Color name of c1 = " << copygen.ColorName(c1) << std::endl;
263  }
264  catch (faudes::Exception& exception){
265  std::cout << "copygen: Color name of c1 does not exist any more" << std::endl;
266  }
267 
268  if (!copygen.ExistsColor(c3)) std::cout << "copygen: Index c3 does not exist!" << std::endl;
269  else std::cout << "copygen: Index c3 exists!" << std::endl;
270  try {
271  std::cout << "copygen: Color name of c3 = " << copygen.ColorName(c1) << std::endl;
272  }
273  catch (faudes::Exception& exception){
274  std::cout << "copygen: Color name of c3 does not exist any more" << std::endl;
275  }
276 
277  }
278 
279  return 0;
280 }
Container for colors: this is a NameSet with its own static symboltable.
Definition: mtc_colorset.h:41
Faudes exception class.
Idx InsState(void)
Add new anonymous state to generator.
virtual void Clear(void)
Clear generator data.
bool InsEvent(Idx index)
Add an existing event to alphabet by index.
bool SetTransition(Idx x1, Idx ev, Idx x2)
Add a transition to generator by indices.
Allows to create colored marking generators (CMGs) as the common five tupel consisting of alphabet,...
Definition: mtc_generator.h:53
std::string ColorName(Idx colorIndex) const
Look up the color name for a given color index.
void NewColorSymbolTable()
Insert a new local color symbol table.
bool ExistsColor(Idx colorIndex) const
Check if color exists in generator.
virtual TmtcGenerator & Assign(const Type &rSrc)
Assignment.
void Colors(ColorSet &rColors) const
Insert all colors used in the generator to a given ColorSet.
void DelColor(Idx stateIndex, const std::string &rColorName)
Remove color by name from an existing state specified by index.
virtual void DotWrite(const std::string &rFileName) const
Writes generator to dot input format.
Idx ColorIndex(const std::string &rColorName) const
Look up the color index for a given color name.
Idx InsColoredState(const std::string &rStateName, const std::string &rColorName)
Create a new named state and set the color rColorName.
void ClearStateAttributes()
Clear all the generator's state attributes.
Idx InsColor(Idx stateIndex, const std::string &rColorName)
Insert a color by name into an existing state.
std::string CStr(Idx index) const
Return pretty printable color name for index.
void DWrite(const Type *pContext=0) const
Write configuration data to console, debugging format.
Definition: cfl_types.cpp:225
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
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.
virtual void ClrStateAttribute(Idx index)
Clear attribute for existing state.
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.32b --- 2024.03.01 --- c++ api documentaion by doxygen