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