| 49 |  | namespace oopse { | 
| 50 |  |  | 
| 51 |  |  | 
| 52 | < | SelectionEvaluator::SelectionEvaluator(SimInfo* si) : info(si), finder(info), isLoaded_(false){ | 
| 53 | < |  | 
| 52 | > | SelectionEvaluator::SelectionEvaluator(SimInfo* si) | 
| 53 | > | : info(si), nameFinder(info), distanceFinder(info), indexFinder(info), isLoaded_(false){ | 
| 54 | > |  | 
| 55 | > | nStuntDouble = info->getNGlobalAtoms() + info->getNRigidBodies(); | 
| 56 |  | } | 
| 57 |  |  | 
| 58 |  | bool SelectionEvaluator::loadScript(const std::string& filename, const std::string& script) { | 
| 86 |  | } | 
| 87 |  |  | 
| 88 |  | void SelectionEvaluator::clearState() { | 
| 87 | – | //for (int i = scriptLevelMax; --i >= 0; ) | 
| 88 | – | //    stack[i].clear(); | 
| 89 | – | //scriptLevel = 0; | 
| 89 |  | error = false; | 
| 90 |  | errorMessage = ""; | 
| 91 |  | } | 
| 101 |  | } | 
| 102 |  |  | 
| 103 |  | bool SelectionEvaluator::loadScriptFileInternal(const  std::string & filename) { | 
| 104 | < | ifstream ifs(filename.c_str()); | 
| 104 | > | std::ifstream ifs(filename.c_str()); | 
| 105 |  | if (!ifs.is_open()) { | 
| 106 |  | return false; | 
| 107 |  | } | 
| 180 |  | case Token::name: | 
| 181 |  | stack.push(nameInstruction(boost::any_cast<std::string>(instruction.value))); | 
| 182 |  | break; | 
| 183 | + | case Token::index: | 
| 184 | + | stack.push(indexInstruction(instruction.value)); | 
| 185 |  | break; | 
| 186 |  | case Token::identifier: | 
| 187 |  | stack.push(lookupValue(boost::any_cast<std::string>(instruction.value))); | 
| 212 |  | float comparisonValue = boost::any_cast<float>(instruction.value); | 
| 213 |  | float propertyValue; | 
| 214 |  | BitSet bs(nStuntDouble); | 
| 215 | + | bs.clearAll(); | 
| 216 |  |  | 
| 217 |  | SimInfo::MoleculeIterator mi; | 
| 218 |  | Molecule* mol; | 
| 227 |  | compareProperty(atom, bs, property, comparator, comparisonValue); | 
| 228 |  | } | 
| 229 |  |  | 
| 228 | – | //change the positions of atoms which belong to the rigidbodies | 
| 230 |  | for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) { | 
| 231 |  | compareProperty(rb, bs, property, comparator, comparisonValue); | 
| 232 |  | } | 
| 236 |  | } | 
| 237 |  |  | 
| 238 |  | void SelectionEvaluator::compareProperty(StuntDouble* sd, BitSet& bs, int property, int comparator, float comparisonValue) { | 
| 239 | < | double propertyValue; | 
| 239 | > | double propertyValue = 0.0; | 
| 240 |  | switch (property) { | 
| 241 |  | case Token::mass: | 
| 242 |  | propertyValue = sd->getMass(); | 
| 243 |  | break; | 
| 244 |  | case Token::charge: | 
| 245 | < | return; | 
| 246 | < | //break; | 
| 247 | < | case Token::dipole: | 
| 248 | < | return; | 
| 249 | < | //break; | 
| 245 | > | if (sd->isAtom()){ | 
| 246 | > | Atom* atom = static_cast<Atom*>(sd); | 
| 247 | > | propertyValue = getCharge(atom); | 
| 248 | > | } else if (sd->isRigidBody()) { | 
| 249 | > | RigidBody* rb = static_cast<RigidBody*>(sd); | 
| 250 | > | RigidBody::AtomIterator ai; | 
| 251 | > | Atom* atom; | 
| 252 | > | for (atom = rb->beginAtom(ai); atom != NULL; atom = rb->nextAtom(ai)) { | 
| 253 | > | propertyValue+=  getCharge(atom); | 
| 254 | > | } | 
| 255 | > | } | 
| 256 | > | break; | 
| 257 |  | default: | 
| 258 |  | unrecognizedAtomProperty(property); | 
| 259 |  | } | 
| 285 |  | } | 
| 286 |  |  | 
| 287 |  | void SelectionEvaluator::withinInstruction(const Token& instruction, BitSet& bs){ | 
| 288 | < |  | 
| 288 | > |  | 
| 289 |  | boost::any withinSpec = instruction.value; | 
| 290 | + | float distance; | 
| 291 |  | if (withinSpec.type() == typeid(float)){ | 
| 292 | < | // | 
| 293 | < | return; | 
| 292 | > | distance = boost::any_cast<float>(withinSpec); | 
| 293 | > | } else if (withinSpec.type() == typeid(int)) { | 
| 294 | > | distance = boost::any_cast<int>(withinSpec); | 
| 295 | > | } else { | 
| 296 | > | evalError("casting error in withinInstruction"); | 
| 297 | > | bs.clearAll(); | 
| 298 |  | } | 
| 299 |  |  | 
| 300 | < | evalError("Unrecognized within parameter"); | 
| 300 | > | bs = distanceFinder.find(bs, distance); | 
| 301 |  | } | 
| 302 |  |  | 
| 303 |  | void SelectionEvaluator::define() { | 
| 305 |  |  | 
| 306 |  | std::string variable = boost::any_cast<std::string>(statement[1].value); | 
| 307 |  |  | 
| 308 | < | variables.insert(std::make_pair(variable, expression(statement, 2))); | 
| 308 | > | variables.insert(VariablesType::value_type(variable, expression(statement, 2))); | 
| 309 |  | } | 
| 310 |  |  | 
| 311 |  |  | 
| 324 |  | int tok = statement[1].tok; | 
| 325 |  | if (tok == Token::identifier || (tok & Token::predefinedset) == Token::predefinedset) { | 
| 326 |  | std::string variable = boost::any_cast<std::string>(statement[1].value); | 
| 327 | < | variables.insert(std::make_pair(variable, statement)); | 
| 327 | > | variables.insert(VariablesType::value_type(variable, statement)); | 
| 328 |  |  | 
| 329 |  | } else { | 
| 330 |  | evalError("invalid variable name:" + script); | 
| 347 |  |  | 
| 348 |  | BitSet SelectionEvaluator::lookupValue(const std::string& variable){ | 
| 349 |  |  | 
| 350 | + | BitSet bs(nStuntDouble); | 
| 351 |  | std::map<std::string, boost::any>::iterator i = variables.find(variable); | 
| 352 | < |  | 
| 352 | > |  | 
| 353 |  | if (i != variables.end()) { | 
| 354 |  | if (i->second.type() == typeid(BitSet)) { | 
| 355 |  | return boost::any_cast<BitSet>(i->second); | 
| 356 |  | } else if (i->second.type() ==  typeid(std::vector<Token>)){ | 
| 357 | < | BitSet bs = expression(boost::any_cast<std::vector<Token> >(i->second), 2); | 
| 357 | > | bs = expression(boost::any_cast<std::vector<Token> >(i->second), 2); | 
| 358 |  | i->second =  bs; /**@todo fixme */ | 
| 359 |  | return bs; | 
| 360 |  | } | 
| 361 |  | } else { | 
| 362 |  | unrecognizedIdentifier(variable); | 
| 363 |  | } | 
| 364 | + |  | 
| 365 | + | return bs; | 
| 366 |  | } | 
| 367 |  |  | 
| 368 |  | BitSet SelectionEvaluator::nameInstruction(const std::string& name){ | 
| 369 |  |  | 
| 370 | < | return finder.match(name); | 
| 370 | > | return nameFinder.match(name); | 
| 371 |  |  | 
| 372 |  | } | 
| 373 |  |  | 
| 391 |  | BitSet SelectionEvaluator::evaluate() { | 
| 392 |  | BitSet bs(nStuntDouble); | 
| 393 |  | if (isLoaded_) { | 
| 394 | + | pc = 0; | 
| 395 |  | instructionDispatchLoop(bs); | 
| 396 |  | } | 
| 397 |  |  | 
| 398 |  | return bs; | 
| 399 |  | } | 
| 383 | – |  | 
| 384 | – | //BitSet SelectionEvaluator::evaluate(int frameNo) { | 
| 385 | – | // | 
| 386 | – | //} | 
| 400 |  |  | 
| 401 | + | BitSet SelectionEvaluator::indexInstruction(const boost::any& value) { | 
| 402 | + | BitSet bs(nStuntDouble); | 
| 403 | + |  | 
| 404 | + | if (value.type() == typeid(int)) { | 
| 405 | + | int index = boost::any_cast<int>(value); | 
| 406 | + | if (index < 0 || index >= bs.size()) { | 
| 407 | + | invalidIndex(index); | 
| 408 | + | } else { | 
| 409 | + | bs = indexFinder.find(index); | 
| 410 | + | } | 
| 411 | + | } else if (value.type() == typeid(std::pair<int, int>)) { | 
| 412 | + | std::pair<int, int> indexRange= boost::any_cast<std::pair<int, int> >(value); | 
| 413 | + | assert(indexRange.first <= indexRange.second); | 
| 414 | + | if (indexRange.first < 0 || indexRange.second >= bs.size()) { | 
| 415 | + | invalidIndexRange(indexRange); | 
| 416 | + | }else { | 
| 417 | + | bs = indexFinder.find(indexRange.first, indexRange.second); | 
| 418 | + | } | 
| 419 | + | } | 
| 420 | + |  | 
| 421 | + | return bs; | 
| 422 |  | } | 
| 423 | + |  | 
| 424 | + |  | 
| 425 | + | double SelectionEvaluator::getCharge(Atom* atom) { | 
| 426 | + | double charge =0.0; | 
| 427 | + | AtomType* atomType = atom->getAtomType(); | 
| 428 | + | if (atomType->isCharge()) { | 
| 429 | + | GenericData* data = atomType->getPropertyByName("Charge"); | 
| 430 | + | if (data != NULL) { | 
| 431 | + | DoubleGenericData* doubleData= dynamic_cast<DoubleGenericData*>(data); | 
| 432 | + |  | 
| 433 | + | if (doubleData != NULL) { | 
| 434 | + | charge = doubleData->getData(); | 
| 435 | + |  | 
| 436 | + | } else { | 
| 437 | + | sprintf( painCave.errMsg, | 
| 438 | + | "Can not cast GenericData to DoubleGenericData\n"); | 
| 439 | + | painCave.severity = OOPSE_ERROR; | 
| 440 | + | painCave.isFatal = 1; | 
| 441 | + | simError(); | 
| 442 | + | } | 
| 443 | + | } | 
| 444 | + | } | 
| 445 | + |  | 
| 446 | + | return charge; | 
| 447 | + | } | 
| 448 | + |  | 
| 449 | + | } |