DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
oks_tutorial.cxx File Reference
#include <boost/date_time/gregorian/gregorian.hpp>
#include "oks/kernel.hpp"
#include "oks/object.hpp"
#include "oks/class.hpp"
#include "oks/attribute.hpp"
#include "oks/relationship.hpp"
#include "oks/query.hpp"
#include "oks/exceptions.hpp"
Include dependency graph for oks_tutorial.cxx:

Go to the source code of this file.

Functions

void setPersonValues (OksObject *o, const char *name, boost::gregorian::date birthday, const char *familySituation)
 
void printPerson (const OksObject *o)
 
void setEmployeeValues (OksObject *o, const char *name, boost::gregorian::date birthday, const char *familySituation, uint32_t salary)
 
void printEmployee (const OksObject *o)
 
void setDepartmentValues (OksObject *o, const char *name)
 
void printDepartment (const OksObject *o)
 
int main (int argc, char **argv)
 

Function Documentation

◆ main()

int main ( int argc,
char ** argv )

Definition at line 160 of file oks_tutorial.cxx.

161{
162 const char * schema_file = "/tmp/tutorial.oks"; // default schema file
163 const char * data_file = "/tmp/tutorial.okd"; // default data file
164
165 if(
166 argc > 1 &&
167 (
168 !strcmp(argv[1], "--help") ||
169 !strcmp(argv[1], "-help") ||
170 !strcmp(argv[1], "--h") ||
171 !strcmp(argv[1], "-h")
172 )
173 ) {
174 std::cout << "Usage: " << argv[0] << " [new_schema new_data]\n";
175 return 0;
176 }
177
178 if(argc == 3) {
179 schema_file = argv[1];
180 data_file = argv[2];
181 }
182
183
184 // Creates OKS kernel
185
186 std::cout << "[OKS TUTORIAL]: Creating OKS kernel...\n";
187
188 OksKernel kernel(false, false, false, false);
189
190 std::cout << "[OKS TUTORIAL]: Done creating OKS kernel\n\n";
191
192
193 try {
194
195
196 // Creates new schema file and tests return status
197
198 std::cout << "[OKS TUTORIAL]: Creating new schema file...\n";
199
200 OksFile * schema_h = kernel.new_schema(schema_file);
201
202 std::cout << "[OKS TUTORIAL]: Done creating new schema file...\n\n"
203 "[OKS TUTORIAL]: Define database class schema...\n\n"
204 " ********** ************ 1..1 **************\n"
205 " * Person *<|------* Employee *--------<>* Department *\n"
206 " ********** ************ 0..N **************\n\n";
207
208
209 // Creates class Person with three attributes "Name", "Birthday" and "Family_Situation"
210
211 OksClass * Person = new OksClass(
212 "Person",
213 "It is a class to describe a person",
214 false,
215 &kernel
216 );
217
218 Person->add(
219 new OksAttribute(
220 "Name",
222 false,
223 "",
224 "Unknown",
225 "A string to describe person name",
226 true
227 )
228 );
229
230 OksAttribute * PersonBirthday = new OksAttribute(
231 "Birthday",
233 false,
234 "",
235 "2009/01/01",
236 "A date to describe person birthday",
237 true
238 );
239
240 Person->add(PersonBirthday);
241
242 Person->add(
243 new OksAttribute(
244 "Family_Situation",
246 false,
247 "Single,Married,Widow(er)",
248 "Single",
249 "A enumeration to describe a person family state",
250 true
251 )
252 );
253
254
255 // Creates class Person with superclass Person, add "Salary" attribute and "Works at" relationship
256
257 OksClass * Employee = new OksClass(
258 "Employee",
259 "It is a class to describe an employee",
260 false,
261 &kernel
262 );
263
264 OksAttribute * EmployeeSalary = new OksAttribute(
265 "Salary",
267 false,
268 "",
269 "1000",
270 "An integer to describe employee salary",
271 false
272 );
273
274 OksRelationship * WorksAt = new OksRelationship(
275 "Works at",
276 "Department",
279 false,
280 false,
281 false,
282 "A employee works at one and only one department"
283 );
284
285 Employee->add_super_class("Person");
286 Employee->add(EmployeeSalary);
287 Employee->add(WorksAt);
288
289
290 // Creates class Department with one attribute and one relationship
291
292 OksClass * Department = new OksClass(
293 "Department",
294 "It is a class to describe a department",
295 false,
296 &kernel
297 );
298
299 OksAttribute * DepartmentName = new OksAttribute(
300 "Name",
302 false,
303 "",
304 "Unknown",
305 "A string to describe department name",
306 true
307 );
308
309 OksRelationship *DepartmentStaff = new OksRelationship(
310 "Staff",
311 "Employee",
314 true,
315 true,
316 true,
317 "A department has zero or many employess"
318 );
319
320 Department->add(DepartmentName);
321 Department->add(DepartmentStaff);
322
323
324 // Saves created schema file
325
326 std::cout << "[OKS TUTORIAL]: Saves created OKS schema file...\n";
327
328 kernel.save_schema(schema_h);
329
330
331 // Creates new data file and tests return status
332
333 std::cout << "[OKS TUTORIAL]: Creating new data file...\n";
334
335 OksFile * data_h = kernel.new_data(data_file, "OKS TUTORIAL DATA FILE");
336
337 data_h->add_include_file(schema_file);
338
339
340 // Creates instances of the classes
341
342 OksObject * person1 = new OksObject(Person, "peter");
343 OksObject * person2 = new OksObject(Person, "mick");
344 OksObject * person3 = new OksObject(Person, "baby");
345 OksObject * employee1 = new OksObject(Employee, "alexander");
346 OksObject * employee2 = new OksObject(Employee, "michel");
347 OksObject * employee3 = new OksObject(Employee, "maria");
348 OksObject * department1 = new OksObject(Department, "IT");
349 OksObject * department2 = new OksObject(Department, "EP");
350
351
352 // Sets attribute values for instances of class 'Person'
353
354 setPersonValues(person1, "Peter", boost::gregorian::from_string("1960/02/01"), "Married");
355 setPersonValues(person2, "Mick", boost::gregorian::from_string("1956-09-01"), "Single");
356 setPersonValues(person3, "Julia", boost::gregorian::from_string("2000-May-25"), "Single");
357
358
359 // Sets attribute values for instances of class 'Employee'
360
361 setEmployeeValues(employee1, "Alexander", boost::gregorian::from_string("1972/05/12"), "Single", 3540) ;
362 setEmployeeValues(employee2, "Michel", boost::gregorian::from_string("1963/01/28"), "Married", 4950) ;
363 setEmployeeValues(employee3, "Maria", boost::gregorian::from_string("1951/08/18"), "Widow(er)", 4020) ;
364
365
366 // Sets attribute values for instance of class 'Department'
367
368 setDepartmentValues(department1, "IT Department");
369 setDepartmentValues(department2, "EP Department");
370
371
372 // Sets relationships
373
374 department1->AddRelationshipValue("Staff", employee1);
375 department1->AddRelationshipValue("Staff", employee2);
376 department2->AddRelationshipValue("Staff", employee3);
377 employee1->SetRelationshipValue("Works at", department1);
378 employee2->SetRelationshipValue("Works at", department1);
379 employee3->SetRelationshipValue("Works at", department2);
380
381
382 // Print out database contents
383
384 std::cout << "\n[OKS TUTORIAL]: Database contains the following data:\n";
385
386 printPerson(person1);
387 printPerson(person2);
388 printPerson(person3);
389 printEmployee(employee1);
390 printEmployee(employee2);
391 printEmployee(employee3);
392 printDepartment(department1);
393 printDepartment(department2);
394
395
396 // Saves created data file
397
398 std::cout << "\n[OKS TUTORIAL]: Saves created OKS data file...\n";
399
400 kernel.save_data(data_h);
401
402
403 std::cout << "[OKS TUTORIAL]: Done with saving created OKS data file\n";
404
405
406 // **********************************************************************
407 // ***** At this moment the database has been created and saved *****
408 // **********************************************************************
409
410
411 std::cout << "\n[OKS TUTORIAL]: Start database querying tests\n\n";
412
413
414 // This is a test for OksComparator and OksQuery
415
416 {
417 std::cout << "[QUERY]: Start simple database querying...\n";
418
419 // Looking for persons were born after 01 January 1960
420
421 boost::gregorian::date aDate(boost::gregorian::from_string("1960/01/01")); // query date
422 OksQuery query(true, new OksComparator(PersonBirthday, new OksData(aDate), OksQuery::greater_cmp));
423
424
425 // executes query
426
427 std::cout << "[QUERY]: Looking for persons were born after " << aDate << " ...\n\n";
428
429 OksObject::List * queryResult = Person->execute_query(&query);
430
431
432 // builds iterator over results if something was found
433
434 if(queryResult) {
435 std::cout << "[QUERY]: Query \'" << query
436 << "\'\n founds the following objects in class \'"
437 << Person->get_name() << "\' and subclasses:\n";
438
439 for(OksObject::List::iterator i = queryResult->begin(); i != queryResult->end(); ++i) {
440 OksObject * o = *i;
441 OksData * d(o->GetAttributeValue("Birthday"));
442 std::cout << " - " << o << " was born " << *d << std::endl;
443 }
444
445
446 // free list: we do not need it anymore
447
448 delete queryResult;
449 }
450
451 std::cout << "[QUERY]: Done with simple database querying\n\n";
452 }
453
454 {
455 std::cout << "[QUERY]: Start database querying with logical function...\n";
456
457
458 // Looking for persons were born after 01 January 1960
459 // and before 01 January 1970
460
461 boost::gregorian::date lowDate(boost::gregorian::from_string("1960/01/01")); // low date
462 boost::gregorian::date highDate(boost::gregorian::from_string("1970/01/01")); // high date
463
464 OksAndExpression * andExpression = new OksAndExpression();
465
466 andExpression->add(new OksComparator(PersonBirthday, new OksData(lowDate), OksQuery::greater_cmp));
467 andExpression->add(new OksComparator(PersonBirthday, new OksData(highDate), OksQuery::less_cmp));
468
469 OksQuery query(true, andExpression);
470
471
472 // executes query
473
474 std::cout << "[QUERY]: Looking for persons were born between " << lowDate
475 << " and " << highDate << " ...\n\n";
476
477 OksObject::List * queryResult = Person->execute_query(&query);
478
479
480 // builds iterator over results if something was found
481
482 if(queryResult) {
483 std::cout << "[QUERY]: Query \'" << query
484 << "\'\n founds the following objects in class \'"
485 << Person->get_name() << "\' and subclasses:\n";
486
487 for(OksObject::List::iterator i = queryResult->begin(); i != queryResult->end(); ++i) {
488 OksObject * o = *i;
489 OksData * d(o->GetAttributeValue("Birthday"));
490 std::cout << " - " << o << " was born " << *d << std::endl;
491 }
492
493
494 // free list: we do not need it anymore
495
496 delete queryResult;
497 }
498
499 std::cout << "[QUERY]: Done database querying with logical function\n\n";
500
501 }
502
503 {
504
505 std::cout << "[QUERY]: Start database querying with relationship expression...\n";
506
507
508 // Looking for employee were born after 01 January 1971
509 // and which works at IT Department
510
511 boost::gregorian::date aDate(boost::gregorian::from_string("1971/01/01")); // a date
512 const char * departmentName = "IT Department";
513
514 OksAndExpression * andExpression = new OksAndExpression();
515
516 andExpression->add(new OksComparator(PersonBirthday, new OksData(aDate), OksQuery::greater_cmp));
517 andExpression->add(new OksRelationshipExpression(WorksAt, new OksComparator(DepartmentName, new OksData(departmentName), OksQuery::equal_cmp)));
518
519 OksQuery query(true, andExpression);
520
521
522 // executes query
523
524 std::cout << "[QUERY]: Looking for employee were born after " << aDate
525 << " and works at department " << departmentName << " ...\n\n";
526
527 OksObject::List * queryResult = Employee->execute_query(&query);
528
529
530 // builds iterator over results if something was found
531
532 if(queryResult) {
533 std::cout << "[QUERY]: Query \'" << query
534 << "\'\n founds the following objects in class \'"
535 << Employee->get_name() << "\' and subclasses:\n";
536
537 for(OksObject::List::iterator i = queryResult->begin(); i != queryResult->end(); ++i) {
538 OksObject * o = *i;
539 OksData * d(o->GetAttributeValue("Birthday"));
540 std::cout << " - " << o << " was born " << *d << std::endl;
541 }
542
543
544 // free list: we do not need it anymore
545
546 delete queryResult;
547 }
548
549 std::cout << "[QUERY]: Done database querying with relationship expression...\n";
550 }
551
552 std::cout << "\n[OKS TUTORIAL]: Done with database querying tests\n\n";
553
554 // It is not necessary to free data or schema because all instances of OKS
555 // classes are automatic C++ objects and they will deleted before last
556 // close bracket `}`.
557
558 }
559 catch (const std::exception & ex) {
560 std::cerr << "Caught exception:\n" << ex.what() << std::endl;
561 }
562
563 // returns success
564
565 return 0;
566}
OKS query logical AND expression class.
Definition query.hpp:282
OKS attribute class.
static const char * u32_int_type
static const char * string_type
static const char * enum_type
static const char * date_type
The OKS class.
Definition class.hpp:200
void add_super_class(const std::string &name)
Definition class.cpp:878
void add(OksAttribute *a)
Add attribute.
Definition class.cpp:1052
const std::string & get_name() const noexcept
Definition class.hpp:363
OksObject::List * execute_query(OksQuery *query) const
Execute query.
Definition query.cpp:431
OKS query expression comparator class.
Definition query.hpp:139
Provides interface to the OKS XML schema and data files.
Definition file.hpp:340
void add_include_file(const std::string &name)
Add include file.
Definition file.cpp:1194
Provides interface to the OKS kernel.
Definition kernel.hpp:577
void add(OksQueryExpression *q)
Definition query.hpp:264
OksObject describes instance of OksClass.
Definition object.hpp:836
void AddRelationshipValue(const std::string &name, OksObject *object)
Add object value to multi-value relationship by name.
Definition object.cpp:2336
std::list< OksObject * > List
Definition object.hpp:875
void SetRelationshipValue(const std::string &name, OksData *data, bool skip_non_null_check=false)
Set value of relationship by name.
Definition object.cpp:2189
OksData * GetAttributeValue(const std::string &name) const
Get value of attribute by name.
Definition object.cpp:1919
OKS query class.
Definition query.hpp:36
static bool equal_cmp(const OksData *, const OksData *)
Definition query.cpp:51
static bool greater_cmp(const OksData *, const OksData *)
Definition query.cpp:56
static bool less_cmp(const OksData *, const OksData *)
Definition query.cpp:55
OKS query relationship expression class.
Definition query.hpp:187
void printEmployee(const OksObject *o)
void setPersonValues(OksObject *o, const char *name, boost::gregorian::date birthday, const char *familySituation)
void setEmployeeValues(OksObject *o, const char *name, boost::gregorian::date birthday, const char *familySituation, uint32_t salary)
void setDepartmentValues(OksObject *o, const char *name)
void printPerson(const OksObject *o)
void printDepartment(const OksObject *o)
the structure to pass common parameters to various read() methods of OksData and OksObject class
Definition object.hpp:449

