OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
query_impl.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of the OpenStructure project <www.openstructure.org>
3 //
4 // Copyright (C) 2008-2011 by the OpenStructure authors
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 3.0 of the License, or (at your option)
9 // any later version.
10 // This library is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 //------------------------------------------------------------------------------
19 #ifndef OST_QUERY_IMPL_HH
20 #define OST_QUERY_IMPL_HH
21 
22 #include <vector>
23 #include <string>
24 #include <set>
25 
26 #include <boost/logic/tribool.hpp>
27 
28 #include <ost/mol/module_config.hh>
29 #include "query_impl_fw.hh"
30 #include "query_ast.hh"
31 #include <ost/range.hh>
32 #include <ost/mol/query_error.hh>
33 #include "atom_impl_fw.hh"
34 #include "residue_impl_fw.hh"
35 #include "chain_impl_fw.hh"
36 #include <ost/mol/mol.hh>
37 #include <ost/mol/query_state.hh>
39 
40 namespace ost { namespace mol { namespace impl {
41 
42 namespace tok {
44  typedef enum {
49  Dot,
50  And,
51  Or,
52  Not,
71  }
72  Type;
73 }
74 
76 class QueryToken {
77 public:
78  QueryToken();
79  QueryToken(const Range& range, tok::Type type,
80  const Range& value_range=Range());
81 
82  tok::Type GetType() const;
84  bool IsEOF() const;
86  const Range& GetRange() const;
89  const Range& GetValueRange() const;
90 private:
91  tok::Type type_;
92  Range range_;
93  Range value_range_;
94 };
95 
97 class QueryLexer {
98 public:
99  QueryLexer(const String& query_string);
101  QueryToken CurrentToken() const;
102 private:
104  // lexer position.
105  void EatWhitespaces();
107  QueryToken LexToken();
108  QueryToken LexQuotedStringLiteral();
109  QueryToken LexNumericToken();
110  QueryToken LexIdentOrStringToken();
111 
112  String query_string_;
113  size_t current_;
114  QueryToken tok_;
115 };
116 
118 struct SelStmt {
123  : sel_id(Prop::UNDEF),
124  comp_op(COP_EQ),
125  param(0) { }
126  SelStmt(Prop::ID id ,CompOP op, const ParamType& parm)
127  : sel_id(id),
128  comp_op(op),
129  param(parm) { }
130 
131  bool operator ==(const SelStmt& rhs) {
132  return sel_id == rhs.sel_id && comp_op == rhs.comp_op && param == rhs.param;
133  }
134 };
135 typedef enum {
137 } SelItemType;
138 
140 struct SelItem {
142  size_t value;
143 };
144 
148 struct GenProp {
150  float default_val;
153  mapper(epm), default_val(0.0), has_default(false) {}
154 };
155 
157 typedef std::vector<SelItem> SelStack;
158 
160 typedef std::vector<SelStmt> SelStmts;
161 
164 class QueryImpl {
165 public:
166  friend class mol::QueryState;
168  QueryImpl(const String& query_string="");
171  QueryImpl(Node* ast);
173  bool IsValid() const;
174 
176  bool IsEmpty() const;
177 
179  const String& GetQueryString() const;
180 
181 
182  const QueryErrorDesc& GetErrorDescription() const;
183 
186 private:
187 
189  Node* BuildAST();
191  void ASTToSelStack(const Node* src_ast,Prop::Level target_level,
192  SelStack& stack);
193 
196  bool IsAlwaysUndef(const Node* ast_node, Prop::Level target_level);
197 
199  void ExtractSelStmts(const Node* ast);
200 
202  Node* ParsePropValueExpr(QueryLexer& lexer);
203 
206  Node* ParseSubExpr(QueryLexer& lexer, bool paren=false);
207 
210  Node* ParseParenSubExpr(QueryLexer& lexer);
211 
215  Node* ParseBracketSubExpr(QueryLexer& lexer);
216 
219  Node* ParseWithinExpr(QueryLexer& lexer);
220 
229  bool ParsePoint(QueryLexer& lexer, geom::Vec3& point);
230 
233  bool Expect(tok::Type type, const String& s, const QueryToken& token);
234 
237  bool ExpectNumeric(const QueryToken& token);
238 
241  bool ExpectLogicalOperator(const QueryToken& token);
242 
245  bool ExpectComparisonOperator(const QueryToken& token);
246 
253  bool ExpectNotEnd(const QueryToken& token, const String& s);
254 
257  bool ParseValue(const Prop& sel, const QueryToken& op,
258  QueryLexer& lexer, ParamType& value);
259 
262  Node* ParseValueOrRange(const Prop& s, const QueryToken& op,
263  QueryLexer& lexer);
265  Node* Concatenate(Node* lhs, Node* rhs, LogicOP logical_op);
266 
267  String query_string_;
268  bool has_error_;
269  bool empty_optimize_;
270  QueryErrorDesc error_desc_;
271  std::vector<GenProp> gen_prop_list_;
272  int num_gen_prop_;
273 protected:
274  std::vector<SelStmt> sel_values_;
275  std::vector<SelStack> sel_stacks_;
276  std::vector<std::set<size_t> > indices_;
277  std::vector<bool> inversion_stack_;
278  std::vector<QueryImplP> bracketed_expr_;
279 };
280 
281 }}} // ns
282 
283 #endif