Pteros  2.0
Molecular modeling library for human beings!
selection_parser.h
1 /*
2  *
3  * This source code is part of
4  * ******************
5  * *** Pteros ***
6  * ******************
7  * molecular modeling library
8  *
9  * Copyright (c) 2009-2013, Semen Yesylevskyy
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of Artistic License:
13  *
14  * Please note, that Artistic License is slightly more restrictive
15  * then GPL license in terms of distributing the modified versions
16  * of this software (they should be approved first).
17  * Read http://www.opensource.org/licenses/artistic-license-2.0.php
18  * for details. Such license fits scientific software better then
19  * GPL because it prevents the distribution of bugged derivatives.
20  *
21 */
22 
23 //#define _DEBUG_PARSER
24 
25 #ifndef SELECTION_PARSER_H
26 #define SELECTION_PARSER_H
27 
28 #include <string>
29 #include <vector>
30 #include <memory>
31 
32 #include "pteros/core/system.h"
33 #include <boost/variant.hpp>
34 
35 namespace pteros {
36 
37 // Codes of tokens
38 enum Codes {
39  TOK_VOID,
40  TOK_MINUS,
41  TOK_UNARY_MINUS,
42  TOK_PLUS,
43  TOK_MULT,
44  TOK_DIV,
45  TOK_POWER,
46  TOK_EQ, // == or =
47  TOK_NEQ, // <> or !=
48  TOK_LT, //<
49  TOK_GT, //>
50  TOK_LEQ, //<=
51  TOK_GEQ, //>=
52  // Operations for atom field codes
53  TOK_X,
54  TOK_Y,
55  TOK_Z,
56  TOK_OCC,
57  TOK_BETA,
58  // Logic
59  TOK_OR,
60  TOK_AND,
61  // Prefixes
62  TOK_NOT,
63  TOK_WITHIN,
64  TOK_SELF,
65  //TOK_NOSELF,
66  TOK_BY,
67  TOK_RESIDUE,
68  // text keywords
69  TOK_NAME,
70  TOK_RESNAME,
71  TOK_TAG,
72  TOK_TYPE,
73  TOK_CHAIN,
74  // int keywords
75  TOK_RESID,
76  TOK_INDEX,
77  TOK_RESINDEX,
78  // all
79  TOK_ALL,
80  // Range
81  TOK_TO, // '-' or 'to'
82  // Data tokens
83  TOK_INT,
84  TOK_UINT,
85  TOK_FLOAT,
86  TOK_STR,
87  // Parens
88  //TOK_LPAREN,
89  //TOK_RPAREN,
90  // Distances
91  //TOK_DIST,
92  TOK_POINT,
93  TOK_VECTOR,
94  TOK_PLANE,
95 
96  TOK_PRECOMPUTED,
97  TOK_REGEX
98 };
99 
100 struct AstNode; // Forward declaration
101 // An element of the tree is either a recursive sub-tree or a leaf
102 typedef boost::variant<
103  float,
104  int,
105  std::string,
106  std::shared_ptr<AstNode>
107 > ast_element;
108 
109 // The tree itself
110 struct AstNode {
111 #ifdef _DEBUG_PARSER
112  AstNode(){ code = TOK_VOID; }
113 #endif
114 
115  Codes code; //Code of operation
116  std::vector<ast_element> children;
117  std::vector<int> precomputed; // Precomputed indexes for coordinate-independent nodes
118 
119  bool is_coordinate_dependent();
120  // Returns child elements
121  int child_as_int(int i);
122  float child_as_float(int i);
123  std::string child_as_str(int i);
124  std::shared_ptr<AstNode>& child_node(int i);
125 
126 
127 #ifdef _DEBUG_PARSER
128  void dump(int indent=0);
129  std::string decode();
130  std::string name;
131 #endif
132 };
133 
134 typedef std::shared_ptr<AstNode> AstNode_ptr;
135 
146 public:
153  bool has_coord; //There are coordinates in selection
155  Selection_parser(std::vector<int>* subset = nullptr);
157  virtual ~Selection_parser();
159  void create_ast(std::string& sel_str);
160 
163  void apply(System* system, std::size_t fr, std::vector<int>& result);
164 
165 private:
167  std::shared_ptr<AstNode> tree;
168 
169  // AST evaluation stuff
170  System* sys;
171  int Natoms;
172  int frame;
173 
174  void eval_node(AstNode_ptr& node, std::vector<int>& result, std::vector<int>* subspace);
175  std::function<float(int)> get_numeric(AstNode_ptr& node);
176  void do_optimization(AstNode_ptr& node);
177 
178  bool is_optimized;
179 
180  std::vector<int>* starting_subset;
181 };
182 
183 }
184 #endif /* SELECTION_PARSER_H */
Selection parser class.
Definition: selection_parser.h:145
Pteros namespace.
Definition: options.cpp:32
The system of atoms.
Definition: system.h:95
bool has_coord
True if there are coordinate keywords in selection.
Definition: selection_parser.h:153