hio_2_hiogenerators.cppGo to the documentation of this file.00001 /** @file hio_2_hiogenerators.cpp 00002 00003 Tutorial, i/o system generator classes. 00004 00005 This tutorial demonstrates basic maintenance of HioPlant, HioController 00006 and HioEnvironment objects. 00007 00008 @ingroup Tutorials 00009 00010 @include hio_2_hiogenerators.cpp 00011 00012 */ 00013 00014 #include "libfaudes.h" 00015 00016 // make the faudes namespace available to our program 00017 using namespace faudes; 00018 00019 ///////////////// 00020 // main program 00021 ///////////////// 00022 00023 int main() { 00024 00025 // for intermediate results 00026 bool tmpresult; 00027 00028 /************************************************** 00029 * HIO PLANT 00030 ***************************************************/ 00031 00032 /****************** 00033 * constructor and file io 00034 ********************/ 00035 00036 // Create an empty HioPlant object 00037 HioPlant g1; 00038 00039 // Create a HioPlant by reading a HioPlant file 00040 HioPlant g2("data/2_hiogenerators/hio_simplemachine_A.gen"); 00041 FAUDES_TEST_DUMP("HioPlant from genfile",g2); 00042 00043 // Create a HioPlant by reading a plain Generator file (no event attributes) 00044 g1.Read("data/2_hiogenerators/simplemachine_A.gen"); 00045 00046 // Assign/copy a plain Generator to a HioPlant (no event attributes) 00047 Generator g4("data/2_hiogenerators/simplemachine_A.gen"); 00048 HioPlant g3=g4; 00049 HioPlant g5(g4); 00050 00051 // Assign/copy a HioPlant to a HioPlant (no event attributes) 00052 HioPlant g6=g2; 00053 FAUDES_TEST_DUMP("HioPlant = operator",g6); 00054 HioPlant g7(g2); 00055 FAUDES_TEST_DUMP("HioPlant construct from HioPlant",g7); 00056 HioPlant g8; 00057 g8.Assign(g2); 00058 FAUDES_TEST_DUMP("HioPlant::Assign()",g8); 00059 00060 // Construct HioPlant from plain generator and event sets 00061 EventSet ypEvents,upEvents,yeEvents,ueEvents; 00062 ypEvents=g2.YpEvents(); 00063 FAUDES_TEST_DUMP("get Yp events",ypEvents); 00064 upEvents=g2.UpEvents(); 00065 yeEvents=g2.YeEvents(); 00066 ueEvents=g2.UeEvents(); 00067 00068 HioPlant g9(g4,ypEvents,upEvents,yeEvents,ueEvents); 00069 FAUDES_TEST_DUMP("HioPlant(vGen,EventSets)",g9); 00070 00071 // Write the HioPlant to files 00072 g1.Write("tmp_hiosimplemachine_A1.gen"); 00073 g2.Write("tmp_hiosimplemachine_A2.gen"); 00074 g3.Write("tmp_hiosimplemachine_A3.gen"); 00075 g5.Write("tmp_hiosimplemachine_A5.gen"); 00076 g6.Write("tmp_hiosimplemachine_A6.gen"); 00077 g9.Write("tmp_hiosimplemachine_A9.gen"); 00078 00079 g1.Write("tmp_hiosimplemachine_A.gen"); 00080 00081 00082 // Report to console 00083 std::cout << "######################################\n"; 00084 std::cout << "# hio simple machine\n"; 00085 g2.Write(); 00086 std::cout << "######################################\n"; 00087 00088 /***************************** 00089 * Test whether HioPlant meets IO-Plant form 00090 ******************************/ 00091 00092 std::cout << "######################################\n"; 00093 00094 // Fails for g1 due to missing event attributes 00095 std::cout << "# IsHioPlantForm A: expect to fail for missing attributes\n"; 00096 std::string report; 00097 tmpresult=IsHioPlantForm(g1,report); 00098 FAUDES_TEST_DUMP("IsHioPlant fail",tmpresult); 00099 std::cout<<report; 00100 00101 // Fulfilled for g2: 00102 std::cout << "# IsHioPlantForm B: expect to pass\n"; 00103 tmpresult=IsHioPlantForm(g2,report); 00104 FAUDES_TEST_DUMP("IsHioPlant pass",tmpresult); 00105 std::cout<<report; 00106 00107 // Remove some transitions with input-event 00108 TransSet::Iterator tit; 00109 for (tit = g2.TransRelBegin(); tit != g2.TransRelEnd(); ++tit) { 00110 //if(g2.IsUp(tit->Ev)) { 00111 if(g2.EventName(tit->Ev)=="A_stp" || g2.EventName(tit->Ev)=="A_nack") { 00112 g2.ClrTransition(*tit); 00113 } 00114 } 00115 g2.Write("tmp_hiosimplemachine_A_broken.gen"); 00116 g2.GraphWrite("tmp_hiosimplemachine_A_broken.png"); 00117 // Now, the input is no longer free in g2 00118 std::cout << "# IsHioPlantForm C: expect to fail for missing input transitions\n"; 00119 IsHioPlantForm(g2,report); 00120 std::cout<<report; 00121 // Repair 00122 HioFreeInput(g2,g2); 00123 FAUDES_TEST_DUMP("HioFreeInput",g2); 00124 g2.Write("tmp_hiosimplemachine_A_fixed.gen"); 00125 // HioPlantForm is retrieved for g2: 00126 std::cout << "# IsHioPlantForm D: expect to pass\n"; 00127 IsHioPlantForm(g2,report); 00128 std::cout<<report; 00129 00130 FAUDES_TEST_DUMP("HioPlant construction",g2); 00131 g2.Write(); 00132 g2.Write("tmp_hiosimplemachine_A_repaired.gen"); 00133 00134 std::cout << "######################################\n"; 00135 00136 /******************************************* 00137 * Access to hio-property of states 00138 * 00139 * Note: hio state properties need not be up to date 00140 * - always run IsHioPlantForm() to set state attributes 00141 **************************************/ 00142 00143 // Get states of g2 sorted according to their active event sets 00144 StateSet QYpYe,QUp,QUe; 00145 QYpYe=g2.QYpYeStates(); 00146 QUp=g2.QUpStates(); 00147 FAUDES_TEST_DUMP("get Up states",QUp); 00148 QUe=g2.QUeStates(); 00149 std::cout<<std::endl; 00150 00151 // Show on console 00152 std::cout << "######################################\n"; 00153 std::cout << "# QYpYe,QUp and QUe of simple machine:\n"; 00154 QYpYe.Write(); 00155 QUp.Write(); 00156 QUe.Write(); 00157 std::cout << "######################################\n"; 00158 00159 00160 /************************* 00161 * Access to hio-property of events 00162 **************************/ 00163 00164 // Retrieve EventSets containing all YP-, UP-, YE-, UE-events from g2 00165 ypEvents=g2.YpEvents(); 00166 upEvents=g2.UpEvents(); 00167 yeEvents=g2.YeEvents(); 00168 ueEvents=g2.UeEvents(); 00169 00170 // Set YP-, UP-, YE-, UE-events in g1 00171 g1.SetYp(ypEvents); 00172 g1.SetUp(upEvents); 00173 g1.SetYe(yeEvents); 00174 g1.SetUe(ueEvents); 00175 00176 // Now, also g1 is in HioPlantForm 00177 std::cout << "######################################\n"; 00178 std::cout << "# IsHioPlantForm:\n"; 00179 IsHioPlantForm(g1,report); 00180 std::cout<<report; 00181 std::cout << "######################################\n"; 00182 00183 // File i/o and access to attributes are analogous for HioConstraint, HioController and HioEnvironment. 00184 std::cout<<std::endl<<std::endl; 00185 /************************************************** 00186 * HIO CONTROLLER 00187 ***************************************************/ 00188 00189 // Construct simple HioController structure 00190 HioController c1; 00191 c1.Name("HioController"); 00192 Idx yp1=c1.InsYpEvent("yp1"); 00193 Idx yp2=c1.InsYpEvent("yp2"); 00194 Idx yc=c1.InsYcEvent("yc"); 00195 Idx uc1=c1.InsUcEvent("uc1"); 00196 Idx uc2=c1.InsUcEvent("uc2"); 00197 Idx up=c1.InsUpEvent("up"); 00198 00199 Idx st1=c1.InsInitState(); 00200 c1.SetMarkedState(st1); 00201 Idx st2=c1.InsMarkedState(); 00202 Idx st3=c1.InsMarkedState(); 00203 Idx st4=c1.InsMarkedState(); 00204 00205 c1.SetTransition(st1,yp1,st2); 00206 c1.SetTransition(st2,yc,st3); 00207 c1.SetTransition(st3,uc1,st4); 00208 c1.SetTransition(st4,up,st1); 00209 00210 // Up to now, no I/O-controller form as inputs yp2 and uc2 are not accepted: 00211 std::cout<<std::endl<<"######################################\n"; 00212 std::cout<<"######################################\n"; 00213 std::cout<<"####### I/O CONTROLLER: #########\n"; 00214 tmpresult=IsHioControllerForm(c1,report); 00215 FAUDES_TEST_DUMP("IsHioControllerForm fail",tmpresult); 00216 00217 // Test correct file I/O 00218 c1.Write(); 00219 c1.Write("tmp_hiocontroller_incomplete.gen"); 00220 c1.Read("tmp_hiocontroller_incomplete.gen"); 00221 c1.Write(); 00222 std::cout<<report; 00223 c1.GraphWrite("tmp_hiocontroller_incomplete.png"); 00224 00225 // Repair HioControllerForm using HioFreeInput 00226 HioFreeInput(c1,c1); 00227 tmpresult=IsHioControllerForm(c1,report); 00228 FAUDES_TEST_DUMP("IsHioControllerForm pass",tmpresult); 00229 std::cout<<report; 00230 FAUDES_TEST_DUMP("HioController construction",c1); 00231 c1.Write("tmp_hiocontroller_repaired.gen"); 00232 c1.GraphWrite("tmp_hiocontroller_repaired.png"); 00233 std::cout<<"######################################\n"; 00234 std::cout<<std::endl<<std::endl; 00235 00236 /************************************************** 00237 * HIO ENVIRONMENT 00238 ***************************************************/ 00239 00240 // Construct simple HioEnvironment structure 00241 HioEnvironment e1; 00242 e1.Name("HioEnvironment"); 00243 Idx ye1=e1.InsYeEvent("ye1"); 00244 Idx ye2=e1.InsYeEvent("ye2"); 00245 Idx yl=e1.InsYlEvent("yl"); 00246 Idx ul1=e1.InsUlEvent("ul1"); 00247 Idx ul2=e1.InsUlEvent("ul2"); 00248 Idx ue=e1.InsUeEvent("ue"); 00249 00250 st1=e1.InsInitState(); 00251 e1.SetMarkedState(st1); 00252 st2=e1.InsMarkedState(); 00253 st3=e1.InsMarkedState(); 00254 st4=e1.InsMarkedState(); 00255 00256 e1.SetTransition(st1,ye1,st2); 00257 e1.SetTransition(st2,yl,st3); 00258 e1.SetTransition(st3,ul1,st4); 00259 e1.SetTransition(st4,ue,st1); 00260 00261 // Up to now, no I/O-environment form as inputs ye2 and ul2 are not accepted: 00262 std::cout<<std::endl<<"######################################\n"; 00263 std::cout<<"######################################\n"; 00264 std::cout<<"####### I/O ENVIRONMENT: #########\n"; 00265 tmpresult=IsHioEnvironmentForm(e1,report); 00266 FAUDES_TEST_DUMP("IsHioEnvironmentForm fail",tmpresult); 00267 // Test correct file I/O 00268 e1.Write(); 00269 e1.Write("tmp_hioenvironment_incomplete.gen"); 00270 e1.Read("tmp_hioenvironment_incomplete.gen"); 00271 e1.Write(); 00272 std::cout<<report; 00273 e1.GraphWrite("tmp_hioenvironment_incomplete.png"); 00274 00275 // Repair HioEnvironmentForm using HioFreeInput 00276 HioFreeInput(e1,e1); 00277 tmpresult=IsHioEnvironmentForm(e1,report); 00278 FAUDES_TEST_DUMP("IsHioEnvironmentForm pass",tmpresult); 00279 e1.Write(); 00280 std::cout<<report; 00281 FAUDES_TEST_DUMP("HioEnvironment construction",e1); 00282 e1.Write("tmp_hioenvironment_repaired.gen"); 00283 e1.GraphWrite("tmp_hioenvironment_repaired.png"); 00284 std::cout<<"######################################\n"; 00285 std::cout<<std::endl<<std::endl; 00286 /************************************************** 00287 * HIO CONSTRAINT 00288 ***************************************************/ 00289 // Construct simple HioConstraint structure 00290 HioConstraint cnstr1; 00291 cnstr1.Name("HioConstraint"); 00292 Idx y1=cnstr1.InsYEvent("y1"); 00293 Idx y2=cnstr1.InsYEvent("y2"); 00294 Idx u=cnstr1.InsUEvent("u"); 00295 00296 st1=cnstr1.InsInitState(); 00297 cnstr1.SetMarkedState(st1); 00298 st2=cnstr1.InsMarkedState(); 00299 00300 cnstr1.SetTransition(st1,y1,st2); 00301 cnstr1.SetTransition(st2,u,st1); 00302 00303 // Up to now, no I/O-constraint form as input u2 is not accepted: 00304 std::cout<<std::endl<<"######################################\n"; 00305 std::cout<<"######################################\n"; 00306 std::cout<<"####### I/O CONSTRAINT: #########\n"; 00307 std::cout<<"#######\n"; 00308 // Access to event properties: 00309 EventSet yEvents=cnstr1.YEvents(); 00310 yEvents.Name("####### HioConstraint: Y-Events"); 00311 yEvents.Write(); 00312 tmpresult=IsHioConstraintForm(cnstr1,report); 00313 FAUDES_TEST_DUMP("IsHioConstraintForm fail",tmpresult); 00314 // Test correct file I/O 00315 cnstr1.Write(); 00316 cnstr1.Write("tmp_hioconstraint_incomplete.gen"); 00317 cnstr1.Read("tmp_hioconstraint_incomplete.gen"); 00318 cnstr1.Write(); 00319 std::cout<<report; 00320 cnstr1.GraphWrite("tmp_hioconstraint_incomplete.png"); 00321 00322 // Repair HioEnvironmentForm using HioFreeInput 00323 HioFreeInput(cnstr1,cnstr1); 00324 tmpresult=IsHioConstraintForm(cnstr1,report); 00325 FAUDES_TEST_DUMP("IsHioConstraintForm pass",tmpresult); 00326 FAUDES_TEST_DUMP("HioController construction",cnstr1); 00327 cnstr1.Write(); 00328 std::cout<<report; 00329 cnstr1.Write("tmp_hioconstraint_repaired.gen"); 00330 cnstr1.GraphWrite("tmp_hioconstraint_repaired.png"); 00331 std::cout<<"######################################\n"; 00332 00333 return 0; 00334 } 00335 00336 00337 libFAUDES 2.23h --- 2014.04.03 --- c++ api documentaion by doxygen |