Lumiera 0.pre.04
»edit your freedom«
Loading...
Searching...
No Matches
query-util.cpp
Go to the documentation of this file.
1/*
2 QueryUtil - support for working with terms and queries
3
4 Copyright (C)
5 2008, 2012 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
20#include "lib/error.hpp"
21#include "lib/query-util.hpp"
22#include "lib/util.hpp"
23
24#include <boost/algorithm/string.hpp>
25#include <functional>
26#include <regex>
27#include <map>
28
29using std::map;
30using std::regex;
31using std::smatch;
32using std::regex_search;
33using std::sregex_iterator;
34
35using util::contains;
36using util::isnil;
37
38
39namespace lib {
40 namespace query {
41
42 namespace { // local definitions
43
44 using ChPredicate = std::function<bool(string::value_type)> ;
45
46 ChPredicate is_alpha = boost::algorithm::is_alpha();
47 ChPredicate is_upper = boost::algorithm::is_upper();
48 } // local defs
49
50
51 void
52 normaliseID (string& id)
53 {
54 id = util::sanitise(id);
55 if (isnil(id) or not is_alpha (id[0]))
56 id.insert(0, "o");
57
58
59 REQUIRE (!isnil(id));
60 REQUIRE (is_alpha (id[0]));
61
62 char first = id[0];
63 if (is_upper (first))
64 id[0] = std::tolower (first);
65 }
66
67
68
70 namespace{ // Implementation details
71
72 map<Symbol, regex> regexTable;
73
74 Literal MATCH_ARGUMENT = R"~(\‍(\s*([\w_\.\-]+)\s*\),?\s*)~";
75 const regex FIND_PREDICATE{string{"(\\w+)"} + MATCH_ARGUMENT};
76
77 inline regex&
79 {
80 if (!contains (regexTable, sym))
81 regexTable[sym] = regex (string(sym)+MATCH_ARGUMENT);
82 return regexTable[sym];
83 }
84 }
85
91 string
92 extractID (Symbol sym, const string& termString)
93 {
94 smatch match;
95 if (regex_search (termString, match, getTermRegex (sym)))
96 return (match[1]);
97 else
98 return "";
99 }
100
101
108 string
109 removeTerm (Symbol sym, string& queryString)
110 {
111 smatch match;
112 if (regex_search (queryString, match, getTermRegex (sym)))
113 {
114 string res (sym); res += "("+match[1]+")";
115 queryString.erase (match.position(), match[0].length());
116 return res;
117 }
118 else
119 return "";
120 }
121
122
123 bool
124 hasTerm (Symbol sym, string const& queryString)
125 {
126 smatch match;
127 return regex_search (queryString, match, getTermRegex (sym));
128 }
129
130
136 uint
137 countPred (const string& q)
138 {
139 uint cnt (0);
140 sregex_iterator end;
141 for (sregex_iterator i (q.begin(),q.end(), FIND_PREDICATE);
142 i != end; ++i)
143 ++cnt;
144 return cnt;
145 }
146
147
151 string
152 appendTerms (string const& pred1, string const& pred2)
153 {
154 return isnil(pred1)? pred2
155 : isnil(pred2)? pred1
156 : pred1 + ", " + pred2;
157 }
158
159
160 } // namespace query
161
162} // namespace lib
Inline string literal.
Definition symbol.hpp:78
Token or Atom with distinct identity.
Definition symbol.hpp:120
Lumiera error handling (C++ interface).
unsigned int uint
Definition integral.hpp:29
std::function< bool(string::value_type)> ChPredicate
bool hasTerm(Symbol sym, string const &queryString)
string appendTerms(string const &pred1, string const &pred2)
void normaliseID(string &id)
ensure standard format for a given id string.
string extractID(Symbol sym, const string &termString)
(preliminary) helper: instead of really parsing and evaluating the terms, just do a regular expressio...
uint countPred(const string &q)
count the top-level predicates in the query string.
string removeTerm(Symbol sym, string &queryString)
(preliminary) helper: cut a term with the given symbol.
Implementation namespace for support and library code.
std::string sanitise(std::string const &)
produce an identifier based on the given string.
Definition util.cpp:57
bool contains(MAP &map, typename MAP::key_type const &key)
shortcut for containment test on a map
Definition util.hpp:230
bool isnil(lib::time::Duration const &dur)
Utilities to support working with predicate queries.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...