libFAUDES

Sections

Index

7_interface.cpp File Reference


Detailed Description

Tutorial, runtime interface.

This tutorial demonstrates access to libFAUDES data types and functions via the type- and function registries. The runtime interface addresses the development of applications that transparently propagate libFAUDES extensions to the user.

/** @file 7_interface.cpp

Tutorial, runtime interface. This tutorial demonstrates access to 
libFAUDES data types and functions via the type- and function
registries. The runtime interface addresses the development
of applications that transparently propagate libFAUDES extensions
to the user. 


@ingroup Tutorials 

@include 7_interface.cpp

*/

#include <iostream>
#include "libfaudes.h"

using namespace faudes;


int main(){

  // ******************** basic ussage of the type- and function registry

  // load (do so once on application startup)
  LoadRegistry("../include/libfaudes.rti");

  // instatiate some generators via the registry 
  Type* data0 = NewObject("Generator");  
  Type* data1 = NewObject("Generator");  
  Type* data2 = NewObject("Generator");  

  // load input data from files
  data0->Read("./data/simplemachine.gen");
  data1->Read("./data/buffer.gen");

  // instantiate a function via registry 
  Function* funct = NewFunction("Parallel");  
  
  // set parameter values (exception on accessing positional parameter out of range)
  funct->ParamValue(0,data0);
  funct->ParamValue(1,data1);
  funct->ParamValue(2,data2);

  // execute function (exception on type mismatch)
  funct->Execute();

  // report to console
  std::cout << "################################\n";
  std::cout << "# tutorial, rti parallel \n";
  data2->Write();
  std::cout << "################################\n";

  // test case
  vGenerator* vgen=dynamic_cast<vGenerator*>(data2);
  if(!vgen) {
    std::cout << "ERR: res does not cast to vGenerator\n";
    exit(1);
  }
  if(vgen->Size()!=6) {
    std::cout << "ERR: res is expected to have 6 states\n";
    exit(1);
  }

  // clear registry for below demos
  ClearRegistry();

  // delete my objects
  delete data0;
  delete data1;
  delete data2;
 
  // ******************** elementary type, String

  // create new String instance
  String fstring;

  // assign from corresponding c type std::string
  fstring = "hello faudes types";

  // assign to corresponding c type std::string
  std::string sstring = fstring;

  // file io
  fstring.Write("tmp_string.txt");
  fstring.Read("tmp_string.txt");

  // report to console
  std::cout << "################################\n";
  std::cout << "# tutorial, rti string \n";
  fstring.Write();
  std::cout << "################################\n";


  // ******************** elementary type, Integer

  // create new Integer instance
  Integer fint;

  // assign from corresponding c type std::int
  fint = 42;

  // assign to corresponding c type std::int
  int sint = fint;

  // arithmetic
  fint = 2*fint+16;

  // file io
  fint.Write("tmp_int.txt");
  fint.Read("tmp_int.txt");

  // report to console
  std::cout << "################################\n";
  std::cout << "# tutorial, rti integer \n";
  fint.Write();
  std::cout << "################################\n";

  // test case
  if(fint!=100) {
    std::cout << "ERR: expected 100\n";
    exit(1);
  }


  // ******************** elementary type, Boolean

  // create new Integer instance
  Boolean fbool;

  // assign from corresponding c type std::int
  fbool = false;

  // assign to corresponding c type std::int
  bool sbool = !fbool;

  // file io
  fbool.Write("tmp_bool.txt");
  fbool.Read("tmp_bool.txt");

  // report to console
  std::cout << "################################\n";
  std::cout << "# tutorial, rti bool \n";
  fbool.Write();
  std::cout << "################################\n";


  // ******************** advanced: type definitions and registry
  
  // defining a type with specified faudes type name (and no documentation)
  TypeDefinition* tdalphabet = TypeDefinition::Constructor<cEventSet>("EventSet");

  // defining a type with type name "Generator" and documentation from specified file
  TypeDefinition* tdgenerator = TypeDefinition::FromFile<cGenerator>("data/generator.rti");
 
  // register previously defined types
  TypeRegistry::G()->Insert(tdalphabet);
  TypeRegistry::G()->Insert(tdgenerator);

  // load any additional documentation from file (event set is missing from above)
  TypeRegistry::G()->MergeDocumentation("../include/libfaudes.rti");

  // dump registry to console
  std::cout << "################################\n";
  std::cout << "# tutorial, type registry \n";
  TypeRegistry::G()->Write();
  std::cout << "################################\n";

  // use the registry to construct an object by its type name
  Type* generator = TypeRegistry::G()->NewObject("Generator");  

  // test generator object
  if(dynamic_cast<cGenerator*>(generator)) 
    std::cout << "Faudes object casts to cGenerator: OK\n";
  else
    std::cout << "Faudes object casts to cGenerator: ERR (cast failed)\n";

  // recover faudes type name from object
  std::cout << "Its a \"" << TypeRegistry::G()->Definition(*generator).Name() << "\"\n";

  // delete my objects
  delete generator;

  // ******************** advanced: function definitions and registry

  // construct a simple function definition with name, signature and documentation from file
  FunctionDefinition* fdsum = FunctionDefinition::FromFile< 
     TSimpleFunction3<Integer,Integer,Integer, IntegerSum> >("./data/sum.rti");

  // construct a simple function definition with name but no documentation 
  FunctionDefinition* fdcomplement = FunctionDefinition::Constructor<
    TSimpleFunction1<vGenerator,LanguageComplement> >("LanguageComplement"); 

  // read missing documentation incl. signatures from file
  fdcomplement->Read("data/complement.rti");

  // register previously defined types (registry takes ownership)
  FunctionRegistry::G()->Insert(fdsum);
  FunctionRegistry::G()->Insert(fdcomplement);

  // dump registry to console
  std::cout << "################################\n";
  std::cout << "# tutorial, function registry   \n";
  FunctionRegistry::G()->Write();
  std::cout << "################################\n";

  // execute a function from the registry
  Function* sumfunc = FunctionRegistry::G()->NewFunction("IntegerSum");
  sumfunc->Variant("Res=Arg1+Arg2");
  Integer sumarg1 = 42;  
  Integer sumarg2 = 48;  
  Integer sumres;
  sumfunc->ParamValue(0,&sumarg1);
  sumfunc->ParamValue(1,&sumarg2);
  sumfunc->ParamValue(2,&sumres);
  sumfunc->Execute();
  delete sumfunc;

  // report to console
  std::cout << "################################\n";
  std::cout << "# tutorial, function IntegerSum  \n";
  sumres.Write();
  std::cout << "################################\n";

  // test case
  if(sumres!=90) {
    std::cout << "ERR: res is expected to be 90\n";
    exit(1);
  }


  // execute a function from the registry
  Function* compfunc = FunctionRegistry::G()->NewFunction("LanguageComplement");
  Generator comppar("data/boolean_g1.gen");  
  compfunc->ParamValue(0,&comppar);
  compfunc->Execute();
  delete compfunc;

  // report to console
  std::cout << "################################\n";
  std::cout << "# tutorial, function LanguageComplement  \n";
  comppar.Write();
  std::cout << "################################\n";

  return(0);
}

Definition in file 7_interface.cpp.

#include <iostream>
#include "libfaudes.h"

Go to the source code of this file.

Functions

int main ()


Function Documentation

int main ( void   ) 

Definition at line 22 of file 7_interface.cpp.

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