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)
 
OksQueryExpressionget () 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 OksQueryExpressioncreate_expression (const OksClass *, const std::string &)
 

Private Attributes

bool p_sub_classes
 
OksQueryExpressionp_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 35 of file query.hpp.

Member Typedef Documentation

◆ Comparator

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

Definition at line 88 of file query.hpp.

Member Enumeration Documentation

◆ QueryType

Enumerator
unknown_type 
comparator_type 
relationship_type 
not_type 
and_type 
or_type 

Definition at line 53 of file query.hpp.

Constructor & Destructor Documentation

◆ OksQuery() [1/2]

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

Definition at line 39 of file query.hpp.

39: p_sub_classes (b), p_expression (q), p_status (0) {};
OksQueryExpression * p_expression
Definition query.hpp:94

◆ OksQuery() [2/2]

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

Definition at line 85 of file query.cpp.

85 : p_expression (0), p_status (1)
86{
88
89 const char * fname = "OksQuery::OksQuery(OksClass *, const char *)";
90 const char * error_str = "Can't create query ";
91
92 char delimiter = '\0';
93
94 if(!c) {
95 Oks::error_msg(fname) << error_str << "without specified class\n";
96 return;
97 }
98
99 if(str.empty()) {
100 Oks::error_msg(fname) << error_str << "from empty string\n";
101 return;
102 }
103
104 std::string s(str);
105
107
108 if(s.empty()) {
109 Oks::error_msg(fname) << error_str << "from string which consists of space symbols\n";
110 return;
111 }
112
113 if(s[0] == '(') {
114 s.erase(0, 1);
115 delimiter = ')';
116 }
117
118 std::string::size_type p = s.find(' ');
119
120 if(p == std::string::npos) {
121 Oks::error_msg(fname)
122 << "Can't parse query expression \"" << str << "\"\n"
123 "it must consists of as minimum two tokens separated by space\n";
124 return;
125 }
126
127 if(s.substr(0, p) == OksQuery::ALL_SUBCLASSES)
128 p_sub_classes = true;
129 else if(s.substr(0, p) == OksQuery::THIS_CLASS)
130 p_sub_classes = false;
131 else {
132 Oks::error_msg(fname)
133 << "Can't parse query expression \"" << str << "\"\n"
134 "the first token must be \'"<< OksQuery::ALL_SUBCLASSES
135 << "\' or \'"<< OksQuery::THIS_CLASS << "\'\n";
136 return;
137 }
138
139 s.erase(0, p + 1);
140
141 if(delimiter == ')') {
142 p = s.rfind(delimiter);
143 if(p == std::string::npos) {
144 Oks::error_msg(fname)
145 << "Can't parse query expression \"" << str << "\"\n"
146 "it must contain closing bracket \')\' if it has opening bracket \'(\'\n";
147 return;
148 }
149
150 s.erase(p);
151 }
152
153
155
156
157 if(s[0] == '(') {
158 p = s.rfind(')');
159
160 if(p == std::string::npos) {
161 Oks::error_msg(fname)
162 << "Can't parse query expression \"" << s << "\"\n"
163 "it must contain closing bracket \')\' if it has opening bracket \'(\'\n";
164 return;
165 }
166
167 s.erase(p);
168 s.erase(0, 1);
169
171
172 if(p_expression) p_status = 0;
173 }
174 else
175 Oks::error_msg(fname)
176 << "Can't parse subquery expression \"" << s << "\"\n"
177 "it must be enclosed by brackets\n";
178}
static const char * ALL_SUBCLASSES
Definition query.hpp:67
static const char * THIS_CLASS
Definition query.hpp:66
OksQuery(bool b, OksQueryExpression *q=0)
Definition query.hpp:39
static OksQueryExpression * create_expression(const OksClass *, const std::string &)
Definition query.cpp:182
static std::ostream & error_msg(const char *)
Definition kernel.cpp:556
#define OSK_PROFILING(FID, K)
Definition defs.hpp:97
void erase_empty_chars(std::string &s)
Definition query.cpp:79

◆ ~OksQuery()

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

Definition at line 303 of file query.hpp.

303{delete p_expression;}

Member Function Documentation

◆ create_expression()

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

Definition at line 182 of file query.cpp.

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

◆ equal_cmp()

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

Definition at line 51 of file query.cpp.

51{return (*d1 == *d2);}

◆ get()

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

Definition at line 48 of file query.hpp.

48{return p_expression;}

◆ good()

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

Definition at line 51 of file query.hpp.

51{return (p_status == 0);}

◆ greater_cmp()

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

Definition at line 56 of file query.cpp.

56{return (*d1 > *d2);}

◆ greater_or_equal_cmp()

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

Definition at line 54 of file query.cpp.

54{return (*d1 >= *d2);}

◆ less_cmp()

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

Definition at line 55 of file query.cpp.

55{return (*d1 < *d2);}

◆ less_or_equal_cmp()

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

Definition at line 53 of file query.cpp.

53{return (*d1 <= *d2);}

◆ not_equal_cmp()

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

Definition at line 52 of file query.cpp.

52{return (*d1 != *d2);}

◆ reg_exp_cmp()

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

Definition at line 57 of file query.cpp.

57 {
58 return boost::regex_match(d->str(), *reinterpret_cast<const boost::regex *>(re));
59}

◆ search_in_subclasses() [1/2]

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

Definition at line 45 of file query.hpp.

45{return p_sub_classes;}

◆ search_in_subclasses() [2/2]

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

Definition at line 46 of file query.hpp.

46{p_sub_classes = b;}

◆ set()

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

Definition at line 49 of file query.hpp.

49{p_expression = q;}

Friends And Related Symbol Documentation

◆ operator<<

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

Definition at line 891 of file query.cpp.

893{
894 s << '('
895 << (gqe.p_sub_classes ? OksQuery::ALL_SUBCLASSES : OksQuery::THIS_CLASS)
896 << ' ';
897
898 if(gqe.p_expression)
899 s << *gqe.p_expression;
900 else
901 s << "(null)";
902
903 s << ')';
904
905 return s;
906}

Member Data Documentation

◆ ALL_SUBCLASSES

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

Definition at line 67 of file query.hpp.

◆ AND

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

Definition at line 63 of file query.hpp.

◆ DIRECT

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

Definition at line 77 of file query.hpp.

◆ EQ

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

Definition at line 69 of file query.hpp.

◆ GE

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

Definition at line 73 of file query.hpp.

◆ GT

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

Definition at line 75 of file query.hpp.

◆ LE

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

Definition at line 72 of file query.hpp.

◆ LS

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

Definition at line 74 of file query.hpp.

◆ NE

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

Definition at line 70 of file query.hpp.

◆ NESTED

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

Definition at line 78 of file query.hpp.

◆ NOT

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

Definition at line 64 of file query.hpp.

◆ OID

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

Definition at line 68 of file query.hpp.

◆ OR

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

Definition at line 62 of file query.hpp.

◆ p_expression

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

Definition at line 94 of file query.hpp.

◆ p_status

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

Definition at line 95 of file query.hpp.

◆ p_sub_classes

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

Definition at line 93 of file query.hpp.

◆ PATH_TO

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

Definition at line 76 of file query.hpp.

◆ RE

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

Definition at line 71 of file query.hpp.

◆ SOME

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

Definition at line 65 of file query.hpp.

◆ THIS_CLASS

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

Definition at line 66 of file query.hpp.


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