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, const std::string& script) : info(si), finder(info){ |
53 |
+ |
} |
54 |
+ |
|
55 |
|
bool SelectionEvaluator::loadScript(const std::string& filename, const std::string& script) { |
56 |
|
this->filename = filename; |
57 |
|
this->script = script; |
70 |
|
|
71 |
|
void SelectionEvaluator::clearState() { |
72 |
|
for (int i = scriptLevelMax; --i >= 0; ) |
73 |
< |
stack[i] = null; |
73 |
> |
stack[i].clear(); |
74 |
|
scriptLevel = 0; |
75 |
|
error = false; |
76 |
< |
errorMessage = null; |
76 |
> |
errorMessage = ""; |
77 |
|
} |
78 |
|
|
79 |
|
bool SelectionEvaluator::loadScriptString(const std::string& script) { |
80 |
|
clearState(); |
81 |
< |
return loadScript(null, script); |
81 |
> |
return loadScript("", script); |
82 |
|
} |
83 |
|
|
84 |
|
bool SelectionEvaluator::loadScriptFile(const std::string& filename) { |
86 |
|
return loadScriptFileInternal(filename); |
87 |
|
} |
88 |
|
|
89 |
+ |
bool SelectionEvaluator::loadScriptFileInternal(const std::string & filename) { |
90 |
+ |
return true; /**@todo */ |
91 |
+ |
} |
92 |
|
|
93 |
|
void SelectionEvaluator::instructionDispatchLoop(){ |
94 |
|
|
95 |
< |
while ( pc < aatoken.length) { |
95 |
> |
while ( pc < aatoken.size()) { |
96 |
|
statement = aatoken[pc++]; |
97 |
< |
statementLength = statement.length; |
97 |
> |
statementLength = statement.size(); |
98 |
|
Token token = statement[0]; |
99 |
|
switch (token.tok) { |
100 |
< |
case Token.define: |
100 |
> |
case Token::define: |
101 |
|
define(); |
102 |
|
break; |
103 |
< |
case Token.select: |
103 |
> |
case Token::select: |
104 |
|
select(); |
105 |
|
break; |
106 |
|
default: |
110 |
|
} |
111 |
|
} |
112 |
|
|
113 |
< |
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(); |
113 |
> |
BitSet SelectionEvaluator::expression(const std::vector<Token>& code, int pcStart) { |
114 |
|
BitSet bs; |
115 |
< |
BitSet[] stack = new BitSet[10]; |
116 |
< |
int sp = 0; |
135 |
< |
|
115 |
> |
std::stack<BitSet> stack; |
116 |
> |
|
117 |
|
for (int pc = pcStart; ; ++pc) { |
118 |
|
Token instruction = code[pc]; |
119 |
< |
if (logMessages) |
139 |
< |
viewer.scriptStatus("instruction=" + instruction); |
119 |
> |
|
120 |
|
switch (instruction.tok) { |
121 |
< |
case Token.expressionBegin: |
121 |
> |
case Token::expressionBegin: |
122 |
|
break; |
123 |
< |
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); |
123 |
> |
case Token::expressionEnd: |
124 |
|
break; |
125 |
< |
case Token.none: |
126 |
< |
stack[sp++] = new BitSet(); |
125 |
> |
case Token::all: |
126 |
> |
bs = BitSet(nStuntDouble); |
127 |
> |
bs.setAll(); |
128 |
> |
stack.push(bs); |
129 |
|
break; |
130 |
< |
case Token.opOr: |
131 |
< |
bs = stack[--sp]; |
132 |
< |
stack[sp-1].or(bs); |
130 |
> |
case Token::none: |
131 |
> |
bs = BitSet(nStuntDouble); |
132 |
> |
stack.push(bs); |
133 |
|
break; |
134 |
< |
case Token.opAnd: |
135 |
< |
bs = stack[--sp]; |
136 |
< |
stack[sp-1].and(bs); |
134 |
> |
case Token::opOr: |
135 |
> |
bs = stack.top(); |
136 |
> |
stack.pop(); |
137 |
> |
stack.top() |= bs; |
138 |
|
break; |
139 |
< |
case Token.opNot: |
140 |
< |
bs = stack[sp - 1]; |
141 |
< |
notSet(bs); |
139 |
> |
case Token::opAnd: |
140 |
> |
bs = stack.top(); |
141 |
> |
stack.pop(); |
142 |
> |
stack.top() &= bs; |
143 |
|
break; |
144 |
< |
case Token.within: |
145 |
< |
bs = stack[sp - 1]; |
167 |
< |
stack[sp - 1] = new BitSet(); |
168 |
< |
withinInstruction(instruction, bs, stack[sp - 1]); |
144 |
> |
case Token::opNot: |
145 |
> |
stack.top().flip(); |
146 |
|
break; |
147 |
< |
case Token.selected: |
148 |
< |
stack[sp++] = copyBitSet(viewer.getSelectionSet()); |
147 |
> |
case Token::within: |
148 |
> |
|
149 |
> |
withinInstruction(instruction, stack.top()); |
150 |
|
break; |
151 |
< |
case Token.y: |
152 |
< |
case Token.amino: |
153 |
< |
case Token.backbone: |
154 |
< |
case Token.solvent: |
155 |
< |
case Token.identifier: |
178 |
< |
case Token.sidechain: |
179 |
< |
case Token.surface: |
180 |
< |
stack[sp++] = lookupIdentifierValue((String)instruction.value); |
151 |
> |
//case Token::selected: |
152 |
> |
// stack.push(getSelectionSet()); |
153 |
> |
// break; |
154 |
> |
case Token::name: |
155 |
> |
stack.push(nameInstruction(boost::any_cast<std::string>(instruction.value))); |
156 |
|
break; |
182 |
– |
case Token.opLT: |
183 |
– |
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); |
157 |
|
break; |
158 |
+ |
case Token::identifier: |
159 |
+ |
stack.push(lookupValue(boost::any_cast<std::string>(instruction.value))); |
160 |
+ |
break; |
161 |
+ |
case Token::opLT: |
162 |
+ |
case Token::opLE: |
163 |
+ |
case Token::opGE: |
164 |
+ |
case Token::opGT: |
165 |
+ |
case Token::opEQ: |
166 |
+ |
case Token::opNE: |
167 |
+ |
stack.push(comparatorInstruction(instruction)); |
168 |
+ |
break; |
169 |
|
default: |
170 |
|
unrecognizedExpression(); |
171 |
|
} |
172 |
|
} |
173 |
< |
if (sp != 1) |
173 |
> |
if (stack.size() != 1) |
174 |
|
evalError("atom expression compiler error - stack over/underflow"); |
175 |
< |
return stack[0]; |
175 |
> |
|
176 |
> |
return stack.top(); |
177 |
|
} |
178 |
|
|
179 |
|
|
180 |
|
|
181 |
< |
void SelectionEvaluator::comparatorInstruction(Token instruction, BitSet bs) { |
181 |
> |
BitSet SelectionEvaluator::comparatorInstruction(const Token& instruction) { |
182 |
|
int comparator = instruction.tok; |
183 |
|
int property = instruction.intValue; |
184 |
< |
float propertyValue = 0; // just for temperature |
185 |
< |
int comparisonValue = ((Integer)instruction.value).intValue(); |
186 |
< |
int numberOfAtoms = viewer.getAtomCount(); |
187 |
< |
Frame frame = viewer.getFrame(); |
188 |
< |
for (int i = 0; i < numberOfAtoms; ++i) { |
189 |
< |
Atom atom = frame.getAtomAt(i); |
190 |
< |
switch (property) { |
191 |
< |
case Token.atomno: |
192 |
< |
propertyValue = atom.getAtomNumber(); |
193 |
< |
break; |
194 |
< |
case Token.elemno: |
195 |
< |
propertyValue = atom.getElementNumber(); |
196 |
< |
break; |
197 |
< |
case Token.temperature: |
198 |
< |
propertyValue = atom.getBfactor100(); |
199 |
< |
if (propertyValue < 0) |
200 |
< |
continue; |
201 |
< |
propertyValue /= 100; |
202 |
< |
break; |
203 |
< |
case Token._atomID: |
204 |
< |
propertyValue = atom.getSpecialAtomID(); |
226 |
< |
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); |
184 |
> |
float comparisonValue = boost::any_cast<float>(instruction.value); |
185 |
> |
float propertyValue; |
186 |
> |
BitSet bs(nStuntDouble); |
187 |
> |
|
188 |
> |
SimInfo::MoleculeIterator mi; |
189 |
> |
Molecule* mol; |
190 |
> |
Molecule::AtomIterator ai; |
191 |
> |
Atom* atom; |
192 |
> |
Molecule::RigidBodyIterator rbIter; |
193 |
> |
RigidBody* rb; |
194 |
> |
|
195 |
> |
for (mol = info->beginMolecule(mi); mol != NULL; mol = info->nextMolecule(mi)) { |
196 |
> |
|
197 |
> |
for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) { |
198 |
> |
compareProperty(atom, bs, property, comparator, comparisonValue); |
199 |
> |
} |
200 |
> |
|
201 |
> |
//change the positions of atoms which belong to the rigidbodies |
202 |
> |
for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) { |
203 |
> |
compareProperty(rb, bs, property, comparator, comparisonValue); |
204 |
> |
} |
205 |
|
} |
270 |
– |
} |
206 |
|
|
207 |
< |
void SelectionEvaluator::withinInstruction(Token instruction, BitSet bs, BitSet bsResult) |
207 |
> |
return bs; |
208 |
> |
} |
209 |
|
|
210 |
< |
Object withinSpec = instruction.value; |
211 |
< |
if (withinSpec instanceof Float) { |
212 |
< |
withinDistance(((Float)withinSpec).floatValue(), bs, bsResult); |
210 |
> |
void SelectionEvaluator::compareProperty(StuntDouble* sd, BitSet& bs, int property, int comparator, float comparisonValue) { |
211 |
> |
double propertyValue; |
212 |
> |
switch (property) { |
213 |
> |
case Token::mass: |
214 |
> |
propertyValue = sd->getMass(); |
215 |
> |
break; |
216 |
> |
case Token::charge: |
217 |
> |
return; |
218 |
> |
//break; |
219 |
> |
case Token::dipole: |
220 |
> |
return; |
221 |
> |
//break; |
222 |
> |
default: |
223 |
> |
unrecognizedAtomProperty(property); |
224 |
> |
} |
225 |
> |
|
226 |
> |
bool match = false; |
227 |
> |
switch (comparator) { |
228 |
> |
case Token::opLT: |
229 |
> |
match = propertyValue < comparisonValue; |
230 |
> |
break; |
231 |
> |
case Token::opLE: |
232 |
> |
match = propertyValue <= comparisonValue; |
233 |
> |
break; |
234 |
> |
case Token::opGE: |
235 |
> |
match = propertyValue >= comparisonValue; |
236 |
> |
break; |
237 |
> |
case Token::opGT: |
238 |
> |
match = propertyValue > comparisonValue; |
239 |
> |
break; |
240 |
> |
case Token::opEQ: |
241 |
> |
match = propertyValue == comparisonValue; |
242 |
> |
break; |
243 |
> |
case Token::opNE: |
244 |
> |
match = propertyValue != comparisonValue; |
245 |
> |
break; |
246 |
> |
} |
247 |
> |
if (match) |
248 |
> |
bs.setBitOn(sd->getGlobalIndex()); |
249 |
> |
|
250 |
> |
} |
251 |
> |
|
252 |
> |
void SelectionEvaluator::withinInstruction(const Token& instruction, BitSet& bs){ |
253 |
> |
|
254 |
> |
boost::any withinSpec = instruction.value; |
255 |
> |
if (withinSpec.type() == typeid(float)){ |
256 |
> |
// |
257 |
|
return; |
258 |
|
} |
259 |
< |
evalError("Unrecognized within parameter:" + withinSpec); |
259 |
> |
|
260 |
> |
evalError("Unrecognized within parameter"); |
261 |
|
} |
262 |
|
|
263 |
< |
void SelectionEvaluator::withinDistance(float distance, BitSet bs, BitSet bsResult) { |
264 |
< |
Frame frame = viewer.getFrame(); |
265 |
< |
for (int i = frame.getAtomCount(); --i >= 0; ) { |
266 |
< |
if (bs.get(i)) { |
267 |
< |
Atom atom = frame.getAtomAt(i); |
268 |
< |
AtomIterator iterWithin = |
269 |
< |
frame.getWithinIterator(atom, distance); |
270 |
< |
while (iterWithin.hasNext()) |
271 |
< |
bsResult.set(iterWithin.next().getAtomIndex()); |
272 |
< |
} |
263 |
> |
void SelectionEvaluator::define() { |
264 |
> |
assert(statement.size() >= 3); |
265 |
> |
|
266 |
> |
std::string variable = boost::any_cast<std::string>(statement[1].value); |
267 |
> |
|
268 |
> |
variables.insert(std::make_pair(variable, expression(statement, 2))); |
269 |
> |
} |
270 |
> |
|
271 |
> |
|
272 |
> |
/** @todo */ |
273 |
> |
void SelectionEvaluator::predefine(const std::string& script) { |
274 |
> |
|
275 |
> |
if (compiler.compile("#predefine", script)) { |
276 |
> |
std::vector<std::vector<Token> > aatoken = compiler.getAatokenCompiled(); |
277 |
> |
if (aatoken.size() != 1) { |
278 |
> |
evalError("predefinition does not have exactly 1 command:" |
279 |
> |
+ script); |
280 |
> |
return; |
281 |
> |
} |
282 |
> |
std::vector<Token> statement = aatoken[0]; |
283 |
> |
if (statement.size() > 2) { |
284 |
> |
int tok = statement[1].tok; |
285 |
> |
if (tok == Token::identifier || (tok & Token::predefinedset) == Token::predefinedset) { |
286 |
> |
std::string variable = boost::any_cast<std::string>(statement[1].value); |
287 |
> |
variables.insert(std::make_pair(variable, statement)); |
288 |
> |
|
289 |
> |
} else { |
290 |
> |
evalError("invalid variable name:" + script); |
291 |
> |
} |
292 |
> |
}else { |
293 |
> |
evalError("bad predefinition length:" + script); |
294 |
> |
} |
295 |
> |
|
296 |
> |
|
297 |
> |
} else { |
298 |
> |
evalError("predefined set compile error:" + script + |
299 |
> |
"\ncompile error:" + compiler.getErrorMessage()); |
300 |
|
} |
293 |
– |
} |
301 |
|
|
295 |
– |
void SelectionEvaluator::define() throws ScriptException { |
296 |
– |
String variable = (String)statement[1].value; |
297 |
– |
variables.put(variable, (expression(statement, 2))); |
298 |
– |
} |
302 |
|
} |
303 |
|
|
304 |
< |
void SelectionEvaluator::predefine(Token[] statement) { |
305 |
< |
String variable = (String)statement[1].value; |
306 |
< |
variables.put(variable, statement); |
304 |
< |
} |
304 |
> |
void SelectionEvaluator::select(){ |
305 |
> |
//viewer.setSelectionSet(expression(statement, 1)); |
306 |
> |
} |
307 |
|
|
308 |
< |
void SelectionEvaluator::select(){ |
309 |
< |
// NOTE this is called by restrict() |
310 |
< |
if (statementLength == 1) { |
311 |
< |
viewer.selectAll(); |
312 |
< |
if (!viewer.getRasmolHydrogenSetting()) |
313 |
< |
viewer.excludeSelectionSet(getHydrogenSet()); |
314 |
< |
if (!viewer.getRasmolHeteroSetting()) |
315 |
< |
viewer.excludeSelectionSet(getHeteroSet()); |
308 |
> |
BitSet SelectionEvaluator::lookupValue(const std::string& variable){ |
309 |
> |
|
310 |
> |
std::map<std::string, boost::any>::iterator i = variables.find(variable); |
311 |
> |
|
312 |
> |
if (i != variables.end()) { |
313 |
> |
if (i->second.type() == typeid(BitSet)) { |
314 |
> |
return boost::any_cast<BitSet>(i->second); |
315 |
> |
} else if (i->second.type() == typeid(std::vector<Token>)){ |
316 |
> |
BitSet bs = expression(boost::any_cast<std::vector<Token> >(i->second), 2); |
317 |
> |
i->second = bs; /**@todo fixme */ |
318 |
> |
return bs; |
319 |
> |
} |
320 |
|
} else { |
321 |
< |
viewer.setSelectionSet(expression(statement, 1)); |
321 |
> |
unrecognizedIdentifier(variable); |
322 |
|
} |
323 |
< |
viewer.scriptStatus("" + viewer.getSelectionCount() + " atoms selected"); |
324 |
< |
} |
325 |
< |
|
326 |
< |
} |
323 |
> |
} |
324 |
> |
|
325 |
> |
BitSet SelectionEvaluator::nameInstruction(const std::string& name){ |
326 |
> |
BitSet bs(nStuntDouble); |
327 |
> |
|
328 |
> |
bool hasError = finder.match(name, bs); |
329 |
> |
|
330 |
> |
return bs; |
331 |
> |
} |
332 |
> |
|
333 |
> |
|
334 |
> |
} |