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 40 of file query.hpp.

Member Typedef Documentation

◆ Comparator

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

Definition at line 93 of file query.hpp.

Member Enumeration Documentation

◆ QueryType

Enumerator
unknown_type 
comparator_type 
relationship_type 
not_type 
and_type 
or_type 

Definition at line 58 of file query.hpp.

Constructor & Destructor Documentation

◆ OksQuery() [1/2]

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

Definition at line 44 of file query.hpp.

44: p_sub_classes (b), p_expression (q), 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 90 of file query.cpp.

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

◆ ~OksQuery()

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

Definition at line 308 of file query.hpp.

308{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 187 of file query.cpp.

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

◆ equal_cmp()

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

Definition at line 56 of file query.cpp.

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

◆ get()

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

Definition at line 53 of file query.hpp.

53{return p_expression;}

◆ good()

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

Definition at line 56 of file query.hpp.

56{return (p_status == 0);}

◆ greater_cmp()

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

Definition at line 61 of file query.cpp.

61{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 59 of file query.cpp.

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

◆ less_cmp()

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

Definition at line 60 of file query.cpp.

60{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 58 of file query.cpp.

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

◆ not_equal_cmp()

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

Definition at line 57 of file query.cpp.

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

◆ reg_exp_cmp()

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

Definition at line 62 of file query.cpp.

62 {
63 return boost::regex_match(d->str(), *reinterpret_cast<const boost::regex *>(re));
64}

◆ search_in_subclasses() [1/2]

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

Definition at line 50 of file query.hpp.

50{return p_sub_classes;}

◆ search_in_subclasses() [2/2]

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

Definition at line 51 of file query.hpp.

51{p_sub_classes = b;}

◆ set()

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

Definition at line 54 of file query.hpp.

54{p_expression = q;}

◆ operator<<

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

Definition at line 896 of file query.cpp.

898{
899 s << '('
900 << (gqe.p_sub_classes ? OksQuery::ALL_SUBCLASSES : OksQuery::THIS_CLASS)
901 << ' ';
902
903 if(gqe.p_expression)
904 s << *gqe.p_expression;
905 else
906 s << "(null)";
907
908 s << ')';
909
910 return s;
911}

Member Data Documentation

◆ ALL_SUBCLASSES

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

Definition at line 72 of file query.hpp.

◆ AND

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

Definition at line 68 of file query.hpp.

◆ DIRECT

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

Definition at line 82 of file query.hpp.

◆ EQ

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

Definition at line 74 of file query.hpp.

◆ GE

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

Definition at line 78 of file query.hpp.

◆ GT

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

Definition at line 80 of file query.hpp.

◆ LE

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

Definition at line 77 of file query.hpp.

◆ LS

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

Definition at line 79 of file query.hpp.

◆ NE

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

Definition at line 75 of file query.hpp.

◆ NESTED

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

Definition at line 83 of file query.hpp.

◆ NOT

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

Definition at line 69 of file query.hpp.

◆ OID

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

Definition at line 73 of file query.hpp.

◆ OR

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

Definition at line 67 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 81 of file query.hpp.

◆ RE

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

Definition at line 76 of file query.hpp.

◆ SOME

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

Definition at line 70 of file query.hpp.

◆ THIS_CLASS

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

Definition at line 71 of file query.hpp.


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