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