/usr/include/lv2-c++-tools/query.hpp is in libpaq-dev 1.0.5-2ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 | /****************************************************************************
query.hpp - Query engine for RDF data
Copyright (C) 2006-2007 Lars Luthman <lars.luthman@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 01222-1307 USA
****************************************************************************/
#ifndef QUERY_HPP
#define QUERY_HPP
#include <map>
#include <string>
#include <vector>
#include "rdf.hpp"
namespace PAQ {
class Pattern;
/** This class contains a query result, i.e. a single matching subgraph. */
class QueryResult {
public:
/** Return the bound node for the variable mentioned in place @c var. */
RDFTerm* operator[](size_t var);
/** Return the bound node for the variable @c var. */
RDFTerm* operator[](Variable& var);
protected:
friend class QueryEngine;
std::map<Variable*, size_t> m_variables;
std::vector<RDFTerm*> m_data;
};
class Query;
class QueryEngine;
/** A filter is a constraint on a bound node. If a candidate for binding
does not match the constraint that particular result is discarded.
All member functions are for internal use only. */
class Filter {
public:
virtual ~Filter() { }
virtual void set_variable_indices(Query& query) = 0;
virtual bool execute(QueryEngine& engine) = 0;
};
/** A filter that matches the language of a string literal against a
language code. */
class LangMatches : public Filter {
public:
LangMatches(Variable& var, const std::string& lang);
void set_variable_indices(Query& query);
bool execute(QueryEngine& engine);
protected:
Variable* m_var;
size_t m_index;
std::string m_lang;
};
/** Creates a filter that matches the language of a string literal against a
language code. */
Filter* lang_matches(Variable& var, const std::string& lang);
/** A filter that matches the values of two bound nodes. */
class TermEquality : public Filter {
public:
TermEquality(Variable& var1, Variable& var2);
void set_variable_indices(Query& query);
bool execute(QueryEngine& engine);
protected:
Variable* m_var1;
Variable* m_var2;
size_t m_index1;
size_t m_index2;
};
/** Creates a filter that matches the values of two bound nodes. */
Filter* operator==(Variable& var1, Variable& var2);
/** Abstract base class for all unary filters. */
class UnaryFilter : public Filter {
public:
UnaryFilter(Variable& var);
virtual void set_variable_indices(Query& query);
protected:
Variable* m_var;
size_t m_index;
};
/** A filter that checks if a bound node is an URI reference. */
class IsURI : public UnaryFilter {
public:
IsURI(Variable& var) : UnaryFilter(var) { }
bool execute(QueryEngine& engine);
};
/** Creates a filter that checks if a bound node is an URI reference. */
Filter* is_uriref(Variable& var);
/** A filter that checks if a bound node is a literal. */
class IsLiteral : public UnaryFilter {
public:
IsLiteral(Variable& var) : UnaryFilter(var) { }
bool execute(QueryEngine& engine);
};
/** Creates a filter that checks if a bound node is a literal. */
Filter* is_literal(Variable& var);
/** A filter that checks if a bound node is a numeric literal. */
class IsNumeric : public UnaryFilter {
public:
IsNumeric(Variable& var) : UnaryFilter(var) { }
bool execute(QueryEngine& engine);
};
/** Creates a filter that checks if a bound node is a numeric literal. */
Filter* is_numeric(Variable& var);
/** A filter that checks if a bound node is an integer literal. */
class IsInteger : public UnaryFilter {
public:
IsInteger(Variable& var) : UnaryFilter(var) { }
bool execute(QueryEngine& engine);
};
/** Creates a filter that checks if a bound node is an integer literal. */
Filter* is_integer(Variable& var);
/** A filter that checks for the inverse of another filter. */
class Negation : public Filter {
public:
Negation(Filter* f);
~Negation();
void set_variable_indices(Query& q);
bool execute(QueryEngine& engine);
protected:
Filter* m_filter;
};
/** Creates a filter that checks for the inverse of another filter. */
Filter* no(Filter* filter);
/** A filter that is built from two other filters. */
class Aggregate : public Filter {
public:
Aggregate(Filter* f1, Filter* f2);
~Aggregate();
void set_variable_indices(Query& q);
protected:
Filter* m_f1;
Filter* m_f2;
};
/** A filter that checks if one filter OR another is true. */
class Or : public Aggregate {
public:
Or(Filter* f1, Filter* f2);
bool execute(QueryEngine& engine);
};
/** Creates a filter that checks if at least one of two other
filters is true. */
Filter* or_filter(Filter* f1, Filter* f2);
/** A filter that checks if one filter AND another is true. */
class And : public Aggregate {
public:
And(Filter* f1, Filter* f2);
bool execute(QueryEngine& engine);
};
/** Creates a filter that checks if two other filters is true. */
Filter* and_filter(Filter* f1, Filter* f2);
/** A filter that compares the numeric value of a node to a constant. */
class NumericCompare : public Filter {
public:
NumericCompare(Variable& var, double value);
virtual void set_variable_indices(Query& q);
protected:
Variable* m_var;
double m_value;
size_t m_index;
};
/** A filter that checks if the node's value is less than a constant. */
class NumericLess : public NumericCompare {
public:
NumericLess(Variable& var, double value) : NumericCompare(var, value) { }
bool execute(QueryEngine& engine);
};
/** Creates a filter that checks if the node's value is less
than a constant. */
Filter* operator<(Variable& var, double value);
/** Creates a filter that checks if the node's value is less
than a constant. */
Filter* operator>=(double value, Variable& var);
/** A filter that checks if the node's value is less than or equal to
a constant. */
class NumericLessEqual : public NumericCompare {
public:
NumericLessEqual(Variable& var, double value) : NumericCompare(var, value) {}
bool execute(QueryEngine& engine);
};
/** Creates a filter that checks if the node's value is less than or equal to
a constant. */
Filter* operator<=(Variable& var, double value);
/** Creates a filter that checks if the node's value is less than or equal to
a constant. */
Filter* operator>(double value, Variable& var);
/** A filter that checks if the node's value is greater than a constant. */
class NumericGreater : public NumericCompare {
public:
NumericGreater(Variable& var, double value) : NumericCompare(var, value) { }
bool execute(QueryEngine& engine);
};
/** Creates a filter that checks if the node's value is greater than
a constant. */
Filter* operator>(Variable& var, double value);
/** Creates a filter that checks if the node's value is greater than
a constant. */
Filter* operator<=(double value, Variable& var);
/** A filter that checks if the node's value is greater than or equal to
a constant. */
class NumericGreaterEqual : public NumericCompare {
public:
NumericGreaterEqual(Variable& var, double value)
: NumericCompare(var, value) { }
bool execute(QueryEngine& engine);
};
/** Creates a filter that checks if the node's value is greater than
or equal to a constant. */
Filter* operator>=(Variable& var, double value);
/** Creates a filter that checks if the node's value is greater than
or equal to a constant. */
Filter* operator<(double value, Variable& var);
/** A filter that checks if the node's value is equal to a numeric
constant. */
class NumericEquality : public NumericCompare {
public:
NumericEquality(Variable& var, double value) : NumericCompare(var, value) { }
bool execute(QueryEngine& engine);
};
/** Creates a filter that checks if the node's value is equal to a constant.*/
Filter* operator==(Variable& var, double value);
/** Creates a filter that checks if the node's value is equal to a constant.*/
Filter* operator==(double value, Variable& var);
/** Creates a filter that checks if the node's value is different from
a constant. */
Filter* operator!=(Variable& var, double value);
/** Creates a filter that checks if the node's value is different from
a constant. */
Filter* operator!=(double value, Variable& var);
/** A filter that checks if the node's value is equal to a string
constant. */
class StringEquality : public Filter {
public:
StringEquality(Variable& var, const std::string& value);
virtual void set_variable_indices(Query& q);
bool execute(QueryEngine& engine);
protected:
Variable* m_var;
std::string m_value;
size_t m_index;
};
/** Creates a filter that checks if the node's value is equal to a constant.*/
Filter* operator==(Variable& var, const std::string& value);
/** Creates a filter that checks if the node's value is equal to a constant.*/
Filter* operator==(const std::string& value, Variable& var);
/** Creates a filter that checks if the node's value is different from
a constant. */
Filter* operator!=(Variable& var, const std::string& value);
/** Creates a filter that checks if the node's value is different from
a constant. */
Filter* operator!=(const std::string& value, Variable& var);
/** Abstract base class for all binary filters. */
class BinaryFilter : public Filter {
public:
BinaryFilter(Variable& var1, Variable& var2);
virtual void set_variable_indices(Query& query);
protected:
Variable* m_var1;
Variable* m_var2;
size_t m_index1;
size_t m_index2;
};
/** A class that holds queries. A query can be executed many times on
different data sets. */
class Query {
public:
Query();
/** Add a new triple pattern to the query. */
Query& where(Variable& s, Variable& p, Variable& o);
Query& where(const std::string& s, Variable& p, Variable& o);
Query& where(Variable& s, const std::string& p, Variable& o);
Query& where(Variable& s, Variable& p, const std::string& o);
Query& where(Variable& s, const std::string& p, const std::string& o);
Query& where(const std::string& s, Variable& p, const std::string& o);
Query& where(const std::string& s, const std::string& p, Variable& o);
Query& where(const std::string& s, const std::string& p,
const std::string& o);
/** Add a filter to the query. */
Query& filter(Filter* f);
/** Run the query on a dataset. */
std::vector<QueryResult> run(RDFData& data);
RDFData data;
protected:
friend class QueryEngine;
friend Query select(Variable& var1, Variable& var2,
Variable& var3, Variable& var4);
Query(Variable& var1, Variable& var2, Variable& var3, Variable& var4);
RDFTerm* add_term(const std::string& str);
std::vector<Filter*> filters;
std::map<Variable*, size_t> selected;
};
/** Create a new query with some selected variables. */
Query select(Variable& var1, Variable& var2 = Variable::nil,
Variable& var3 = Variable::nil, Variable& var4 = Variable::nil);
/** This is the class that does all the subgraph matching work. It is for
internal use only. */
class QueryEngine {
public:
QueryEngine(Query& q, RDFData& d);
std::vector<QueryResult> run();
std::vector<size_t> mapping;
const std::vector<RDFTerm*>& N1;
const std::vector<RDFTerm*>& N2;
const std::vector<Triple*>& T1;
const std::vector<Triple*>& T2;
protected:
const Query& query;
void match(size_t depth);
bool match_node(RDFTerm* p, RDFTerm* t, size_t& bound);
size_t NULL_NODE;
std::vector<QueryResult> result;
};
}
#endif
|