◆ printDepartment()

void printDepartment ( const OksObject * o)

Definition at line 146 of file oks_tutorial.cxx.

149{
150 OksData *name(o->GetAttributeValue("Name")), // is used to store 'Staff'
151 *staff(o->GetRelationshipValue("Staff")); // is used to store 'Name'
152
153 std::cout << "Object " << o << "\n"
154 " Name: " << *name << " \n"
155 " Staff: \"" << *staff << "\"\n";
156}
OksData * GetRelationshipValue(const std::string &) const
Get value of relationship by name.
Definition object.cpp:2004

◆ printEmployee()

void printEmployee ( const OksObject * o)

Definition at line 108 of file oks_tutorial.cxx.

111{
112 // we can use printPerson() because 'Employee' class
113 // is derived from 'Person' class
114
115 printPerson(o);
116
117 OksData *department(o->GetRelationshipValue("Works at")), // is used to store 'Works at'
118 *salary(o->GetAttributeValue("Salary")); // is used to store 'salary'
119
120 std::cout << " Salary: " << *salary << " \n"
121 " Works at: \"" << department->data.OBJECT->GetId() << "\"\n";
122}

◆ printPerson()

void printPerson ( const OksObject * o)

Definition at line 65 of file oks_tutorial.cxx.

68{
69 OksData * name(o->GetAttributeValue("Name")); // is used to store 'Name'
70 OksData * birthday(o->GetAttributeValue("Birthday")); // is used to store 'Birthday'
71 OksData * family(o->GetAttributeValue("Family_Situation")); // is used to store 'Family_Situation'
72
73 std::cout << "Object " << o << " \n"
74 " Name: " << *name << " \n"
75 " Birthday: \'" << *birthday << "\" \n"
76 " Family_Situation: " << *family << std::endl;
77}

