7_interface.cpp

Go to the documentation of this file.
00001 /** @file 7_interface.cpp
00002 
00003 Tutorial, runtime interface. This tutorial demonstrates access to 
00004 libFAUDES data types and functions via the type- and function
00005 registries. The runtime interface addresses the development
00006 of applications that transparently propagate libFAUDES extensions
00007 to the user. 
00008 
00009 
00010 @ingroup Tutorials 
00011 
00012 @include 7_interface.cpp
00013 
00014 */
00015 
00016 #include <iostream>
00017 #include "libfaudes.h"
00018 
00019 using namespace faudes;
00020 
00021 
00022 int main(){
00023 
00024   // ******************** basic ussage of the faudes type interface
00025 
00026   // load (do so once on application startup)
00027   LoadRegistry("../include/libfaudes.rti");
00028 
00029   // report to console
00030   std::cout << "################################\n";
00031   std::cout << "# tutorial, faudes types \n";
00032 
00033   // instatiate an object by faudes type
00034   Type* fobject = NewFaudesObject("System");  
00035 
00036   // query type of an object
00037   std::cout << " faudes type of object: " << FaudesTypeName(*fobject) << "\n";
00038 
00039   // type test: can we cast this to a plain generator?
00040   Generator gen;
00041   bool isvg= ( gen.Cast(fobject)!=NULL );
00042   if(isvg) 
00043     std::cout << " faudes object casts to Generator: ok [expected]\n";
00044   else    
00045     std::cout << " faudes object casts to Generator: failed\n";
00046 
00047   // type test: can we cast this to an eventset?
00048   EventSet eset;
00049   bool ises = ( eset.Cast(fobject) != NULL ) ;
00050   if(ises) 
00051     std::cout << " faudes object cast to EventSet: ok\n";
00052   else    
00053     std::cout << " faudes object cast to EventSet: failed [expected]\n";
00054 
00055   // Record test case
00056   FAUDES_TEST_DUMP("faudes type",FaudesTypeName(*fobject));
00057   FAUDES_TEST_DUMP("cast to generator",isvg);
00058   FAUDES_TEST_DUMP("cast to eventset",ises);
00059 
00060   // report
00061   std::cout << "################################\n";
00062 
00063   // ******************** basic ussage of the function registry
00064 
00065   // instatiate some generators via the registry 
00066   Type* data0 = NewFaudesObject("Generator");  
00067   Type* data1 = NewFaudesObject("Generator");  
00068   Type* data2 = NewFaudesObject("Generator");  
00069 
00070   // load input data from files
00071   data0->Read("./data/simplemachine.gen");
00072   data1->Read("./data/buffer.gen");
00073 
00074   // instantiate a function via registry 
00075   Function* funct = NewFaudesFunction("Parallel");  
00076   
00077   // set parameter values (exception on accessing positional parameter out of range)
00078   funct->ParamValue(0,data0);
00079   funct->ParamValue(1,data1);
00080   funct->ParamValue(2,data2);
00081 
00082   // execute function (exception on type mismatch)
00083   funct->Execute();
00084 
00085   // report to console
00086   std::cout << "################################\n";
00087   std::cout << "# tutorial, rti parallel \n";
00088   data2->Write();
00089   std::cout << "################################\n";
00090 
00091   // test case
00092   Generator* vgen=dynamic_cast<Generator*>(data2);
00093   if(!vgen) {
00094     std::cout << "ERR: res does not cast to Generator\n";
00095     exit(1);
00096   }
00097   if(vgen->Size()!=6) {
00098     std::cout << "ERR: res is expected to have 6 states\n";
00099     exit(1);
00100   }
00101 
00102   // record test case
00103   FAUDES_TEST_DUMP("rti parallel",*data2);
00104 
00105   // clear registry for below demos
00106   ClearRegistry();
00107 
00108   // delete my objects
00109   delete data0;
00110   delete data1;
00111   delete data2;
00112  
00113   // ******************** elementary type, String
00114 
00115   // create new String instance
00116   String fstring;
00117 
00118   // assign from corresponding c type std::string
00119   fstring = "hello faudes types";
00120 
00121   // assign to corresponding c type std::string
00122   std::string sstring = fstring;
00123 
00124   // file io
00125   fstring.Write("tmp_string.txt");
00126   fstring.Read("tmp_string.txt");
00127 
00128   // report to console
00129   std::cout << "################################\n";
00130   std::cout << "# tutorial, rti string \n";
00131   fstring.Write();
00132   std::cout << "################################\n";
00133 
00134 
00135   // ******************** elementary type, Integer
00136 
00137   // create new Integer instance
00138   Integer fint;
00139 
00140   // assign from corresponding c type std::int
00141   fint = 42;
00142 
00143   // assign to corresponding c type std::int
00144   int sint = fint;
00145 
00146   // arithmetic
00147   fint = 2*fint+16;
00148 
00149   // file io
00150   fint.Write("tmp_int.txt");
00151   fint.Read("tmp_int.txt");
00152 
00153   // report to console
00154   std::cout << "################################\n";
00155   std::cout << "# tutorial, rti integer \n";
00156   fint.Write();
00157   std::cout << "################################\n";
00158 
00159   // test case
00160   if(fint!=100) {
00161     std::cout << "ERR: expected 100\n";
00162     exit(1);
00163   }
00164 
00165 
00166   // ******************** elementary type, Boolean
00167 
00168   // create new Integer instance
00169   Boolean fbool;
00170 
00171   // assign from corresponding c type std::int
00172   fbool = false;
00173 
00174   // assign to corresponding c type std::int
00175   bool sbool = !fbool;
00176 
00177   // file io
00178   fbool.Write("tmp_bool.txt");
00179   fbool.Read("tmp_bool.txt");
00180 
00181   // report to console
00182   std::cout << "################################\n";
00183   std::cout << "# tutorial, rti bool \n";
00184   fbool.Write();
00185   std::cout << "################################\n";
00186 
00187 
00188   // ******************** advanced: type definitions and registry
00189   
00190   // defining a type with specified faudes type name (and no documentation)
00191   TypeDefinition* tdalphabet = TypeDefinition::Constructor<Alphabet>("EventSet");
00192 
00193   // defining a type with type name "System" and documentation from specified file
00194   TypeDefinition* tdgenerator = TypeDefinition::FromFile<System>("data/generator.rti");
00195  
00196 
00197   // register previously defined types
00198   TypeRegistry::G()->Insert(tdalphabet);
00199   TypeRegistry::G()->Insert(tdgenerator);
00200 
00201   // load any additional documentation from file (event set is missing from above)
00202   TypeRegistry::G()->MergeDocumentation("../include/libfaudes.rti");
00203 
00204   // dump registry to console
00205   std::cout << "################################\n";
00206   std::cout << "# tutorial, type registry \n";
00207   TypeRegistry::G()->Write();
00208   std::cout << "################################\n";
00209 
00210   // use the registry to construct an object by its type name
00211   Type* generator = NewFaudesObject("System");  
00212 
00213   // test generator object
00214   if(dynamic_cast<System*>(generator)) 
00215     std::cout << "Faudes object casts to System: OK [expected]\n";
00216   else
00217     std::cout << "Faudes object does not casts to System: ERR [test case error]\n";
00218 
00219   // recover faudes type name from object
00220   std::cout << "Its a \"" << FaudesTypeName(*generator) << "\"\n";
00221 
00222   // delete my objects
00223   delete generator;
00224 
00225   // done
00226   std::cout << "################################\n";
00227 
00228   return(0);
00229 }

libFAUDES 2.23h --- 2014.04.03 --- c++ api documentaion by doxygen