6_algorithm.cppGo to the documentation of this file.00001 /** @file 6_algorithm.cpp 00002 00003 Tutorial, implementing generator algorithms. 00004 00005 The example function provides an 00006 alternative implementation of the Accessible() method of the Generator 00007 class, that removes all states from the Generator which are not accessible 00008 through a transition path from an initial state. 00009 00010 @ingroup Tutorials 00011 00012 @include 6_algorithm.cpp 00013 00014 */ 00015 00016 00017 00018 #include "corefaudes.h" 00019 00020 00021 // for simplicity we make the faudes namespace available to our program 00022 00023 using namespace faudes; 00024 00025 00026 ///////////////// 00027 // Example algorithm 00028 ///////////////// 00029 00030 void AlternativeAccessible(Generator& rGen) { 00031 00032 // create a todo stack for state indices 00033 00034 std::stack<Idx> todo; 00035 00036 // create a empty StateSet for the set of accessible state 00037 00038 StateSet accessible_states; 00039 00040 // iterator for a StateSet 00041 00042 StateSet::Iterator sit; 00043 00044 // initialize the algorithm by pushing all initial states on the todo stack 00045 00046 for (sit = rGen.InitStatesBegin(); sit != rGen.InitStatesEnd(); ++sit) { 00047 todo.push(*sit); 00048 } 00049 00050 // process the todo stack until it's empty 00051 00052 while (not todo.empty()) { 00053 00054 // get the next state index from the todo stack 00055 00056 const Idx current = todo.top(); 00057 00058 // delete the top element 00059 00060 todo.pop(); 00061 00062 // insert the current state in the set of accessible states 00063 00064 accessible_states.Insert(current); 00065 00066 // create transition iterator for the states of the current state 00067 00068 TransSet::Iterator tit = rGen.TransRelBegin(current); 00069 TransSet::Iterator tit_end = rGen.TransRelEnd(current); 00070 00071 while (tit != tit_end) { 00072 00073 // push successor states ton todo stack if not already discovered 00074 if (not accessible_states.Exists(tit->X2)) { 00075 todo.push(tit->X2); 00076 } 00077 00078 // increment the transition iterator 00079 ++tit; 00080 } 00081 } 00082 00083 // delete the states and transitions which are not accessible 00084 00085 rGen.DelStates(rGen.States() - accessible_states); 00086 } 00087 00088 00089 ///////////////// 00090 // main program 00091 ///////////////// 00092 00093 int main() { 00094 00095 // create a Generator 00096 00097 Generator g1; 00098 00099 // do some random "user interaction" stuff with the Generator g1 00100 00101 g1.InsState("s1"); 00102 g1.InsState("s2"); 00103 g1.InsState("s3"); 00104 g1.InsState("s4"); 00105 g1.InsEvent("a"); 00106 g1.InsEvent("b"); 00107 g1.SetTransition("s1", "a", "s2"); 00108 g1.SetTransition("s2", "a", "s3"); 00109 g1.SetTransition("s3", "b", "s1"); 00110 g1.SetInitState("s1"); 00111 g1.SetMarkedState("s2"); 00112 g1.SetMarkedState("s3"); 00113 00114 // write Generator to console in debugging mode 00115 00116 g1.DWrite(); 00117 00118 // we call our example function "AlternativeAccessible", that makes the Generator's 00119 // set of states accessible 00120 00121 AlternativeAccessible(g1); 00122 00123 // write Generator to console in debugging mode 00124 00125 g1.DWrite(); 00126 00127 00128 return 0; 00129 } 00130 00131 libFAUDES 2.23h --- 2014.04.03 --- c++ api documentaion by doxygen |