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 type- and function registry
00025 
00026   // load (do so once on application startup)
00027   LoadRegistry("../include/libfaudes.rti");
00028 
00029   // instatiate some generators via the registry 
00030   Type* data0 = NewObject("Generator");  
00031   Type* data1 = NewObject("Generator");  
00032   Type* data2 = NewObject("Generator");  
00033 
00034   // load input data from files
00035   data0->Read("./data/simplemachine.gen");
00036   data1->Read("./data/buffer.gen");
00037 
00038   // instantiate a function via registry 
00039   Function* funct = NewFunction("Parallel");  
00040   
00041   // set parameter values (exception on accessing positional parameter out of range)
00042   funct->ParamValue(0,data0);
00043   funct->ParamValue(1,data1);
00044   funct->ParamValue(2,data2);
00045 
00046   // execute function (exception on type mismatch)
00047   funct->Execute();
00048 
00049   // report to console
00050   std::cout << "################################\n";
00051   std::cout << "# tutorial, rti parallel \n";
00052   data2->Write();
00053   std::cout << "################################\n";
00054 
00055   // test case
00056   vGenerator* vgen=dynamic_cast<vGenerator*>(data2);
00057   if(!vgen) {
00058     std::cout << "ERR: res does not cast to vGenerator\n";
00059     exit(1);
00060   }
00061   if(vgen->Size()!=6) {
00062     std::cout << "ERR: res is expected to have 6 states\n";
00063     exit(1);
00064   }
00065 
00066   // clear registry for below demos
00067   ClearRegistry();
00068 
00069   // delete my objects
00070   delete data0;
00071   delete data1;
00072   delete data2;
00073  
00074   // ******************** elementary type, String
00075 
00076   // create new String instance
00077   String fstring;
00078 
00079   // assign from corresponding c type std::string
00080   fstring = "hello faudes types";
00081 
00082   // assign to corresponding c type std::string
00083   std::string sstring = fstring;
00084 
00085   // file io
00086   fstring.Write("tmp_string.txt");
00087   fstring.Read("tmp_string.txt");
00088 
00089   // report to console
00090   std::cout << "################################\n";
00091   std::cout << "# tutorial, rti string \n";
00092   fstring.Write();
00093   std::cout << "################################\n";
00094 
00095 
00096   // ******************** elementary type, Integer
00097 
00098   // create new Integer instance
00099   Integer fint;
00100 
00101   // assign from corresponding c type std::int
00102   fint = 42;
00103 
00104   // assign to corresponding c type std::int
00105   int sint = fint;
00106 
00107   // arithmetic
00108   fint = 2*fint+16;
00109 
00110   // file io
00111   fint.Write("tmp_int.txt");
00112   fint.Read("tmp_int.txt");
00113 
00114   // report to console
00115   std::cout << "################################\n";
00116   std::cout << "# tutorial, rti integer \n";
00117   fint.Write();
00118   std::cout << "################################\n";
00119 
00120   // test case
00121   if(fint!=100) {
00122     std::cout << "ERR: expected 100\n";
00123     exit(1);
00124   }
00125 
00126 
00127   // ******************** elementary type, Boolean
00128 
00129   // create new Integer instance
00130   Boolean fbool;
00131 
00132   // assign from corresponding c type std::int
00133   fbool = false;
00134 
00135   // assign to corresponding c type std::int
00136   bool sbool = !fbool;
00137 
00138   // file io
00139   fbool.Write("tmp_bool.txt");
00140   fbool.Read("tmp_bool.txt");
00141 
00142   // report to console
00143   std::cout << "################################\n";
00144   std::cout << "# tutorial, rti bool \n";
00145   fbool.Write();
00146   std::cout << "################################\n";
00147 
00148 
00149   // ******************** advanced: type definitions and registry
00150   
00151   // defining a type with specified faudes type name (and no documentation)
00152   TypeDefinition* tdalphabet = TypeDefinition::Constructor<cEventSet>("EventSet");
00153 
00154   // defining a type with type name "Generator" and documentation from specified file
00155   TypeDefinition* tdgenerator = TypeDefinition::FromFile<cGenerator>("data/generator.rti");
00156  
00157   // register previously defined types
00158   TypeRegistry::G()->Insert(tdalphabet);
00159   TypeRegistry::G()->Insert(tdgenerator);
00160 
00161   // load any additional documentation from file (event set is missing from above)
00162   TypeRegistry::G()->MergeDocumentation("../include/libfaudes.rti");
00163 
00164   // dump registry to console
00165   std::cout << "################################\n";
00166   std::cout << "# tutorial, type registry \n";
00167   TypeRegistry::G()->Write();
00168   std::cout << "################################\n";
00169 
00170   // use the registry to construct an object by its type name
00171   Type* generator = TypeRegistry::G()->NewObject("Generator");  
00172 
00173   // test generator object
00174   if(dynamic_cast<cGenerator*>(generator)) 
00175     std::cout << "Faudes object casts to cGenerator: OK\n";
00176   else
00177     std::cout << "Faudes object casts to cGenerator: ERR (cast failed)\n";
00178 
00179   // recover faudes type name from object
00180   std::cout << "Its a \"" << TypeRegistry::G()->Definition(*generator).Name() << "\"\n";
00181 
00182   // delete my objects
00183   delete generator;
00184 
00185   // ******************** advanced: function definitions and registry
00186 
00187   // construct a simple function definition with name, signature and documentation from file
00188   FunctionDefinition* fdsum = FunctionDefinition::FromFile< 
00189      TSimpleFunction3<Integer,Integer,Integer, IntegerSum> >("./data/sum.rti");
00190 
00191   // construct a simple function definition with name but no documentation 
00192   FunctionDefinition* fdcomplement = FunctionDefinition::Constructor<
00193     TSimpleFunction1<vGenerator,LanguageComplement> >("LanguageComplement"); 
00194 
00195   // read missing documentation incl. signatures from file
00196   fdcomplement->Read("data/complement.rti");
00197 
00198   // register previously defined types (registry takes ownership)
00199   FunctionRegistry::G()->Insert(fdsum);
00200   FunctionRegistry::G()->Insert(fdcomplement);
00201 
00202   // dump registry to console
00203   std::cout << "################################\n";
00204   std::cout << "# tutorial, function registry   \n";
00205   FunctionRegistry::G()->Write();
00206   std::cout << "################################\n";
00207 
00208   // execute a function from the registry
00209   Function* sumfunc = FunctionRegistry::G()->NewFunction("IntegerSum");
00210   sumfunc->Variant("Res=Arg1+Arg2");
00211   Integer sumarg1 = 42;  
00212   Integer sumarg2 = 48;  
00213   Integer sumres;
00214   sumfunc->ParamValue(0,&sumarg1);
00215   sumfunc->ParamValue(1,&sumarg2);
00216   sumfunc->ParamValue(2,&sumres);
00217   sumfunc->Execute();
00218   delete sumfunc;
00219 
00220   // report to console
00221   std::cout << "################################\n";
00222   std::cout << "# tutorial, function IntegerSum  \n";
00223   sumres.Write();
00224   std::cout << "################################\n";
00225 
00226   // test case
00227   if(sumres!=90) {
00228     std::cout << "ERR: res is expected to be 90\n";
00229     exit(1);
00230   }
00231 
00232 
00233   // execute a function from the registry
00234   Function* compfunc = FunctionRegistry::G()->NewFunction("LanguageComplement");
00235   Generator comppar("data/boolean_g1.gen");  
00236   compfunc->ParamValue(0,&comppar);
00237   compfunc->Execute();
00238   delete compfunc;
00239 
00240   // report to console
00241   std::cout << "################################\n";
00242   std::cout << "# tutorial, function LanguageComplement  \n";
00243   comppar.Write();
00244   std::cout << "################################\n";
00245 
00246   return(0);
00247 }

libFAUDES 2.14g --- 2009-12-3 --- c++ source docu by doxygen 1.5.6