39 |
|
* such damages. |
40 |
|
*/ |
41 |
|
|
42 |
+ |
#include <stack> |
43 |
|
#include "selection/SelectionEvaluator.hpp" |
44 |
+ |
#include "primitives/Atom.hpp" |
45 |
+ |
#include "primitives/DirectionalAtom.hpp" |
46 |
+ |
#include "primitives/RigidBody.hpp" |
47 |
+ |
#include "primitives/Molecule.hpp" |
48 |
+ |
|
49 |
|
namespace oopse { |
50 |
|
|
51 |
|
|
52 |
+ |
SelectionEvaluator::SelectionEvaluator(SimInfo* si) : info(si), finder(info), isLoaded_(false){ |
53 |
+ |
|
54 |
+ |
} |
55 |
+ |
|
56 |
|
bool SelectionEvaluator::loadScript(const std::string& filename, const std::string& script) { |
57 |
+ |
clearDefinitionsAndLoadPredefined(); |
58 |
|
this->filename = filename; |
59 |
|
this->script = script; |
60 |
|
if (! compiler.compile(filename, script)) { |
61 |
|
error = true; |
62 |
|
errorMessage = compiler.getErrorMessage(); |
63 |
+ |
std::cerr << "SelectionCompiler Error: " << errorMessage << std::endl; |
64 |
|
return false; |
65 |
|
} |
66 |
|
|
68 |
|
aatoken = compiler.getAatokenCompiled(); |
69 |
|
linenumbers = compiler.getLineNumbers(); |
70 |
|
lineIndices = compiler.getLineIndices(); |
71 |
+ |
|
72 |
+ |
std::vector<std::vector<Token> >::const_iterator i; |
73 |
+ |
|
74 |
+ |
isDynamic_ = false; |
75 |
+ |
for (i = aatoken.begin(); i != aatoken.end(); ++i) { |
76 |
+ |
if (containDynamicToken(*i)) { |
77 |
+ |
isDynamic_ = true; |
78 |
+ |
break; |
79 |
+ |
} |
80 |
+ |
} |
81 |
+ |
|
82 |
+ |
isLoaded_ = true; |
83 |
|
return true; |
84 |
|
} |
85 |
|
|
86 |
|
void SelectionEvaluator::clearState() { |
87 |
< |
for (int i = scriptLevelMax; --i >= 0; ) |
88 |
< |
stack[i].clear(); |
89 |
< |
scriptLevel = 0; |
87 |
> |
//for (int i = scriptLevelMax; --i >= 0; ) |
88 |
> |
// stack[i].clear(); |
89 |
> |
//scriptLevel = 0; |
90 |
|
error = false; |
91 |
|
errorMessage = ""; |
92 |
|
} |
101 |
|
return loadScriptFileInternal(filename); |
102 |
|
} |
103 |
|
|
104 |
< |
bool SelectionEvaluator::loadScriptFileInternal(const string & filename) { |
104 |
> |
bool SelectionEvaluator::loadScriptFileInternal(const std::string & filename) { |
105 |
> |
ifstream ifs(filename.c_str()); |
106 |
> |
if (!ifs.is_open()) { |
107 |
> |
return false; |
108 |
> |
} |
109 |
|
|
110 |
+ |
const int bufferSize = 65535; |
111 |
+ |
char buffer[bufferSize]; |
112 |
+ |
std::string script; |
113 |
+ |
while(ifs.getline(buffer, bufferSize)) { |
114 |
+ |
script += buffer; |
115 |
+ |
} |
116 |
+ |
return loadScript(filename, script); |
117 |
|
} |
118 |
|
|
119 |
< |
void SelectionEvaluator::instructionDispatchLoop(){ |
120 |
< |
|
119 |
> |
void SelectionEvaluator::instructionDispatchLoop(BitSet& bs){ |
120 |
> |
|
121 |
|
while ( pc < aatoken.size()) { |
122 |
|
statement = aatoken[pc++]; |
123 |
|
statementLength = statement.size(); |
127 |
|
define(); |
128 |
|
break; |
129 |
|
case Token::select: |
130 |
< |
select(); |
130 |
> |
select(bs); |
131 |
|
break; |
132 |
|
default: |
133 |
|
unrecognizedCommand(token); |
134 |
|
return; |
135 |
|
} |
136 |
|
} |
137 |
+ |
|
138 |
|
} |
139 |
|
|
140 |
< |
BitSet SelectionEvaluator::expression(std::vector<Token>& code, int pcStart) { |
105 |
< |
int numberOfAtoms = viewer.getAtomCount(); |
140 |
> |
BitSet SelectionEvaluator::expression(const std::vector<Token>& code, int pcStart) { |
141 |
|
BitSet bs; |
142 |
< |
BitSet[] stack = new BitSet[10]; |
143 |
< |
int sp = 0; |
144 |
< |
|
110 |
< |
for (int pc = pcStart; ; ++pc) { |
142 |
> |
std::stack<BitSet> stack; |
143 |
> |
|
144 |
> |
for (int pc = pcStart; pc < code.size(); ++pc) { |
145 |
|
Token instruction = code[pc]; |
146 |
|
|
147 |
|
switch (instruction.tok) { |
150 |
|
case Token::expressionEnd: |
151 |
|
break; |
152 |
|
case Token::all: |
153 |
< |
bs = stack[sp++] = new BitSet(numberOfAtoms); |
154 |
< |
for (int i = numberOfAtoms; --i >= 0; ) |
155 |
< |
bs.set(i); |
153 |
> |
bs = BitSet(nStuntDouble); |
154 |
> |
bs.setAll(); |
155 |
> |
stack.push(bs); |
156 |
|
break; |
157 |
|
case Token::none: |
158 |
< |
stack[sp++] = new BitSet(); |
158 |
> |
bs = BitSet(nStuntDouble); |
159 |
> |
stack.push(bs); |
160 |
|
break; |
161 |
|
case Token::opOr: |
162 |
< |
bs = stack[--sp]; |
163 |
< |
stack[sp-1].or(bs); |
162 |
> |
bs = stack.top(); |
163 |
> |
stack.pop(); |
164 |
> |
stack.top() |= bs; |
165 |
|
break; |
166 |
|
case Token::opAnd: |
167 |
< |
bs = stack[--sp]; |
168 |
< |
stack[sp-1].and(bs); |
167 |
> |
bs = stack.top(); |
168 |
> |
stack.pop(); |
169 |
> |
stack.top() &= bs; |
170 |
|
break; |
171 |
|
case Token::opNot: |
172 |
< |
bs = stack[sp - 1]; |
136 |
< |
notSet(bs); |
172 |
> |
stack.top().flip(); |
173 |
|
break; |
174 |
|
case Token::within: |
175 |
< |
bs = stack[sp - 1]; |
176 |
< |
stack[sp - 1] = new BitSet(); |
141 |
< |
withinInstruction(instruction, bs, stack[sp - 1]); |
175 |
> |
|
176 |
> |
withinInstruction(instruction, stack.top()); |
177 |
|
break; |
178 |
< |
case Token::selected: |
179 |
< |
stack[sp++] = copyBitSet(viewer.getSelectionSet()); |
180 |
< |
break; |
178 |
> |
//case Token::selected: |
179 |
> |
// stack.push(getSelectionSet()); |
180 |
> |
// break; |
181 |
|
case Token::name: |
182 |
< |
|
182 |
> |
stack.push(nameInstruction(boost::any_cast<std::string>(instruction.value))); |
183 |
|
break; |
149 |
– |
case Token::index: |
150 |
– |
|
184 |
|
break; |
152 |
– |
case Token::molname: |
153 |
– |
|
154 |
– |
break; |
155 |
– |
case Token::molindex: |
156 |
– |
break; |
185 |
|
case Token::identifier: |
186 |
< |
stack[sp++] = lookupIdentifierValue((std::string)instruction.value); |
186 |
> |
stack.push(lookupValue(boost::any_cast<std::string>(instruction.value))); |
187 |
|
break; |
188 |
|
case Token::opLT: |
189 |
|
case Token::opLE: |
191 |
|
case Token::opGT: |
192 |
|
case Token::opEQ: |
193 |
|
case Token::opNE: |
194 |
< |
bs = stack[sp++] = new BitSet(); |
167 |
< |
comparatorInstruction(instruction, bs); |
194 |
> |
stack.push(comparatorInstruction(instruction)); |
195 |
|
break; |
196 |
|
default: |
197 |
|
unrecognizedExpression(); |
198 |
|
} |
199 |
|
} |
200 |
< |
if (sp != 1) |
200 |
> |
if (stack.size() != 1) |
201 |
|
evalError("atom expression compiler error - stack over/underflow"); |
202 |
< |
return stack[0]; |
202 |
> |
|
203 |
> |
return stack.top(); |
204 |
|
} |
205 |
|
|
206 |
|
|
207 |
|
|
208 |
< |
void SelectionEvaluator::comparatorInstruction(Token instruction, BitSet bs) { |
208 |
> |
BitSet SelectionEvaluator::comparatorInstruction(const Token& instruction) { |
209 |
|
int comparator = instruction.tok; |
210 |
|
int property = instruction.intValue; |
211 |
< |
float propertyValue = 0; // just for temperature |
212 |
< |
int comparisonValue = ((Integer)instruction.value).intValue(); |
213 |
< |
int numberOfAtoms = viewer.getAtomCount(); |
214 |
< |
Frame frame = viewer.getFrame(); |
215 |
< |
for (int i = 0; i < numberOfAtoms; ++i) { |
216 |
< |
Atom atom = frame.getAtomAt(i); |
217 |
< |
switch (property) { |
218 |
< |
case Token::mass: |
219 |
< |
//propertyValue = atom.getAtomNumber(); |
220 |
< |
break; |
221 |
< |
case Token::charge: |
211 |
> |
float comparisonValue = boost::any_cast<float>(instruction.value); |
212 |
> |
float propertyValue; |
213 |
> |
BitSet bs(nStuntDouble); |
214 |
> |
|
215 |
> |
SimInfo::MoleculeIterator mi; |
216 |
> |
Molecule* mol; |
217 |
> |
Molecule::AtomIterator ai; |
218 |
> |
Atom* atom; |
219 |
> |
Molecule::RigidBodyIterator rbIter; |
220 |
> |
RigidBody* rb; |
221 |
> |
|
222 |
> |
for (mol = info->beginMolecule(mi); mol != NULL; mol = info->nextMolecule(mi)) { |
223 |
|
|
224 |
< |
break; |
225 |
< |
case Token::dipole: |
224 |
> |
for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) { |
225 |
> |
compareProperty(atom, bs, property, comparator, comparisonValue); |
226 |
> |
} |
227 |
> |
|
228 |
> |
//change the positions of atoms which belong to the rigidbodies |
229 |
> |
for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) { |
230 |
> |
compareProperty(rb, bs, property, comparator, comparisonValue); |
231 |
> |
} |
232 |
> |
} |
233 |
|
|
234 |
< |
break; |
235 |
< |
default: |
200 |
< |
unrecognizedAtomProperty(property); |
201 |
< |
} |
202 |
< |
bool match = false; |
203 |
< |
switch (comparator) { |
204 |
< |
case Token::opLT: |
205 |
< |
match = propertyValue < comparisonValue; |
206 |
< |
break; |
207 |
< |
case Token::opLE: |
208 |
< |
match = propertyValue <= comparisonValue; |
209 |
< |
break; |
210 |
< |
case Token::opGE: |
211 |
< |
match = propertyValue >= comparisonValue; |
212 |
< |
break; |
213 |
< |
case Token::opGT: |
214 |
< |
match = propertyValue > comparisonValue; |
215 |
< |
break; |
216 |
< |
case Token::opEQ: |
217 |
< |
match = propertyValue == comparisonValue; |
218 |
< |
break; |
219 |
< |
case Token::opNE: |
220 |
< |
match = propertyValue != comparisonValue; |
221 |
< |
break; |
222 |
< |
} |
223 |
< |
if (match) |
224 |
< |
bs.set(i); |
225 |
< |
} |
226 |
< |
} |
234 |
> |
return bs; |
235 |
> |
} |
236 |
|
|
237 |
< |
void SelectionEvaluator::withinInstruction(const Token& instruction, BitSet& bs, BitSet& bsResult) |
237 |
> |
void SelectionEvaluator::compareProperty(StuntDouble* sd, BitSet& bs, int property, int comparator, float comparisonValue) { |
238 |
> |
double propertyValue; |
239 |
> |
switch (property) { |
240 |
> |
case Token::mass: |
241 |
> |
propertyValue = sd->getMass(); |
242 |
> |
break; |
243 |
> |
case Token::charge: |
244 |
> |
return; |
245 |
> |
//break; |
246 |
> |
case Token::dipole: |
247 |
> |
return; |
248 |
> |
//break; |
249 |
> |
default: |
250 |
> |
unrecognizedAtomProperty(property); |
251 |
> |
} |
252 |
> |
|
253 |
> |
bool match = false; |
254 |
> |
switch (comparator) { |
255 |
> |
case Token::opLT: |
256 |
> |
match = propertyValue < comparisonValue; |
257 |
> |
break; |
258 |
> |
case Token::opLE: |
259 |
> |
match = propertyValue <= comparisonValue; |
260 |
> |
break; |
261 |
> |
case Token::opGE: |
262 |
> |
match = propertyValue >= comparisonValue; |
263 |
> |
break; |
264 |
> |
case Token::opGT: |
265 |
> |
match = propertyValue > comparisonValue; |
266 |
> |
break; |
267 |
> |
case Token::opEQ: |
268 |
> |
match = propertyValue == comparisonValue; |
269 |
> |
break; |
270 |
> |
case Token::opNE: |
271 |
> |
match = propertyValue != comparisonValue; |
272 |
> |
break; |
273 |
> |
} |
274 |
> |
if (match) |
275 |
> |
bs.setBitOn(sd->getGlobalIndex()); |
276 |
|
|
277 |
+ |
} |
278 |
+ |
|
279 |
+ |
void SelectionEvaluator::withinInstruction(const Token& instruction, BitSet& bs){ |
280 |
+ |
|
281 |
|
boost::any withinSpec = instruction.value; |
282 |
|
if (withinSpec.type() == typeid(float)){ |
283 |
< |
withinDistance(boost::any_cast<float>(withinSpec), bs, bsResult); |
283 |
> |
// |
284 |
|
return; |
285 |
|
} |
286 |
|
|
287 |
< |
evalError("Unrecognized within parameter:" + withinSpec); |
287 |
> |
evalError("Unrecognized within parameter"); |
288 |
|
} |
289 |
|
|
290 |
< |
void SelectionEvaluator::withinDistance(float distance, const BitSet& bs, const BitSet& bsResult) { |
240 |
< |
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 |
< |
void SelectionEvaluator::define() { |
290 |
> |
void SelectionEvaluator::define() { |
291 |
|
assert(statement.size() >= 3); |
292 |
|
|
293 |
|
std::string variable = boost::any_cast<std::string>(statement[1].value); |
294 |
< |
|
294 |
> |
|
295 |
|
variables.insert(std::make_pair(variable, expression(statement, 2))); |
258 |
– |
} |
296 |
|
} |
297 |
|
|
298 |
+ |
|
299 |
|
/** @todo */ |
300 |
|
void SelectionEvaluator::predefine(const std::string& script) { |
301 |
|
|
310 |
|
if (statement.size() > 2) { |
311 |
|
int tok = statement[1].tok; |
312 |
|
if (tok == Token::identifier || (tok & Token::predefinedset) == Token::predefinedset) { |
313 |
< |
std::string variable = (std::string)statement[1].value; |
313 |
> |
std::string variable = boost::any_cast<std::string>(statement[1].value); |
314 |
|
variables.insert(std::make_pair(variable, statement)); |
315 |
|
|
316 |
|
} else { |
328 |
|
|
329 |
|
} |
330 |
|
|
331 |
< |
void SelectionEvaluator::select(){ |
332 |
< |
viewer.setSelectionSet(expression(statement, 1)); |
331 |
> |
void SelectionEvaluator::select(BitSet& bs){ |
332 |
> |
bs = expression(statement, 1); |
333 |
|
} |
334 |
|
|
335 |
|
BitSet SelectionEvaluator::lookupValue(const std::string& variable){ |
340 |
|
if (i->second.type() == typeid(BitSet)) { |
341 |
|
return boost::any_cast<BitSet>(i->second); |
342 |
|
} else if (i->second.type() == typeid(std::vector<Token>)){ |
343 |
< |
BitSet bs = expression(boost::any_cast(i->second), 2); |
343 |
> |
BitSet bs = expression(boost::any_cast<std::vector<Token> >(i->second), 2); |
344 |
|
i->second = bs; /**@todo fixme */ |
345 |
|
return bs; |
346 |
|
} |
347 |
+ |
} else { |
348 |
+ |
unrecognizedIdentifier(variable); |
349 |
|
} |
350 |
+ |
} |
351 |
|
|
352 |
+ |
BitSet SelectionEvaluator::nameInstruction(const std::string& name){ |
353 |
+ |
|
354 |
+ |
return finder.match(name); |
355 |
+ |
|
356 |
+ |
} |
357 |
+ |
|
358 |
+ |
bool SelectionEvaluator::containDynamicToken(const std::vector<Token>& tokens){ |
359 |
+ |
std::vector<Token>::const_iterator i; |
360 |
+ |
for (i = tokens.begin(); i != tokens.end(); ++i) { |
361 |
+ |
if (i->tok & Token::dynamic) { |
362 |
+ |
return true; |
363 |
+ |
} |
364 |
+ |
} |
365 |
+ |
|
366 |
+ |
return false; |
367 |
+ |
} |
368 |
+ |
|
369 |
+ |
void SelectionEvaluator::clearDefinitionsAndLoadPredefined() { |
370 |
+ |
variables.clear(); |
371 |
+ |
//load predefine |
372 |
+ |
//predefine(); |
373 |
|
} |
374 |
|
|
375 |
+ |
BitSet SelectionEvaluator::evaluate() { |
376 |
+ |
BitSet bs(nStuntDouble); |
377 |
+ |
if (isLoaded_) { |
378 |
+ |
instructionDispatchLoop(bs); |
379 |
+ |
} |
380 |
+ |
|
381 |
+ |
return bs; |
382 |
|
} |
383 |
+ |
|
384 |
+ |
//BitSet SelectionEvaluator::evaluate(int frameNo) { |
385 |
+ |
// |
386 |
+ |
//} |
387 |
+ |
|
388 |
+ |
} |