| 1 |
/* |
| 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 |
#ifndef SELECTION_SELECTIONEVALUATOR_HPP |
| 43 |
#define SELECTION_SELECTIONEVALUATOR_HPP |
| 44 |
|
| 45 |
#include <map> |
| 46 |
#include <string> |
| 47 |
#include <vector> |
| 48 |
|
| 49 |
#include "brains/SimInfo.hpp" |
| 50 |
#include "selection/Token.hpp" |
| 51 |
#include "selection/SelectionCompiler.hpp" |
| 52 |
#include "selection/NameFinder.hpp" |
| 53 |
#include "selection/DistanceFinder.hpp" |
| 54 |
#include "utils/BitSet.hpp" |
| 55 |
#include "primitives/StuntDouble.hpp" |
| 56 |
#include "utils/StringUtils.hpp" |
| 57 |
namespace oopse { |
| 58 |
|
| 59 |
|
| 60 |
//class Context { |
| 61 |
// public: |
| 62 |
// |
| 63 |
// void clear() { |
| 64 |
// linenumbers.clear(); |
| 65 |
// lineIndices.clear(); |
| 66 |
// aatoken.clear(); |
| 67 |
// } |
| 68 |
// |
| 69 |
// std::string filename; |
| 70 |
// std::string script; |
| 71 |
// std::vector<int> linenumbers; |
| 72 |
// std::vector<int> lineIndices; |
| 73 |
// std::vector<std::vector<Token> > aatoken; |
| 74 |
// int pc; |
| 75 |
//}; |
| 76 |
|
| 77 |
|
| 78 |
/** |
| 79 |
* @class SelectionEvaluator SelectionEvaluator.hpp "selection/SelectionEvaluator" |
| 80 |
* @brief Evalute the tokens compiled by SelectionCompiler and return a BitSet |
| 81 |
*/ |
| 82 |
class SelectionEvaluator{ |
| 83 |
public: |
| 84 |
|
| 85 |
SelectionEvaluator(SimInfo* info); |
| 86 |
|
| 87 |
|
| 88 |
bool loadScriptString(const std::string& script); |
| 89 |
bool loadScriptFile(const std::string& filename); |
| 90 |
|
| 91 |
BitSet evaluate(); |
| 92 |
|
| 93 |
//BitSet evaluate(Snapshot* snapshot); |
| 94 |
|
| 95 |
/** |
| 96 |
* Tests if the result from evaluation of script is dynamic. |
| 97 |
*/ |
| 98 |
bool isDynamic() { |
| 99 |
return isDynamic_; |
| 100 |
} |
| 101 |
|
| 102 |
bool hadRuntimeError() const{ |
| 103 |
return error; |
| 104 |
} |
| 105 |
|
| 106 |
std::string getErrorMessage() const { |
| 107 |
return errorMessage; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
int getLinenumber() { |
| 112 |
return linenumbers[pc]; |
| 113 |
} |
| 114 |
|
| 115 |
std::string getLine() { |
| 116 |
int ichBegin = lineIndices[pc]; |
| 117 |
int ichEnd; |
| 118 |
if ((ichEnd = script.find('\r', ichBegin)) == std::string::npos && |
| 119 |
(ichEnd = script.find('\n', ichBegin)) == std::string::npos) { |
| 120 |
ichEnd = script.size(); |
| 121 |
} |
| 122 |
return script.substr(ichBegin, ichEnd); |
| 123 |
} |
| 124 |
|
| 125 |
private: |
| 126 |
|
| 127 |
void clearState(); |
| 128 |
|
| 129 |
bool loadScript(const std::string& filename, const std::string& script); |
| 130 |
|
| 131 |
bool loadScriptFileInternal(const std::string& filename); |
| 132 |
|
| 133 |
|
| 134 |
void clearDefinitionsAndLoadPredefined(); |
| 135 |
|
| 136 |
void define(); |
| 137 |
void select(BitSet& bs); |
| 138 |
void predefine(const std::string& script); |
| 139 |
|
| 140 |
void instructionDispatchLoop(BitSet& bs); |
| 141 |
|
| 142 |
void withinInstruction(const Token& instruction, BitSet& bs); |
| 143 |
|
| 144 |
BitSet comparatorInstruction(const Token& instruction); |
| 145 |
void compareProperty(StuntDouble* sd, BitSet& bs, int property, int comparator, float comparisonValue); |
| 146 |
BitSet nameInstruction(const std::string& name); |
| 147 |
BitSet indexInstruction(const boost::any& value); |
| 148 |
BitSet expression(const std::vector<Token>& tokens, int pc); |
| 149 |
|
| 150 |
BitSet lookupValue(const std::string& variable); |
| 151 |
|
| 152 |
void evalError(const std::string& message) { |
| 153 |
std::cerr << "SelectionEvaulator Error: " << message << std::endl; |
| 154 |
} |
| 155 |
|
| 156 |
void unrecognizedCommand(const Token& token) { |
| 157 |
evalError("unrecognized command:" + boost::any_cast<std::string>(token.value)); |
| 158 |
} |
| 159 |
|
| 160 |
void unrecognizedExpression() { |
| 161 |
evalError("unrecognized expression"); |
| 162 |
} |
| 163 |
|
| 164 |
void unrecognizedAtomProperty(int property){ |
| 165 |
evalError("unrecognized atom property"); |
| 166 |
} |
| 167 |
|
| 168 |
void unrecognizedIdentifier(const std::string& identifier) { |
| 169 |
evalError("unrecognized identifier:" + identifier); |
| 170 |
} |
| 171 |
|
| 172 |
void invalidIndexRange(std::pair<int, int> range) { |
| 173 |
evalError("invalid index range: [" + toString(range.first) + ", " + toString(range.second) + ")"); |
| 174 |
} |
| 175 |
|
| 176 |
void invalidIndex(int index) { |
| 177 |
evalError("invalid index : " + toString(index) ); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
bool containDynamicToken(const std::vector<Token>& tokens); |
| 182 |
|
| 183 |
SelectionCompiler compiler; |
| 184 |
|
| 185 |
//const static int scriptLevelMax = 10; |
| 186 |
//int scriptLevel; |
| 187 |
|
| 188 |
//Context stack[scriptLevelMax]; |
| 189 |
|
| 190 |
std::string filename; |
| 191 |
std::string script; |
| 192 |
std::vector<int> linenumbers; |
| 193 |
std::vector<int> lineIndices; |
| 194 |
std::vector<std::vector<Token> > aatoken; |
| 195 |
int pc; // program counter |
| 196 |
|
| 197 |
bool error; |
| 198 |
std::string errorMessage; |
| 199 |
|
| 200 |
std::vector<Token> statement; |
| 201 |
int statementLength; |
| 202 |
|
| 203 |
SimInfo* info; |
| 204 |
NameFinder nameFinder; |
| 205 |
DistanceFinder distanceFinder; |
| 206 |
int nStuntDouble; //natoms + nrigidbodies |
| 207 |
std::map<std::string, boost::any > variables; |
| 208 |
|
| 209 |
bool isDynamic_; |
| 210 |
bool isLoaded_; |
| 211 |
|
| 212 |
}; |
| 213 |
|
| 214 |
} |
| 215 |
#endif |