--- trunk/src/selection/NameFinder.cpp 2005/02/04 04:57:04 284 +++ trunk/src/selection/NameFinder.cpp 2005/04/05 23:09:48 452 @@ -40,6 +40,9 @@ */ #include "selection/NameFinder.hpp" #include "utils/wildcards.hpp" +#include "utils/StringTokenizer.hpp" +#include "primitives/Molecule.hpp" +#include "utils/StringUtils.hpp" namespace oopse { TreeNode::~TreeNode(){ @@ -52,7 +55,7 @@ NameFinder::NameFinder(SimInfo* info) : info_(info), r NameFinder::NameFinder(SimInfo* info) : info_(info), root_(NULL){ - nStuntDouble_ = info_->getNGlobalAtoms() + info_->getNGlobalRigidBodies() + nStuntDouble_ = info_->getNGlobalAtoms() + info_->getNGlobalRigidBodies(); loadNames(); } @@ -72,64 +75,34 @@ void NameFinder::loadNames() { RigidBody* rb; root_ = new TreeNode; - root_->bs.resize(nStuntDobule_); + root_->bs.resize(nStuntDouble_); root_->bs.setAll(); // for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) { - TreeNode* currentMolNode; + std::string molName = mol->getMoleculeName(); - - foundIter = root_->children.find(molName); - if ( foundIter == root_->children.end()) { - currentMolNode = new TreeNode; - currentMolNode->name = molName; - currentMolNode->bs.resize(nStuntDouble_); - }else { - currentMolNode = i->second; - } + TreeNode* currentMolNode = createNode(root_, molName); for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) { std::string atomName = atom->getType(); - TreeNode* currentAtomNode; - foundIter = currentMolNode->children.find(molName); - if (foundIter == currentMolNode->children.end()) { - currentAtomNode = new TreeNode; - currentAtomNode->name = atomName; - currentAtomNode->bs.resize(nStuntDouble_); - } else { - currentAtomNode = foundIter->second; - } + TreeNode* currentAtomNode = createNode(currentMolNode, atomName); + currentMolNode->bs.setBitOn(atom->getGlobalIndex()); currentAtomNode->bs.setBitOn(atom->getGlobalIndex()); } for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) { - std::string rbName = atom->getType(); - TreeNode* currentRbNode; - foundIter = currentMolNode->children.find(molName); - if (foundIter == currentMolNode->children.end()) { - currentRbNode = new TreeNode; - currentRbNode->name = rbName; - currentRbNode->bs.resize(nStuntDouble_); - } else { - currentRbNode = foundIter->second; - } + std::string rbName = rb->getType(); + TreeNode* currentRbNode = createNode(currentMolNode, rbName); currentMolNode->bs.setBitOn(rb->getGlobalIndex()); currentRbNode->bs.setBitOn(rb->getGlobalIndex()); //create nodes for atoms belong to this rigidbody - for(atom = rb->beginAtom(ai); rb != NULL; atom = rb->nextAtom(ai)) { + for(atom = rb->beginAtom(ai); atom != NULL; atom = rb->nextAtom(ai)) { std::string rbAtomName = atom->getType(); - TreeNode* currentRbAtomNode; - foundIter = currentRbNode->children.find(molName); - if (foundIter == currentRbNode->children.end()) { - currentRbAtomNode = new TreeNode; - currentRbAtomNode->name = rbAtomName; - currentRbAtomNode->bs.resize(nStuntDouble_); - } else { - currentRbAtomNode = foundIter->second; - } + TreeNode* currentRbAtomNode = createNode(currentRbNode, rbName);; + currentRbAtomNode->bs.setBitOn(atom->getGlobalIndex()); } @@ -137,15 +110,26 @@ void NameFinder::loadNames() { } - std::map::iterator i; - for( i = root_->children.begin(); i != ; ++i){ - i->bs = +} + +TreeNode* NameFinder::createNode(TreeNode* parent, const std::string& name) { + TreeNode* node; + std::map::iterator foundIter; + foundIter = parent->children.find(name); + if ( foundIter == parent->children.end()) { + node = new TreeNode; + node->name = name; + node->bs.resize(nStuntDouble_); + parent->children.insert(std::make_pair(name, node)); + }else { + node = foundIter->second; } + return node; } -bool NameFinder::match(const std::string& name, BitSet& bs){ - - bool error = true; +BitSet NameFinder::match(const std::string& name){ + BitSet bs(nStuntDouble_); + StringTokenizer tokenizer(name, "."); std::vector names; @@ -157,37 +141,43 @@ bool NameFinder::match(const std::string& name, BitSet switch(size) { case 1 : //could be molecule name, atom name and rigidbody name - if (names[0] == "*"){ - //if all molecules are selected, we don't need to do the matching, just set all of the bits - bs.setAll(); - } else{ - matchMolecule(name[0]); - matchStuntDouble("*", names[0]); - } + matchMolecule(names[0], bs); + matchStuntDouble("*", names[0], bs); break; case 2: //could be molecule.*(include atoms and rigidbodies) or rigidbody.*(atoms belong to rigidbody) - matchRigidAtoms("*", names[0], names[1], bs); - matchStuntDouble(names[0], names[1]); + + if (!isInteger(names[1])){ + matchRigidAtoms("*", names[0], names[1], bs); + matchStuntDouble(names[0], names[1], bs); + } else { + int internalIndex = lexi_cast(names[1]); + if (internalIndex < 0) { + std::cerr << names[0] << ". " << names[1] << " is an invalid name" << std::endl; + } else { + matchInternalIndex(names[0], internalIndex, bs); + } + } break; case 3: //must be molecule.rigidbody.* - matchRigidAtoms(names[0], names[1], names[2], bs) + matchRigidAtoms(names[0], names[1], names[2], bs); break; - default: + default: + std::cerr << "invalid name: " << name << std::endl; break; } - return matched; + return bs; } void NameFinder::matchMolecule(const std::string& molName, BitSet& bs) { std::vector molNodes = getMatchedChildren(root_, molName); std::vector::iterator i; for( i = molNodes.begin(); i != molNodes.end(); ++i ) { - bs |= i->bs; + bs |= (*i)->bs; } } @@ -198,7 +188,7 @@ void NameFinder::matchStuntDouble(const std::string& m std::vector sdNodes = getMatchedChildren(*i, sdName); std::vector::iterator j; for (j = sdNodes.begin(); j != sdNodes.end(); ++j) { - bs |= j->bs; + bs |= (*j)->bs; } } @@ -214,7 +204,7 @@ void NameFinder::matchRigidAtoms(const std::string& mo std::vector rbAtomNodes = getMatchedChildren(*j, rbAtomName); std::vector::iterator k; for(k = rbAtomNodes.begin(); k != rbAtomNodes.end(); ++k){ - bs |= k->bs; + bs |= (*k)->bs; } } } @@ -238,4 +228,40 @@ bool NameFinder::isMatched(const std::string& str, con return Wildcard::wildcardfit (wildcard.c_str(), str.c_str()); } + +void NameFinder::matchInternalIndex(const std::string& name, int internalIndex, BitSet& bs){ + + std::map::iterator foundIter; + SimInfo::MoleculeIterator mi; + Molecule* mol; + + for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) { + + if (isMatched(mol->getMoleculeName(), name) ) { + int natoms = mol->getNAtoms(); + int nrigidbodies = mol->getNRigidBodies(); + if (internalIndex >= natoms + nrigidbodies) { + continue; + } else if (internalIndex < natoms) { + bs.setBitOn(mol->getAtomAt(internalIndex)->getGlobalIndex()); + continue; + } else if ( internalIndex < natoms + nrigidbodies) { + bs.setBitOn(mol->getRigidBodyAt(internalIndex - natoms)->getGlobalIndex()); + } + } + + } + } + + bool NameFinder::isInteger(const std::string str) { + for(int i =0; i < str.size(); ++i){ + if (!std::isdigit(str[i])) { + return false; + } + } + + return true; + } + +}