1_generator.cpp

Go to the documentation of this file.
00001 
00021 #include "libfaudes.h"
00022 
00023 // make the faudes namespace available to our program
00024 using namespace faudes;
00025 
00026 
00027 
00029 // main program
00031 
00033 int main() {
00034 
00036   // Constructors (part 1) and filling example
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 
00070   // Constructors (part 2) & Copying and versioning
00072 
00073   // Create a  1:1 copy of the Generator with the copy constructor. 
00074 
00075   Generator g_copy(g1);
00076 
00077   // ... with copy method, or the assignement operator
00078 
00079   Generator g2;
00080   g1.Copy(g2);
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   g1.Version(3, version1);
00089 
00090   // versioning by a string. "a" -> "a_versionstring"
00091 
00092   Generator version2;
00093   g1.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 
00105   // Methods for Input/Output
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 
00170   // Accessing the Generator's Members
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   // retrieve the unique id of the Generator (statically counted)
00182 
00183   Idx genid = g1.Id();
00184 
00185 
00186   // the core members alphabet, stateset and transitionrelation may be retrieved 
00187   // as const references; ie. they can be inspected freely, but write access is 
00188   // exclusively via the provided Generator methods.
00189 
00190   // retrieve a const reference to and copy of the Generator's alphabet
00191 
00192   const EventSet&  eset_ref_alph  = g1.Alphabet();
00193   EventSet         eset_copy_alph = g1.Alphabet();
00194 
00195   // you cannot alter the alphabet of a generator via an  alphabet method
00196   // eset_ref_alph.Insert("new_event");  // compile time error!
00197 
00198   // however, the copy can be altered, but with no effect on the original generator
00199 
00200   eset_copy_alph.Insert("new_event"); 
00201   if(g1.ExistsEvent("new_event")) std::cout << "### THIS CANNOT HAPPEN ###";
00202 
00203   // retrieve a const reference to and copy of the Generator's set of states "mStates"
00204 
00205   const StateSet& sset_ref_states = g1.States();
00206   StateSet sset_copy_states = g1.States();
00207 
00208   // retrieve a const reference to and a copy of the Generator's transition relation "mTransRel"
00209 
00210   const TransSet& tset_ref_trel = g1.TransRel();
00211   TransSet tset_copy_trel = g1.TransRel();
00212 
00213   // same with initial states and marked states
00214 
00215   const StateSet& sset_ref_istates = g1.InitStates();
00216   StateSet sset_copy_istates = g1.InitStates();
00217 
00218   const StateSet& sset_ref_mstates = g1.MarkedStates();
00219   StateSet sset_copy_mstates = g1.MarkedStates();
00220 
00221 
00222 
00224   // Modifying the 5-tuple Generator (X, Sigma, Delta, X0 and Xm)
00226 
00227   // insert an event by it's symbolic name in the alphabet
00228   // (if the event is not known so far, a new index for the symbolic name is generated) 
00229   g1.InsEvent("newevent");
00230 
00231 
00232   // insert an existing event into the Generator's alphabet (mAlphabet)
00233   // (by "existing event" we refer to an event that has been previously inserted to some Generator)
00234 
00235   g1.InsEvent(1);   // of course index 1 is already in the alphabet here...
00236 
00237   // insert a bunch of events (EventSet) and get the integer index if requested
00238 
00239   EventSet eset1;
00240   eset1.Insert("newevent1");
00241   Idx idx_tmp = eset1.Insert("newevent2");
00242   g1.InsEvents(eset1);
00243 
00244   // delete an event from Generator ie delete from alphabet and transition relation
00245 
00246   g1.DelEvent("newevent1"); // by symbolic name
00247   g1.DelEvent(idx_tmp);     // by index
00248 
00249   // delete a bunch of events
00250   // g1.DelEvents(eset1); // .. of course we have already deleted them before...
00251 
00252   // insert a new state. The state gets a integer index that is unique within
00253   // the Generator
00254 
00255   idx_tmp = g1.InsState();            // anonymous state
00256   idx_tmp = g1.InsState("newstate2"); // named state
00257 
00258   // insert a new state as initial state
00259 
00260   idx_tmp = g1.InsInitState();
00261   idx_tmp = g1.InsInitState("newinitstate");
00262 
00263   // ... same for marked states
00264 
00265   idx_tmp = g1.InsMarkedState();
00266   idx_tmp = g1.InsMarkedState("newmarkedstate");
00267 
00268 
00269   // delete single states from Generator ie stateset and transitionrelation
00270 
00271   g1.DelState(idx_tmp); // by index (relatively fast, for algorithms)
00272   g1.DelState("newinitstate"); // by symbolic name, if name assigned
00273 
00274   // delete a bunch of states 
00275   // (this should be more efficient than deleting states individually)
00276 
00277   StateSet stateset1;
00278   stateset1.Insert(1);
00279   stateset1.Insert(2);
00280   stateset1.Insert(3);
00281   g1.DelStates(stateset1);
00282 
00283   // for further proceeding we insert some new states and events...
00284 
00285   Idx idx_s10 = g1.InsState("s10");
00286   Idx idx_s11 = g1.InsState("s11");
00287   Idx idx_s12 = g1.InsState("s12");
00288   Idx idx_e10 = g1.InsEvent("e10");
00289   Idx idx_e11 = g1.InsEvent("e11");
00290 
00291   // set a state that already exists in Generator as initial state
00292 
00293   g1.SetInitState(idx_s10);
00294 
00295   // set a state that already exists in Generator as marked state
00296 
00297   g1.SetMarkedState(idx_s11);
00298 
00299   // unset an existing state as initial state (does not remove from mStates)
00300 
00301   g1.ClrInitState(idx_s10); 
00302 
00303   // unset an existing state as marked state (does not remove from stateset)
00304 
00305   g1.ClrMarkedState(idx_s10); 
00306 
00307   // clear all initial states (does not remove from stateset)
00308 
00309   //   g1.ClrInitStates();  // we do not really do it here, so it's commented
00310 
00311   // clear all marked states (mStates stays untouched)
00312 
00313   //   g1.ClrMarkedStates();  // we do not really do it here, so it's commented
00314 
00315   // set a transition for existing states and events
00316 
00317   g1.SetTransition(idx_s10, idx_e10, idx_s11); // by indices
00318   g1.SetTransition("s10", "e11", "s10"); // by symbolic names (slow)
00319 
00320 
00321   // report back to console
00322 
00323   std::cout << "################################\n";
00324   std::cout << "# tutorial, on the way \n";
00325   g1.DWrite();
00326   std::cout << "################################\n";
00327 
00328 
00329   // clear a transition (does not touch mStates, mInitStates and mMarkedStates)
00330 
00331   g1.ClrTransition(idx_s10, idx_e10, idx_s11); // by index
00332 
00333   // transitions can also be cleared by names (slower) or by an assigned
00334   // TransSet::Iterator (faster); use ClearTransRel() to remove all transitions
00335 
00336 
00337   // clear the symbolic name for a state in the StateSymbolTable
00338 
00339   g1.ClrStateName(idx_s10); 
00340 
00341   // exists event index/name in mAlphabet?
00342 
00343   bool bool_eventexists1 = g1.ExistsEvent("e11"); 
00344   bool bool_eventexists2 = g1.ExistsEvent(2); 
00345 
00346 
00347   // exists state in mStates?
00348 
00349   bool bool_stateexists1 = g1.ExistsState(4); 
00350 
00351 
00352   // check if a state is an initial state
00353 
00354   bool bool_initstateexists = g1.ExistsInitState(4); 
00355 
00356   // check if a state is a marked state
00357 
00358   bool bool_ismarkedstate = g1.ExistsMarkedState(4); 
00359 
00360   // look up event name for index in the EventSymbolTable of the event domain
00361 
00362   std::string str_eventname1 = g1.EventName(1);
00363 
00364   // look up event index for name in the EventSymbolTable of the event domain
00365 
00366   Idx idx_eventindex = g1.EventIndex("e11");
00367 
00368   // get symbolic name assigned to state (returns "" if no name assigned).
00369 
00370   std::string str_tmp = g1.StateName(idx_s10);
00371 
00372   // get index for symbolic state name. only possible for state names of states in
00373   // the Generator 
00374 
00375   idx_tmp = g1.StateIndex("s12");
00376 
00377   // clear Generator (including alphabet)
00378 
00379   g4.Clear();
00380 
00381   // get the number of events in the Generator's alphabet
00382 
00383   Idx idx_eventnum = g1.AlphabetSize();
00384 
00385   // get the number of states
00386 
00387   Idx idx_statenum = g1.Size();
00388 
00389   // get the number of transitions
00390 
00391   Idx idx_transnum = g1.TransRelSize();
00392 
00393   // there also are InitStatesSize(), MarkedStatesSize()
00394 
00395   // is the alphabet of the Generator empty?
00396 
00397   bool bool_alphempty = g1.AlphabetEmpty();
00398 
00399   // is the Generator empty (number of states == 0) ?
00400 
00401   bool bool_isempty = g1.Empty();
00402 
00403   // see also TransRelEmpty, InitStatesEmpty, MarkedStatesEmpty
00404 
00405 
00406   // insert a small loop 
00407 
00408   Idx initstate = g1.InsInitState("in");
00409   Idx markedstate = g1.InsMarkedState("out");
00410   g1.SetTransition("in","a","out");
00411   g1.SetTransition("out","a","in");
00412 
00413 
00414   // show effect on console 
00415 
00416   std::cout << "################################\n";
00417   std::cout << "# tutorial, after ins and del \n";
00418   g1.DWrite();
00419   std::cout << "################################\n";
00420 
00421 
00423   // Iterators
00425 
00426   // since the core members are all implemented as sets, iterators
00427   // effectively are cont_iterators, i.e. you cannot change the 
00428   // current value of an iterator. instead you may remove the value 
00429   // and insert the new value.
00430 
00431   // iteration over alphabet indices (member "mAlphabet")
00432 
00433   std::cout << "################################\n";
00434   std::cout << "# tutorial, iterators 1         \n";
00435   EventSet::Iterator eit;
00436   for (eit = g1.AlphabetBegin(); eit != g1.AlphabetEnd(); ++eit) {
00437     std::cout << "event \"" << g1.EventName(*eit) << "\" with index "<< *eit << std::endl;
00438   }
00439   std::cout << "################################\n";
00440 
00441   // iteration over state indices (member "mStates")
00442 
00443   std::cout << "################################\n";
00444   std::cout << "# tutorial, iterators 2         \n";
00445   StateSet::Iterator sit;
00446   for (sit = g1.StatesBegin(); sit != g1.StatesEnd(); ++sit) {
00447     std::cout << *sit << std::endl;
00448   }
00449   std::cout << "################################\n";
00450 
00451   // iteration over complete transition relation (member "mTransRel")
00452 
00453   std::cout << "################################\n";
00454   std::cout << "# tutorial, iterators 3         \n";
00455   TransSet::Iterator tit;
00456   for (tit = g1.TransRelBegin(); tit != g1.TransRelEnd(); ++tit) {
00457     std::cout << g1.TStr(*tit) << std::endl;
00458   }
00459   std::cout << "################################\n";
00460 
00461   // iteration over transitions from a given state; note that we avoid
00462   // computation of the end of the iteration in every step
00463 
00464   std::cout << "################################\n";
00465   std::cout << "# tutorial, iterators 4         \n";
00466   idx_tmp = g1.StateIndex("s1");
00467   TransSet::Iterator tit_end;
00468   tit = g1.TransRelBegin(idx_tmp);
00469   tit_end = g1.TransRelEnd(idx_tmp);
00470   for (; tit != tit_end; ++tit) {
00471     std::cout << g1.TStr(*tit) << std::endl;
00472   }
00473   std::cout << "################################\n";
00474 
00475   // variations: transitions of given state index + given event index:
00476   // TransRelBegin(x1, ev) - TransRelEnd(x1, ev)
00477 
00478   // iteration over initial and marked states:
00479   // InitStatesBegin() - InitStatesEnd()  (member "mInitStates")
00480   // MarkedStatesBegin() - MarkedStatesEnd() (member "mMarkedStates")
00481 
00482 
00484   // retrieve copies of the Generator's transition releation
00485   // in different sorting orders than X1 -> Ev -> X2
00487 
00488   // note: the availabity of iterator ranges depends on the sorting order;
00489   // eg iteration with specified x2 requires X2->Ev->X1 or X2->X1->Ev sorting.
00490 
00491   // retrieve a copy that is sorted by X2 -> Ev -> X1 by the binary
00492   // predicate TransSort::X2EvX1. 
00493   
00494   TransSetX2EvX1 tset_x2evx1;
00495   g1.TransRel(tset_x2evx1);
00496 
00497   // report to console
00498 
00499   std::cout << "################################\n";
00500   std::cout << "# tutorial, x2-ev-x1 sorting\n";
00501   TransSetX2EvX1::Iterator tit2;
00502   for (tit2 = tset_x2evx1.Begin(); tit2 != tset_x2evx1.End(); ++tit2) {
00503     std::cout << g1.TStr(*tit2) << std::endl;
00504   }
00505   std::cout << "################################\n";
00506 
00507 
00508 
00510   // Convenience Methods
00512 
00513   // remove all events from mAlphabet, that do not have a transition in
00514   // mTransRel:  g1.MinimizeAlphabet()
00515 
00516   // get an EventSet containing all the events that drive some transition 
00517 
00518   EventSet eset_usedevents = g1.UsedEvents();
00519 
00520   // get an EventSet containing all the events that do not drive any transition
00521 
00522   EventSet eset_unusedevents = g1.UnusedEvents();
00523 
00524   // return the active event set at a given state 
00525 
00526   EventSet eset_activeeventset = g1.ActiveEventSet(idx_s12);
00527 
00528   // return a StateSet containing all the states that are connected by
00529   // some transition 
00530 
00531   StateSet sset_trel_sspace = g1.TransRelStateSpace();
00532 
00533   // return a StateSet containing all the successor states of a given predecessor
00534   // state.
00535 
00536   StateSet sset_successors = g1.TransRelStateSpace(idx_s12);
00537 
00538   // note: if you need predecessor states, use a resorted transition relation
00539 
00541   // Symbolic state name handling
00543 
00544   // are symbolic state names enabled? depending on this boolean value
00545   // library functions like Determine or StateMin may create symbolic
00546   // state names.
00547 
00548   bool bool_statenamesenabled = g1.StateNamesEnabled();
00549 
00550   // disable state name creation in resulting generators for functions in
00551   // the faudes library, that support this feature (nearly all) with
00552   // "false"; enable state name creation with "true".
00553 
00554   g1.StateNamesEnabled(true); // anyway .. true is the default value
00555 
00556   // clear existing symbolic statenames for states in the Generator
00557 
00558   // g1.ClearStateNames(); 
00559 
00560   // set symbolic names for all states in the generator. the symbolic name becomes
00561   // the equivalent string representation of the state's integer index. This is
00562   // only usefull for debugging purposes.
00563 
00564   g1.SetDefaultStateNames();
00565 
00566 
00567   // show effect on console 
00568 
00569   std::cout << "################################\n";
00570   std::cout << "# tutorial, default names \n";
00571   g1.Write();
00572   std::cout << "################################\n";
00573 
00574 
00576   // Accessible, Coaccessible, Trim
00578 
00579   // StateSet containing all accessible states
00580 
00581   std::cout << "################################\n";
00582   std::cout << "# tutorial, accessible  \n";
00583   StateSet sset_accessibleset = g1.AccessibleSet();
00584   sset_accessibleset.Write();
00585   std::cout << "################################\n";
00586 
00587 
00588   // Make the Generator accessible by removing transitions and states. If
00589   // the Generator containes at least one initial state the Generator
00590   // can be made accessible and the return value is "true", else "false".
00591 
00592   bool bool_isaccessiblenow = g1.Accessible();
00593 
00594   // Is the Generator accessible. (No modification of the Generator)
00595 
00596   bool bool_isaccessible = g1.IsAccessible();
00597 
00598   // StateSet containing all coaccessible states
00599 
00600   std::cout << "################################\n";
00601   std::cout << "# tutorial, coaccessible \n";
00602   StateSet sset_coaccessibleset = g1.CoaccessibleSet();
00603   sset_coaccessibleset.Write();
00604   std::cout << "################################\n";
00605 
00606   // make the Generator coaccessible by removing transitions and states; if
00607   // the Generator containes at least one initial state the Generator
00608   // can be made coaccessible and the return value is "true", else "false".
00609 
00610   bool bool_iscoaccessiblenow = g1.Coaccessible();
00611 
00612   // is the Generator coaccessible? (No modification of the Generator)
00613 
00614   bool bool_iscoaccessible = g1.IsCoaccessible();
00615 
00616   // StateSet containing all trim states
00617 
00618   StateSet sset_trimset = g1.TrimSet();
00619 
00620   // make the Generator trim by removing transitions and states; if
00621   // the Generator containes at least one initial state and one marked state
00622   // the return value is "true", else "false".
00623 
00624   bool bool_istrimnow = g1.Trim();
00625 
00626   // is the Generator trim? (No modification of the Generator)
00627 
00628   bool bool_istrim = g1.IsTrim();
00629 
00630   // show effect on console
00631 
00632   std::cout << "################################\n";
00633   std::cout << "# tutorial, coaccessible \n";
00634   g1.Write();
00635   std::cout << "################################\n";
00636 
00637   return 0;
00638 }
00639 
00640 
00641 

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