6_algorithm.cpp

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

Generated on Fri May 9 11:26:47 2008 for libFAUDES 2.09b by  doxygen 1.4.4