--- trunk/src/selection/SelectionEvaluator.hpp 2005/02/02 23:13:11 281 +++ trunk/src/selection/SelectionEvaluator.hpp 2005/12/02 15:38:03 770 @@ -45,119 +45,158 @@ #include #include #include +#include #include "brains/SimInfo.hpp" -#include "selection/Token.hpp" +#include "selection/SelectionToken.hpp" #include "selection/SelectionCompiler.hpp" -#include "utils/BitSet.hpp" - +#include "selection/NameFinder.hpp" +#include "selection/DistanceFinder.hpp" +#include "selection/IndexFinder.hpp" +#include "utils/OOPSEBitSet.hpp" +#include "primitives/StuntDouble.hpp" +#include "utils/StringUtils.hpp" namespace oopse { -class Context { - public: - - void clear() { - linenumbers.clear(); - lineIndices.clear(); - aatoken.clear(); - } - - std::string filename; - std::string script; - std::vector linenumbers; - std::vector lineIndices; - std::vector > aatoken; - int pc; -}; -/** - * @class SelectionEvaluator SelectionEvaluator.hpp "selection/SelectionEvaluator" - * @brief Evalute the tokens compiled by SelectionCompiler and return a BitSet - */ -class SelectionEvaluator{ - public: + /** + * @class SelectionEvaluator SelectionEvaluator.hpp "selection/SelectionEvaluator" + * @brief Evalute the tokens compiled by SelectionCompiler and return a OOPSEBitSet + */ + class SelectionEvaluator{ + public: - SelectionEvaluator(SimInfo* info, const std::string& script); + SelectionEvaluator(SimInfo* info); - BitSet evaluate(); + bool loadScriptString(const std::string& script); + bool loadScriptFile(const std::string& filename); - BitSet evaluate(int frameNo); + OOPSEBitSet evaluate(); + + /** + * Tests if the result from evaluation of script is dynamic. + */ + bool isDynamic() { + return isDynamic_; + } - /** - * Tests if the result from evaluation of script is static. - * @return true if the result keeps the same even the frame change, otherwise return false - */ - bool isStatic(); + bool hadRuntimeError() const{ + return error; + } - bool hadRuntimeError() const{ - return error; - } + std::string getErrorMessage() const { + return errorMessage; + } - std::string getErrorMessage() const { - return errorMessage; - } + int getLinenumber() { + return linenumbers[pc]; + } - int getLinenumber() { - return linenumbers[pc]; - } - - std::string getLine() { - int ichBegin = lineIndices[pc]; - int ichEnd; - if ((ichEnd = script.find('\r', ichBegin)) == std::string::npos && - (ichEnd = script.find('\n', ichBegin)) == std::string::npos) { - ichEnd = script.size(); - } - return script.substr(ichBegin, ichEnd); - } + std::string getLine() { + int ichBegin = lineIndices[pc]; + int ichEnd; + if ((ichEnd = script.find('\r', ichBegin)) == std::string::npos && + (ichEnd = script.find('\n', ichBegin)) == std::string::npos) { + ichEnd = script.size(); + } + return script.substr(ichBegin, ichEnd); + } - private: + private: - void clearState(); + void clearState(); - bool loadScript(const std::string& filename, const std::string& script); - bool loadScriptString(const std::string& script); - bool loadScriptFileInternal(const std::string& filename); - bool loadScriptFile(const std::string& filename); + bool loadScript(const std::string& filename, const std::string& script); - void clearDefinitionsAndLoadPredefined(); + bool loadScriptFileInternal(const std::string& filename); + + + void clearDefinitionsAndLoadPredefined(); - void define(); - - void predefine(); + void define(); + void select(OOPSEBitSet& bs); + void predefine(const std::string& script); - void instructionDispatchLoop(); + void instructionDispatchLoop(OOPSEBitSet& bs); - void withinInstruction(const Token& instruction, BitSet& , BitSet&); + void withinInstruction(const Token& instruction, OOPSEBitSet& bs); + + OOPSEBitSet comparatorInstruction(const Token& instruction); + void compareProperty(StuntDouble* sd, OOPSEBitSet& bs, int property, int comparator, float comparisonValue); + OOPSEBitSet nameInstruction(const std::string& name); + OOPSEBitSet indexInstruction(const boost::any& value); + OOPSEBitSet expression(const std::vector& tokens, int pc); - BitSet expression(std::vector& tokens, int pc); + OOPSEBitSet lookupValue(const std::string& variable); + + void evalError(const std::string& message) { + std::cerr << "SelectionEvaulator Error: " << message << std::endl; + } - SelectionCompiler compiler; + void unrecognizedCommand(const Token& token) { + evalError("unrecognized command:" + boost::any_cast(token.value)); + } - const static int scriptLevelMax = 10; - int scriptLevel; + void unrecognizedExpression() { + evalError("unrecognized expression"); + } - Context stack[scriptLevelMax]; + void unrecognizedAtomProperty(int property){ + evalError("unrecognized atom property"); + } - std::string filename; - std::string script; - std::vector linenumbers; - std::vector lineIndices; - std::vector > aatoken; - int pc; // program counter + void unrecognizedIdentifier(const std::string& identifier) { + evalError("unrecognized identifier:" + identifier); + } - bool error; - std::string errorMessage; + void invalidIndexRange(std::pair range) { + evalError("invalid index range: [" + toString(range.first) + ", " + toString(range.second) + ")"); + } - std::vector statement; - int statementLength; + void invalidIndex(int index) { + evalError("invalid index : " + toString(index) ); + } - SimInfo* info; + + bool containDynamicToken(const std::vector& tokens); - std::map > variables; -}; + double getCharge(Atom* atom); + + SelectionCompiler compiler; + //const static int scriptLevelMax = 10; + //int scriptLevel; + + //Context stack[scriptLevelMax]; + + std::string filename; + std::string script; + std::vector linenumbers; + std::vector lineIndices; + std::vector > aatoken; + int pc; // program counter + + bool error; + std::string errorMessage; + + std::vector statement; + int statementLength; + + SimInfo* info; + NameFinder nameFinder; + DistanceFinder distanceFinder; + IndexFinder indexFinder; + int nStuntDouble; //natoms + nrigidbodies + + typedef std::map VariablesType; + VariablesType variables; + + bool isDynamic_; + bool isLoaded_; + + }; + } #endif