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/attribute.hpp"
#include "oks/class.hpp"
#include "oks/exceptions.hpp"
#include "oks/kernel.hpp"
#include "oks/object.hpp"
#include "oks/query.hpp"
#include "oks/relationship.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 167 of file oks_tutorial.cxx.

168{
169 const char* schema_file = "/tmp/tutorial.oks"; // default schema file
170 const char* data_file = "/tmp/tutorial.okd"; // default data file
171
172 if (argc > 1 &&
173 (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-help") || !strcmp(argv[1], "--h") || !strcmp(argv[1], "-h"))) {
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 // Creates OKS kernel
184
185 std::cout << "[OKS TUTORIAL]: Creating OKS kernel...\n";
186
187 OksKernel kernel(false, false, false, false);
188
189 std::cout << "[OKS TUTORIAL]: Done creating OKS kernel\n\n";
190
191 try {
192
193 // Creates new schema file and tests return status
194
195 std::cout << "[OKS TUTORIAL]: Creating new schema file...\n";
196
197 OksFile* schema_h = kernel.new_schema(schema_file);
198
199 std::cout << "[OKS TUTORIAL]: Done creating new schema file...\n\n"
200 "[OKS TUTORIAL]: Define database class schema...\n\n"
201 " ********** ************ 1..1 **************\n"
202 " * Person *<|------* Employee *--------<>* Department *\n"
203 " ********** ************ 0..N **************\n\n";
204
205 // Creates class Person with three attributes "Name", "Birthday" and "Family_Situation"
206
207 OksClass* Person = new OksClass("Person", "It is a class to describe a person", false, &kernel);
208
209 Person->add(new OksAttribute(
210 "Name", OksAttribute::string_type, false, "", "Unknown", "A string to describe person name", true));
211
212 OksAttribute* PersonBirthday = new OksAttribute(
213 "Birthday", OksAttribute::date_type, false, "", "2009/01/01", "A date to describe person birthday", true);
214
215 Person->add(PersonBirthday);
216
217 Person->add(new OksAttribute("Family_Situation",
219 false,
220 "Single,Married,Widow(er)",
221 "Single",
222 "A enumeration to describe a person family state",
223 true));
224
225 // Creates class Person with superclass Person, add "Salary" attribute and "Works at" relationship
226
227 OksClass* Employee = new OksClass("Employee", "It is a class to describe an employee", false, &kernel);
228
229 OksAttribute* EmployeeSalary = new OksAttribute(
230 "Salary", OksAttribute::u32_int_type, false, "", "1000", "An integer to describe employee salary", false);
231
232 OksRelationship* WorksAt = new OksRelationship("Works at",
233 "Department",
236 false,
237 false,
238 false,
239 "A employee works at one and only one department");
240
241 Employee->add_super_class("Person");
242 Employee->add(EmployeeSalary);
243 Employee->add(WorksAt);
244
245 // Creates class Department with one attribute and one relationship
246
247 OksClass* Department = new OksClass("Department", "It is a class to describe a department", false, &kernel);
248
249 OksAttribute* DepartmentName = new OksAttribute(
250 "Name", OksAttribute::string_type, false, "", "Unknown", "A string to describe department name", true);
251
252 OksRelationship* DepartmentStaff = new OksRelationship("Staff",
253 "Employee",
256 true,
257 true,
258 true,
259 "A department has zero or many employess");
260
261 Department->add(DepartmentName);
262 Department->add(DepartmentStaff);
263
264 // Saves created schema file
265
266 std::cout << "[OKS TUTORIAL]: Saves created OKS schema file...\n";
267
268 kernel.save_schema(schema_h);
269
270 // Creates new data file and tests return status
271
272 std::cout << "[OKS TUTORIAL]: Creating new data file...\n";
273
274 OksFile* data_h = kernel.new_data(data_file, "OKS TUTORIAL DATA FILE");
275
276 data_h->add_include_file(schema_file);
277
278 // Creates instances of the classes
279
280 OksObject* person1 = new OksObject(Person, "peter");
281 OksObject* person2 = new OksObject(Person, "mick");
282 OksObject* person3 = new OksObject(Person, "baby");
283 OksObject* employee1 = new OksObject(Employee, "alexander");
284 OksObject* employee2 = new OksObject(Employee, "michel");
285 OksObject* employee3 = new OksObject(Employee, "maria");
286 OksObject* department1 = new OksObject(Department, "IT");
287 OksObject* department2 = new OksObject(Department, "EP");
288
289 // Sets attribute values for instances of class 'Person'
290
291 setPersonValues(person1, "Peter", boost::gregorian::from_string("1960/02/01"), "Married");
292 setPersonValues(person2, "Mick", boost::gregorian::from_string("1956-09-01"), "Single");
293 setPersonValues(person3, "Julia", boost::gregorian::from_string("2000-May-25"), "Single");
294
295 // Sets attribute values for instances of class 'Employee'
296
297 setEmployeeValues(employee1, "Alexander", boost::gregorian::from_string("1972/05/12"), "Single", 3540);
298 setEmployeeValues(employee2, "Michel", boost::gregorian::from_string("1963/01/28"), "Married", 4950);
299 setEmployeeValues(employee3, "Maria", boost::gregorian::from_string("1951/08/18"), "Widow(er)", 4020);
300
301 // Sets attribute values for instance of class 'Department'
302
303 setDepartmentValues(department1, "IT Department");
304 setDepartmentValues(department2, "EP Department");
305
306 // Sets relationships
307
308 department1->AddRelationshipValue("Staff", employee1);
309 department1->AddRelationshipValue("Staff", employee2);
310 department2->AddRelationshipValue("Staff", employee3);
311 employee1->SetRelationshipValue("Works at", department1);
312 employee2->SetRelationshipValue("Works at", department1);
313 employee3->SetRelationshipValue("Works at", department2);
314
315 // Print out database contents
316
317 std::cout << "\n[OKS TUTORIAL]: Database contains the following data:\n";
318
319 printPerson(person1);
320 printPerson(person2);
321 printPerson(person3);
322 printEmployee(employee1);
323 printEmployee(employee2);
324 printEmployee(employee3);
325 printDepartment(department1);
326 printDepartment(department2);
327
328 // Saves created data file
329
330 std::cout << "\n[OKS TUTORIAL]: Saves created OKS data file...\n";
331
332 kernel.save_data(data_h);
333
334 std::cout << "[OKS TUTORIAL]: Done with saving created OKS data file\n";
335
336 // **********************************************************************
337 // ***** At this moment the database has been created and saved *****
338 // **********************************************************************
339
340 std::cout << "\n[OKS TUTORIAL]: Start database querying tests\n\n";
341
342 // This is a test for OksComparator and OksQuery
343
344 {
345 std::cout << "[QUERY]: Start simple database querying...\n";
346
347 // Looking for persons were born after 01 January 1960
348
349 boost::gregorian::date aDate(boost::gregorian::from_string("1960/01/01")); // query date
350 OksQuery query(true, new OksComparator(PersonBirthday, new OksData(aDate), OksQuery::greater_cmp));
351
352 // executes query
353
354 std::cout << "[QUERY]: Looking for persons were born after " << aDate << " ...\n\n";
355
356 OksObject::List* queryResult = Person->execute_query(&query);
357
358 // builds iterator over results if something was found
359
360 if (queryResult) {
361 std::cout << "[QUERY]: Query \'" << query << "\'\n founds the following objects in class \'"
362 << Person->get_name() << "\' and subclasses:\n";
363
364 for (OksObject::List::iterator i = queryResult->begin(); i != queryResult->end(); ++i) {
365 OksObject* o = *i;
366 OksData* d(o->GetAttributeValue("Birthday"));
367 std::cout << " - " << o << " was born " << *d << std::endl;
368 }
369
370 // free list: we do not need it anymore
371
372 delete queryResult;
373 }
374
375 std::cout << "[QUERY]: Done with simple database querying\n\n";
376 }
377
378 {
379 std::cout << "[QUERY]: Start database querying with logical function...\n";
380
381 // Looking for persons were born after 01 January 1960
382 // and before 01 January 1970
383
384 boost::gregorian::date lowDate(boost::gregorian::from_string("1960/01/01")); // low date
385 boost::gregorian::date highDate(boost::gregorian::from_string("1970/01/01")); // high date
386
387 OksAndExpression* andExpression = new OksAndExpression();
388
389 andExpression->add(new OksComparator(PersonBirthday, new OksData(lowDate), OksQuery::greater_cmp));
390 andExpression->add(new OksComparator(PersonBirthday, new OksData(highDate), OksQuery::less_cmp));
391
392 OksQuery query(true, andExpression);
393
394 // executes query
395
396 std::cout << "[QUERY]: Looking for persons were born between " << lowDate << " and " << highDate << " ...\n\n";
397
398 OksObject::List* queryResult = Person->execute_query(&query);
399
400 // builds iterator over results if something was found
401
402 if (queryResult) {
403 std::cout << "[QUERY]: Query \'" << query << "\'\n founds the following objects in class \'"
404 << Person->get_name() << "\' and subclasses:\n";
405
406 for (OksObject::List::iterator i = queryResult->begin(); i != queryResult->end(); ++i) {
407 OksObject* o = *i;
408 OksData* d(o->GetAttributeValue("Birthday"));
409 std::cout << " - " << o << " was born " << *d << std::endl;
410 }
411
412 // free list: we do not need it anymore
413
414 delete queryResult;
415 }
416
417 std::cout << "[QUERY]: Done database querying with logical function\n\n";
418 }
419
420 {
421
422 std::cout << "[QUERY]: Start database querying with relationship expression...\n";
423
424 // Looking for employee were born after 01 January 1971
425 // and which works at IT Department
426
427 boost::gregorian::date aDate(boost::gregorian::from_string("1971/01/01")); // a date
428 const char* departmentName = "IT Department";
429
430 OksAndExpression* andExpression = new OksAndExpression();
431
432 andExpression->add(new OksComparator(PersonBirthday, new OksData(aDate), OksQuery::greater_cmp));
433 andExpression->add(new OksRelationshipExpression(
434 WorksAt, new OksComparator(DepartmentName, new OksData(departmentName), OksQuery::equal_cmp)));
435
436 OksQuery query(true, andExpression);
437
438 // executes query
439
440 std::cout << "[QUERY]: Looking for employee were born after " << aDate << " and works at department "
441 << departmentName << " ...\n\n";
442
443 OksObject::List* queryResult = Employee->execute_query(&query);
444
445 // builds iterator over results if something was found
446
447 if (queryResult) {
448 std::cout << "[QUERY]: Query \'" << query << "\'\n founds the following objects in class \'"
449 << Employee->get_name() << "\' and subclasses:\n";
450
451 for (OksObject::List::iterator i = queryResult->begin(); i != queryResult->end(); ++i) {
452 OksObject* o = *i;
453 OksData* d(o->GetAttributeValue("Birthday"));
454 std::cout << " - " << o << " was born " << *d << std::endl;
455 }
456
457 // free list: we do not need it anymore
458
459 delete queryResult;
460 }
461
462 std::cout << "[QUERY]: Done database querying with relationship expression...\n";
463 }
464
465 std::cout << "\n[OKS TUTORIAL]: Done with database querying tests\n\n";
466
467 // It is not necessary to free data or schema because all instances of OKS
468 // classes are automatic C++ objects and they will deleted before last
469 // close bracket `}`.
470
471 } catch (const std::exception& ex) {
472 std::cerr << "Caught exception:\n" << ex.what() << std::endl;
473 }
474
475 // returns success
476
477 return 0;
478}
OKS query logical AND expression class.
Definition query.hpp:272
OKS attribute class.
Definition attribute.hpp:94
static const char * u32_int_type
static const char * string_type
static const char * enum_type
OksAttribute(const std::string &name, OksClass *p=nullptr)
OKS attribute simple constructor.
static const char * date_type
The OKS class.
Definition class.hpp:223
void add_super_class(const std::string &name)
Definition class.cpp:840
void add(OksAttribute *a)
Add attribute.
Definition class.cpp:1009
const std::string & get_name() const noexcept
Definition class.hpp:374
OksClass(const std::string &name, OksKernel *kernel, bool transient=false)
Create OKS class.
Definition class.cpp:81
OksObject::List * execute_query(OksQuery *query) const
Execute query.
Definition query.cpp:446
OksComparator(const OksAttribute *a, OksData *v, OksQuery::Comparator f)
Definition query.hpp:144
Provides interface to the OKS XML schema and data files.
Definition file.hpp:355
void add_include_file(const std::string &name)
Add include file.
Definition file.cpp:1157
Provides interface to the OKS kernel.
Definition kernel.hpp:615
void add(OksQueryExpression *q)
Definition query.hpp:258
OksObject describes instance of OksClass.
Definition object.hpp:1144
void AddRelationshipValue(const std::string &name, OksObject *object)
Add object value to multi-value relationship by name.
Definition object.cpp:2266
std::list< OksObject * > List
Definition object.hpp:1179
void SetRelationshipValue(const std::string &name, OksData *data, bool skip_non_null_check=false)
Set value of relationship by name.
Definition object.cpp:2125
OksObject(const OksClass *oks_class, const char *object_id=0, bool skip_init=false)
OKS object constructor.
Definition object.cpp:1151
OksData * GetAttributeValue(const std::string &name) const
Get value of attribute by name.
Definition object.cpp:1852
OKS query class.
Definition query.hpp:40
static bool equal_cmp(const OksData *, const OksData *)
Definition query.cpp:56
static bool greater_cmp(const OksData *, const OksData *)
Definition query.cpp:81
static bool less_cmp(const OksData *, const OksData *)
Definition query.cpp:76
OksRelationshipExpression(const OksRelationship *r, OksQueryExpression *q, bool b=false)
Definition query.hpp:189
OksRelationship(const std::string &name, OksClass *p=nullptr)
OKS relationship simple constructor.
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)
Struct OKS data.
Definition object.hpp:510

◆ printDepartment()

void printDepartment ( const OksObject * o)

Definition at line 151 of file oks_tutorial.cxx.

153{
154 OksData *name(o->GetAttributeValue("Name")), // is used to store 'Staff'
155 *staff(o->GetRelationshipValue("Staff")); // is used to store 'Name'
156
157 std::cout << "Object " << o
158 << "\n"
159 " Name: "
160 << *name
161 << " \n"
162 " Staff: \""
163 << *staff << "\"\n";
164}
OksData * GetRelationshipValue(const std::string &) const
Get value of relationship by name.
Definition object.cpp:1944

◆ printEmployee()

void printEmployee ( const OksObject * o)

Definition at line 115 of file oks_tutorial.cxx.

117{
118 // we can use printPerson() because 'Employee' class
119 // is derived from 'Person' class
120
121 printPerson(o);
122
123 OksData *department(o->GetRelationshipValue("Works at")), // is used to store 'Works at'
124 *salary(o->GetAttributeValue("Salary")); // is used to store 'salary'
125
126 std::cout << " Salary: " << *salary
127 << " \n"
128 " Works at: \""
129 << department->data.OBJECT->GetId() << "\"\n";
130}

◆ printPerson()

void printPerson ( const OksObject * o)

Definition at line 70 of file oks_tutorial.cxx.

72{
73 OksData* name(o->GetAttributeValue("Name")); // is used to store 'Name'
74 OksData* birthday(o->GetAttributeValue("Birthday")); // is used to store 'Birthday'
75 OksData* family(o->GetAttributeValue("Family_Situation")); // is used to store 'Family_Situation'
76
77 std::cout << "Object " << o
78 << " \n"
79 " Name: "
80 << *name
81 << " \n"
82 " Birthday: \'"
83 << *birthday
84 << "\" \n"
85 " Family_Situation: "
86 << *family << std::endl;
87}

◆ setDepartmentValues()

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

Definition at line 137 of file oks_tutorial.cxx.

140{
141 OksData d(name); // creates OKS data with string 'name'
142
143 o->SetAttributeValue("Name", &d);
144}
void SetAttributeValue(const std::string &name, OksData *data)
Set value of attribute by name.
Definition object.cpp:1930

◆ setEmployeeValues()

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

Definition at line 94 of file oks_tutorial.cxx.

100{
101 // we can use setPersonValues() because 'Employee' class
102 // derived from 'Person' class
103
104 setPersonValues(o, name, birthday, familySituation);
105
106 OksData d(salary); // creates OKS data with ulong 'salary'
107 o->SetAttributeValue("Salary", &d);
108}

◆ setPersonValues()

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

Definition at line 46 of file oks_tutorial.cxx.

51{
52 OksData d; // creates OKS data with unknown type
53
54 d.Set(name); // sets OKS data to string 'name'
55 o->SetAttributeValue("Name", &d);
56
57 d.Set(birthday); // sets OKS data to date 'birthday'
58 o->SetAttributeValue("Birthday", &d);
59
60 d.Set(familySituation); // sets OKS data to 'familySituation'
61 d.type = OksData::enum_type; // sets OKS data type to enumeration
62 o->SetAttributeValue("Family_Situation", &d);
63}