DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
dunedaq::oks::OksQuery Class Reference

OKS query class. More...

#include <query.hpp>

Collaboration diagram for dunedaq::oks::OksQuery:
[legend]

Public Types

enum  QueryType {
  unknown_type , comparator_type , relationship_type , not_type ,
  and_type , or_type
}
typedef bool(*) Comparator(const OksData *, const OksData *)

Public Member Functions

 OksQuery (bool b, OksQueryExpression *q=0)
 OksQuery (const OksClass *, const std::string &)
virtual ~OksQuery ()
bool search_in_subclasses () const
void search_in_subclasses (bool b)
OksQueryExpression * get () const
void set (OksQueryExpression *q)
bool good () const

Static Public Member Functions

static bool equal_cmp (const OksData *, const OksData *)
static bool not_equal_cmp (const OksData *, const OksData *)
static bool less_or_equal_cmp (const OksData *, const OksData *)
static bool greater_or_equal_cmp (const OksData *, const OksData *)
static bool less_cmp (const OksData *, const OksData *)
static bool greater_cmp (const OksData *, const OksData *)
static bool reg_exp_cmp (const OksData *, const OksData *regexp)

Static Public Attributes

static const char * OR = "or"
static const char * AND = "and"
static const char * NOT = "not"
static const char * SOME = "some"
static const char * THIS_CLASS = "this"
static const char * ALL_SUBCLASSES = "all"
static const char * OID = "object-id"
static const char * EQ = "="
static const char * NE = "!="
static const char * RE = "~="
static const char * LE = "<="
static const char * GE = ">="
static const char * LS = "<"
static const char * GT = ">"
static const char * PATH_TO = "path-to"
static const char * DIRECT = "direct"
static const char * NESTED = "nested"

Static Private Member Functions

static OksQueryExpression * create_expression (const OksClass *, const std::string &)

Private Attributes

bool p_sub_classes
OksQueryExpression * p_expression
int p_status

Friends

std::ostream & operator<< (std::ostream &s, const OksQuery &gqe)

Detailed Description

OKS query class.

The class implements OKS query.
A query can be executed over some class (and optionally subclasses)

with given query expression. A query expression can be constructed dynamically for given query expression or can be read from string.

Definition at line 39 of file query.hpp.

Member Typedef Documentation

◆ Comparator

typedef bool(*) dunedaq::oks::OksQuery::Comparator(const OksData *, const OksData *)

Definition at line 95 of file query.hpp.

Member Enumeration Documentation

◆ QueryType

Enumerator
unknown_type 
comparator_type 
relationship_type 
not_type 
and_type 
or_type 

Definition at line 59 of file query.hpp.

Constructor & Destructor Documentation

◆ OksQuery() [1/2]

dunedaq::oks::OksQuery::OksQuery ( bool b,
OksQueryExpression * q = 0 )
inline

Definition at line 42 of file query.hpp.

43 : p_sub_classes(b)
44 , p_expression(q)
45 , p_status(0) {};
OksQueryExpression * p_expression
Definition query.hpp:99

◆ OksQuery() [2/2]

dunedaq::oks::OksQuery::OksQuery ( const OksClass * c,
const std::string & str )

Definition at line 116 of file query.cpp.

