--- trunk/src/selection/SelectionEvaluator.cpp 2005/02/03 23:14:05 283 +++ trunk/src/selection/SelectionEvaluator.cpp 2005/02/07 19:13:18 295 @@ -49,15 +49,18 @@ namespace oopse { namespace oopse { -SelectionEvaluator::SelectionEvaluator(SimInfo* si, const std::string& script) : info(si), finder(info){ +SelectionEvaluator::SelectionEvaluator(SimInfo* si) : info(si), nameFinder(info), distanceFinder(info), isLoaded_(false){ + nStuntDouble = info->getNGlobalAtoms() + info->getNRigidBodies(); } bool SelectionEvaluator::loadScript(const std::string& filename, const std::string& script) { + clearDefinitionsAndLoadPredefined(); this->filename = filename; this->script = script; if (! compiler.compile(filename, script)) { error = true; errorMessage = compiler.getErrorMessage(); + std::cerr << "SelectionCompiler Error: " << errorMessage << std::endl; return false; } @@ -65,13 +68,25 @@ bool SelectionEvaluator::loadScript(const std::string& aatoken = compiler.getAatokenCompiled(); linenumbers = compiler.getLineNumbers(); lineIndices = compiler.getLineIndices(); + + std::vector >::const_iterator i; + + isDynamic_ = false; + for (i = aatoken.begin(); i != aatoken.end(); ++i) { + if (containDynamicToken(*i)) { + isDynamic_ = true; + break; + } + } + + isLoaded_ = true; return true; } void SelectionEvaluator::clearState() { - for (int i = scriptLevelMax; --i >= 0; ) - stack[i].clear(); - scriptLevel = 0; + //for (int i = scriptLevelMax; --i >= 0; ) + // stack[i].clear(); + //scriptLevel = 0; error = false; errorMessage = ""; } @@ -87,11 +102,22 @@ bool SelectionEvaluator::loadScriptFileInternal(const } bool SelectionEvaluator::loadScriptFileInternal(const std::string & filename) { - return true; /**@todo */ -} + ifstream ifs(filename.c_str()); + if (!ifs.is_open()) { + return false; + } -void SelectionEvaluator::instructionDispatchLoop(){ + const int bufferSize = 65535; + char buffer[bufferSize]; + std::string script; + while(ifs.getline(buffer, bufferSize)) { + script += buffer; + } + return loadScript(filename, script); +} +void SelectionEvaluator::instructionDispatchLoop(BitSet& bs){ + while ( pc < aatoken.size()) { statement = aatoken[pc++]; statementLength = statement.size(); @@ -101,20 +127,21 @@ void SelectionEvaluator::instructionDispatchLoop(){ define(); break; case Token::select: - select(); + select(bs); break; default: unrecognizedCommand(token); return; } } + } BitSet SelectionEvaluator::expression(const std::vector& code, int pcStart) { BitSet bs; std::stack stack; - for (int pc = pcStart; ; ++pc) { + for (int pc = pcStart; pc < code.size(); ++pc) { Token instruction = code[pc]; switch (instruction.tok) { @@ -154,6 +181,8 @@ void SelectionEvaluator::instructionDispatchLoop(){ case Token::name: stack.push(nameInstruction(boost::any_cast(instruction.value))); break; + case Token::index: + stack.push(indexInstruction(instruction.value)); break; case Token::identifier: stack.push(lookupValue(boost::any_cast(instruction.value))); @@ -184,6 +213,7 @@ BitSet SelectionEvaluator::comparatorInstruction(const float comparisonValue = boost::any_cast(instruction.value); float propertyValue; BitSet bs(nStuntDouble); + bs.clearAll(); SimInfo::MoleculeIterator mi; Molecule* mol; @@ -250,14 +280,19 @@ void SelectionEvaluator::withinInstruction(const Token } void SelectionEvaluator::withinInstruction(const Token& instruction, BitSet& bs){ - + boost::any withinSpec = instruction.value; + float distance; if (withinSpec.type() == typeid(float)){ - // - return; + distance = boost::any_cast(withinSpec); + } else if (withinSpec.type() == typeid(int)) { + distance = boost::any_cast(withinSpec); + } else { + evalError("casting error in withinInstruction"); + bs.clearAll(); } - evalError("Unrecognized within parameter"); + bs = distanceFinder.find(bs, distance); } void SelectionEvaluator::define() { @@ -301,34 +336,87 @@ void SelectionEvaluator::predefine(const std::string& } -void SelectionEvaluator::select(){ - //viewer.setSelectionSet(expression(statement, 1)); +void SelectionEvaluator::select(BitSet& bs){ + bs = expression(statement, 1); } BitSet SelectionEvaluator::lookupValue(const std::string& variable){ + BitSet bs(nStuntDouble); std::map::iterator i = variables.find(variable); - + if (i != variables.end()) { if (i->second.type() == typeid(BitSet)) { return boost::any_cast(i->second); } else if (i->second.type() == typeid(std::vector)){ - BitSet bs = expression(boost::any_cast >(i->second), 2); + bs = expression(boost::any_cast >(i->second), 2); i->second = bs; /**@todo fixme */ return bs; } } else { unrecognizedIdentifier(variable); } + + return bs; } BitSet SelectionEvaluator::nameInstruction(const std::string& name){ - BitSet bs(nStuntDouble); - bool hasError = finder.match(name, bs); - - return bs; + return nameFinder.match(name); + } +bool SelectionEvaluator::containDynamicToken(const std::vector& tokens){ + std::vector::const_iterator i; + for (i = tokens.begin(); i != tokens.end(); ++i) { + if (i->tok & Token::dynamic) { + return true; + } + } + return false; +} + +void SelectionEvaluator::clearDefinitionsAndLoadPredefined() { + variables.clear(); + //load predefine + //predefine(); } + +BitSet SelectionEvaluator::evaluate() { + BitSet bs(nStuntDouble); + if (isLoaded_) { + instructionDispatchLoop(bs); + } + + return bs; +} + +BitSet SelectionEvaluator::indexInstruction(const boost::any& value) { + BitSet bs(nStuntDouble); + + if (value.type() == typeid(int)) { + int index = boost::any_cast(value); + if (index < 0 || index >= bs.size()) { + invalidIndex(index); + } else { + bs.setBitOn(index); + } + } else if (value.type() == typeid(std::pair)) { + std::pair indexRange= boost::any_cast >(value); + assert(indexRange.first <= indexRange.second); + if (indexRange.first < 0 || indexRange.second >= bs.size()) { + invalidIndexRange(indexRange); + }else { + bs.setRangeOn(indexRange.first, indexRange.second); + } + } + + return bs; +} + +//BitSet SelectionEvaluator::evaluate(int frameNo) { +// +//} + +}