libFAUDES

Sections

Index

1_generator.cpp

Go to the documentation of this file.
00001 /** @file 1_generator.cpp 
00002 
00003 Tutorial, basic faudes::Generator methods. 
00004 
00005 The Generator class implements the 5-tuple automaton G, consisting of
00006 - Set of States Q, 
00007 - Alphabet Sigma, 
00008 - Transition Relation Delta,
00009 - Set of Initial States Qo, and
00010 - Set of Marked States Qm.
00011 
00012 This tutorial demonstrates how to insert/erase states, events and transitions. 
00013 It also demonstrates file IO.
00014 
00015 @ingroup Tutorials 
00016 @include 1_generator.cpp
00017 
00018 */
00019 
00020 
00021 #include "libfaudes.h"
00022 
00023 // make the faudes namespace available to our program
00024 using namespace faudes;
00025 
00026 
00027 
00028 /////////////////
00029 // main program
00030 /////////////////
00031 
00032 /** Run the tutorial */
00033 int main() {
00034 
00035   ////////////////////////////////////////////
00036   // Constructors (part 1) and filling example
00037   ////////////////////////////////////////////
00038 
00039   // at first we create an empty Generator object
00040 
00041   Generator g1;
00042 
00043   // do some random "user interaction" stuff with the Generator g1
00044 
00045   g1.InsState("s1");  
00046   g1.InsState("s2");                  
00047   g1.InsState("s3");                  
00048 
00049   g1.InsEvent("a");         
00050   g1.InsEvent("b");
00051 
00052   g1.SetTransition("s1", "a", "s2");  
00053   g1.SetTransition("s2", "a", "s3");  
00054   g1.SetTransition("s3", "b", "s1");
00055 
00056   g1.SetInitState("s1");              
00057   g1.SetMarkedState("s2");
00058   g1.SetMarkedState("s3");
00059 
00060   // inspect result on console
00061 
00062   std::cout << "################################\n";
00063   std::cout << "# tutorial, handcraft generator \n";
00064   g1.Write();
00065   std::cout << "################################\n";
00066 
00067 
00068 
00069   ///////////////////////////////////////////////////
00070   // Constructors (part 2) & Copying and versioning
00071   ///////////////////////////////////////////////////
00072 
00073   // Create a  1:1 copy of the Generator with the copy constructor ... 
00074 
00075   Generator g_copy(g1);
00076 
00077   // ... with assignment method, or assignement operator
00078 
00079   Generator g2;
00080   g2.Assign(g1);
00081   Generator g3=g2;
00082 
00083   // create a Generator copy with versioned events (for testing algorithms):
00084   // versioning by an integer. E.g. for integer 3 events {"a", "b", "c"}
00085   // become {"a_3", "b_3", "c_3"}.
00086 
00087   Generator version1;
00088   g3.Version(3, version1);
00089 
00090   // versioning by a string. "a" -> "a_versionstring"
00091 
00092   Generator version2;
00093   g3.Version("str", version2);
00094 
00095   // inspect result on console
00096 
00097   std::cout << "################################\n";
00098   std::cout << "# tutorial, version of generator \n";
00099   version2.Write();
00100   std::cout << "################################\n";
00101 
00102 
00103 
00104   ///////////////////////////////////////////////
00105   // Methods for Input/Output
00106   ///////////////////////////////////////////////
00107 
00108   // read a Generator from file 
00109 
00110   g2.Read("data/simplemachine.gen");
00111 
00112   // create a Generator by reading a Generator file
00113 
00114   Generator g4("data/simplemachine.gen");
00115 
00116 
00117   // write a Generator to file 
00118   // (map state indices to begin with 1)
00119 
00120   g4.Write("tmp_simplemachine.gen");
00121 
00122   // write a Generator to file by appending the output 
00123   // (map state indeces to begin with 1)
00124 
00125   g4.Write("tmp_simplemachine.gen", std::ios::out|std::ios::app);
00126 
00127   // debug output of Generator to console 
00128   // (use actual state indices)
00129 
00130   std::cout << "################################\n";
00131   std::cout << "# tutorial, debug dump \n";
00132   g4.DWrite();
00133   std::cout << "################################\n";
00134 
00135   // create dotfile for further processing by graphviz 
00136   // (map state indices to begin with 1)
00137 
00138   g4.DotWrite("tmp_simplemachine.dot"); 
00139   g4.DDotWrite("tmp_simplemachine_debug.dot");
00140 
00141   // there also is a convenience method, that runs graphviz to
00142   // generate graphical output; requires  "dot" binary in $PATH
00143   try {
00144     g4.GraphWrite("tmp_simplemachin.png");
00145   } catch(faudes::Exception& exception) {
00146     std::cout << "1_generator: cannot execute graphviz' dot. " << std::endl;
00147   } 
00148 
00149 
00150   // create a debug string for an event with symbolic name + index
00151 
00152   std::string str_singleevent = g1.EStr(2);
00153 
00154   // create a debug string for a state with symbolic name + index.
00155   // If there is no symblic name, a symbolic name of the index is constructed.
00156 
00157   std::string str_singlestate = g1.SStr(3);
00158 
00159   // build string of events in the Generator's alphabet
00160 
00161   std::string str_alph =  g1.AlphabetToString();
00162 
00163   // build string of states in the Generator's set of states
00164   std::string str_states = g1.StatesToString();
00165 
00166   // there also are TransRelToString(), InitStatesToString() and  MarkedStatesToString()  
00167 
00168 
00169   //////////////////////////////////////
00170   // Accessing the Generator's Members
00171   //////////////////////////////////////
00172 
00173   // get the Generator's name
00174 
00175   std::string str_name = g1.Name();
00176 
00177   // set new name for Generator
00178 
00179   g1.Name("NewName");
00180 
00181 
00182 
00183   // the core members alphabet, stateset and transitionrelation may be retrieved 
00184   // as const references; ie. they can be inspected freely, but write access is 
00185   // exclusively via the provided Generator methods.
00186 
00187   // retrieve a const reference to and copy of the Generator's alphabet
00188 
00189   const EventSet&  eset_ref_alph  = g1.Alphabet();
00190   EventSet         eset_copy_alph = g1.Alphabet();
00191 
00192   // you cannot alter the alphabet of a generator via an  alphabet method
00193   // eset_ref_alph.Insert("new_event");  // compile time error!
00194 
00195   // however, the copy can be altered, but with no effect on the original generator
00196 
00197   eset_copy_alph.Insert("new_event"); 
00198   if(g1.ExistsEvent("new_event")) std::cout << "### THIS CANNOT HAPPEN ###";
00199 
00200   // retrieve a const reference to and copy of the Generator's set of states "mStates"
00201 
00202   const StateSet& sset_ref_states = g1.States();
00203   StateSet sset_copy_states = g1.States();
00204 
00205   // retrieve a const reference to and a copy of the Generator's transition relation "mTransRel"
00206 
00207   const TransSet& tset_ref_trel = g1.TransRel();
00208   TransSet tset_copy_trel = g1.TransRel();
00209 
00210   // same with initial states and marked states
00211 
00212   const StateSet& sset_ref_istates = g1.InitStates();
00213   StateSet sset_copy_istates = g1.InitStates();
00214 
00215   const StateSet& sset_ref_mstates = g1.MarkedStates();
00216   StateSet sset_copy_mstates = g1.MarkedStates();
00217 
00218 
00219 
00220   //////////////////////////////////////////////////////////////////////////////
00221   // Modifying the 5-tuple Generator (X, Sigma, Delta, X0 and Xm)
00222   //////////////////////////////////////////////////////////////////////////////
00223 
00224   // insert an event by it's symbolic name in the alphabet
00225   // (if the event is not known so far, a new index for the symbolic name is generated) 
00226   g1.InsEvent("newevent");
00227 
00228 
00229   // insert an existing event into the Generator's alphabet (mAlphabet)
00230   // (by "existing event" we refer to an event that has been previously inserted to some Generator)
00231 
00232   g1.InsEvent(1);   // of course index 1 is already in the alphabet here...
00233 
00234   // insert a bunch of events (EventSet) and get the integer index if requested
00235 
00236   EventSet eset1;
00237   eset1.Insert("newevent1");
00238   Idx idx_tmp = eset1.Insert("newevent2");
00239   g1.InsEvents(eset1);
00240 
00241   // delete an event from Generator ie delete from alphabet and transition relation
00242 
00243   g1.DelEvent("newevent1"); // by symbolic name
00244   g1.DelEvent(idx_tmp);     // by index
00245 
00246   // delete a bunch of events
00247   // g1.DelEvents(eset1); // .. of course we have already deleted them before...
00248 
00249   // insert a new state. The state gets a integer index that is unique within
00250   // the Generator
00251 
00252   idx_tmp = g1.InsState();            // anonymous state
00253   idx_tmp = g1.InsState("newstate2"); // named state
00254 
00255   // insert a new state as initial state
00256 
00257   idx_tmp = g1.InsInitState();
00258   idx_tmp = g1.InsInitState("newinitstate");
00259 
00260   // ... same for marked states
00261 
00262   idx_tmp = g1.InsMarkedState();
00263   idx_tmp = g1.InsMarkedState("newmarkedstate");
00264 
00265 
00266   // delete single states from Generator ie stateset and transitionrelation
00267 
00268   g1.DelState(idx_tmp); // by index (relatively fast, for algorithms)
00269   g1.DelState("newinitstate"); // by symbolic name, if name assigned
00270 
00271   // delete a bunch of states 
00272   // (this should be more efficient than deleting states individually)
00273 
00274   StateSet stateset1;
00275   stateset1.Insert(1);
00276   stateset1.Insert(2);
00277   stateset1.Insert(3);
00278   g1.DelStates(stateset1);
00279 
00280   // for further proceeding we insert some new states and events...
00281 
00282   Idx idx_s10 = g1.InsState("s10");
00283   Idx idx_s11 = g1.InsState("s11");
00284   Idx idx_s12 = g1.InsState("s12");
00285   Idx idx_e10 = g1.InsEvent("e10");
00286   Idx idx_e11 = g1.InsEvent("e11");
00287 
00288   // set a state that already exists in Generator as initial state
00289 
00290   g1.SetInitState(idx_s10);
00291 
00292   // set a state that already exists in Generator as marked state
00293 
00294   g1.SetMarkedState(idx_s11);
00295 
00296   // unset an existing state as initial state (does not remove from mStates)
00297 
00298   g1.ClrInitState(idx_s10); 
00299 
00300   // unset an existing state as marked state (does not remove from stateset)
00301 
00302   g1.ClrMarkedState(idx_s10); 
00303 
00304   // clear all initial states (does not remove from stateset)
00305 
00306   //   g1.ClrInitStates();  // we do not really do it here, so it's commented
00307 
00308   // clear all marked states (mStates stays untouched)
00309 
00310   //   g1.ClrMarkedStates();  // we do not really do it here, so it's commented
00311 
00312   // set a transition for existing states and events
00313 
00314   g1.SetTransition(idx_s10, idx_e10, idx_s11); // by indices
00315   g1.SetTransition("s10", "e11", "s10"); // by symbolic names (slow)
00316 
00317 
00318   // report back to console
00319 
00320   std::cout << "################################\n";
00321   std::cout << "# tutorial, on the way \n";
00322   g1.DWrite();
00323   std::cout << "################################\n";
00324 
00325 
00326   // clear a transition (does not touch mStates, mInitStates and mMarkedStates)
00327 
00328   g1.ClrTransition(idx_s10, idx_e10, idx_s11); // by index
00329 
00330   // transitions can also be cleared by names (slower) or by an assigned
00331   // TransSet::Iterator (faster); use ClearTransRel() to remove all transitions
00332 
00333 
00334   // clear the symbolic name for a state in the StateSymbolTable
00335 
00336   g1.ClrStateName(idx_s10); 
00337 
00338   // exists event index/name in mAlphabet?
00339 
00340   bool bool_eventexists1 = g1.ExistsEvent("e11"); 
00341   bool bool_eventexists2 = g1.ExistsEvent(2); 
00342 
00343 
00344   // exists state in mStates?
00345 
00346   bool bool_stateexists1 = g1.ExistsState(4); 
00347 
00348 
00349   // check if a state is an initial state
00350 
00351   bool bool_initstateexists = g1.ExistsInitState(4); 
00352 
00353   // check if a state is a marked state
00354 
00355   bool bool_ismarkedstate = g1.ExistsMarkedState(4); 
00356 
00357   // look up event name for index in the EventSymbolTable of the event domain
00358 
00359   std::string str_eventname1 = g1.EventName(1);
00360 
00361   // look up event index for name in the EventSymbolTable of the event domain
00362 
00363   Idx idx_eventindex = g1.EventIndex("e11");
00364 
00365   // get symbolic name assigned to state (returns "" if no name assigned).
00366 
00367   std::string str_tmp = g1.StateName(idx_s10);
00368 
00369   // get index for symbolic state name. only possible for state names of states in
00370   // the Generator 
00371 
00372   idx_tmp = g1.StateIndex("s12");
00373 
00374   // clear Generator (including alphabet)
00375 
00376   g4.Clear();
00377 
00378   // get the number of events in the Generator's alphabet
00379 
00380   Idx idx_eventnum = g1.AlphabetSize();
00381 
00382   // get the number of states
00383 
00384   Idx idx_statenum = g1.Size();
00385 
00386   // get the number of transitions
00387 
00388   Idx idx_transnum = g1.TransRelSize();
00389 
00390   // there also are InitStatesSize(), MarkedStatesSize()
00391 
00392   // is the alphabet of the Generator empty?
00393 
00394   bool bool_alphempty = g1.AlphabetEmpty();
00395 
00396   // is the Generator empty (number of states == 0) ?
00397 
00398   bool bool_isempty = g1.Empty();
00399 
00400   // see also TransRelEmpty, InitStatesEmpty, MarkedStatesEmpty
00401 
00402 
00403   // insert a small loop 
00404 
00405   Idx initstate = g1.InsInitState("in");
00406   Idx markedstate = g1.InsMarkedState("out");
00407   g1.SetTransition("in","a","out");
00408   g1.SetTransition("out","a","in");
00409 
00410 
00411   // show effect on console 
00412 
00413   std::cout << "################################\n";
00414   std::cout << "# tutorial, after ins and del \n";
00415   g1.DWrite();
00416   std::cout << "################################\n";
00417 
00418 
00419   ///////////////////////
00420   // Iterators
00421   ///////////////////////
00422 
00423   // since the core members are all implemented as sets, iterators
00424   // effectively are cont_iterators, i.e. you cannot change the 
00425   // current value of an iterator. instead you may remove the value 
00426   // and insert the new value.
00427 
00428   // iteration over alphabet indices (member "mAlphabet")
00429 
00430   std::cout << "################################\n";
00431   std::cout << "# tutorial, iterators 1         \n";
00432   EventSet::Iterator eit;
00433   for (eit = g1.AlphabetBegin(); eit != g1.AlphabetEnd(); ++eit) {
00434     std::cout << "event \"" << g1.EventName(*eit) << "\" with index "<< *eit << std::endl;
00435   }
00436   std::cout << "################################\n";
00437 
00438   // iteration over state indices (member "mStates")
00439 
00440   std::cout << "################################\n";
00441   std::cout << "# tutorial, iterators 2         \n";
00442   StateSet::Iterator sit;
00443   for (sit = g1.StatesBegin(); sit != g1.StatesEnd(); ++sit) {
00444     std::cout << *sit << std::endl;
00445   }
00446   std::cout << "################################\n";
00447 
00448   // iteration over complete transition relation (member "mTransRel")
00449 
00450   std::cout << "################################\n";
00451   std::cout << "# tutorial, iterators 3         \n";
00452   TransSet::Iterator tit;
00453   for (tit = g1.TransRelBegin(); tit != g1.TransRelEnd(); ++tit) {
00454     std::cout << g1.TStr(*tit) << std::endl;
00455   }
00456   std::cout << "################################\n";
00457 
00458   // iteration over transitions from a given state; note that we avoid
00459   // computation of the end of the iteration in every step
00460 
00461   std::cout << "################################\n";
00462   std::cout << "# tutorial, iterators 4         \n";
00463   idx_tmp = g1.StateIndex("s1");
00464   TransSet::Iterator tit_end;
00465   tit = g1.TransRelBegin(idx_tmp);
00466   tit_end = g1.TransRelEnd(idx_tmp);
00467   for (; tit != tit_end; ++tit) {
00468     std::cout << g1.TStr(*tit) << std::endl;
00469   }
00470   std::cout << "################################\n";
00471 
00472   // variations: transitions of given state index + given event index:
00473   // TransRelBegin(x1, ev) - TransRelEnd(x1, ev)
00474 
00475   // iteration over initial and marked states:
00476   // InitStatesBegin() - InitStatesEnd()  (member "mInitStates")
00477   // MarkedStatesBegin() - MarkedStatesEnd() (member "mMarkedStates")
00478 
00479 
00480   ////////////////////////////////////////////////////////////
00481   // retrieve copies of the Generator's transition releation
00482   // in different sorting orders than X1 -> Ev -> X2
00483   ////////////////////////////////////////////////////////////
00484 
00485   // note: the availabity of iterator ranges depends on the sorting order;
00486   // eg iteration with specified x2 requires X2->Ev->X1 or X2->X1->Ev sorting.
00487 
00488   // retrieve a copy that is sorted by X2 -> Ev -> X1 by the binary
00489   // predicate TransSort::X2EvX1. 
00490   
00491   TransSetX2EvX1 tset_x2evx1;
00492   g1.TransRel(tset_x2evx1);
00493 
00494   // report to console
00495 
00496   std::cout << "################################\n";
00497   std::cout << "# tutorial, x2-ev-x1 sorting\n";
00498   TransSetX2EvX1::Iterator tit2;
00499   for (tit2 = tset_x2evx1.Begin(); tit2 != tset_x2evx1.End(); ++tit2) {
00500     std::cout << g1.TStr(*tit2) << std::endl;
00501   }
00502   std::cout << "################################\n";
00503 
00504 
00505 
00506   ////////////////////////
00507   // Convenience Methods
00508   ////////////////////////
00509 
00510   // remove all events from mAlphabet, that do not have a transition in
00511   // mTransRel:  g1.MinimizeAlphabet()
00512 
00513   // get an EventSet containing all the events that drive some transition 
00514 
00515   EventSet eset_usedevents = g1.UsedEvents();
00516 
00517   // get an EventSet containing all the events that do not drive any transition
00518 
00519   EventSet eset_unusedevents = g1.UnusedEvents();
00520 
00521   // return the active event set at a given state 
00522 
00523   EventSet eset_activeeventset = g1.ActiveEventSet(idx_s12);
00524 
00525   // return a StateSet containing all the states that are connected by
00526   // some transition 
00527 
00528   StateSet sset_trel_sspace = g1.TransRelStateSpace();
00529 
00530   // return a StateSet containing all the successor states of a given predecessor
00531   // state.
00532 
00533   StateSet sset_successors = g1.TransRelStateSpace(idx_s12);
00534 
00535   // note: if you need predecessor states, use a resorted transition relation
00536 
00537   /////////////////////////////////
00538   // Symbolic state name handling
00539   /////////////////////////////////
00540 
00541   // are symbolic state names enabled? depending on this boolean value
00542   // library functions like Determine or StateMin may create symbolic
00543   // state names.
00544 
00545   bool bool_statenamesenabled = g1.StateNamesEnabled();
00546 
00547   // disable state name creation in resulting generators for functions in
00548   // the faudes library, that support this feature (nearly all) with
00549   // "false"; enable state name creation with "true".
00550 
00551   g1.StateNamesEnabled(true); // anyway .. true is the default value
00552 
00553   // clear existing symbolic statenames for states in the Generator
00554 
00555   // g1.ClearStateNames(); 
00556 
00557   // set symbolic names for all states in the generator. the symbolic name becomes
00558   // the equivalent string representation of the state's integer index. This is
00559   // only usefull for debugging purposes.
00560 
00561   g1.SetDefaultStateNames();
00562 
00563 
00564   // show effect on console 
00565 
00566   std::cout << "################################\n";
00567   std::cout << "# tutorial, default names \n";
00568   g1.Write();
00569   std::cout << "################################\n";
00570 
00571 
00572   ///////////////////////////////////
00573   // Accessible, Coaccessible, Trim
00574   ///////////////////////////////////
00575 
00576   // StateSet containing all accessible states
00577 
00578   std::cout << "################################\n";
00579   std::cout << "# tutorial, accessible  \n";
00580   StateSet sset_accessibleset = g1.AccessibleSet();
00581   sset_accessibleset.Write();
00582   std::cout << "################################\n";
00583 
00584 
00585   // Make the Generator accessible by removing transitions and states. If
00586   // the Generator containes at least one initial state the Generator
00587   // can be made accessible and the return value is "true", else "false".
00588 
00589   bool bool_isaccessiblenow = g1.Accessible();
00590 
00591   // Is the Generator accessible. (No modification of the Generator)
00592 
00593   bool bool_isaccessible = g1.IsAccessible();
00594 
00595   // StateSet containing all coaccessible states
00596 
00597   std::cout << "################################\n";
00598   std::cout << "# tutorial, coaccessible \n";
00599   StateSet sset_coaccessibleset = g1.CoaccessibleSet();
00600   sset_coaccessibleset.Write();
00601   std::cout << "################################\n";
00602 
00603   // make the Generator coaccessible by removing transitions and states; if
00604   // the Generator containes at least one initial state the Generator
00605   // can be made coaccessible and the return value is "true", else "false".
00606 
00607   bool bool_iscoaccessiblenow = g1.Coaccessible();
00608 
00609   // is the Generator coaccessible? (No modification of the Generator)
00610 
00611   bool bool_iscoaccessible = g1.IsCoaccessible();
00612 
00613   // StateSet containing all trim states
00614 
00615   StateSet sset_trimset = g1.TrimSet();
00616 
00617   // make the Generator trim by removing transitions and states; if
00618   // the Generator containes at least one initial state and one marked state
00619   // the return value is "true", else "false".
00620 
00621   bool bool_istrimnow = g1.Trim();
00622 
00623   // is the Generator trim? (No modification of the Generator)
00624 
00625   bool bool_istrim = g1.IsTrim();
00626 
00627   // show effect on console
00628 
00629   std::cout << "################################\n";
00630   std::cout << "# tutorial, coaccessible \n";
00631   g1.Write();
00632   std::cout << "################################\n";
00633 
00634   return 0;
00635 }
00636 
00637 
00638 

libFAUDES 2.14g --- 2009-12-3 --- c++ source docu by doxygen 1.5.6