Gaia
filter.h
1 /*
2  * Copyright (C) 2006-2013 Music Technology Group - Universitat Pompeu Fabra
3  *
4  * This file is part of Gaia
5  *
6  * Gaia is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License as published by the Free
8  * Software Foundation (FSF), either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the Affero GNU General Public License
17  * version 3 along with this program. If not, see http://www.gnu.org/licenses/
18  */
19 
20 #ifndef GAIA_PARSER_FILTER_H
21 #define GAIA_PARSER_FILTER_H
22 
23 #include <QMutex>
24 #include "parsertypes.h"
25 
26 namespace gaia2 {
27 namespace parser {
28 
29 class FilterParser;
30 struct yyParser;
31 
32 #ifndef SWIG
33 # ifdef INCLUDED_FROM_LEMON
34 // declare this here so we can tell Filter to make it one of its friends. Yay, friends!
35 static void yy_reduce(yyParser*, int);
36 # else
37 void yy_reduce(yyParser*, int);
38 # endif
39 #endif
40 
41 } // namespace parser
42 
43 
48 inline bool isAlwaysTrue(parser::Predicate* pred) {
49  parser::BooleanConstant* b = dynamic_cast<parser::BooleanConstant*>(pred);
50  return (b && (b->value() == true));
51 }
52 
57 inline bool isAlwaysFalse(parser::Predicate* pred) {
58  parser::BooleanConstant* b = dynamic_cast<parser::BooleanConstant*>(pred);
59  return (b && (b->value() == false));
60 }
61 
62 
63 
64 
73 class Filter {
74  protected:
75  parser::Predicate* _pred;
76 
77  // we keep these to be able to bind all of them once we know the layout
78  QList<parser::ValueVariable*> _valueVarList;
79  QList<parser::LabelVariable*> _labelVarList;
80  QList<parser::LabelPointID*> _pointVarList;
81 
82  // temporary lists for storing values in a IN (..., ...) clause
83  // TODO: remove those, they don't belong here...
84  QStringList _slist;
85  QList<Real> _vlist;
86 
87  // WARNING: having mutable makes the class strongly non-reentrant
88  // eg.: we might think isTrue() is reentrant, but it's definitely not
89  mutable const Point* _currentPoint;
90 
91  // these are friends because we don't want to expose internal fields, even
92  // through public setters/getters, but they need to have access to them
93  // because they are the ones in charge of constructing the object
94  friend class parser::FilterParser;
95  friend void parser::yy_reduce(parser::yyParser*, int);
96 
97  template <typename SearchPointType, typename DataSetType>
98  friend class BaseQueryOptimizer;
99 
100  void clear();
101  void clearVariables();
102 
103  void updateVariables(parser::Predicate* pred);
104  void updateVariables(parser::Value* value);
105  void updateVariables(parser::Label* label);
106 
107  static Filter* stealDataFrom(const Filter& other);
108 
109 
110  public:
111 
112  Filter() : _pred(0) {}
113 
114  Filter(const Filter& filter);
115 
119  static Filter* parse(const QString& str);
120 
127  void updateVariables();
128 
134  bool needsBinding() const;
135 
136  void bindToLayout(const PointLayout& layout);
137  void bindToPointAttributes();
138 
139  ~Filter();
140 
144  bool isTrue(const Point* p) const;
145 
149  bool isAlwaysTrue() const;
150 
151  const parser::Predicate* pred() const { return _pred; }
152 
153  static int opFromString(const QString& str);
154 
155  static int opFromString(const std::string& str) {
156  return opFromString(QString::fromUtf8(str.c_str(), str.size()));
157  }
158 
159  QString toString() const;
160 };
161 
162 
163 
164 namespace parser {
165 
171 
172  public:
178  static Filter* parse(const QString& str);
179 
180 
181  protected:
182  // we need a global mutex for the parser because it uses global vars, and as
183  // such is not re-entrant. By using this mutex, we even make it thread-safe!
184  static QMutex _parserMutex;
185 
186  mutable void* _lemonParser; // C makes us do ugly things... (mutable void*?!?)
187 
188  FilterParser();
189  ~FilterParser();
190  Filter* parseFilter(const QString& str) const;
191 };
192 
193 
194 
195 } // namespace parser
196 } // namespace gaia2
197 
198 #endif // GAIA_PARSER_FILTER_H
Definition: parsertypes.h:59
Definition: parsertypes.h:40
The QueryOptimizer class tries to optimize a query by reducing the SearchSpace on which it is suppose...
Definition: parsertypes.h:30
The FilterParser class is used to parse filter queries given as strings and create the corresponding ...
Definition: filter.h:170
Definition: grammar.c:269
This class describes the layout of a point.
Definition: pointlayout.h:60
The Filter class allows to check whether a predicate is true for a given Point.
Definition: filter.h:73
Main Gaia namespace, which contains all the library functions.
Definition: addfield.cpp:22
bool isAlwaysFalse(parser::Predicate *pred)
Returns true if the given predicate is always false (ie: it is a boolean constant which value is Fals...
Definition: filter.h:57
bool isAlwaysTrue(parser::Predicate *pred)
Returns true if the given predicate is always true (ie: it is a boolean constant which value is True)...
Definition: filter.h:48
Definition: parsertypes.h:50
Definition: parsertypes.h:281
Definition: point.h:106