| 49 |  | namespace oopse { | 
| 50 |  |  | 
| 51 |  |  | 
| 52 | < | SelectionEvaluator::SelectionEvaluator(SimInfo* si, const std::string& script) : info(si), finder(info){ | 
| 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 |  |  | 
| 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 |  | } | 
| 102 |  | } | 
| 103 |  |  | 
| 104 |  | bool SelectionEvaluator::loadScriptFileInternal(const  std::string & filename) { | 
| 105 | < | return true; /**@todo */ | 
| 106 | < | } | 
| 105 | > | std::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 | + | 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(const std::vector<Token>& code, int pcStart) { | 
| 141 |  | BitSet bs; | 
| 142 |  | std::stack<BitSet> stack; | 
| 143 |  |  | 
| 144 | < | for (int pc = pcStart; ; ++pc) { | 
| 144 | > | for (int pc = pcStart; pc < code.size(); ++pc) { | 
| 145 |  | Token instruction = code[pc]; | 
| 146 |  |  | 
| 147 |  | switch (instruction.tok) { | 
| 181 |  | case Token::name: | 
| 182 |  | stack.push(nameInstruction(boost::any_cast<std::string>(instruction.value))); | 
| 183 |  | break; | 
| 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))); | 
| 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; | 
| 280 |  | } | 
| 281 |  |  | 
| 282 |  | void SelectionEvaluator::withinInstruction(const Token& instruction, BitSet& bs){ | 
| 283 | < |  | 
| 283 | > |  | 
| 284 |  | boost::any withinSpec = instruction.value; | 
| 285 | + | float distance; | 
| 286 |  | if (withinSpec.type() == typeid(float)){ | 
| 287 | < | // | 
| 288 | < | return; | 
| 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 |  |  | 
| 295 | < | evalError("Unrecognized within parameter"); | 
| 295 | > | bs = distanceFinder.find(bs, distance); | 
| 296 |  | } | 
| 297 |  |  | 
| 298 |  | void SelectionEvaluator::define() { | 
| 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)); | 
| 322 | > | variables.insert(VariablesType::value_type(variable, statement)); | 
| 323 |  |  | 
| 324 |  | } else { | 
| 325 |  | evalError("invalid variable name:" + script); | 
| 336 |  |  | 
| 337 |  | } | 
| 338 |  |  | 
| 339 | < | void SelectionEvaluator::select(){ | 
| 340 | < | //viewer.setSelectionSet(expression(statement, 1)); | 
| 339 | > | void SelectionEvaluator::select(BitSet& bs){ | 
| 340 | > | bs = expression(statement, 1); | 
| 341 |  | } | 
| 342 |  |  | 
| 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 | < |  | 
| 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 | < | BitSet bs = expression(boost::any_cast<std::vector<Token> >(i->second), 2); | 
| 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 |  | unrecognizedIdentifier(variable); | 
| 358 |  | } | 
| 359 | + |  | 
| 360 | + | return bs; | 
| 361 |  | } | 
| 362 |  |  | 
| 363 |  | BitSet SelectionEvaluator::nameInstruction(const std::string& name){ | 
| 326 | – | BitSet bs(nStuntDouble); | 
| 364 |  |  | 
| 365 | < | bool hasError = finder.match(name, bs); | 
| 366 | < |  | 
| 330 | < | return bs; | 
| 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 | + | pc = 0; | 
| 390 | + | instructionDispatchLoop(bs); | 
| 391 | + | } | 
| 392 | + |  | 
| 393 | + | return bs; | 
| 394 | + | } | 
| 395 | + |  | 
| 396 | + | BitSet SelectionEvaluator::indexInstruction(const boost::any& value) { | 
| 397 | + | BitSet bs(nStuntDouble); | 
| 398 | + |  | 
| 399 | + | if (value.type() == typeid(int)) { | 
| 400 | + | int index = boost::any_cast<int>(value); | 
| 401 | + | if (index < 0 || index >= bs.size()) { | 
| 402 | + | invalidIndex(index); | 
| 403 | + | } else { | 
| 404 | + | bs.setBitOn(index); | 
| 405 | + | } | 
| 406 | + | } else if (value.type() == typeid(std::pair<int, int>)) { | 
| 407 | + | std::pair<int, int> indexRange= boost::any_cast<std::pair<int, int> >(value); | 
| 408 | + | assert(indexRange.first <= indexRange.second); | 
| 409 | + | if (indexRange.first < 0 || indexRange.second >= bs.size()) { | 
| 410 | + | invalidIndexRange(indexRange); | 
| 411 | + | }else { | 
| 412 | + | bs.setRangeOn(indexRange.first, indexRange.second); | 
| 413 | + | } | 
| 414 | + | } | 
| 415 | + |  | 
| 416 | + | return bs; | 
| 417 | + | } | 
| 418 | + |  | 
| 419 | + | //BitSet SelectionEvaluator::evaluate(int frameNo) { | 
| 420 | + | // | 
| 421 | + | //} | 
| 422 | + |  | 
| 423 | + | } |