Lumiera 0.pre.04~rc.1
»edit your freedom«
Loading...
Searching...
No Matches
query.hpp
Go to the documentation of this file.
1/*
2 QUERY.hpp - interface for generic queries
3
4 Copyright (C)
5 2008, Hermann Vosseler <Ichthyostega@web.de>
6
7  **Lumiera** is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License as published by the
9  Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version. See the file COPYING for further details.
11
12*/
13
14
65#ifndef LUMIERA_QUERY_H
66#define LUMIERA_QUERY_H
67
68
69#include "lib/hash-combine.hpp"
70#include "lib/typed-counter.hpp"
71#include "lib/iter-adapter.hpp"
72#include "lib/query-text.hpp"
73#include "lib/query-util.hpp"
74#include "lib/nocopy.hpp"
75#include "lib/symbol.hpp"
76#include "lib/util.hpp"
77
78#include <boost/lexical_cast.hpp>
79#include <typeinfo>
80#include <compare>
81#include <memory>
82#include <cctype>
83#include <string>
84
85
86namespace lumiera {
87
88 using lib::IxID;
89 using lib::Symbol;
90 using lib::Literal;
91 using util::isnil;
92 using util::unConst;
93 using boost::lexical_cast;
94 using std::string;
95
96
97 /* ==== common definitions for rule based queries ==== */
98
99 class Goal;
100 class Resolution;
101 class QueryResolver;
102 class QueryKey;
103
104
106 typedef std::shared_ptr<Resolution> PReso;
107
108
109
116 class Goal
118 {
119 public:
120 virtual ~Goal();
121
128
129 struct QueryID
130 {
132 IxID type;
133
134 explicit
135 QueryID(Kind k =EMPTY, IxID t=1)
136 : kind(k)
137 , type(t)
138 { }
139
140 auto operator<=> (QueryID const&) const =default;
141 };
142
143 QueryID const&
144 getQID() const
145 {
146 return id_;
147 }
148
149
156 class Result
157 {
158 void* cur_;
159
160 protected:
161 void point_at(void* p) { cur_ = p; }
162
163 template<typename RES>
164 RES&
166 {
167 REQUIRE (cur_);
168 return *reinterpret_cast<RES*> (cur_);
169 }
170
171 public:
172 explicit operator bool() const { return isValid(); }
173 bool isValid() const { return bool(cur_); }
174
175 Result() : cur_(0) { }
176 };
177
178
179
180 protected:
182
184 : id_(qid)
185 { }
186
187 };
188
189
190
191
192 namespace {
196
197 template<typename RES>
198 inline IxID
200 {
202 }
203
205 inline size_t
207 {
208 lib::hash::combine (hash, typeID.kind);
209 lib::hash::combine (hash, typeID.type);
210 return hash;
211 }
212 }
213
214
215
216
251 template<class RES>
252 class Query
253 : public Goal
254 {
257
258 protected:
259 static QueryID
261 {
262 QueryID id(queryType, getResultTypeID<RES>());
263 return id;
264 }
265
276 virtual lib::QueryText
278 {
279 WARN (query, "internal query not outfitted with a suitable query definition");
280 return string("bottom");
281 }
282
287 {
288 if (isnil(this->def_))
290 return def_;
291 }
292
293
294 class Builder;
295
296 Query (QueryID typeID, lib::QueryText const& genericQuerySpec)
297 : Goal (typeID)
298 , def_(genericQuerySpec)
299 { }
300
301 Query (QueryID typeID, string querySpec)
302 : Goal (defineQueryTypeID(typeID.kind))
303 , def_(querySpec)
304 {
305 REQUIRE (this->getQID().type == typeID.type);
306 }
307
308 friend class Builder;
309
310
311 public:
314 { }
315
316 explicit
317 Query (string querySpec)
319 , def_(querySpec)
320 { }
321
322 operator QueryKey() const;
323
324 static Builder build (Kind queryType = Goal::GENERIC);
325 Builder rebuild() const;
326
327 string extractID (Symbol predicate) const;
328 bool usesPredicate (Symbol predicate) const;
329
330
331
332 /* results retrieval */
333 class Cursor
334 : public Goal::Result
335 {
336 public:
337 typedef RES value_type;
338 typedef RES& reference;
339 typedef RES* pointer;
340
341 RES& operator* () { return access<RES>(); }
342 RES* operator->() { return & access<RES>(); }
343
346 };
347
348
350
351 iterator operator() (QueryResolver const& resolver) const;
352 iterator resolveBy (QueryResolver const& resolver) const;
353
354
355 friend size_t
356 hash_value (Query const& q)
357 {
358 return taggedHash (hash_value(q.getQueryDefinition()), q.id_);
359 }
360 };
361
362
363
364
371 {
374
375 public:
377 : id_(id)
378 , def_(q)
379 {
380 ENSURE (!isnil(def_));
381 }
382
385 : id_()
386 , def_("false")
387 { }
388
389 // default copyable
390
391 template<class RES>
392 operator Query<RES>() const
393 {
394 REQUIRE (getResultTypeID<RES>() == id_.type);
396 }
397
398 string
399 display() const
400 {
401 return "kind=" + lexical_cast<string>(id_.kind)
402 +",type=" + lexical_cast<string>(id_.type)
403 +",def=" + string(def_);
404 }
405
406 string
408 {
409 return string(def_);
410 }
411
412
413 uint
414 degree() const
415 {
417 }
418
419 bool
420 empty() const
421 {
422 return Goal::EMPTY == id_.kind;
423 }
424
425
426 friend std::strong_ordering
427 operator<=> (QueryKey const& q1, QueryKey const& q2)
428 {
429 uint d1 = q1.degree();
430 uint d2 = q2.degree();
431 if (auto o1 = d1 <=> d2; o1 != 0)
432 return o1;
433 if (auto o2 = q1.def_ <=> q2.def_; o2 != 0)
434 return o2;
435 else
436 return q1.id_ <=> q2.id_;
437 }
438 bool operator== (QueryKey const&) const =default;
439
440 friend size_t
442 {
443 return taggedHash (hash_value(q.def_), q.id_);
444 }
445 };
446
447
448
449
450
455 template<class RES>
456 class Query<RES>::Builder
457 {
460
461 Builder (QueryID kind, string baseDef ="")
462 : typeID_(kind)
463 , predicateForm_(baseDef)
464 { }
465
466 friend class Query<RES>;
467
468
469 public:
472 operator Query<RES>()
473 {
474 return Query<RES>(typeID_, predicateForm_);
475 }
476
477
481 string
482 asKey() const
483 {
484 return "type("
485 + lexical_cast<string> (getResultTypeID<RES>())
486 + "), "+predicateForm_;
487 }
488
489
495 string
496 extractID (Symbol predicate) const
497 {
498 return lib::query::extractID (predicate, this->predicateForm_);
499 }
500
501
506 Builder&
507 removeTerm (Symbol termPredicate)
508 {
509 lib::query::removeTerm(termPredicate, this->predicateForm_);
510 return *this;
511 }
512
513 Builder&
514 withConditions (string additionalQueryPredicates)
515 {
516 this->predicateForm_ =
517 lib::query::appendTerms(this->predicateForm_, additionalQueryPredicates);
518 return *this;
519 }
520
521 Builder&
522 prependConditions (string additionalQueryPredicates)
523 {
524 this->predicateForm_ =
525 lib::query::appendTerms(additionalQueryPredicates, this->predicateForm_);
526 return *this;
527 }
528
529 Builder&
530 fromText (string queryPredicates)
531 {
532 this->predicateForm_ = queryPredicates;
533 return *this;
534 }
535 };
536
537
538
539
540 template<class RES>
543 {
544 return Builder(defineQueryTypeID (queryType));
545 }
546
547
548 template<class RES>
551 {
552 return Builder(this->id_, getQueryDefinition());
553 }
554
555
562 template<class RES>
563 inline string
565 {
566 return this->rebuild().extractID (predicate);
567 }
568
569
570 template<class RES>
571 inline bool
573 {
574 return lib::query::hasTerm(predicate, getQueryDefinition());
575 }
576
577
583 template<class RES>
584 inline
586 {
587 return QueryKey (this->id_, getQueryDefinition());
588 }
589
590
591
592} // namespace lumiera
593#endif
Adapter for building an implementation of the »Lumiera Forward Iterator« concept.
Inline string literal.
Definition symbol.hpp:78
Syntactical query representation.
uint degree_of_constriction() const
synthetic total order to classify query definitions.
Token or Atom with distinct identity.
Definition symbol.hpp:120
Provide type-IDs for a specific context.
Single Solution, possibly part of a result set.
Definition query.hpp:157
void point_at(void *p)
Definition query.hpp:161
bool isValid() const
Definition query.hpp:173
Result()
create an NIL result
Definition query.hpp:175
Query ABC: unspecific goal for resolution or retrieval.
Definition query.hpp:118
virtual ~Goal()
this is a marker baseclass
QueryID const & getQID() const
Definition query.hpp:144
QueryID id_
Definition query.hpp:181
Goal(QueryID qid)
Definition query.hpp:183
Wrapper for indexing and ordering.
Definition query.hpp:371
QueryKey()
the empty or bottom query key
Definition query.hpp:384
friend size_t hash_value(QueryKey const &q)
Definition query.hpp:441
lib::QueryText def_
Definition query.hpp:373
string getQueryString() const
Definition query.hpp:407
friend std::strong_ordering operator<=>(QueryKey const &q1, QueryKey const &q2)
Definition query.hpp:427
bool empty() const
Definition query.hpp:420
uint degree() const
Definition query.hpp:414
string display() const
Definition query.hpp:399
QueryKey(Goal::QueryID id, lib::QueryText q)
Definition query.hpp:376
Goal::QueryID id_
Definition query.hpp:372
bool operator==(QueryKey const &) const =default
Interface: a facility for resolving (some kind of) queries A concrete subclass has the ability to cre...
Helper for establishing, reworking and remolding queries.
Definition query.hpp:457
Builder(QueryID kind, string baseDef="")
Definition query.hpp:461
Builder & prependConditions(string additionalQueryPredicates)
Definition query.hpp:522
Builder & fromText(string queryPredicates)
Definition query.hpp:530
Builder & removeTerm(Symbol termPredicate)
remove the first term from this query definition, which matches the given predicate symbol
Definition query.hpp:507
string asKey() const
Definition query.hpp:482
Builder & withConditions(string additionalQueryPredicates)
Definition query.hpp:514
string extractID(Symbol predicate) const
extract an ID term defined as (single) parameter for the given predicate.
Definition query.hpp:496
void point_at(RES *r)
Definition query.hpp:344
void point_at(RES &r)
Definition query.hpp:345
Generic interface to express a query for specifically typed result elements exposing some capabilitie...
Definition query.hpp:254
iterator operator()(QueryResolver const &resolver) const
notational convenience shortcut, synonymous to Query<RES>::resolveBy()
static Builder build(Kind queryType=Goal::GENERIC)
Definition query.hpp:542
static QueryID defineQueryTypeID(Kind queryType=Goal::GENERIC)
Definition query.hpp:260
Query(QueryID typeID, string querySpec)
Definition query.hpp:301
Query(QueryID typeID, lib::QueryText const &genericQuerySpec)
Definition query.hpp:296
bool usesPredicate(Symbol predicate) const
Definition query.hpp:572
Query(string querySpec)
Definition query.hpp:317
lib::QueryText def_
generic syntactical definition
Definition query.hpp:256
virtual lib::QueryText buildSyntacticRepresentation() const
Extension point to generate a generic query definition on demand.
Definition query.hpp:277
Builder rebuild() const
Definition query.hpp:550
lib::IterAdapter< Cursor, PReso > iterator
Definition query.hpp:349
friend size_t hash_value(Query const &q)
Definition query.hpp:356
string extractID(Symbol predicate) const
convenience shortcut to extract a desired name-ID.
Definition query.hpp:564
lib::QueryText getQueryDefinition() const
access the complete syntactical representation of this query.
Definition query.hpp:286
iterator resolveBy(QueryResolver const &resolver) const
Types marked with this mix-in may be duplicated by copy-construction, yet may not be moved or transfe...
Definition nocopy.hpp:96
Hash combine function extracted from LibBoost 1.67 Combine two hash values to form a composite depend...
#define hash
unsigned int uint
Definition integral.hpp:29
Helper template(s) for creating Lumiera Forward Iterators.
void combine(size_t &combinedHash, size_t additionalHash)
meld the additional hash value into the given base hash value.
bool hasTerm(Symbol sym, string const &queryString)
string appendTerms(string const &pred1, string const &pred2)
string extractID(Symbol sym, const string &termString)
(preliminary) helper: instead of really parsing and evaluating the terms, just do a regular expressio...
string removeTerm(Symbol sym, string &queryString)
(preliminary) helper: cut a term with the given symbol.
size_t IxID
size_t taggedHash(size_t hash, Goal::QueryID typeID)
includes the QueryID type distinction into the given hash value
Definition query.hpp:206
Lumiera public interface.
Definition advice.hpp:102
std::shared_ptr< Resolution > PReso
Allow to take ownership of a result set.
Definition query.hpp:106
OBJ * unConst(const OBJ *)
shortcut to save some typing when having to define const and non-const variants of member functions
Definition util.hpp:358
bool isnil(lib::time::Duration const &dur)
Mix-Ins to allow or prohibit various degrees of copying and cloning.
A generic syntactical representation for all kinds of queries.
Utilities to support working with predicate queries.
QueryID(Kind k=EMPTY, IxID t=1)
Definition query.hpp:135
auto operator<=>(QueryID const &) const =default
Marker types to indicate a literal string and a Symbol.
Creating series of type-based contexts.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...