libFAUDES

Sections

Index

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 = NewObject("System");  
00035 
00036   // query type of an object
00037   std::cout << " faudes type of object: " << faudes::TypeName(*fobject) << "\n";
00038 
00039   // type test: can we cast this to a generator?
00040   vGenerator gen;
00041   if(gen.Cast(fobject)) 
00042     std::cout << " faudes object cast to Generator: ok [expected]\n";
00043   else    
00044     std::cout << " faudes object cast to Generator: failed\n";
00045 
00046   // type test: can we cast this to an eventset?
00047   EventSet eset;
00048   if(eset.Cast(fobject)) 
00049     std::cout << " faudes object cast to EventSet: ok\n";
00050   else    
00051     std::cout << " faudes object cast to EventSet: failed [expected]\n";
00052 
00053 
00054   // report
00055   std::cout << "################################\n";
00056 
00057   // ******************** basic ussage of the function registry
00058 
00059   // instatiate some generators via the registry 
00060   Type* data0 = NewObject("Generator");  
00061   Type* data1 = NewObject("Generator");  
00062   Type* data2 = NewObject("Generator");  
00063 
00064   // load input data from files
00065   data0->Read("./data/simplemachine.gen");
00066   data1->Read("./data/buffer.gen");
00067 
00068   // instantiate a function via registry 
00069   Function* funct = NewFunction("Parallel");  
00070   
00071   // set parameter values (exception on accessing positional parameter out of range)
00072   funct->ParamValue(0,data0);
00073   funct->ParamValue(1,data1);
00074   funct->ParamValue(2,data2);
00075 
00076   // execute function (exception on type mismatch)
00077   funct->Execute();
00078 
00079   // report to console
00080   std::cout << "################################\n";
00081   std::cout << "# tutorial, rti parallel \n";
00082   data2->Write();
00083   std::cout << "################################\n";
00084 
00085   // test case
00086   vGenerator* vgen=dynamic_cast<vGenerator*>(data2);
00087   if(!vgen) {
00088     std::cout << "ERR: res does not cast to vGenerator\n";
00089     exit(1);
00090   }
00091   if(vgen->Size()!=6) {
00092     std::cout << "ERR: res is expected to have 6 states\n";
00093     exit(1);
00094   }
00095 
00096   // clear registry for below demos
00097   ClearRegistry();
00098 
00099   // delete my objects
00100   delete data0;
00101   delete data1;
00102   delete data2;
00103  
00104   // ******************** elementary type, String
00105 
00106   // create new String instance
00107   String fstring;
00108 
00109   // assign from corresponding c type std::string
00110   fstring = "hello faudes types";
00111 
00112   // assign to corresponding c type std::string
00113   std::string sstring = fstring;
00114 
00115   // file io
00116   fstring.Write("tmp_string.txt");
00117   fstring.Read("tmp_string.txt");
00118 
00119   // report to console
00120   std::cout << "################################\n";
00121   std::cout << "# tutorial, rti string \n";
00122   fstring.Write();
00123   std::cout << "################################\n";
00124 
00125 
00126   // ******************** elementary type, Integer
00127 
00128   // create new Integer instance
00129   Integer fint;
00130 
00131   // assign from corresponding c type std::int
00132   fint = 42;
00133 
00134   // assign to corresponding c type std::int
00135   int sint = fint;
00136 
00137   // arithmetic
00138   fint = 2*fint+16;
00139 
00140   // file io
00141   fint.Write("tmp_int.txt");
00142   fint.Read("tmp_int.txt");
00143 
00144   // report to console
00145   std::cout << "################################\n";
00146   std::cout << "# tutorial, rti integer \n";
00147   fint.Write();
00148   std::cout << "################################\n";
00149 
00150   // test case
00151   if(fint!=100) {
00152     std::cout << "ERR: expected 100\n";
00153     exit(1);
00154   }
00155 
00156 
00157   // ******************** elementary type, Boolean
00158 
00159   // create new Integer instance
00160   Boolean fbool;
00161 
00162   // assign from corresponding c type std::int
00163   fbool = false;
00164 
00165   // assign to corresponding c type std::int
00166   bool sbool = !fbool;
00167 
00168   // file io
00169   fbool.Write("tmp_bool.txt");
00170   fbool.Read("tmp_bool.txt");
00171 
00172   // report to console
00173   std::cout << "################################\n";
00174   std::cout << "# tutorial, rti bool \n";
00175   fbool.Write();
00176   std::cout << "################################\n";
00177 
00178 
00179   // ******************** advanced: type definitions and registry
00180   
00181   // defining a type with specified faudes type name (and no documentation)
00182   TypeDefinition* tdalphabet = TypeDefinition::Constructor<cEventSet>("EventSet");
00183 
00184   // defining a type with type name "Generator" and documentation from specified file
00185   TypeDefinition* tdgenerator = TypeDefinition::FromFile<cGenerator>("data/generator.rti");
00186  
00187   // register previously defined types
00188   TypeRegistry::G()->Insert(tdalphabet);
00189   TypeRegistry::G()->Insert(tdgenerator);
00190 
00191   // load any additional documentation from file (event set is missing from above)
00192   TypeRegistry::G()->MergeDocumentation("../include/libfaudes.rti");
00193 
00194   // dump registry to console
00195   std::cout << "################################\n";
00196   std::cout << "# tutorial, type registry \n";
00197   TypeRegistry::G()->Write();
00198   std::cout << "################################\n";
00199 
00200   // use the registry to construct an object by its type name
00201   Type* generator = TypeRegistry::G()->NewObject("Generator");  
00202 
00203   // test generator object
00204   if(dynamic_cast<cGenerator*>(generator)) 
00205     std::cout << "Faudes object casts to cGenerator: OK\n";
00206   else
00207     std::cout << "Faudes object casts to cGenerator: ERR (cast failed)\n";
00208 
00209   // recover faudes type name from object
00210   std::cout << "Its a \"" << TypeRegistry::G()->Definition(*generator).Name() << "\"\n";
00211 
00212   // delete my objects
00213   delete generator;
00214 
00215   return(0);
00216 }

libFAUDES 2.16b --- 2010-9-8 --- c++ source docu by doxygen 1.6.3