61 |
|
|
62 |
|
void SelectionEvaluator::clearState() { |
63 |
|
for (int i = scriptLevelMax; --i >= 0; ) |
64 |
< |
stack[i] = null; |
64 |
> |
stack[i].clear(); |
65 |
|
scriptLevel = 0; |
66 |
|
error = false; |
67 |
< |
errorMessage = null; |
67 |
> |
errorMessage = ""; |
68 |
|
} |
69 |
|
|
70 |
|
bool SelectionEvaluator::loadScriptString(const std::string& script) { |
71 |
|
clearState(); |
72 |
< |
return loadScript(null, script); |
72 |
> |
return loadScript("", script); |
73 |
|
} |
74 |
|
|
75 |
|
bool SelectionEvaluator::loadScriptFile(const std::string& filename) { |
77 |
|
return loadScriptFileInternal(filename); |
78 |
|
} |
79 |
|
|
80 |
+ |
bool SelectionEvaluator::loadScriptFileInternal(const string & filename) { |
81 |
|
|
82 |
+ |
} |
83 |
+ |
|
84 |
|
void SelectionEvaluator::instructionDispatchLoop(){ |
85 |
|
|
86 |
< |
while ( pc < aatoken.length) { |
86 |
> |
while ( pc < aatoken.size()) { |
87 |
|
statement = aatoken[pc++]; |
88 |
< |
statementLength = statement.length; |
88 |
> |
statementLength = statement.size(); |
89 |
|
Token token = statement[0]; |
90 |
|
switch (token.tok) { |
91 |
< |
case Token.define: |
91 |
> |
case Token::define: |
92 |
|
define(); |
93 |
|
break; |
94 |
< |
case Token.select: |
94 |
> |
case Token::select: |
95 |
|
select(); |
96 |
|
break; |
97 |
|
default: |
101 |
|
} |
102 |
|
} |
103 |
|
|
104 |
< |
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 { |
104 |
> |
BitSet SelectionEvaluator::expression(std::vector<Token>& code, int pcStart) { |
105 |
|
int numberOfAtoms = viewer.getAtomCount(); |
106 |
|
BitSet bs; |
107 |
|
BitSet[] stack = new BitSet[10]; |
109 |
|
|
110 |
|
for (int pc = pcStart; ; ++pc) { |
111 |
|
Token instruction = code[pc]; |
112 |
< |
if (logMessages) |
139 |
< |
viewer.scriptStatus("instruction=" + instruction); |
112 |
> |
|
113 |
|
switch (instruction.tok) { |
114 |
< |
case Token.expressionBegin: |
114 |
> |
case Token::expressionBegin: |
115 |
|
break; |
116 |
< |
case Token.expressionEnd: |
117 |
< |
break expression_loop; |
118 |
< |
case Token.all: |
116 |
> |
case Token::expressionEnd: |
117 |
> |
break; |
118 |
> |
case Token::all: |
119 |
|
bs = stack[sp++] = new BitSet(numberOfAtoms); |
120 |
|
for (int i = numberOfAtoms; --i >= 0; ) |
121 |
|
bs.set(i); |
122 |
|
break; |
123 |
< |
case Token.none: |
123 |
> |
case Token::none: |
124 |
|
stack[sp++] = new BitSet(); |
125 |
|
break; |
126 |
< |
case Token.opOr: |
126 |
> |
case Token::opOr: |
127 |
|
bs = stack[--sp]; |
128 |
|
stack[sp-1].or(bs); |
129 |
|
break; |
130 |
< |
case Token.opAnd: |
130 |
> |
case Token::opAnd: |
131 |
|
bs = stack[--sp]; |
132 |
|
stack[sp-1].and(bs); |
133 |
|
break; |
134 |
< |
case Token.opNot: |
134 |
> |
case Token::opNot: |
135 |
|
bs = stack[sp - 1]; |
136 |
|
notSet(bs); |
137 |
|
break; |
138 |
< |
case Token.within: |
138 |
> |
case Token::within: |
139 |
|
bs = stack[sp - 1]; |
140 |
|
stack[sp - 1] = new BitSet(); |
141 |
|
withinInstruction(instruction, bs, stack[sp - 1]); |
142 |
|
break; |
143 |
< |
case Token.selected: |
143 |
> |
case Token::selected: |
144 |
|
stack[sp++] = copyBitSet(viewer.getSelectionSet()); |
145 |
|
break; |
146 |
< |
case Token.y: |
147 |
< |
case Token.amino: |
175 |
< |
case Token.backbone: |
176 |
< |
case Token.solvent: |
177 |
< |
case Token.identifier: |
178 |
< |
case Token.sidechain: |
179 |
< |
case Token.surface: |
180 |
< |
stack[sp++] = lookupIdentifierValue((String)instruction.value); |
146 |
> |
case Token::name: |
147 |
> |
|
148 |
|
break; |
149 |
< |
case Token.opLT: |
150 |
< |
case Token.opLE: |
151 |
< |
case Token.opGE: |
152 |
< |
case Token.opGT: |
153 |
< |
case Token.opEQ: |
154 |
< |
case Token.opNE: |
149 |
> |
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 |
|
bs = stack[sp++] = new BitSet(); |
167 |
|
comparatorInstruction(instruction, bs); |
168 |
|
break; |
187 |
|
for (int i = 0; i < numberOfAtoms; ++i) { |
188 |
|
Atom atom = frame.getAtomAt(i); |
189 |
|
switch (property) { |
190 |
< |
case Token.atomno: |
191 |
< |
propertyValue = atom.getAtomNumber(); |
190 |
> |
case Token::mass: |
191 |
> |
//propertyValue = atom.getAtomNumber(); |
192 |
|
break; |
193 |
< |
case Token.elemno: |
194 |
< |
propertyValue = atom.getElementNumber(); |
193 |
> |
case Token::charge: |
194 |
> |
|
195 |
|
break; |
196 |
< |
case Token.temperature: |
197 |
< |
propertyValue = atom.getBfactor100(); |
220 |
< |
if (propertyValue < 0) |
221 |
< |
continue; |
222 |
< |
propertyValue /= 100; |
196 |
> |
case Token::dipole: |
197 |
> |
|
198 |
|
break; |
224 |
– |
case Token._atomID: |
225 |
– |
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; |
199 |
|
default: |
200 |
|
unrecognizedAtomProperty(property); |
201 |
|
} |
202 |
< |
boolean match = false; |
202 |
> |
bool match = false; |
203 |
|
switch (comparator) { |
204 |
< |
case Token.opLT: |
204 |
> |
case Token::opLT: |
205 |
|
match = propertyValue < comparisonValue; |
206 |
|
break; |
207 |
< |
case Token.opLE: |
207 |
> |
case Token::opLE: |
208 |
|
match = propertyValue <= comparisonValue; |
209 |
|
break; |
210 |
< |
case Token.opGE: |
210 |
> |
case Token::opGE: |
211 |
|
match = propertyValue >= comparisonValue; |
212 |
|
break; |
213 |
< |
case Token.opGT: |
213 |
> |
case Token::opGT: |
214 |
|
match = propertyValue > comparisonValue; |
215 |
|
break; |
216 |
< |
case Token.opEQ: |
216 |
> |
case Token::opEQ: |
217 |
|
match = propertyValue == comparisonValue; |
218 |
|
break; |
219 |
< |
case Token.opNE: |
219 |
> |
case Token::opNE: |
220 |
|
match = propertyValue != comparisonValue; |
221 |
|
break; |
222 |
|
} |
225 |
|
} |
226 |
|
} |
227 |
|
|
228 |
< |
void SelectionEvaluator::withinInstruction(Token instruction, BitSet bs, BitSet bsResult) |
228 |
> |
void SelectionEvaluator::withinInstruction(const Token& instruction, BitSet& bs, BitSet& bsResult) |
229 |
|
|
230 |
< |
Object withinSpec = instruction.value; |
231 |
< |
if (withinSpec instanceof Float) { |
232 |
< |
withinDistance(((Float)withinSpec).floatValue(), bs, bsResult); |
230 |
> |
boost::any withinSpec = instruction.value; |
231 |
> |
if (withinSpec.type() == typeid(float)){ |
232 |
> |
withinDistance(boost::any_cast<float>(withinSpec), bs, bsResult); |
233 |
|
return; |
234 |
|
} |
235 |
+ |
|
236 |
|
evalError("Unrecognized within parameter:" + withinSpec); |
237 |
|
} |
238 |
|
|
239 |
< |
void SelectionEvaluator::withinDistance(float distance, BitSet bs, BitSet bsResult) { |
239 |
> |
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)) { |
249 |
|
} |
250 |
|
} |
251 |
|
|
252 |
< |
void SelectionEvaluator::define() throws ScriptException { |
253 |
< |
String variable = (String)statement[1].value; |
254 |
< |
variables.put(variable, (expression(statement, 2))); |
252 |
> |
void SelectionEvaluator::define() { |
253 |
> |
assert(statement.size() >= 3); |
254 |
> |
|
255 |
> |
std::string variable = boost::any_cast<std::string>(statement[1].value); |
256 |
> |
|
257 |
> |
variables.insert(std::make_pair(variable, expression(statement, 2))); |
258 |
|
} |
259 |
|
} |
260 |
|
|
261 |
< |
void SelectionEvaluator::predefine(Token[] statement) { |
262 |
< |
String variable = (String)statement[1].value; |
303 |
< |
variables.put(variable, statement); |
304 |
< |
} |
261 |
> |
/** @todo */ |
262 |
> |
void SelectionEvaluator::predefine(const std::string& script) { |
263 |
|
|
264 |
< |
void SelectionEvaluator::select(){ |
265 |
< |
// NOTE this is called by restrict() |
266 |
< |
if (statementLength == 1) { |
267 |
< |
viewer.selectAll(); |
268 |
< |
if (!viewer.getRasmolHydrogenSetting()) |
269 |
< |
viewer.excludeSelectionSet(getHydrogenSet()); |
270 |
< |
if (!viewer.getRasmolHeteroSetting()) |
271 |
< |
viewer.excludeSelectionSet(getHeteroSet()); |
264 |
> |
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 |
< |
viewer.setSelectionSet(expression(statement, 1)); |
287 |
> |
evalError("predefined set compile error:" + script + |
288 |
> |
"\ncompile error:" + compiler.getErrorMessage()); |
289 |
|
} |
290 |
< |
viewer.scriptStatus("" + viewer.getSelectionCount() + " atoms selected"); |
291 |
< |
} |
292 |
< |
|
293 |
< |
} |
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 |
> |
} |