Lumiera  0.pre.03
»edit your freedom«
query-util.cpp
Go to the documentation of this file.
1 /*
2  QueryUtil - support for working with terms and queries
3 
4  Copyright (C) Lumiera.org
5  2008, 2012 Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
23 
29 #include "lib/error.hpp"
30 #include "lib/query-util.hpp"
31 #include "lib/util.hpp"
32 
33 #include <boost/algorithm/string.hpp>
34 #include <functional>
35 #include <regex>
36 #include <map>
37 
38 using std::map;
39 using std::regex;
40 using std::smatch;
41 using std::regex_search;
42 using std::sregex_iterator;
43 
44 using util::contains;
45 using util::isnil;
46 
47 
48 namespace lib {
49  namespace query {
50 
51  namespace { // local definitions
52 
53  using ChPredicate = std::function<bool(string::value_type)> ;
54 
55  ChPredicate is_alpha = boost::algorithm::is_alpha();
56  ChPredicate is_upper = boost::algorithm::is_upper();
57  } // local defs
58 
59 
60  void
61  normaliseID (string& id)
62  {
63  id = util::sanitise(id);
64  if (isnil(id) || !is_alpha (id[0]))
65  id.insert(0, "o");
66 
67 
68  REQUIRE (!isnil(id));
69  REQUIRE (is_alpha (id[0]));
70 
71  char first = id[0];
72  if (is_upper (first))
73  id[0] = std::tolower (first);
74  }
75 
76 
77 
79  namespace{ // Implementation details
80 
81  map<Symbol, regex> regexTable;
82 
83  Literal MATCH_ARGUMENT = R"~(\(\s*([\w_\.\-]+)\s*\),?\s*)~";
84  const regex FIND_PREDICATE{string{"(\\w+)"} + MATCH_ARGUMENT};
85 
86  inline regex&
87  getTermRegex (Symbol sym)
88  {
89  if (!contains (regexTable, sym))
90  regexTable[sym] = regex (string(sym)+MATCH_ARGUMENT);
91  return regexTable[sym];
92  }
93  }
94 
100  string
101  extractID (Symbol sym, const string& termString)
102  {
103  smatch match;
104  if (regex_search (termString, match, getTermRegex (sym)))
105  return (match[1]);
106  else
107  return "";
108  }
109 
110 
117  string
118  removeTerm (Symbol sym, string& queryString)
119  {
120  smatch match;
121  if (regex_search (queryString, match, getTermRegex (sym)))
122  {
123  string res (sym); res += "("+match[1]+")";
124  queryString.erase (match.position(), match[0].length());
125  return res;
126  }
127  else
128  return "";
129  }
130 
131 
132  bool
133  hasTerm (Symbol sym, string const& queryString)
134  {
135  smatch match;
136  return regex_search (queryString, match, getTermRegex (sym));
137  }
138 
139 
145  uint
146  countPred (const string& q)
147  {
148  uint cnt (0);
149  sregex_iterator end;
150  for (sregex_iterator i (q.begin(),q.end(), FIND_PREDICATE);
151  i != end; ++i)
152  ++cnt;
153  return cnt;
154  }
155 
156 
160  string
161  appendTerms (string const& pred1, string const& pred2)
162  {
163  return isnil(pred1)? pred2
164  : isnil(pred2)? pred1
165  : pred1 + ", " + pred2;
166  }
167 
168 
169  } // namespace query
170 
171 } // namespace lib
Utilities to support working with predicate queries.
uint countPred(const string &q)
count the top-level predicates in the query string.
Definition: query-util.cpp:146
void normaliseID(string &id)
ensure standard format for a given id string.
Definition: query-util.cpp:61
inline string literal This is a marker type to indicate that
Definition: symbol.hpp:85
Implementation namespace for support and library code.
string appendTerms(string const &pred1, string const &pred2)
Definition: query-util.cpp:161
string extractID(Symbol sym, const string &termString)
(preliminary) helper: instead of really parsing and evaluating the terms, just do a regular expressio...
Definition: query-util.cpp:101
Token or Atom with distinct identity.
Definition: symbol.hpp:126
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
string removeTerm(Symbol sym, string &queryString)
(preliminary) helper: cut a term with the given symbol.
Definition: query-util.cpp:118
Lumiera error handling (C++ interface).
bool contains(SEQ const &cont, typename SEQ::const_reference val)
shortcut for brute-force containment test in any sequential container
Definition: util.hpp:255