117 : p_expression(0)
118 , p_status(1)
119{
121
122 const char* fname = "OksQuery::OksQuery(OksClass *, const char *)";
123 const char* error_str = "Can't create query ";
124
125 char delimiter = '\0';
126
127 if (!c) {
128 Oks::error_msg(fname) << error_str << "without specified class\n";
129 return;
130 }
131
132 if (str.empty()) {
133 Oks::error_msg(fname) << error_str << "from empty string\n";
134 return;
135 }
136
137 std::string s(str);
138
140
141 if (s.empty()) {
142 Oks::error_msg(fname) << error_str << "from string which consists of space symbols\n";
143 return;
144 }
145
146 if (s[0] == '(') {
147 s.erase(0, 1);
148 delimiter = ')';
149 }
150
151 std::string::size_type p = s.find(' ');
152
153 if (p == std::string::npos) {
154 Oks::error_msg(fname) << "Can't parse query expression \"" << str
155 << "\"\n"
156 "it must consists of as minimum two tokens separated by space\n";
157 return;
158 }
159
160 if (s.substr(0, p) == OksQuery::ALL_SUBCLASSES)
161 p_sub_classes = true;
162 else if (s.substr(0, p) == OksQuery::THIS_CLASS)
163 p_sub_classes = false;
164 else {
165 Oks::error_msg(fname) << "Can't parse query expression \"" << str
166 << "\"\n"
167 "the first token must be \'"
168 << OksQuery::ALL_SUBCLASSES << "\' or \'" << OksQuery::THIS_CLASS << "\'\n";
169 return;
170 }
171
172 s.erase(0, p + 1);
173
174 if (delimiter == ')') {
175 p = s.rfind(delimiter);
176 if (p == std::string::npos) {
177 Oks::error_msg(fname) << "Can't parse query expression \"" << str
178 << "\"\n"
179 "it must contain closing bracket \')\' if it has opening bracket \'(\'\n";
180 return;
181 }
182
183 s.erase(p);
184 }
185
187
188 if (s[0] == '(') {
189 p = s.rfind(')');
190
191 if (p == std::string::npos) {
192 Oks::error_msg(fname) << "Can't parse query expression \"" << s
193 << "\"\n"
194 "it must contain closing bracket \')\' if it has opening bracket \'(\'\n";
195 return;
196 }
197
198 s.erase(p);
199 s.erase(0, 1);
200
202
203 if (p_expression)
204 p_status = 0;
205 } else
206 Oks::error_msg(fname) << "Can't parse subquery expression \"" << s
207 << "\"\n"
208 "it must be enclosed by brackets\n";
209}
static const char * ALL_SUBCLASSES
Definition query.hpp:74
static const char * THIS_CLASS
Definition query.hpp:73
static OksQueryExpression * create_expression(const OksClass *, const std::string &)
Definition query.cpp:212
static std::ostream & error_msg(const char *)
Definition kernel.cpp:511
#define OSK_PROFILING(FID, K)
Definition defs.hpp:96
void erase_empty_chars(std::string &s)
Definition query.cpp:110

◆ ~OksQuery()

dunedaq::oks::OksQuery::~OksQuery ( )
inlinevirtual

Definition at line 293 of file query.hpp.

294{
295 delete p_expression;
296}

Member Function Documentation

◆ create_expression()

OksQueryExpression * dunedaq::oks::OksQuery::create_expression ( const OksClass * c,
const std::string & str )
staticprivate

Definition at line 212 of file query.cpp.

