--- trunk/src/selection/SelectionEvaluator.cpp 2005/02/03 14:04:59 282 +++ trunk/src/selection/SelectionEvaluator.cpp 2005/03/09 17:30:29 413 @@ -39,16 +39,30 @@ * such damages. */ +#include #include "selection/SelectionEvaluator.hpp" +#include "primitives/Atom.hpp" +#include "primitives/DirectionalAtom.hpp" +#include "primitives/RigidBody.hpp" +#include "primitives/Molecule.hpp" + namespace oopse { +SelectionEvaluator::SelectionEvaluator(SimInfo* si) + : info(si), nameFinder(info), distanceFinder(info), indexFinder(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; } @@ -56,13 +70,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 = ""; } @@ -77,12 +103,23 @@ bool SelectionEvaluator::loadScriptFile(const std::str return loadScriptFileInternal(filename); } -bool SelectionEvaluator::loadScriptFileInternal(const string & filename) { +bool SelectionEvaluator::loadScriptFileInternal(const std::string & filename) { + std::ifstream ifs(filename.c_str()); + if (!ifs.is_open()) { + return false; + } + const int bufferSize = 65535; + char buffer[bufferSize]; + std::string script; + while(ifs.getline(buffer, bufferSize)) { + script += buffer; + } + return loadScript(filename, script); } -void SelectionEvaluator::instructionDispatchLoop(){ - +void SelectionEvaluator::instructionDispatchLoop(BitSet& bs){ + while ( pc < aatoken.size()) { statement = aatoken[pc++]; statementLength = statement.size(); @@ -92,22 +129,21 @@ void SelectionEvaluator::instructionDispatchLoop(){ define(); break; case Token::select: - select(); + select(bs); break; default: unrecognizedCommand(token); return; } } + } - BitSet SelectionEvaluator::expression(std::vector& code, int pcStart) { - int numberOfAtoms = viewer.getAtomCount(); + BitSet SelectionEvaluator::expression(const std::vector& code, int pcStart) { BitSet bs; - BitSet[] stack = new BitSet[10]; - int sp = 0; - - for (int pc = pcStart; ; ++pc) { + std::stack stack; + + for (int pc = pcStart; pc < code.size(); ++pc) { Token instruction = code[pc]; switch (instruction.tok) { @@ -116,46 +152,42 @@ void SelectionEvaluator::instructionDispatchLoop(){ case Token::expressionEnd: break; case Token::all: - bs = stack[sp++] = new BitSet(numberOfAtoms); - for (int i = numberOfAtoms; --i >= 0; ) - bs.set(i); + bs = BitSet(nStuntDouble); + bs.setAll(); + stack.push(bs); break; case Token::none: - stack[sp++] = new BitSet(); + bs = BitSet(nStuntDouble); + stack.push(bs); break; case Token::opOr: - bs = stack[--sp]; - stack[sp-1].or(bs); + bs = stack.top(); + stack.pop(); + stack.top() |= bs; break; case Token::opAnd: - bs = stack[--sp]; - stack[sp-1].and(bs); + bs = stack.top(); + stack.pop(); + stack.top() &= bs; break; case Token::opNot: - bs = stack[sp - 1]; - notSet(bs); + stack.top().flip(); break; case Token::within: - bs = stack[sp - 1]; - stack[sp - 1] = new BitSet(); - withinInstruction(instruction, bs, stack[sp - 1]); + + withinInstruction(instruction, stack.top()); break; - case Token::selected: - stack[sp++] = copyBitSet(viewer.getSelectionSet()); - break; + //case Token::selected: + // stack.push(getSelectionSet()); + // break; case Token::name: - + stack.push(nameInstruction(boost::any_cast(instruction.value))); break; - case Token::index: - + case Token::index: + stack.push(indexInstruction(instruction.value)); break; - case Token::molname: - - break; - case Token::molindex: - break; case Token::identifier: - stack[sp++] = lookupIdentifierValue((std::string)instruction.value); + stack.push(lookupValue(boost::any_cast(instruction.value))); break; case Token::opLT: case Token::opLE: @@ -163,101 +195,117 @@ void SelectionEvaluator::instructionDispatchLoop(){ case Token::opGT: case Token::opEQ: case Token::opNE: - bs = stack[sp++] = new BitSet(); - comparatorInstruction(instruction, bs); + stack.push(comparatorInstruction(instruction)); break; default: unrecognizedExpression(); } } - if (sp != 1) + if (stack.size() != 1) evalError("atom expression compiler error - stack over/underflow"); - return stack[0]; + + return stack.top(); } - void SelectionEvaluator::comparatorInstruction(Token instruction, BitSet bs) { +BitSet SelectionEvaluator::comparatorInstruction(const Token& instruction) { int comparator = instruction.tok; int property = instruction.intValue; - float propertyValue = 0; // just for temperature - int comparisonValue = ((Integer)instruction.value).intValue(); - int numberOfAtoms = viewer.getAtomCount(); - Frame frame = viewer.getFrame(); - for (int i = 0; i < numberOfAtoms; ++i) { - Atom atom = frame.getAtomAt(i); - switch (property) { - case Token::mass: - //propertyValue = atom.getAtomNumber(); - break; - case Token::charge: + float comparisonValue = boost::any_cast(instruction.value); + float propertyValue; + BitSet bs(nStuntDouble); + bs.clearAll(); + + SimInfo::MoleculeIterator mi; + Molecule* mol; + Molecule::AtomIterator ai; + Atom* atom; + Molecule::RigidBodyIterator rbIter; + RigidBody* rb; + + for (mol = info->beginMolecule(mi); mol != NULL; mol = info->nextMolecule(mi)) { - break; - case Token::dipole: - - break; - default: - unrecognizedAtomProperty(property); - } - bool match = false; - switch (comparator) { - case Token::opLT: - match = propertyValue < comparisonValue; - break; - case Token::opLE: - match = propertyValue <= comparisonValue; - break; - case Token::opGE: - match = propertyValue >= comparisonValue; - break; - case Token::opGT: - match = propertyValue > comparisonValue; - break; - case Token::opEQ: - match = propertyValue == comparisonValue; - break; - case Token::opNE: - match = propertyValue != comparisonValue; - break; - } - if (match) - bs.set(i); + for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) { + compareProperty(atom, bs, property, comparator, comparisonValue); + } + + //change the positions of atoms which belong to the rigidbodies + for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) { + compareProperty(rb, bs, property, comparator, comparisonValue); + } } - } -void SelectionEvaluator::withinInstruction(const Token& instruction, BitSet& bs, BitSet& bsResult) + return bs; +} +void SelectionEvaluator::compareProperty(StuntDouble* sd, BitSet& bs, int property, int comparator, float comparisonValue) { + double propertyValue; + switch (property) { + case Token::mass: + propertyValue = sd->getMass(); + break; + case Token::charge: + return; + //break; + case Token::dipole: + return; + //break; + default: + unrecognizedAtomProperty(property); + } + + bool match = false; + switch (comparator) { + case Token::opLT: + match = propertyValue < comparisonValue; + break; + case Token::opLE: + match = propertyValue <= comparisonValue; + break; + case Token::opGE: + match = propertyValue >= comparisonValue; + break; + case Token::opGT: + match = propertyValue > comparisonValue; + break; + case Token::opEQ: + match = propertyValue == comparisonValue; + break; + case Token::opNE: + match = propertyValue != comparisonValue; + break; + } + if (match) + bs.setBitOn(sd->getGlobalIndex()); + +} + +void SelectionEvaluator::withinInstruction(const Token& instruction, BitSet& bs){ + boost::any withinSpec = instruction.value; + float distance; if (withinSpec.type() == typeid(float)){ - withinDistance(boost::any_cast(withinSpec), bs, bsResult); - 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:" + withinSpec); + bs = distanceFinder.find(bs, distance); } - void SelectionEvaluator::withinDistance(float distance, const BitSet& bs, const BitSet& bsResult) { - Frame frame = viewer.getFrame(); - for (int i = frame.getAtomCount(); --i >= 0; ) { - if (bs.get(i)) { - Atom atom = frame.getAtomAt(i); - AtomIterator iterWithin = - frame.getWithinIterator(atom, distance); - while (iterWithin.hasNext()) - bsResult.set(iterWithin.next().getAtomIndex()); - } - } - } - - void SelectionEvaluator::define() { +void SelectionEvaluator::define() { assert(statement.size() >= 3); std::string variable = boost::any_cast(statement[1].value); - - variables.insert(std::make_pair(variable, expression(statement, 2))); - } + + variables.insert(VariablesType::value_type(variable, expression(statement, 2))); } + /** @todo */ void SelectionEvaluator::predefine(const std::string& script) { @@ -272,8 +320,8 @@ void SelectionEvaluator::predefine(const std::string& if (statement.size() > 2) { int tok = statement[1].tok; if (tok == Token::identifier || (tok & Token::predefinedset) == Token::predefinedset) { - std::string variable = (std::string)statement[1].value; - variables.insert(std::make_pair(variable, statement)); + std::string variable = boost::any_cast(statement[1].value); + variables.insert(VariablesType::value_type(variable, statement)); } else { evalError("invalid variable name:" + script); @@ -290,24 +338,84 @@ 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){ + + 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_) { + pc = 0; + 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 { + indexFinder.find(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 { + indexFinder.find(indexRange.first, indexRange.second); + } + } + + return bs; +} + +}