7_interface.cpp
Go to the documentation of this file.
1 /** @file 7_interface.cpp
2 
3 Tutorial, runtime interface. This tutorial demonstrates access to
4 libFAUDES data types and functions via the type- and function
5 registries. The runtime interface addresses the development
6 of applications that transparently propagate libFAUDES extensions
7 to the user.
8 
9 
10 @ingroup Tutorials
11 
12 @include 7_interface.cpp
13 
14 */
15 
16 #include <iostream>
17 #include "libfaudes.h"
18 
19 using namespace faudes;
20 
21 
22 int main(){
23 
24  // ******************** basic ussage of the faudes type interface
25 
26  // load (do so once on application startup)
27  LoadRegistry("../include/libfaudes.rti");
28 
29  // report to console
30  std::cout << "################################\n";
31  std::cout << "# tutorial, faudes types \n";
32 
33  // instatiate an object by faudes type
34  Type* fobject = NewFaudesObject("System");
35 
36  // query type of an object
37  std::cout << " faudes type of object: " << FaudesTypeName(*fobject) << "\n";
38 
39  // type test: can we cast this to a plain generator?
40  Generator gen;
41  bool isvg= ( gen.Cast(fobject)!=NULL );
42  if(isvg)
43  std::cout << " faudes object casts to Generator: ok [expected]\n";
44  else
45  std::cout << " faudes object casts to Generator: failed\n";
46 
47  // type test: can we cast this to an eventset?
48  EventSet eset;
49  bool ises = ( eset.Cast(fobject) != NULL ) ;
50  if(ises)
51  std::cout << " faudes object cast to EventSet: ok\n";
52  else
53  std::cout << " faudes object cast to EventSet: failed [expected]\n";
54 
55  // Record test case
56  FAUDES_TEST_DUMP("faudes type",FaudesTypeName(*fobject));
57  FAUDES_TEST_DUMP("cast to generator",isvg);
58  FAUDES_TEST_DUMP("cast to eventset",ises);
59 
60  // report
61  std::cout << "################################\n";
62 
63  // ******************** basic ussage of the function registry
64 
65  // instatiate some generators via the registry
66  Type* data0 = NewFaudesObject("Generator");
67  Type* data1 = NewFaudesObject("Generator");
68  Type* data2 = NewFaudesObject("Generator");
69 
70  // load input data from files
71  data0->Read("./data/simplemachine.gen");
72  data1->Read("./data/buffer.gen");
73 
74  // instantiate a function via registry
75  Function* funct = NewFaudesFunction("Parallel");
76 
77  // set parameter values (exception on accessing positional parameter out of range)
78  funct->ParamValue(0,data0);
79  funct->ParamValue(1,data1);
80  funct->ParamValue(2,data2);
81 
82  // execute function (exception on type mismatch)
83  funct->Execute();
84 
85  // report to console
86  std::cout << "################################\n";
87  std::cout << "# tutorial, rti parallel \n";
88  data2->Write();
89  std::cout << "################################\n";
90 
91  // test case
92  Generator* vgen=dynamic_cast<Generator*>(data2);
93  if(!vgen) {
94  std::cout << "ERR: res does not cast to Generator\n";
95  exit(1);
96  }
97  if(vgen->Size()!=6) {
98  std::cout << "ERR: res is expected to have 6 states\n";
99  exit(1);
100  }
101 
102  // record test case
103  FAUDES_TEST_DUMP("rti parallel",*data2);
104 
105  // clear registry for below demos
106  ClearRegistry();
107 
108  // delete my objects
109  delete data0;
110  delete data1;
111  delete data2;
112 
113  // ******************** elementary type, String
114 
115  // create new String instance
116  String fstring;
117 
118  // assign from corresponding c type std::string
119  fstring = "hello faudes types";
120 
121  // assign to corresponding c type std::string
122  std::string sstring = fstring;
123 
124  // file io
125  fstring.Write("tmp_string.txt");
126  fstring.Read("tmp_string.txt");
127 
128  // report to console
129  std::cout << "################################\n";
130  std::cout << "# tutorial, rti string \n";
131  fstring.Write();
132  std::cout << "################################\n";
133 
134 
135  // ******************** elementary type, Integer
136 
137  // create new Integer instance
138  Integer fint;
139 
140  // assign from corresponding c type std::int
141  fint = 42;
142 
143  // assign to corresponding c type std::int
144  int sint = fint;
145 
146  // arithmetic
147  fint = 2*fint+16;
148 
149  // file io
150  fint.Write("tmp_int.txt");
151  fint.Read("tmp_int.txt");
152 
153  // report to console
154  std::cout << "################################\n";
155  std::cout << "# tutorial, rti integer \n";
156  fint.Write();
157  std::cout << "################################\n";
158 
159  // test case
160  if(fint!=100) {
161  std::cout << "ERR: expected 100\n";
162  exit(1);
163  }
164 
165 
166  // ******************** elementary type, Boolean
167 
168  // create new Integer instance
169  Boolean fbool;
170 
171  // assign from corresponding c type std::int
172  fbool = false;
173 
174  // assign to corresponding c type std::int
175  bool sbool = !fbool;
176 
177  // file io
178  fbool.Write("tmp_bool.txt");
179  fbool.Read("tmp_bool.txt");
180 
181  // report to console
182  std::cout << "################################\n";
183  std::cout << "# tutorial, rti bool \n";
184  fbool.Write();
185  std::cout << "################################\n";
186 
187 
188  // ******************** advanced: type definitions and registry
189 
190  // defining a type with specified faudes type name (and no documentation)
191  TypeDefinition* tdalphabet = TypeDefinition::Constructor<Alphabet>("EventSet");
192 
193  // defining a type with type name "System" and documentation from specified file
194  TypeDefinition* tdgenerator = TypeDefinition::FromFile<System>("data/generator.rti");
195 
196 
197  // register previously defined types
198  TypeRegistry::G()->Insert(tdalphabet);
199  TypeRegistry::G()->Insert(tdgenerator);
200 
201  // load any additional documentation from file (event set is missing from above)
202  TypeRegistry::G()->MergeDocumentation("../include/libfaudes.rti");
203 
204  // dump registry to console
205  std::cout << "################################\n";
206  std::cout << "# tutorial, type registry \n";
207  TypeRegistry::G()->Write();
208  std::cout << "################################\n";
209 
210  // use the registry to construct an object by its type name
211  Type* generator = NewFaudesObject("System");
212 
213  // test generator object
214  if(dynamic_cast<System*>(generator))
215  std::cout << "Faudes object casts to System: OK [expected]\n";
216  else
217  std::cout << "Faudes object does not casts to System: ERR [test case error]\n";
218 
219  // recover faudes type name from object
220  std::cout << "Its a \"" << FaudesTypeName(*generator) << "\"\n";
221 
222  // delete my objects
223  delete generator;
224 
225  // done
226  std::cout << "################################\n";
227 
228  return(0);
229 }
int main()
Definition: 7_interface.cpp:22
#define FAUDES_TEST_DUMP(mes, dat)
Test protocol record macro ("mangle" filename for platform independance)
Definition: cfl_helper.h:483
Elementary type.
A faudes-function hosts parameter values of some faudes type and provides a method to perform an oper...
void Execute(void)
Perform operation.
void ParamValue(int n, Type *param)
Set parameter at certain position.
Elementary type.
Set of indices with symbolic names.
Definition: cfl_nameset.h:69
Elementary type.
Generator with controllability attributes.
A TypeDefinition defines a faudes-type in that it specifies a faudes-type name to identify the type a...
Definition: cfl_types.h:1462
void MergeDocumentation(TokenReader &rTr)
Scan token input for type documentation.
static TypeRegistry * G()
Method to access the single global instance of the registry.
void Insert(TypeDefinition *pTypeDef)
Add another type definition to the registry.
Base class of all libFAUDES objects that participate in the run-time interface.
Definition: cfl_types.h:239
void Read(const std::string &rFileName, const std::string &rLabel="", const Type *pContext=0)
Read configuration data from file with label specified.
Definition: cfl_types.cpp:261
void Write(const Type *pContext=0) const
Write configuration data to console.
Definition: cfl_types.cpp:139
virtual const Type * Cast(const Type *pOther) const
Cast other object to this type.
Definition: cfl_types.cpp:66
Base class of all FAUDES generators.
virtual const Type * Cast(const Type *pOther) const
Type test.
Idx Size(void) const
Get generator size (number of states)
Function * NewFaudesFunction(const std::string &rFunctName)
Instantiate faudes function objects by function name.
void ClearRegistry(void)
Clear all registry.
void LoadRegistry(const std::string &rPath)
Load all registered types and functions.
const std::string & FaudesTypeName(const Type &rObject)
Query type name.
Type * NewFaudesObject(const std::string &rTypeName)
Instantiate faudes typed objects by type name.
Includes all libFAUDES headers, incl plugings
libFAUDES resides within the namespace faudes.

libFAUDES 2.32b --- 2024.03.01 --- c++ api documentaion by doxygen