1 |
tim |
277 |
/* |
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 |
|
|
#include "selection/SelectionEvaluator.hpp" |
43 |
|
|
namespace oopse { |
44 |
|
|
|
45 |
|
|
|
46 |
tim |
278 |
bool SelectionEvaluator::loadScript(const std::string& filename, const std::string& script) { |
47 |
|
|
this->filename = filename; |
48 |
|
|
this->script = script; |
49 |
|
|
if (! compiler.compile(filename, script)) { |
50 |
|
|
error = true; |
51 |
|
|
errorMessage = compiler.getErrorMessage(); |
52 |
|
|
return false; |
53 |
|
|
} |
54 |
|
|
|
55 |
|
|
pc = 0; |
56 |
|
|
aatoken = compiler.getAatokenCompiled(); |
57 |
|
|
linenumbers = compiler.getLineNumbers(); |
58 |
|
|
lineIndices = compiler.getLineIndices(); |
59 |
|
|
return true; |
60 |
tim |
277 |
} |
61 |
tim |
278 |
|
62 |
|
|
void SelectionEvaluator::clearState() { |
63 |
|
|
for (int i = scriptLevelMax; --i >= 0; ) |
64 |
tim |
281 |
stack[i].clear(); |
65 |
tim |
278 |
scriptLevel = 0; |
66 |
|
|
error = false; |
67 |
tim |
281 |
errorMessage = ""; |
68 |
tim |
278 |
} |
69 |
|
|
|
70 |
|
|
bool SelectionEvaluator::loadScriptString(const std::string& script) { |
71 |
|
|
clearState(); |
72 |
tim |
281 |
return loadScript("", script); |
73 |
tim |
278 |
} |
74 |
|
|
|
75 |
|
|
bool SelectionEvaluator::loadScriptFile(const std::string& filename) { |
76 |
|
|
clearState(); |
77 |
|
|
return loadScriptFileInternal(filename); |
78 |
|
|
} |
79 |
|
|
|
80 |
tim |
282 |
bool SelectionEvaluator::loadScriptFileInternal(const string & filename) { |
81 |
tim |
278 |
|
82 |
tim |
282 |
} |
83 |
|
|
|
84 |
tim |
278 |
void SelectionEvaluator::instructionDispatchLoop(){ |
85 |
|
|
|
86 |
tim |
281 |
while ( pc < aatoken.size()) { |
87 |
tim |
278 |
statement = aatoken[pc++]; |
88 |
tim |
281 |
statementLength = statement.size(); |
89 |
tim |
278 |
Token token = statement[0]; |
90 |
|
|
switch (token.tok) { |
91 |
tim |
281 |
case Token::define: |
92 |
tim |
278 |
define(); |
93 |
|
|
break; |
94 |
tim |
281 |
case Token::select: |
95 |
tim |
278 |
select(); |
96 |
|
|
break; |
97 |
|
|
default: |
98 |
|
|
unrecognizedCommand(token); |
99 |
|
|
return; |
100 |
|
|
} |
101 |
|
|
} |
102 |
|
|
} |
103 |
|
|
|
104 |
tim |
281 |
BitSet SelectionEvaluator::expression(std::vector<Token>& code, int pcStart) { |
105 |
tim |
278 |
int numberOfAtoms = viewer.getAtomCount(); |
106 |
|
|
BitSet bs; |
107 |
|
|
BitSet[] stack = new BitSet[10]; |
108 |
|
|
int sp = 0; |
109 |
|
|
|
110 |
|
|
for (int pc = pcStart; ; ++pc) { |
111 |
|
|
Token instruction = code[pc]; |
112 |
tim |
282 |
|
113 |
tim |
278 |
switch (instruction.tok) { |
114 |
tim |
281 |
case Token::expressionBegin: |
115 |
tim |
278 |
break; |
116 |
tim |
281 |
case Token::expressionEnd: |
117 |
tim |
282 |
break; |
118 |
tim |
281 |
case Token::all: |
119 |
tim |
278 |
bs = stack[sp++] = new BitSet(numberOfAtoms); |
120 |
|
|
for (int i = numberOfAtoms; --i >= 0; ) |
121 |
|
|
bs.set(i); |
122 |
|
|
break; |
123 |
tim |
281 |
case Token::none: |
124 |
tim |
278 |
stack[sp++] = new BitSet(); |
125 |
|
|
break; |
126 |
tim |
281 |
case Token::opOr: |
127 |
tim |
278 |
bs = stack[--sp]; |
128 |
|
|
stack[sp-1].or(bs); |
129 |
|
|
break; |
130 |
tim |
281 |
case Token::opAnd: |
131 |
tim |
278 |
bs = stack[--sp]; |
132 |
|
|
stack[sp-1].and(bs); |
133 |
|
|
break; |
134 |
tim |
281 |
case Token::opNot: |
135 |
tim |
278 |
bs = stack[sp - 1]; |
136 |
|
|
notSet(bs); |
137 |
|
|
break; |
138 |
tim |
281 |
case Token::within: |
139 |
tim |
278 |
bs = stack[sp - 1]; |
140 |
|
|
stack[sp - 1] = new BitSet(); |
141 |
|
|
withinInstruction(instruction, bs, stack[sp - 1]); |
142 |
|
|
break; |
143 |
tim |
281 |
case Token::selected: |
144 |
tim |
278 |
stack[sp++] = copyBitSet(viewer.getSelectionSet()); |
145 |
|
|
break; |
146 |
tim |
281 |
case Token::name: |
147 |
|
|
|
148 |
tim |
278 |
break; |
149 |
tim |
281 |
case Token::index: |
150 |
|
|
|
151 |
|
|
break; |
152 |
|
|
case Token::molname: |
153 |
|
|
|
154 |
|
|
break; |
155 |
|
|
case Token::molindex: |
156 |
|
|
break; |
157 |
|
|
case Token::identifier: |
158 |
|
|
stack[sp++] = lookupIdentifierValue((std::string)instruction.value); |
159 |
|
|
break; |
160 |
|
|
case Token::opLT: |
161 |
|
|
case Token::opLE: |
162 |
|
|
case Token::opGE: |
163 |
|
|
case Token::opGT: |
164 |
|
|
case Token::opEQ: |
165 |
|
|
case Token::opNE: |
166 |
tim |
278 |
bs = stack[sp++] = new BitSet(); |
167 |
|
|
comparatorInstruction(instruction, bs); |
168 |
|
|
break; |
169 |
|
|
default: |
170 |
|
|
unrecognizedExpression(); |
171 |
|
|
} |
172 |
|
|
} |
173 |
|
|
if (sp != 1) |
174 |
|
|
evalError("atom expression compiler error - stack over/underflow"); |
175 |
|
|
return stack[0]; |
176 |
|
|
} |
177 |
|
|
|
178 |
|
|
|
179 |
|
|
|
180 |
|
|
void SelectionEvaluator::comparatorInstruction(Token instruction, BitSet bs) { |
181 |
|
|
int comparator = instruction.tok; |
182 |
|
|
int property = instruction.intValue; |
183 |
|
|
float propertyValue = 0; // just for temperature |
184 |
|
|
int comparisonValue = ((Integer)instruction.value).intValue(); |
185 |
|
|
int numberOfAtoms = viewer.getAtomCount(); |
186 |
|
|
Frame frame = viewer.getFrame(); |
187 |
|
|
for (int i = 0; i < numberOfAtoms; ++i) { |
188 |
|
|
Atom atom = frame.getAtomAt(i); |
189 |
|
|
switch (property) { |
190 |
tim |
281 |
case Token::mass: |
191 |
|
|
//propertyValue = atom.getAtomNumber(); |
192 |
tim |
278 |
break; |
193 |
tim |
281 |
case Token::charge: |
194 |
|
|
|
195 |
tim |
278 |
break; |
196 |
tim |
281 |
case Token::dipole: |
197 |
|
|
|
198 |
tim |
278 |
break; |
199 |
|
|
default: |
200 |
|
|
unrecognizedAtomProperty(property); |
201 |
|
|
} |
202 |
tim |
281 |
bool match = false; |
203 |
tim |
278 |
switch (comparator) { |
204 |
tim |
281 |
case Token::opLT: |
205 |
tim |
278 |
match = propertyValue < comparisonValue; |
206 |
|
|
break; |
207 |
tim |
281 |
case Token::opLE: |
208 |
tim |
278 |
match = propertyValue <= comparisonValue; |
209 |
|
|
break; |
210 |
tim |
281 |
case Token::opGE: |
211 |
tim |
278 |
match = propertyValue >= comparisonValue; |
212 |
|
|
break; |
213 |
tim |
281 |
case Token::opGT: |
214 |
tim |
278 |
match = propertyValue > comparisonValue; |
215 |
|
|
break; |
216 |
tim |
281 |
case Token::opEQ: |
217 |
tim |
278 |
match = propertyValue == comparisonValue; |
218 |
|
|
break; |
219 |
tim |
281 |
case Token::opNE: |
220 |
tim |
278 |
match = propertyValue != comparisonValue; |
221 |
|
|
break; |
222 |
|
|
} |
223 |
|
|
if (match) |
224 |
|
|
bs.set(i); |
225 |
|
|
} |
226 |
|
|
} |
227 |
|
|
|
228 |
tim |
281 |
void SelectionEvaluator::withinInstruction(const Token& instruction, BitSet& bs, BitSet& bsResult) |
229 |
tim |
278 |
|
230 |
tim |
281 |
boost::any withinSpec = instruction.value; |
231 |
tim |
282 |
if (withinSpec.type() == typeid(float)){ |
232 |
|
|
withinDistance(boost::any_cast<float>(withinSpec), bs, bsResult); |
233 |
tim |
278 |
return; |
234 |
|
|
} |
235 |
tim |
282 |
|
236 |
tim |
278 |
evalError("Unrecognized within parameter:" + withinSpec); |
237 |
|
|
} |
238 |
|
|
|
239 |
tim |
282 |
void SelectionEvaluator::withinDistance(float distance, const BitSet& bs, const BitSet& bsResult) { |
240 |
tim |
278 |
Frame frame = viewer.getFrame(); |
241 |
|
|
for (int i = frame.getAtomCount(); --i >= 0; ) { |
242 |
|
|
if (bs.get(i)) { |
243 |
|
|
Atom atom = frame.getAtomAt(i); |
244 |
|
|
AtomIterator iterWithin = |
245 |
|
|
frame.getWithinIterator(atom, distance); |
246 |
|
|
while (iterWithin.hasNext()) |
247 |
|
|
bsResult.set(iterWithin.next().getAtomIndex()); |
248 |
|
|
} |
249 |
|
|
} |
250 |
|
|
} |
251 |
|
|
|
252 |
tim |
281 |
void SelectionEvaluator::define() { |
253 |
tim |
282 |
assert(statement.size() >= 3); |
254 |
|
|
|
255 |
|
|
std::string variable = boost::any_cast<std::string>(statement[1].value); |
256 |
|
|
|
257 |
tim |
281 |
variables.insert(std::make_pair(variable, expression(statement, 2))); |
258 |
tim |
278 |
} |
259 |
|
|
} |
260 |
|
|
|
261 |
tim |
282 |
/** @todo */ |
262 |
|
|
void SelectionEvaluator::predefine(const std::string& script) { |
263 |
tim |
278 |
|
264 |
tim |
282 |
if (compiler.compile("#predefine", script)) { |
265 |
|
|
std::vector<std::vector<Token> > aatoken = compiler.getAatokenCompiled(); |
266 |
|
|
if (aatoken.size() != 1) { |
267 |
|
|
evalError("predefinition does not have exactly 1 command:" |
268 |
|
|
+ script); |
269 |
|
|
return; |
270 |
|
|
} |
271 |
|
|
std::vector<Token> statement = aatoken[0]; |
272 |
|
|
if (statement.size() > 2) { |
273 |
|
|
int tok = statement[1].tok; |
274 |
|
|
if (tok == Token::identifier || (tok & Token::predefinedset) == Token::predefinedset) { |
275 |
|
|
std::string variable = (std::string)statement[1].value; |
276 |
|
|
variables.insert(std::make_pair(variable, statement)); |
277 |
|
|
|
278 |
|
|
} else { |
279 |
|
|
evalError("invalid variable name:" + script); |
280 |
|
|
} |
281 |
|
|
}else { |
282 |
|
|
evalError("bad predefinition length:" + script); |
283 |
|
|
} |
284 |
|
|
|
285 |
|
|
|
286 |
|
|
} else { |
287 |
|
|
evalError("predefined set compile error:" + script + |
288 |
|
|
"\ncompile error:" + compiler.getErrorMessage()); |
289 |
|
|
} |
290 |
|
|
|
291 |
|
|
} |
292 |
|
|
|
293 |
|
|
void SelectionEvaluator::select(){ |
294 |
|
|
viewer.setSelectionSet(expression(statement, 1)); |
295 |
|
|
} |
296 |
|
|
|
297 |
|
|
BitSet SelectionEvaluator::lookupValue(const std::string& variable){ |
298 |
|
|
|
299 |
|
|
std::map<std::string, boost::any>::iterator i = variables.find(variable); |
300 |
|
|
|
301 |
|
|
if (i != variables.end()) { |
302 |
|
|
if (i->second.type() == typeid(BitSet)) { |
303 |
|
|
return boost::any_cast<BitSet>(i->second); |
304 |
|
|
} else if (i->second.type() == typeid(std::vector<Token>)){ |
305 |
|
|
BitSet bs = expression(boost::any_cast(i->second), 2); |
306 |
|
|
i->second = bs; /**@todo fixme */ |
307 |
|
|
return bs; |
308 |
|
|
} |
309 |
|
|
} |
310 |
|
|
|
311 |
|
|
} |
312 |
|
|
|
313 |
|
|
} |