# | Line 39 | Line 39 | |
---|---|---|
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), nameFinder(info), distanceFinder(info), isLoaded_(false){ |
53 | + | nStuntDouble = info->getNGlobalAtoms() + info->getNRigidBodies(); |
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 | ||
# | Line 56 | Line 68 | bool SelectionEvaluator::loadScript(const std::string& | |
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] = null; |
89 | < | scriptLevel = 0; |
87 | > | //for (int i = scriptLevelMax; --i >= 0; ) |
88 | > | // stack[i].clear(); |
89 | > | //scriptLevel = 0; |
90 | error = false; | |
91 | < | errorMessage = null; |
91 | > | errorMessage = ""; |
92 | } | |
93 | ||
94 | bool SelectionEvaluator::loadScriptString(const std::string& script) { | |
95 | clearState(); | |
96 | < | return loadScript(null, script); |
96 | > | return loadScript("", script); |
97 | } | |
98 | ||
99 | bool SelectionEvaluator::loadScriptFile(const std::string& filename) { | |
# | Line 77 | Line 101 | bool SelectionEvaluator::loadScriptFile(const std::str | |
101 | return loadScriptFileInternal(filename); | |
102 | } | |
103 | ||
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 | < | void SelectionEvaluator::instructionDispatchLoop(){ |
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 | < | while ( pc < aatoken.length) { |
119 | > | void SelectionEvaluator::instructionDispatchLoop(BitSet& bs){ |
120 | > | |
121 | > | while ( pc < aatoken.size()) { |
122 | statement = aatoken[pc++]; | |
123 | < | statementLength = statement.length; |
123 | > | statementLength = statement.size(); |
124 | Token token = statement[0]; | |
125 | switch (token.tok) { | |
126 | < | case Token.define: |
126 | > | case Token::define: |
127 | define(); | |
128 | break; | |
129 | < | case Token.select: |
130 | < | select(); |
129 | > | case Token::select: |
130 | > | select(bs); |
131 | break; | |
132 | default: | |
133 | unrecognizedCommand(token); | |
134 | return; | |
135 | } | |
136 | } | |
137 | + | |
138 | } | |
139 | ||
140 | < | void SelectionEvaluator::predefine(String script) { |
102 | < | if (compiler.compile("#predefine", script)) { |
103 | < | Token [][] aatoken = compiler.getAatokenCompiled(); |
104 | < | if (aatoken.length != 1) { |
105 | < | viewer.scriptStatus("predefinition does not have exactly 1 command:" |
106 | < | + script); |
107 | < | return; |
108 | < | } |
109 | < | Token[] statement = aatoken[0]; |
110 | < | if (statement.length > 2) { |
111 | < | int tok = statement[1].tok; |
112 | < | if (tok == Token.identifier || |
113 | < | (tok & Token.predefinedset) == Token.predefinedset) { |
114 | < | String variable = (String)statement[1].value; |
115 | < | variables.put(variable, statement); |
116 | < | } else { |
117 | < | viewer.scriptStatus("invalid variable name:" + script); |
118 | < | } |
119 | < | } else { |
120 | < | viewer.scriptStatus("bad predefinition length:" + script); |
121 | < | } |
122 | < | } else { |
123 | < | viewer.scriptStatus("predefined set compile error:" + script + |
124 | < | "\ncompile error:" + compiler.getErrorMessage()); |
125 | < | } |
126 | < | } |
127 | < | |
128 | < | |
129 | < | |
130 | < | BitSet SelectionEvaluator::expression(Token[] code, int pcStart) throws ScriptException { |
131 | < | 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 | < | |
136 | < | 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 | < | if (logMessages) |
139 | < | viewer.scriptStatus("instruction=" + instruction); |
146 | > | |
147 | switch (instruction.tok) { | |
148 | < | case Token.expressionBegin: |
148 | > | case Token::expressionBegin: |
149 | break; | |
150 | < | case Token.expressionEnd: |
144 | < | break expression_loop; |
145 | < | case Token.all: |
146 | < | bs = stack[sp++] = new BitSet(numberOfAtoms); |
147 | < | for (int i = numberOfAtoms; --i >= 0; ) |
148 | < | bs.set(i); |
150 | > | case Token::expressionEnd: |
151 | break; | |
152 | < | case Token.none: |
153 | < | stack[sp++] = new BitSet(); |
152 | > | case Token::all: |
153 | > | bs = BitSet(nStuntDouble); |
154 | > | bs.setAll(); |
155 | > | stack.push(bs); |
156 | break; | |
157 | < | case Token.opOr: |
158 | < | bs = stack[--sp]; |
159 | < | stack[sp-1].or(bs); |
157 | > | case Token::none: |
158 | > | bs = BitSet(nStuntDouble); |
159 | > | stack.push(bs); |
160 | break; | |
161 | < | case Token.opAnd: |
162 | < | bs = stack[--sp]; |
163 | < | stack[sp-1].and(bs); |
161 | > | case Token::opOr: |
162 | > | bs = stack.top(); |
163 | > | stack.pop(); |
164 | > | stack.top() |= bs; |
165 | break; | |
166 | < | case Token.opNot: |
167 | < | bs = stack[sp - 1]; |
168 | < | notSet(bs); |
166 | > | case Token::opAnd: |
167 | > | bs = stack.top(); |
168 | > | stack.pop(); |
169 | > | stack.top() &= bs; |
170 | break; | |
171 | < | case Token.within: |
172 | < | bs = stack[sp - 1]; |
167 | < | stack[sp - 1] = new BitSet(); |
168 | < | withinInstruction(instruction, bs, stack[sp - 1]); |
171 | > | case Token::opNot: |
172 | > | stack.top().flip(); |
173 | break; | |
174 | < | case Token.selected: |
175 | < | stack[sp++] = copyBitSet(viewer.getSelectionSet()); |
174 | > | case Token::within: |
175 | > | |
176 | > | withinInstruction(instruction, stack.top()); |
177 | break; | |
178 | < | case Token.y: |
179 | < | case Token.amino: |
180 | < | case Token.backbone: |
181 | < | case Token.solvent: |
182 | < | case Token.identifier: |
178 | < | case Token.sidechain: |
179 | < | case Token.surface: |
180 | < | stack[sp++] = lookupIdentifierValue((String)instruction.value); |
178 | > | //case Token::selected: |
179 | > | // stack.push(getSelectionSet()); |
180 | > | // break; |
181 | > | case Token::name: |
182 | > | stack.push(nameInstruction(boost::any_cast<std::string>(instruction.value))); |
183 | break; | |
184 | < | case Token.opLT: |
185 | < | case Token.opLE: |
184 | < | case Token.opGE: |
185 | < | case Token.opGT: |
186 | < | case Token.opEQ: |
187 | < | case Token.opNE: |
188 | < | bs = stack[sp++] = new BitSet(); |
189 | < | comparatorInstruction(instruction, bs); |
184 | > | case Token::index: |
185 | > | stack.push(indexInstruction(instruction.value)); |
186 | break; | |
187 | + | case Token::identifier: |
188 | + | stack.push(lookupValue(boost::any_cast<std::string>(instruction.value))); |
189 | + | break; |
190 | + | case Token::opLT: |
191 | + | case Token::opLE: |
192 | + | case Token::opGE: |
193 | + | case Token::opGT: |
194 | + | case Token::opEQ: |
195 | + | case Token::opNE: |
196 | + | stack.push(comparatorInstruction(instruction)); |
197 | + | break; |
198 | default: | |
199 | unrecognizedExpression(); | |
200 | } | |
201 | } | |
202 | < | if (sp != 1) |
202 | > | if (stack.size() != 1) |
203 | evalError("atom expression compiler error - stack over/underflow"); | |
204 | < | return stack[0]; |
204 | > | |
205 | > | return stack.top(); |
206 | } | |
207 | ||
208 | ||
209 | ||
210 | < | void SelectionEvaluator::comparatorInstruction(Token instruction, BitSet bs) { |
210 | > | BitSet SelectionEvaluator::comparatorInstruction(const Token& instruction) { |
211 | int comparator = instruction.tok; | |
212 | int property = instruction.intValue; | |
213 | < | float propertyValue = 0; // just for temperature |
214 | < | int comparisonValue = ((Integer)instruction.value).intValue(); |
215 | < | int numberOfAtoms = viewer.getAtomCount(); |
216 | < | Frame frame = viewer.getFrame(); |
217 | < | for (int i = 0; i < numberOfAtoms; ++i) { |
218 | < | Atom atom = frame.getAtomAt(i); |
219 | < | switch (property) { |
220 | < | case Token.atomno: |
221 | < | propertyValue = atom.getAtomNumber(); |
222 | < | break; |
223 | < | case Token.elemno: |
224 | < | propertyValue = atom.getElementNumber(); |
225 | < | break; |
226 | < | case Token.temperature: |
227 | < | propertyValue = atom.getBfactor100(); |
228 | < | if (propertyValue < 0) |
229 | < | continue; |
230 | < | propertyValue /= 100; |
231 | < | break; |
232 | < | case Token._atomID: |
233 | < | propertyValue = atom.getSpecialAtomID(); |
234 | < | if (propertyValue < 0) |
227 | < | continue; |
228 | < | break; |
229 | < | case Token._structure: |
230 | < | propertyValue = getProteinStructureType(atom); |
231 | < | if (propertyValue == -1) |
232 | < | continue; |
233 | < | break; |
234 | < | case Token.radius: |
235 | < | propertyValue = atom.getRasMolRadius(); |
236 | < | break; |
237 | < | case Token._bondedcount: |
238 | < | propertyValue = atom.getCovalentBondCount(); |
239 | < | break; |
240 | < | case Token.model: |
241 | < | propertyValue = atom.getModelTagNumber(); |
242 | < | break; |
243 | < | default: |
244 | < | unrecognizedAtomProperty(property); |
245 | < | } |
246 | < | boolean match = false; |
247 | < | switch (comparator) { |
248 | < | case Token.opLT: |
249 | < | match = propertyValue < comparisonValue; |
250 | < | break; |
251 | < | case Token.opLE: |
252 | < | match = propertyValue <= comparisonValue; |
253 | < | break; |
254 | < | case Token.opGE: |
255 | < | match = propertyValue >= comparisonValue; |
256 | < | break; |
257 | < | case Token.opGT: |
258 | < | match = propertyValue > comparisonValue; |
259 | < | break; |
260 | < | case Token.opEQ: |
261 | < | match = propertyValue == comparisonValue; |
262 | < | break; |
263 | < | case Token.opNE: |
264 | < | match = propertyValue != comparisonValue; |
265 | < | break; |
266 | < | } |
267 | < | if (match) |
268 | < | bs.set(i); |
213 | > | float comparisonValue = boost::any_cast<float>(instruction.value); |
214 | > | float propertyValue; |
215 | > | BitSet bs(nStuntDouble); |
216 | > | bs.clearAll(); |
217 | > | |
218 | > | SimInfo::MoleculeIterator mi; |
219 | > | Molecule* mol; |
220 | > | Molecule::AtomIterator ai; |
221 | > | Atom* atom; |
222 | > | Molecule::RigidBodyIterator rbIter; |
223 | > | RigidBody* rb; |
224 | > | |
225 | > | for (mol = info->beginMolecule(mi); mol != NULL; mol = info->nextMolecule(mi)) { |
226 | > | |
227 | > | for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) { |
228 | > | compareProperty(atom, bs, property, comparator, comparisonValue); |
229 | > | } |
230 | > | |
231 | > | //change the positions of atoms which belong to the rigidbodies |
232 | > | for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) { |
233 | > | compareProperty(rb, bs, property, comparator, comparisonValue); |
234 | > | } |
235 | } | |
270 | – | } |
236 | ||
237 | < | void SelectionEvaluator::withinInstruction(Token instruction, BitSet bs, BitSet bsResult) |
237 | > | return bs; |
238 | > | } |
239 | ||
240 | < | Object withinSpec = instruction.value; |
241 | < | if (withinSpec instanceof Float) { |
242 | < | withinDistance(((Float)withinSpec).floatValue(), bs, bsResult); |
243 | < | return; |
240 | > | void SelectionEvaluator::compareProperty(StuntDouble* sd, BitSet& bs, int property, int comparator, float comparisonValue) { |
241 | > | double propertyValue; |
242 | > | switch (property) { |
243 | > | case Token::mass: |
244 | > | propertyValue = sd->getMass(); |
245 | > | break; |
246 | > | case Token::charge: |
247 | > | return; |
248 | > | //break; |
249 | > | case Token::dipole: |
250 | > | return; |
251 | > | //break; |
252 | > | default: |
253 | > | unrecognizedAtomProperty(property); |
254 | > | } |
255 | > | |
256 | > | bool match = false; |
257 | > | switch (comparator) { |
258 | > | case Token::opLT: |
259 | > | match = propertyValue < comparisonValue; |
260 | > | break; |
261 | > | case Token::opLE: |
262 | > | match = propertyValue <= comparisonValue; |
263 | > | break; |
264 | > | case Token::opGE: |
265 | > | match = propertyValue >= comparisonValue; |
266 | > | break; |
267 | > | case Token::opGT: |
268 | > | match = propertyValue > comparisonValue; |
269 | > | break; |
270 | > | case Token::opEQ: |
271 | > | match = propertyValue == comparisonValue; |
272 | > | break; |
273 | > | case Token::opNE: |
274 | > | match = propertyValue != comparisonValue; |
275 | > | break; |
276 | > | } |
277 | > | if (match) |
278 | > | bs.setBitOn(sd->getGlobalIndex()); |
279 | > | |
280 | > | } |
281 | > | |
282 | > | void SelectionEvaluator::withinInstruction(const Token& instruction, BitSet& bs){ |
283 | > | |
284 | > | boost::any withinSpec = instruction.value; |
285 | > | float distance; |
286 | > | if (withinSpec.type() == typeid(float)){ |
287 | > | distance = boost::any_cast<float>(withinSpec); |
288 | > | } else if (withinSpec.type() == typeid(int)) { |
289 | > | distance = boost::any_cast<int>(withinSpec); |
290 | > | } else { |
291 | > | evalError("casting error in withinInstruction"); |
292 | > | bs.clearAll(); |
293 | } | |
294 | < | evalError("Unrecognized within parameter:" + withinSpec); |
294 | > | |
295 | > | bs = distanceFinder.find(bs, distance); |
296 | } | |
297 | ||
298 | < | void SelectionEvaluator::withinDistance(float distance, BitSet bs, BitSet bsResult) { |
299 | < | Frame frame = viewer.getFrame(); |
300 | < | for (int i = frame.getAtomCount(); --i >= 0; ) { |
301 | < | if (bs.get(i)) { |
302 | < | Atom atom = frame.getAtomAt(i); |
303 | < | AtomIterator iterWithin = |
304 | < | frame.getWithinIterator(atom, distance); |
305 | < | while (iterWithin.hasNext()) |
306 | < | bsResult.set(iterWithin.next().getAtomIndex()); |
307 | < | } |
298 | > | void SelectionEvaluator::define() { |
299 | > | assert(statement.size() >= 3); |
300 | > | |
301 | > | std::string variable = boost::any_cast<std::string>(statement[1].value); |
302 | > | |
303 | > | variables.insert(std::make_pair(variable, expression(statement, 2))); |
304 | > | } |
305 | > | |
306 | > | |
307 | > | /** @todo */ |
308 | > | void SelectionEvaluator::predefine(const std::string& script) { |
309 | > | |
310 | > | if (compiler.compile("#predefine", script)) { |
311 | > | std::vector<std::vector<Token> > aatoken = compiler.getAatokenCompiled(); |
312 | > | if (aatoken.size() != 1) { |
313 | > | evalError("predefinition does not have exactly 1 command:" |
314 | > | + script); |
315 | > | return; |
316 | > | } |
317 | > | std::vector<Token> statement = aatoken[0]; |
318 | > | if (statement.size() > 2) { |
319 | > | int tok = statement[1].tok; |
320 | > | if (tok == Token::identifier || (tok & Token::predefinedset) == Token::predefinedset) { |
321 | > | std::string variable = boost::any_cast<std::string>(statement[1].value); |
322 | > | variables.insert(std::make_pair(variable, statement)); |
323 | > | |
324 | > | } else { |
325 | > | evalError("invalid variable name:" + script); |
326 | > | } |
327 | > | }else { |
328 | > | evalError("bad predefinition length:" + script); |
329 | > | } |
330 | > | |
331 | > | |
332 | > | } else { |
333 | > | evalError("predefined set compile error:" + script + |
334 | > | "\ncompile error:" + compiler.getErrorMessage()); |
335 | } | |
293 | – | } |
336 | ||
295 | – | void SelectionEvaluator::define() throws ScriptException { |
296 | – | String variable = (String)statement[1].value; |
297 | – | variables.put(variable, (expression(statement, 2))); |
298 | – | } |
337 | } | |
338 | ||
339 | < | void SelectionEvaluator::predefine(Token[] statement) { |
340 | < | String variable = (String)statement[1].value; |
341 | < | variables.put(variable, statement); |
304 | < | } |
339 | > | void SelectionEvaluator::select(BitSet& bs){ |
340 | > | bs = expression(statement, 1); |
341 | > | } |
342 | ||
343 | < | void SelectionEvaluator::select(){ |
344 | < | // NOTE this is called by restrict() |
345 | < | if (statementLength == 1) { |
346 | < | viewer.selectAll(); |
347 | < | if (!viewer.getRasmolHydrogenSetting()) |
348 | < | viewer.excludeSelectionSet(getHydrogenSet()); |
349 | < | if (!viewer.getRasmolHeteroSetting()) |
350 | < | viewer.excludeSelectionSet(getHeteroSet()); |
343 | > | BitSet SelectionEvaluator::lookupValue(const std::string& variable){ |
344 | > | |
345 | > | BitSet bs(nStuntDouble); |
346 | > | std::map<std::string, boost::any>::iterator i = variables.find(variable); |
347 | > | |
348 | > | if (i != variables.end()) { |
349 | > | if (i->second.type() == typeid(BitSet)) { |
350 | > | return boost::any_cast<BitSet>(i->second); |
351 | > | } else if (i->second.type() == typeid(std::vector<Token>)){ |
352 | > | bs = expression(boost::any_cast<std::vector<Token> >(i->second), 2); |
353 | > | i->second = bs; /**@todo fixme */ |
354 | > | return bs; |
355 | > | } |
356 | } else { | |
357 | < | viewer.setSelectionSet(expression(statement, 1)); |
357 | > | unrecognizedIdentifier(variable); |
358 | } | |
359 | < | viewer.scriptStatus("" + viewer.getSelectionCount() + " atoms selected"); |
360 | < | } |
361 | < | |
362 | < | } |
359 | > | |
360 | > | return bs; |
361 | > | } |
362 | > | |
363 | > | BitSet SelectionEvaluator::nameInstruction(const std::string& name){ |
364 | > | |
365 | > | return nameFinder.match(name); |
366 | > | |
367 | > | } |
368 | > | |
369 | > | bool SelectionEvaluator::containDynamicToken(const std::vector<Token>& tokens){ |
370 | > | std::vector<Token>::const_iterator i; |
371 | > | for (i = tokens.begin(); i != tokens.end(); ++i) { |
372 | > | if (i->tok & Token::dynamic) { |
373 | > | return true; |
374 | > | } |
375 | > | } |
376 | > | |
377 | > | return false; |
378 | > | } |
379 | > | |
380 | > | void SelectionEvaluator::clearDefinitionsAndLoadPredefined() { |
381 | > | variables.clear(); |
382 | > | //load predefine |
383 | > | //predefine(); |
384 | > | } |
385 | > | |
386 | > | BitSet SelectionEvaluator::evaluate() { |
387 | > | BitSet bs(nStuntDouble); |
388 | > | if (isLoaded_) { |
389 | > | instructionDispatchLoop(bs); |
390 | > | } |
391 | > | |
392 | > | return bs; |
393 | > | } |
394 | > | |
395 | > | BitSet SelectionEvaluator::indexInstruction(const boost::any& value) { |
396 | > | BitSet bs(nStuntDouble); |
397 | > | |
398 | > | if (value.type() == typeid(int)) { |
399 | > | int index = boost::any_cast<int>(value); |
400 | > | if (index < 0 || index >= bs.size()) { |
401 | > | invalidIndex(index); |
402 | > | } else { |
403 | > | bs.setBitOn(index); |
404 | > | } |
405 | > | } else if (value.type() == typeid(std::pair<int, int>)) { |
406 | > | std::pair<int, int> indexRange= boost::any_cast<std::pair<int, int> >(value); |
407 | > | assert(indexRange.first <= indexRange.second); |
408 | > | if (indexRange.first < 0 || indexRange.second >= bs.size()) { |
409 | > | invalidIndexRange(indexRange); |
410 | > | }else { |
411 | > | bs.setRangeOn(indexRange.first, indexRange.second); |
412 | > | } |
413 | > | } |
414 | > | |
415 | > | return bs; |
416 | > | } |
417 | > | |
418 | > | //BitSet SelectionEvaluator::evaluate(int frameNo) { |
419 | > | // |
420 | > | //} |
421 | > | |
422 | > | } |
– | Removed lines |
+ | Added lines |
< | Changed lines |
> | Changed lines |