213{
214 const char* fname = "OksQuery::create_expression()";
215
217
218 OksQueryExpression* qe = 0;
219
220 if (!c) {
221 Oks::error_msg(fname) << "Can't create query without specified class\n";
222 return qe;
223 }
224
225 if (str.empty()) {
226 Oks::error_msg(fname) << "Can't create query from empty string\n";
227 return qe;
228 }
229
230 std::string s(str);
231
232 std::list<std::string> slist;
233
234 while (s.length()) {
236
237 if (!s.length())
238 break;
239
240 if (s[0] == '\"' || s[0] == '\'' || s[0] == '`') {
241 char delimiter = s[0];
242 s.erase(0, 1);
243
244 std::string::size_type p = s.find(delimiter);
245
246 if (p == std::string::npos) {
247 Oks::error_msg(fname) << "Can't parse query expression \"" << str
248 << "\"\n"
249 "the delimiter is \' "
250 << delimiter
251 << " \'\n"
252 "the rest of the expression is \""
253 << s << "\"\n";
254 return qe;
255 }
256
257 s.erase(p, 1);
258 slist.push_back(std::string(s, 0, p));
259
260 s.erase(0, p + 1);
261 } else if (s[0] == '(') {
262 std::string::size_type p = 1;
263 size_t strLength = s.length();
264 size_t r = 1;
265
266 while (p < strLength) {
267 if (s[p] == '(')
268 r++;
269 if (s[p] == ')') {
270 r--;
271 if (!r)
272 break;
273 }
274 p++;
275 }
276
277 if (r) {
278 Oks::error_msg(fname) << "Can't parse query expression \"" << str << "\"\n"
279 << "There is no closing \')\' for " << '\"' << s << "\"\n";
280 return qe;
281 }
282
283 s.erase(p, 1);
284 s.erase(0, 1);
285
286 slist.push_back(std::string(s, 0, p - 1));
287
288 s.erase(0, p - 1);
289 } else {
290 std::string::size_type p = 0;
291 size_t strLength = s.length();
292
293 while (p < strLength && s[p] != ' ')
294 p++;
295
296 slist.push_back(std::string(s, 0, p));
297
298 s.erase(0, p);
299 }
300 }
301
302 if (slist.empty()) {
303 Oks::error_msg(fname) << "Can't create query from empty string \"" << str << "\"\n";
304 return qe;
305 }
306
307 const std::string first = slist.front();
308 slist.pop_front();
309
310 if (first == OksQuery::AND || first == OksQuery::OR) {
311 if (slist.size() < 2) {
312 Oks::error_msg(fname) << "\'" << first << "\' must have two or more arguments: (" << str << ")'\n";
313 return qe;
314 }
315
316 qe = ((first == OksQuery::AND) ? (OksQueryExpression*)new OksAndExpression()
317 : (OksQueryExpression*)new OksOrExpression());
318
319 while (!slist.empty()) {
320 const std::string item2 = slist.front();
321 slist.pop_front();
322
323 OksQueryExpression* qe2 = create_expression(c, item2);
324
325 if (qe2) {
326 if (first == OksQuery::AND)
327 ((OksAndExpression*)qe)->add(qe2);
328 else
329 ((OksOrExpression*)qe)->add(qe2);
330 }
331 }
332
333 return qe; /* SUCCESS */
334 } else if (first == OksQuery::NOT) {
335 if (slist.size() != 1) {
336 Oks::error_msg(fname) << "\'" << first << "\' must have exactly one argument: (" << str << ")\n";
337 return qe;
338 }
339
340 qe = (OksQueryExpression*)new OksNotExpression();
341
342 const std::string item2 = slist.front();
343 slist.pop_front();
344
345 OksQueryExpression* qe2 = create_expression(c, item2);
346
347 if (qe2)
348 ((OksNotExpression*)qe)->set(qe2);
349
350 return qe; /* SUCCESS */
351 } else if (slist.size() != 2) {
352 Oks::error_msg(fname) << "Can't parse query expression \"" << str << "\"\n";
353 return qe;
354 } else {
355 const std::string second = slist.front();
356 slist.pop_front();
357
358 const std::string third = slist.front();
359 slist.pop_front();
360
361 if (second == OksQuery::SOME || second == OksQuery::ALL_SUBCLASSES) {
362 OksRelationship* r = c->find_relationship(first);
363
364 if (!r) {
365 Oks::error_msg(fname) << "For expression \"" << str
366 << "\"\n"
367 "can't find relationship \""
368 << first << "\" in class \"" << c->get_name() << "\"\n";
369
370 return qe;
371 }
372
373 bool b;
374
375 if (second == OksQuery::SOME)
376 b = false;
377 else if (second == OksQuery::ALL_SUBCLASSES)
378 b = true;
379 else {
380 Oks::error_msg(fname) << "For relationship expression \"" << str
381 << "\"\n"
382 "second parameter \'"
383 << second << "\' must be \'" << *OksQuery::SOME << "\' or \'" << *OksQuery::ALL_SUBCLASSES
384 << "\'\n";
385 return qe;
386 }
387
388 OksClass* relc = c->get_kernel()->find_class(r->get_type());
389
390 if (!relc) {
391 Oks::error_msg(fname) << "For expression \"" << str << "\"\n"
392 << "can't find class \"" << r->get_type() << "\"\n";
393 return qe;
394 }
395
396 OksQueryExpression* qe2 = create_expression(relc, third);
397
398 if (qe2)
399 qe = (OksQueryExpression*)new OksRelationshipExpression(r, qe2, b);
400
401 return qe; /* SUCCESS */
402 } else {
403 OksAttribute* a = ((first != OksQuery::OID) ? c->find_attribute(first) : 0);
404
405 if (first != OksQuery::OID && !a) {
406 Oks::error_msg(fname) << "For expression \"" << str << "\"\n"
407 << "can't find attribute \"" << first << "\" in class \"" << c->get_name() << "\"\n";
408 return qe;
409 }
410
411 OksData* d = new OksData();
412
418 : (third == OksQuery::LS) ? OksQuery::less_cmp
420 : 0);
421
422 if (a) {
423 if (f == OksQuery::reg_exp_cmp) {
424 d->type = OksData::string_type;
425 d->data.STRING = new OksString(second);
426 } else {
427 d->type = OksData::unknown_type;
428 d->SetValues(second.c_str(), a);
429 }
430 } else {
431 d->Set(second);
432 }
433
434 if (!f)
435 Oks::error_msg(fname) << "For expression \"" << str << "\"\n"
436 << "can't find comparator function \"" << third << "\"\n";
437 else
438 qe = (OksQueryExpression*)new OksComparator(a, d, f);
439
440 return qe; /* (UN)SUCCESS */
441 }
442 }
443}
OksComparator(const OksAttribute *a, OksData *v, OksQuery::Comparator f)
Definition query.hpp:144
OksNotExpression(OksQueryExpression *q=0)
Definition query.hpp:225
static bool equal_cmp(const OksData *, const OksData *)
Definition query.cpp:56
static bool reg_exp_cmp(const OksData *, const OksData *regexp)
Definition query.cpp:86
bool(*) Comparator(const OksData *, const OksData *)
Definition query.hpp:95
static bool greater_cmp(const OksData *, const OksData *)
Definition query.cpp:81
static const char * OR
Definition query.hpp:69
static const char * RE
Definition query.hpp:78
static const char * OID
Definition query.hpp:75
static bool less_or_equal_cmp(const OksData *, const OksData *)
Definition query.cpp:66
static bool not_equal_cmp(const OksData *, const OksData *)
Definition query.cpp:61
static const char * GT
Definition query.hpp:82
static const char * NOT
Definition query.hpp:71
static const char * SOME
Definition query.hpp:72
static const char * GE
Definition query.hpp:80
static const char * AND
Definition query.hpp:70
static bool greater_or_equal_cmp(const OksData *, const OksData *)
Definition query.cpp:71
static const char * EQ
Definition query.hpp:76
static bool less_cmp(const OksData *, const OksData *)
Definition query.cpp:76
static const char * LE
Definition query.hpp:79
static const char * NE
Definition query.hpp:77
static const char * LS
Definition query.hpp:81
OksRelationshipExpression(const OksRelationship *r, OksQueryExpression *q, bool b=false)
Definition query.hpp:189
OksString()
to deallocate memory when destroyed
Definition object.hpp:421

