| 1 | tim | 277 | /* | 
| 2 |  |  | * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. | 
| 3 |  |  | * | 
| 4 |  |  | * The University of Notre Dame grants you ("Licensee") a | 
| 5 |  |  | * non-exclusive, royalty free, license to use, modify and | 
| 6 |  |  | * redistribute this software in source and binary code form, provided | 
| 7 |  |  | * that the following conditions are met: | 
| 8 |  |  | * | 
| 9 |  |  | * 1. Acknowledgement of the program authors must be made in any | 
| 10 |  |  | *    publication of scientific results based in part on use of the | 
| 11 |  |  | *    program.  An acceptable form of acknowledgement is citation of | 
| 12 |  |  | *    the article in which the program was described (Matthew | 
| 13 |  |  | *    A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher | 
| 14 |  |  | *    J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented | 
| 15 |  |  | *    Parallel Simulation Engine for Molecular Dynamics," | 
| 16 |  |  | *    J. Comput. Chem. 26, pp. 252-271 (2005)) | 
| 17 |  |  | * | 
| 18 |  |  | * 2. Redistributions of source code must retain the above copyright | 
| 19 |  |  | *    notice, this list of conditions and the following disclaimer. | 
| 20 |  |  | * | 
| 21 |  |  | * 3. Redistributions in binary form must reproduce the above copyright | 
| 22 |  |  | *    notice, this list of conditions and the following disclaimer in the | 
| 23 |  |  | *    documentation and/or other materials provided with the | 
| 24 |  |  | *    distribution. | 
| 25 |  |  | * | 
| 26 |  |  | * This software is provided "AS IS," without a warranty of any | 
| 27 |  |  | * kind. All express or implied conditions, representations and | 
| 28 |  |  | * warranties, including any implied warranty of merchantability, | 
| 29 |  |  | * fitness for a particular purpose or non-infringement, are hereby | 
| 30 |  |  | * excluded.  The University of Notre Dame and its licensors shall not | 
| 31 |  |  | * be liable for any damages suffered by licensee as a result of | 
| 32 |  |  | * using, modifying or distributing the software or its | 
| 33 |  |  | * derivatives. In no event will the University of Notre Dame or its | 
| 34 |  |  | * licensors be liable for any lost revenue, profit or data, or for | 
| 35 |  |  | * direct, indirect, special, consequential, incidental or punitive | 
| 36 |  |  | * damages, however caused and regardless of the theory of liability, | 
| 37 |  |  | * arising out of the use of or inability to use software, even if the | 
| 38 |  |  | * University of Notre Dame has been advised of the possibility of | 
| 39 |  |  | * such damages. | 
| 40 |  |  | */ | 
| 41 |  |  |  | 
| 42 | tim | 283 | #include <stack> | 
| 43 | tim | 277 | #include "selection/SelectionEvaluator.hpp" | 
| 44 | tim | 283 | #include "primitives/Atom.hpp" | 
| 45 |  |  | #include "primitives/DirectionalAtom.hpp" | 
| 46 |  |  | #include "primitives/RigidBody.hpp" | 
| 47 |  |  | #include "primitives/Molecule.hpp" | 
| 48 |  |  |  | 
| 49 | tim | 277 | namespace oopse { | 
| 50 |  |  |  | 
| 51 |  |  |  | 
| 52 | tim | 283 | SelectionEvaluator::SelectionEvaluator(SimInfo* si, const std::string& script) : info(si), finder(info){ | 
| 53 |  |  | } | 
| 54 |  |  |  | 
| 55 | tim | 278 | bool SelectionEvaluator::loadScript(const std::string& filename, const std::string& script) { | 
| 56 |  |  | this->filename = filename; | 
| 57 |  |  | this->script = script; | 
| 58 |  |  | if (! compiler.compile(filename, script)) { | 
| 59 |  |  | error = true; | 
| 60 |  |  | errorMessage = compiler.getErrorMessage(); | 
| 61 |  |  | return false; | 
| 62 |  |  | } | 
| 63 |  |  |  | 
| 64 |  |  | pc = 0; | 
| 65 |  |  | aatoken = compiler.getAatokenCompiled(); | 
| 66 |  |  | linenumbers = compiler.getLineNumbers(); | 
| 67 |  |  | lineIndices = compiler.getLineIndices(); | 
| 68 |  |  | return true; | 
| 69 | tim | 277 | } | 
| 70 | tim | 278 |  | 
| 71 |  |  | void SelectionEvaluator::clearState() { | 
| 72 |  |  | for (int i = scriptLevelMax; --i >= 0; ) | 
| 73 | tim | 281 | stack[i].clear(); | 
| 74 | tim | 278 | scriptLevel = 0; | 
| 75 |  |  | error = false; | 
| 76 | tim | 281 | errorMessage = ""; | 
| 77 | tim | 278 | } | 
| 78 |  |  |  | 
| 79 |  |  | bool SelectionEvaluator::loadScriptString(const std::string& script) { | 
| 80 |  |  | clearState(); | 
| 81 | tim | 281 | return loadScript("", script); | 
| 82 | tim | 278 | } | 
| 83 |  |  |  | 
| 84 |  |  | bool SelectionEvaluator::loadScriptFile(const std::string& filename) { | 
| 85 |  |  | clearState(); | 
| 86 |  |  | return loadScriptFileInternal(filename); | 
| 87 |  |  | } | 
| 88 |  |  |  | 
| 89 | tim | 283 | bool SelectionEvaluator::loadScriptFileInternal(const  std::string & filename) { | 
| 90 |  |  | return true; /**@todo */ | 
| 91 | tim | 282 | } | 
| 92 |  |  |  | 
| 93 | tim | 278 | void SelectionEvaluator::instructionDispatchLoop(){ | 
| 94 |  |  |  | 
| 95 | tim | 281 | while ( pc < aatoken.size()) { | 
| 96 | tim | 278 | statement = aatoken[pc++]; | 
| 97 | tim | 281 | statementLength = statement.size(); | 
| 98 | tim | 278 | Token token = statement[0]; | 
| 99 |  |  | switch (token.tok) { | 
| 100 | tim | 281 | case Token::define: | 
| 101 | tim | 278 | define(); | 
| 102 |  |  | break; | 
| 103 | tim | 281 | case Token::select: | 
| 104 | tim | 278 | select(); | 
| 105 |  |  | break; | 
| 106 |  |  | default: | 
| 107 |  |  | unrecognizedCommand(token); | 
| 108 |  |  | return; | 
| 109 |  |  | } | 
| 110 |  |  | } | 
| 111 |  |  | } | 
| 112 |  |  |  | 
| 113 | tim | 283 | BitSet SelectionEvaluator::expression(const std::vector<Token>& code, int pcStart) { | 
| 114 | tim | 278 | BitSet bs; | 
| 115 | tim | 283 | std::stack<BitSet> stack; | 
| 116 |  |  |  | 
| 117 | tim | 278 | for (int pc = pcStart; ; ++pc) { | 
| 118 |  |  | Token instruction = code[pc]; | 
| 119 | tim | 282 |  | 
| 120 | tim | 278 | switch (instruction.tok) { | 
| 121 | tim | 281 | case Token::expressionBegin: | 
| 122 | tim | 278 | break; | 
| 123 | tim | 281 | case Token::expressionEnd: | 
| 124 | tim | 282 | break; | 
| 125 | tim | 281 | case Token::all: | 
| 126 | tim | 283 | bs = BitSet(nStuntDouble); | 
| 127 |  |  | bs.setAll(); | 
| 128 |  |  | stack.push(bs); | 
| 129 | tim | 278 | break; | 
| 130 | tim | 281 | case Token::none: | 
| 131 | tim | 283 | bs = BitSet(nStuntDouble); | 
| 132 |  |  | stack.push(bs); | 
| 133 | tim | 278 | break; | 
| 134 | tim | 281 | case Token::opOr: | 
| 135 | tim | 283 | bs = stack.top(); | 
| 136 |  |  | stack.pop(); | 
| 137 |  |  | stack.top() |= bs; | 
| 138 | tim | 278 | break; | 
| 139 | tim | 281 | case Token::opAnd: | 
| 140 | tim | 283 | bs = stack.top(); | 
| 141 |  |  | stack.pop(); | 
| 142 |  |  | stack.top() &= bs; | 
| 143 | tim | 278 | break; | 
| 144 | tim | 281 | case Token::opNot: | 
| 145 | tim | 283 | stack.top().flip(); | 
| 146 | tim | 278 | break; | 
| 147 | tim | 281 | case Token::within: | 
| 148 | tim | 283 |  | 
| 149 |  |  | withinInstruction(instruction, stack.top()); | 
| 150 | tim | 278 | break; | 
| 151 | tim | 283 | //case Token::selected: | 
| 152 |  |  | //  stack.push(getSelectionSet()); | 
| 153 |  |  | //  break; | 
| 154 | tim | 281 | case Token::name: | 
| 155 | tim | 283 | stack.push(nameInstruction(boost::any_cast<std::string>(instruction.value))); | 
| 156 | tim | 278 | break; | 
| 157 | tim | 281 | break; | 
| 158 |  |  | case Token::identifier: | 
| 159 | tim | 283 | stack.push(lookupValue(boost::any_cast<std::string>(instruction.value))); | 
| 160 | tim | 281 | 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 | tim | 283 | stack.push(comparatorInstruction(instruction)); | 
| 168 | tim | 278 | break; | 
| 169 |  |  | default: | 
| 170 |  |  | unrecognizedExpression(); | 
| 171 |  |  | } | 
| 172 |  |  | } | 
| 173 | tim | 283 | if (stack.size() != 1) | 
| 174 | tim | 278 | evalError("atom expression compiler error - stack over/underflow"); | 
| 175 | tim | 283 |  | 
| 176 |  |  | return stack.top(); | 
| 177 | tim | 278 | } | 
| 178 |  |  |  | 
| 179 |  |  |  | 
| 180 |  |  |  | 
| 181 | tim | 283 | BitSet SelectionEvaluator::comparatorInstruction(const Token& instruction) { | 
| 182 | tim | 278 | int comparator = instruction.tok; | 
| 183 |  |  | int property = instruction.intValue; | 
| 184 | tim | 283 | 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 | tim | 281 |  | 
| 197 | tim | 283 | 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 | tim | 278 | } | 
| 206 |  |  |  | 
| 207 | tim | 283 | return bs; | 
| 208 |  |  | } | 
| 209 | tim | 278 |  | 
| 210 | tim | 283 | 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 | tim | 281 | boost::any withinSpec = instruction.value; | 
| 255 | tim | 282 | if (withinSpec.type() == typeid(float)){ | 
| 256 | tim | 283 | // | 
| 257 | tim | 278 | return; | 
| 258 |  |  | } | 
| 259 | tim | 282 |  | 
| 260 | tim | 283 | evalError("Unrecognized within parameter"); | 
| 261 | tim | 278 | } | 
| 262 |  |  |  | 
| 263 | tim | 283 | void SelectionEvaluator::define() { | 
| 264 | tim | 282 | assert(statement.size() >= 3); | 
| 265 |  |  |  | 
| 266 |  |  | std::string variable = boost::any_cast<std::string>(statement[1].value); | 
| 267 | tim | 283 |  | 
| 268 | tim | 281 | variables.insert(std::make_pair(variable, expression(statement, 2))); | 
| 269 | tim | 278 | } | 
| 270 |  |  |  | 
| 271 | tim | 283 |  | 
| 272 | tim | 282 | /** @todo */ | 
| 273 |  |  | void SelectionEvaluator::predefine(const std::string& script) { | 
| 274 | tim | 278 |  | 
| 275 | tim | 282 | 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 | tim | 283 | std::string variable = boost::any_cast<std::string>(statement[1].value); | 
| 287 | tim | 282 | 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 |  |  | } | 
| 301 |  |  |  | 
| 302 |  |  | } | 
| 303 |  |  |  | 
| 304 |  |  | void SelectionEvaluator::select(){ | 
| 305 | tim | 283 | //viewer.setSelectionSet(expression(statement, 1)); | 
| 306 | tim | 282 | } | 
| 307 |  |  |  | 
| 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 | tim | 283 | BitSet bs = expression(boost::any_cast<std::vector<Token> >(i->second), 2); | 
| 317 | tim | 282 | i->second =  bs; /**@todo fixme */ | 
| 318 |  |  | return bs; | 
| 319 |  |  | } | 
| 320 | tim | 283 | } else { | 
| 321 |  |  | unrecognizedIdentifier(variable); | 
| 322 | tim | 282 | } | 
| 323 |  |  | } | 
| 324 |  |  |  | 
| 325 | tim | 283 | 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 | tim | 282 | } |