◆ setDepartmentValues()

void setDepartmentValues ( OksObject * o,
const char * name )

Definition at line 130 of file oks_tutorial.cxx.

134{
135 OksData d(name); // creates OKS data with string 'name'
136
137 o->SetAttributeValue("Name", &d);
138}
void SetAttributeValue(const std::string &name, OksData *data)
Set value of attribute by name.
Definition object.cpp:1990

◆ setEmployeeValues()

void setEmployeeValues ( OksObject * o,
const char * name,
boost::gregorian::date birthday,
const char * familySituation,
uint32_t salary )

Definition at line 85 of file oks_tutorial.cxx.

92{
93 // we can use setPersonValues() because 'Employee' class
94 // derived from 'Person' class
95
96 setPersonValues(o, name, birthday, familySituation);
97
98 OksData d(salary); // creates OKS data with ulong 'salary'
99 o->SetAttributeValue("Salary", &d);
100}

◆ setPersonValues()

void setPersonValues ( OksObject * o,
const char * name,
boost::gregorian::date birthday,
const char * familySituation )

Definition at line 39 of file oks_tutorial.cxx.

45{
46 OksData d; // creates OKS data with unknown type
47
48 d.Set(name); // sets OKS data to string 'name'
49 o->SetAttributeValue("Name", &d);
50
51 d.Set(birthday); // sets OKS data to date 'birthday'
52 o->SetAttributeValue("Birthday", &d);
53
54 d.Set(familySituation); // sets OKS data to 'familySituation'
55 d.type = OksData::enum_type; // sets OKS data type to enumeration
56 o->SetAttributeValue("Family_Situation", &d);
57}