◆ equal_cmp()

bool dunedaq::oks::OksQuery::equal_cmp ( const OksData * d1,
const OksData * d2 )
static

Definition at line 56 of file query.cpp.

57{
58 return (*d1 == *d2);
59}

◆ get()

OksQueryExpression * dunedaq::oks::OksQuery::get ( ) const
inline

Definition at line 54 of file query.hpp.

54{ return p_expression; }

◆ good()

bool dunedaq::oks::OksQuery::good ( ) const
inline

Definition at line 57 of file query.hpp.

57{ return (p_status == 0); }

◆ greater_cmp()

bool dunedaq::oks::OksQuery::greater_cmp ( const OksData * d1,
const OksData * d2 )
static

Definition at line 81 of file query.cpp.

82{
83 return (*d1 > *d2);
84}

◆ greater_or_equal_cmp()

bool dunedaq::oks::OksQuery::greater_or_equal_cmp ( const OksData * d1,
const OksData * d2 )
static

Definition at line 71 of file query.cpp.

72{
73 return (*d1 >= *d2);
74}

◆ less_cmp()

bool dunedaq::oks::OksQuery::less_cmp ( const OksData * d1,
const OksData * d2 )
static

Definition at line 76 of file query.cpp.

77{
78 return (*d1 < *d2);
79}

◆ less_or_equal_cmp()

bool dunedaq::oks::OksQuery::less_or_equal_cmp ( const OksData * d1,
const OksData * d2 )
static

Definition at line 66 of file query.cpp.

67{
68 return (*d1 <= *d2);
69}

◆ not_equal_cmp()

bool dunedaq::oks::OksQuery::not_equal_cmp ( const OksData * d1,
const OksData * d2 )
static

