6_algorithm.cpp
Go to the documentation of this file.
1 /** @file 6_algorithm.cpp
2 
3 Tutorial, implementing generator algorithms.
4 
5 The example function provides an
6 alternative implementation of the Accessible() method of the Generator
7 class, that removes all states from the Generator which are not accessible
8 through a transition path from an initial state.
9 
10 @ingroup Tutorials
11 
12 @include 6_algorithm.cpp
13 
14 */
15 
16 
17 
18 #include "corefaudes.h"
19 
20 
21 // for simplicity we make the faudes namespace available to our program
22 
23 using namespace faudes;
24 
25 
26 /////////////////
27 // Example algorithm
28 /////////////////
29 
31 
32  // create a todo stack for state indices
33 
34  std::stack<Idx> todo;
35 
36  // create a empty StateSet for the set of accessible state
37 
38  StateSet accessible_states;
39 
40  // iterator for a StateSet
41 
42  StateSet::Iterator sit;
43 
44  // initialize the algorithm by pushing all initial states on the todo stack
45 
46  for(sit = rGen.InitStatesBegin(); sit != rGen.InitStatesEnd(); ++sit) {
47  todo.push(*sit);
48  }
49 
50  // process the todo stack until it's empty
51 
52  while(!todo.empty()) {
53 
54  // get the next state index from the todo stack
55 
56  const Idx current = todo.top();
57 
58  // delete the top element
59 
60  todo.pop();
61 
62  // insert the current state in the set of accessible states
63 
64  accessible_states.Insert(current);
65 
66  // create transition iterator for the states of the current state
67 
68  TransSet::Iterator tit = rGen.TransRelBegin(current);
69  TransSet::Iterator tit_end = rGen.TransRelEnd(current);
70 
71  while (tit != tit_end) {
72 
73  // push successor states ton todo stack if not already discovered
74  if(!accessible_states.Exists(tit->X2)) {
75  todo.push(tit->X2);
76  }
77 
78  // increment the transition iterator
79  ++tit;
80  }
81  }
82 
83  // delete the states and transitions which are not accessible
84 
85  rGen.DelStates(rGen.States() - accessible_states);
86 }
87 
88 
89 /////////////////
90 // main program
91 /////////////////
92 
93 int main() {
94 
95  // create a Generator
96 
97  Generator g1;
98 
99  // do some random "user interaction" stuff with the Generator g1
100 
101  g1.InsState("s1");
102  g1.InsState("s2");
103  g1.InsState("s3");
104  g1.InsState("s4");
105  g1.InsEvent("a");
106  g1.InsEvent("b");
107  g1.SetTransition("s1", "a", "s2");
108  g1.SetTransition("s2", "a", "s3");
109  g1.SetTransition("s3", "b", "s1");
110  g1.SetInitState("s1");
111  g1.SetMarkedState("s2");
112  g1.SetMarkedState("s3");
113 
114  // write Generator to console in debugging mode
115 
116  g1.DWrite();
117 
118  // we call our example function "AlternativeAccessible", that makes the Generator's
119  // set of states accessible
120 
122 
123  // write Generator to console in debugging mode
124 
125  g1.DWrite();
126 
127 
128  return 0;
129 }
130 
131 
int main()
Definition: 6_algorithm.cpp:93
Set of indices.
Definition: cfl_indexset.h:78
Idx Insert(void)
Insert new index to set.
TBaseSet< Transition, TransSort::X1EvX2 >::Iterator Iterator
Iterator on transition.
Definition: cfl_transset.h:269
void DWrite(const Type *pContext=0) const
Write configuration data to console, debugging format.
Definition: cfl_types.cpp:225
Base class of all FAUDES generators.
StateSet::Iterator InitStatesBegin(void) const
Iterator to Begin() of mInitStates.
bool SetTransition(Idx x1, Idx ev, Idx x2)
Add a transition to generator by indices.
TransSet::Iterator TransRelBegin(void) const
Iterator to Begin() of transition relation.
void SetInitState(Idx index)
Set an existing state as initial state by index.
void DelStates(const StateSet &rDelStates)
Delete a set of states Cleans mpStates, mInitStates, mMarkedStates, mpTransrel, and mpStateSymboltabl...
TransSet::Iterator TransRelEnd(void) const
Iterator to End() of transition relation.
Idx InsState(void)
Add new anonymous state to generator.
void SetMarkedState(Idx index)
Set an existing state as marked state by index.
bool InsEvent(Idx index)
Add an existing event to alphabet by index.
StateSet::Iterator InitStatesEnd(void) const
Iterator to End() of mInitStates.
const StateSet & States(void) const
Return reference to state set.
Includes all libFAUDES headers, no plugins.
bool Exists(const T &rElem) const
Test existence of element.
Definition: cfl_baseset.h:2115
void AlternativeAccessible(Generator &rGen)
Alternative accessibility algorithm.
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