| 39 |  | * such damages. | 
| 40 |  | */ | 
| 41 |  | #include "selection/NameFinder.hpp" | 
| 42 | + | #include "utils/wildcards.hpp" | 
| 43 |  | namespace oopse { | 
| 44 |  |  | 
| 45 | < | NameFinder::NameFinder(SimInfo* info) { | 
| 45 | > | TreeNode::~TreeNode(){ | 
| 46 | > | std::map<std::string, TreeNode*>::iterator i; | 
| 47 | > | for ( i = children.begin(); i != children.end(); ++i) { | 
| 48 | > | i->second->~TreeNode(); | 
| 49 | > | } | 
| 50 | > | children.clear(); | 
| 51 | > | } | 
| 52 | > |  | 
| 53 | > |  | 
| 54 | > | NameFinder::NameFinder(SimInfo* info) : info_(info), root_(NULL){ | 
| 55 | > | nStuntDouble_ = info_->getNGlobalAtoms() + info_->getNGlobalRigidBodies() | 
| 56 |  | loadNames(); | 
| 57 |  | } | 
| 58 |  |  | 
| 59 |  |  | 
| 60 | + | NameFinder::~NameFinder(){ | 
| 61 | + | delete root_; | 
| 62 | + | } | 
| 63 |  |  | 
| 64 |  | void NameFinder::loadNames() { | 
| 65 |  |  | 
| 66 | < | root_ = new NameNode; | 
| 53 | < | root_.type = rootNode; | 
| 54 | < |  | 
| 55 | < | NameNode* newNode; | 
| 66 | > | std::map<std::string, TreeNode*>::iterator foundIter; | 
| 67 |  | SimInfo::MoleculeIterator mi; | 
| 68 |  | Molecule* mol; | 
| 69 |  | Molecule::AtomIterator ai; | 
| 70 |  | Atom* atom; | 
| 71 |  | Molecule::RigidBodyIterator rbIter; | 
| 72 |  | RigidBody* rb; | 
| 73 | + |  | 
| 74 | + | root_ = new TreeNode; | 
| 75 | + | root_->bs.resize(nStuntDobule_); | 
| 76 | + | root_->bs.setAll(); // | 
| 77 | + |  | 
| 78 |  | for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) { | 
| 79 | < | newNode = new NameNode; | 
| 80 | < | newNode.type = molNode; | 
| 81 | < | newNode.name = mol->getMoleculeName(); | 
| 79 | > | TreeNode* currentMolNode; | 
| 80 | > | std::string molName = mol->getMoleculeName(); | 
| 81 | > |  | 
| 82 | > | foundIter = root_->children.find(molName); | 
| 83 | > | if ( foundIter  == root_->children.end()) { | 
| 84 | > | currentMolNode = new TreeNode; | 
| 85 | > | currentMolNode->name = molName; | 
| 86 | > | currentMolNode->bs.resize(nStuntDouble_); | 
| 87 | > | }else { | 
| 88 | > | currentMolNode = i->second; | 
| 89 | > | } | 
| 90 |  |  | 
| 91 |  | for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) { | 
| 92 | < | atomNames_.insert(atom->getType()); | 
| 92 | > | std::string atomName = atom->getType(); | 
| 93 | > | TreeNode* currentAtomNode; | 
| 94 | > | foundIter = currentMolNode->children.find(molName); | 
| 95 | > | if (foundIter == currentMolNode->children.end()) { | 
| 96 | > | currentAtomNode = new TreeNode; | 
| 97 | > | currentAtomNode->name = atomName; | 
| 98 | > | currentAtomNode->bs.resize(nStuntDouble_); | 
| 99 | > | } else { | 
| 100 | > | currentAtomNode = foundIter->second; | 
| 101 | > | } | 
| 102 | > | currentMolNode->bs.setBitOn(atom->getGlobalIndex()); | 
| 103 | > | currentAtomNode->bs.setBitOn(atom->getGlobalIndex()); | 
| 104 |  | } | 
| 105 | < |  | 
| 71 | < | //change the positions of atoms which belong to the rigidbodies | 
| 105 | > |  | 
| 106 |  | for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) { | 
| 107 | < | rbNames_.insert(rb->getType()); | 
| 108 | < | } | 
| 107 | > | std::string rbName = atom->getType(); | 
| 108 | > | TreeNode* currentRbNode; | 
| 109 | > | foundIter = currentMolNode->children.find(molName); | 
| 110 | > | if (foundIter == currentMolNode->children.end()) { | 
| 111 | > | currentRbNode = new TreeNode; | 
| 112 | > | currentRbNode->name = rbName; | 
| 113 | > | currentRbNode->bs.resize(nStuntDouble_); | 
| 114 | > | } else { | 
| 115 | > | currentRbNode = foundIter->second; | 
| 116 | > | } | 
| 117 | > |  | 
| 118 | > | currentMolNode->bs.setBitOn(rb->getGlobalIndex()); | 
| 119 | > | currentRbNode->bs.setBitOn(rb->getGlobalIndex()); | 
| 120 | > |  | 
| 121 | > | //create nodes for atoms belong to this rigidbody | 
| 122 | > | for(atom = rb->beginAtom(ai); rb != NULL; atom = rb->nextAtom(ai)) { | 
| 123 | > | std::string rbAtomName = atom->getType(); | 
| 124 | > | TreeNode* currentRbAtomNode; | 
| 125 | > | foundIter = currentRbNode->children.find(molName); | 
| 126 | > | if (foundIter == currentRbNode->children.end()) { | 
| 127 | > | currentRbAtomNode = new TreeNode; | 
| 128 | > | currentRbAtomNode->name = rbAtomName; | 
| 129 | > | currentRbAtomNode->bs.resize(nStuntDouble_); | 
| 130 | > | } else { | 
| 131 | > | currentRbAtomNode = foundIter->second; | 
| 132 | > | } | 
| 133 | > | currentRbAtomNode->bs.setBitOn(atom->getGlobalIndex()); | 
| 134 | > | } | 
| 135 | > |  | 
| 136 | > | } | 
| 137 | > |  | 
| 138 |  | } | 
| 139 | + |  | 
| 140 | + | std::map<std::string, TreeNode*>::iterator i; | 
| 141 | + | for( i = root_->children.begin(); i != ; ++i){ | 
| 142 | + | i->bs = | 
| 143 | + | } | 
| 144 |  | } | 
| 145 |  |  | 
| 146 |  | bool NameFinder::match(const std::string& name, BitSet& bs){ | 
| 156 |  | int size = names.size(); | 
| 157 |  | switch(size) { | 
| 158 |  | case 1 : | 
| 159 | < | //could be molecule name, atom name, rigidbody name | 
| 160 | < | isMolName(); | 
| 161 | < | isAtomName(); | 
| 162 | < | isRigidBodyName(); | 
| 159 | > | //could be molecule name, atom name and rigidbody name | 
| 160 | > | if (names[0] == "*"){ | 
| 161 | > | //if all molecules are selected, we don't need to do the matching, just set all of the bits | 
| 162 | > | bs.setAll(); | 
| 163 | > | } else{ | 
| 164 | > | matchMolecule(name[0]); | 
| 165 | > | matchStuntDouble("*", names[0]); | 
| 166 | > | } | 
| 167 | > |  | 
| 168 |  | break; | 
| 169 |  | case 2: | 
| 170 | < | //could be molecule.*(include atoms and rigidbodies) or rigidbody.* | 
| 170 | > | //could be molecule.*(include atoms and rigidbodies) or rigidbody.*(atoms belong to rigidbody) | 
| 171 | > | matchRigidAtoms("*", names[0], names[1], bs); | 
| 172 | > | matchStuntDouble(names[0], names[1]); | 
| 173 | > |  | 
| 174 |  | break; | 
| 175 |  | case 3: | 
| 176 |  | //must be molecule.rigidbody.* | 
| 177 | < |  | 
| 177 | > | matchRigidAtoms(names[0], names[1], names[2], bs) | 
| 178 |  | break; | 
| 179 |  | default: | 
| 180 |  | break; | 
| 183 |  | return matched; | 
| 184 |  | } | 
| 185 |  |  | 
| 186 | + | void NameFinder::matchMolecule(const std::string& molName, BitSet& bs) { | 
| 187 | + | std::vector<TreeNode*> molNodes = getMatchedChildren(root_, molName); | 
| 188 | + | std::vector<TreeNode*>::iterator i; | 
| 189 | + | for( i = molNodes.begin(); i != molNodes.end(); ++i ) { | 
| 190 | + | bs |= i->bs; | 
| 191 | + | } | 
| 192 |  | } | 
| 193 | + |  | 
| 194 | + | void NameFinder::matchStuntDouble(const std::string& molName, const std::string& sdName, BitSet& bs){ | 
| 195 | + | std::vector<TreeNode*> molNodes = getMatchedChildren(root_, molName); | 
| 196 | + | std::vector<TreeNode*>::iterator i; | 
| 197 | + | for( i = molNodes.begin(); i != molNodes.end(); ++i ) { | 
| 198 | + | std::vector<TreeNode*> sdNodes = getMatchedChildren(*i, sdName); | 
| 199 | + | std::vector<TreeNode*>::iterator j; | 
| 200 | + | for (j = sdNodes.begin(); j != sdNodes.end(); ++j) { | 
| 201 | + | bs |= j->bs; | 
| 202 | + | } | 
| 203 | + | } | 
| 204 | + |  | 
| 205 | + | } | 
| 206 | + |  | 
| 207 | + | void NameFinder::matchRigidAtoms(const std::string& molName, const std::string& rbName, const std::string& rbAtomName, BitSet& bs){ | 
| 208 | + | std::vector<TreeNode*> molNodes = getMatchedChildren(root_, molName); | 
| 209 | + | std::vector<TreeNode*>::iterator i; | 
| 210 | + | for( i = molNodes.begin(); i != molNodes.end(); ++i ) { | 
| 211 | + | std::vector<TreeNode*> rbNodes = getMatchedChildren(*i, rbName); | 
| 212 | + | std::vector<TreeNode*>::iterator j; | 
| 213 | + | for (j = rbNodes.begin(); j != rbNodes.end(); ++j) { | 
| 214 | + | std::vector<TreeNode*> rbAtomNodes = getMatchedChildren(*j, rbAtomName); | 
| 215 | + | std::vector<TreeNode*>::iterator k; | 
| 216 | + | for(k = rbAtomNodes.begin(); k != rbAtomNodes.end(); ++k){ | 
| 217 | + | bs |= k->bs; | 
| 218 | + | } | 
| 219 | + | } | 
| 220 | + | } | 
| 221 | + |  | 
| 222 | + | } | 
| 223 | + |  | 
| 224 | + |  | 
| 225 | + | std::vector<TreeNode*> NameFinder::getMatchedChildren(TreeNode* node, const std::string& name) { | 
| 226 | + | std::vector<TreeNode*> matchedNodes; | 
| 227 | + | std::map<std::string, TreeNode*>::iterator i; | 
| 228 | + | for (i = node->children.begin(); i != node->children.end(); ++i) { | 
| 229 | + | if (isMatched( i->first, name)) { | 
| 230 | + | matchedNodes.push_back(i->second); | 
| 231 | + | } | 
| 232 | + | } | 
| 233 | + |  | 
| 234 | + | return matchedNodes; | 
| 235 | + | } | 
| 236 | + |  | 
| 237 | + | bool NameFinder::isMatched(const std::string& str, const std::string& wildcard) { | 
| 238 | + | return Wildcard::wildcardfit (wildcard.c_str(), str.c_str()); | 
| 239 | + | } | 
| 240 | + |  | 
| 241 | + | } |