Definition at line 61 of file query.cpp.

62{
63 return (*d1 != *d2);
64}

◆ reg_exp_cmp()

bool dunedaq::oks::OksQuery::reg_exp_cmp ( const OksData * d,
const OksData * regexp )
static

Definition at line 86 of file query.cpp.

87{
88 return boost::regex_match(d->str(), *reinterpret_cast<const boost::regex*>(re));
89}

◆ search_in_subclasses() [1/2]

bool dunedaq::oks::OksQuery::search_in_subclasses ( ) const
inline

Definition at line 51 of file query.hpp.

51{ return p_sub_classes; }

◆ search_in_subclasses() [2/2]

void dunedaq::oks::OksQuery::search_in_subclasses ( bool b)
inline

Definition at line 52 of file query.hpp.

52{ p_sub_classes = b; }

◆ set()

void dunedaq::oks::OksQuery::set ( OksQueryExpression * q)
inline

Definition at line 55 of file query.hpp.

55{ p_expression = q; }

◆ operator<<

std::ostream & operator<< ( std::ostream & s,
const OksQuery & gqe )
friend

Definition at line 881 of file query.cpp.

883{
884 s << '(' << (gqe.p_sub_classes ? OksQuery::ALL_SUBCLASSES : OksQuery::THIS_CLASS) << ' ';
885
886 if (gqe.p_expression)
887 s << *gqe.p_expression;
888 else
889 s << "(null)";
890
891 s << ')';
892
893 return s;
894}

Member Data Documentation

◆ ALL_SUBCLASSES

const char * dunedaq::oks::OksQuery::ALL_SUBCLASSES = "all"
static

Definition at line 74 of file query.hpp.

◆ AND

const char * dunedaq::oks::OksQuery::AND = "and"
static

Definition at line 70 of file query.hpp.

◆ DIRECT

const char * dunedaq::oks::OksQuery::DIRECT = "direct"
static

Definition at line 84 of file query.hpp.

◆ EQ

const char * dunedaq::oks::OksQuery::EQ = "="
static

Definition at line 76 of file query.hpp.

◆ GE

const char * dunedaq::oks::OksQuery::GE = ">="
static

Definition at line 80 of file query.hpp.

◆ GT

const char * dunedaq::oks::OksQuery::GT = ">"
static

Definition at line 82 of file query.hpp.

◆ LE

const char * dunedaq::oks::OksQuery::LE = "<="
static

Definition at line 79 of file query.hpp.

◆ LS

const char * dunedaq::oks::OksQuery::LS = "<"
static

Definition at line 81 of file query.hpp.

◆ NE

const char * dunedaq::oks::OksQuery::NE = "!="
static

Definition at line 77 of file query.hpp.

◆ NESTED

const char * dunedaq::oks::OksQuery::NESTED = "nested"
static

Definition at line 85 of file query.hpp.

◆ NOT

const char * dunedaq::oks::OksQuery::NOT = "not"
static

Definition at line 71 of file query.hpp.

◆ OID

const char * dunedaq::oks::OksQuery::OID = "object-id"
static

Definition at line 75 of file query.hpp.

◆ OR

const char * dunedaq::oks::OksQuery::OR = "or"
static

Definition at line 69 of file query.hpp.

◆ p_expression

OksQueryExpression* dunedaq::oks::OksQuery::p_expression
private

Definition at line 99 of file query.hpp.

◆ p_status

int dunedaq::oks::OksQuery::p_status
private

Definition at line 100 of file query.hpp.

◆ p_sub_classes

bool dunedaq::oks::OksQuery::p_sub_classes
private

Definition at line 98 of file query.hpp.

◆ PATH_TO

const char * dunedaq::oks::OksQuery::PATH_TO = "path-to"
static

Definition at line 83 of file query.hpp.

◆ RE

const char * dunedaq::oks::OksQuery::RE = "~="
static

Definition at line 78 of file query.hpp.

◆ SOME

const char * dunedaq::oks::OksQuery::SOME = "some"
static

Definition at line 72 of file query.hpp.

◆ THIS_CLASS

const char * dunedaq::oks::OksQuery::THIS_CLASS = "this"
static

Definition at line 73 of file query.hpp.


The documentation for this class was generated from the following files: