ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-3.0/src/selection/SelectionEvaluator.hpp
Revision: 1979
Committed: Mon Feb 7 19:13:18 2005 UTC (19 years, 4 months ago) by tim
File size: 6939 byte(s)
Log Message:
selection library is working

File Contents

# User Rev Content
1 tim 1961 /*
2     * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved.
3     *
4     * The University of Notre Dame grants you ("Licensee") a
5     * non-exclusive, royalty free, license to use, modify and
6     * redistribute this software in source and binary code form, provided
7     * that the following conditions are met:
8     *
9     * 1. Acknowledgement of the program authors must be made in any
10     * publication of scientific results based in part on use of the
11     * program. An acceptable form of acknowledgement is citation of
12     * the article in which the program was described (Matthew
13     * A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher
14     * J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented
15     * Parallel Simulation Engine for Molecular Dynamics,"
16     * J. Comput. Chem. 26, pp. 252-271 (2005))
17     *
18     * 2. Redistributions of source code must retain the above copyright
19     * notice, this list of conditions and the following disclaimer.
20     *
21     * 3. Redistributions in binary form must reproduce the above copyright
22     * notice, this list of conditions and the following disclaimer in the
23     * documentation and/or other materials provided with the
24     * distribution.
25     *
26     * This software is provided "AS IS," without a warranty of any
27     * kind. All express or implied conditions, representations and
28     * warranties, including any implied warranty of merchantability,
29     * fitness for a particular purpose or non-infringement, are hereby
30     * excluded. The University of Notre Dame and its licensors shall not
31     * be liable for any damages suffered by licensee as a result of
32     * using, modifying or distributing the software or its
33     * derivatives. In no event will the University of Notre Dame or its
34     * licensors be liable for any lost revenue, profit or data, or for
35     * direct, indirect, special, consequential, incidental or punitive
36     * damages, however caused and regardless of the theory of liability,
37     * arising out of the use of or inability to use software, even if the
38     * University of Notre Dame has been advised of the possibility of
39     * such damages.
40     */
41    
42     #ifndef SELECTION_SELECTIONEVALUATOR_HPP
43     #define SELECTION_SELECTIONEVALUATOR_HPP
44    
45     #include <map>
46     #include <string>
47     #include <vector>
48    
49     #include "brains/SimInfo.hpp"
50     #include "selection/Token.hpp"
51     #include "selection/SelectionCompiler.hpp"
52 tim 1967 #include "selection/NameFinder.hpp"
53 tim 1979 #include "selection/DistanceFinder.hpp"
54 tim 1961 #include "utils/BitSet.hpp"
55 tim 1967 #include "primitives/StuntDouble.hpp"
56 tim 1979 #include "utils/StringUtils.hpp"
57 tim 1961 namespace oopse {
58    
59 tim 1967
60 tim 1972 //class Context {
61     // public:
62     //
63     // void clear() {
64     // linenumbers.clear();
65     // lineIndices.clear();
66     // aatoken.clear();
67     // }
68     //
69     // std::string filename;
70     // std::string script;
71     // std::vector<int> linenumbers;
72     // std::vector<int> lineIndices;
73     // std::vector<std::vector<Token> > aatoken;
74     // int pc;
75     //};
76 tim 1962
77 tim 1967
78 tim 1961 /**
79     * @class SelectionEvaluator SelectionEvaluator.hpp "selection/SelectionEvaluator"
80     * @brief Evalute the tokens compiled by SelectionCompiler and return a BitSet
81     */
82     class SelectionEvaluator{
83     public:
84    
85 tim 1972 SelectionEvaluator(SimInfo* info);
86 tim 1961
87 tim 1962
88 tim 1972 bool loadScriptString(const std::string& script);
89     bool loadScriptFile(const std::string& filename);
90    
91 tim 1961 BitSet evaluate();
92    
93 tim 1972 //BitSet evaluate(Snapshot* snapshot);
94 tim 1961
95     /**
96 tim 1972 * Tests if the result from evaluation of script is dynamic.
97 tim 1961 */
98 tim 1972 bool isDynamic() {
99     return isDynamic_;
100     }
101 tim 1962
102     bool hadRuntimeError() const{
103     return error;
104     }
105    
106     std::string getErrorMessage() const {
107     return errorMessage;
108     }
109    
110    
111     int getLinenumber() {
112     return linenumbers[pc];
113     }
114    
115     std::string getLine() {
116     int ichBegin = lineIndices[pc];
117     int ichEnd;
118 tim 1965 if ((ichEnd = script.find('\r', ichBegin)) == std::string::npos &&
119     (ichEnd = script.find('\n', ichBegin)) == std::string::npos) {
120 tim 1962 ichEnd = script.size();
121     }
122     return script.substr(ichBegin, ichEnd);
123     }
124    
125 tim 1961 private:
126    
127 tim 1962 void clearState();
128    
129     bool loadScript(const std::string& filename, const std::string& script);
130 tim 1972
131 tim 1962 bool loadScriptFileInternal(const std::string& filename);
132    
133 tim 1972
134 tim 1962 void clearDefinitionsAndLoadPredefined();
135    
136 tim 1961 void define();
137 tim 1972 void select(BitSet& bs);
138 tim 1967 void predefine(const std::string& script);
139 tim 1961
140 tim 1972 void instructionDispatchLoop(BitSet& bs);
141 tim 1962
142 tim 1967 void withinInstruction(const Token& instruction, BitSet& bs);
143    
144     BitSet comparatorInstruction(const Token& instruction);
145     void compareProperty(StuntDouble* sd, BitSet& bs, int property, int comparator, float comparisonValue);
146     BitSet nameInstruction(const std::string& name);
147 tim 1979 BitSet indexInstruction(const boost::any& value);
148 tim 1967 BitSet expression(const std::vector<Token>& tokens, int pc);
149 tim 1962
150 tim 1967 BitSet lookupValue(const std::string& variable);
151    
152 tim 1972 void evalError(const std::string& message) {
153     std::cerr << "SelectionEvaulator Error: " << message << std::endl;
154     }
155 tim 1966
156     void unrecognizedCommand(const Token& token) {
157 tim 1967 evalError("unrecognized command:" + boost::any_cast<std::string>(token.value));
158 tim 1966 }
159 tim 1967
160     void unrecognizedExpression() {
161     evalError("unrecognized expression");
162     }
163    
164     void unrecognizedAtomProperty(int property){
165     evalError("unrecognized atom property");
166     }
167    
168     void unrecognizedIdentifier(const std::string& identifier) {
169     evalError("unrecognized identifier:" + identifier);
170     }
171 tim 1972
172 tim 1979 void invalidIndexRange(std::pair<int, int> range) {
173     evalError("invalid index range: [" + toString(range.first) + ", " + toString(range.second) + ")");
174     }
175    
176     void invalidIndex(int index) {
177     evalError("invalid index : " + toString(index) );
178     }
179    
180    
181 tim 1972 bool containDynamicToken(const std::vector<Token>& tokens);
182    
183 tim 1962 SelectionCompiler compiler;
184    
185 tim 1972 //const static int scriptLevelMax = 10;
186     //int scriptLevel;
187 tim 1962
188 tim 1972 //Context stack[scriptLevelMax];
189 tim 1962
190     std::string filename;
191     std::string script;
192     std::vector<int> linenumbers;
193     std::vector<int> lineIndices;
194     std::vector<std::vector<Token> > aatoken;
195     int pc; // program counter
196    
197 tim 1965 bool error;
198 tim 1962 std::string errorMessage;
199    
200     std::vector<Token> statement;
201     int statementLength;
202    
203 tim 1965 SimInfo* info;
204 tim 1979 NameFinder nameFinder;
205     DistanceFinder distanceFinder;
206 tim 1967 int nStuntDouble; //natoms + nrigidbodies
207     std::map<std::string, boost::any > variables;
208 tim 1972
209     bool isDynamic_;
210     bool isLoaded_;
211    
212 tim 1961 };
213    
214     }
215     #endif