# | Line 42 | Line 42 | |
---|---|---|
42 | #include "utils/wildcards.hpp" | |
43 | #include "utils/StringTokenizer.hpp" | |
44 | #include "primitives/Molecule.hpp" | |
45 | + | #include "utils/StringUtils.hpp" |
46 | namespace oopse { | |
47 | ||
48 | TreeNode::~TreeNode(){ | |
# | Line 78 | Line 79 | void NameFinder::loadNames() { | |
79 | root_->bs.setAll(); // | |
80 | ||
81 | for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) { | |
82 | < | TreeNode* currentMolNode; |
82 | > | |
83 | std::string molName = mol->getMoleculeName(); | |
84 | < | |
84 | < | foundIter = root_->children.find(molName); |
85 | < | if ( foundIter == root_->children.end()) { |
86 | < | currentMolNode = new TreeNode; |
87 | < | currentMolNode->name = molName; |
88 | < | currentMolNode->bs.resize(nStuntDouble_); |
89 | < | }else { |
90 | < | currentMolNode = foundIter->second; |
91 | < | } |
84 | > | TreeNode* currentMolNode = createNode(root_, molName); |
85 | ||
86 | for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) { | |
87 | std::string atomName = atom->getType(); | |
88 | < | TreeNode* currentAtomNode; |
89 | < | foundIter = currentMolNode->children.find(molName); |
97 | < | if (foundIter == currentMolNode->children.end()) { |
98 | < | currentAtomNode = new TreeNode; |
99 | < | currentAtomNode->name = atomName; |
100 | < | currentAtomNode->bs.resize(nStuntDouble_); |
101 | < | } else { |
102 | < | currentAtomNode = foundIter->second; |
103 | < | } |
88 | > | TreeNode* currentAtomNode = createNode(currentMolNode, atomName); |
89 | > | |
90 | currentMolNode->bs.setBitOn(atom->getGlobalIndex()); | |
91 | currentAtomNode->bs.setBitOn(atom->getGlobalIndex()); | |
92 | } | |
93 | ||
94 | for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) { | |
95 | < | std::string rbName = atom->getType(); |
96 | < | TreeNode* currentRbNode; |
111 | < | foundIter = currentMolNode->children.find(molName); |
112 | < | if (foundIter == currentMolNode->children.end()) { |
113 | < | currentRbNode = new TreeNode; |
114 | < | currentRbNode->name = rbName; |
115 | < | currentRbNode->bs.resize(nStuntDouble_); |
116 | < | } else { |
117 | < | currentRbNode = foundIter->second; |
118 | < | } |
95 | > | std::string rbName = rb->getType(); |
96 | > | TreeNode* currentRbNode = createNode(currentMolNode, rbName); |
97 | ||
98 | currentMolNode->bs.setBitOn(rb->getGlobalIndex()); | |
99 | currentRbNode->bs.setBitOn(rb->getGlobalIndex()); | |
100 | ||
101 | //create nodes for atoms belong to this rigidbody | |
102 | < | for(atom = rb->beginAtom(ai); rb != NULL; atom = rb->nextAtom(ai)) { |
102 | > | for(atom = rb->beginAtom(ai); atom != NULL; atom = rb->nextAtom(ai)) { |
103 | std::string rbAtomName = atom->getType(); | |
104 | < | TreeNode* currentRbAtomNode; |
105 | < | foundIter = currentRbNode->children.find(molName); |
128 | < | if (foundIter == currentRbNode->children.end()) { |
129 | < | currentRbAtomNode = new TreeNode; |
130 | < | currentRbAtomNode->name = rbAtomName; |
131 | < | currentRbAtomNode->bs.resize(nStuntDouble_); |
132 | < | } else { |
133 | < | currentRbAtomNode = foundIter->second; |
134 | < | } |
104 | > | TreeNode* currentRbAtomNode = createNode(currentRbNode, rbName);; |
105 | > | |
106 | currentRbAtomNode->bs.setBitOn(atom->getGlobalIndex()); | |
107 | } | |
108 | ||
# | Line 141 | Line 112 | void NameFinder::loadNames() { | |
112 | ||
113 | } | |
114 | ||
115 | < | bool NameFinder::match(const std::string& name, BitSet& bs){ |
115 | > | TreeNode* NameFinder::createNode(TreeNode* parent, const std::string& name) { |
116 | > | TreeNode* node; |
117 | > | std::map<std::string, TreeNode*>::iterator foundIter; |
118 | > | foundIter = parent->children.find(name); |
119 | > | if ( foundIter == parent->children.end()) { |
120 | > | node = new TreeNode; |
121 | > | node->name = name; |
122 | > | node->bs.resize(nStuntDouble_); |
123 | > | parent->children.insert(std::make_pair(name, node)); |
124 | > | }else { |
125 | > | node = foundIter->second; |
126 | > | } |
127 | > | return node; |
128 | > | } |
129 | ||
130 | < | bool hasError = false; |
130 | > | BitSet NameFinder::match(const std::string& name){ |
131 | > | BitSet bs(nStuntDouble_); |
132 | > | |
133 | StringTokenizer tokenizer(name, "."); | |
134 | ||
135 | std::vector<std::string> names; | |
# | Line 166 | Line 152 | bool NameFinder::match(const std::string& name, BitSet | |
152 | break; | |
153 | case 2: | |
154 | //could be molecule.*(include atoms and rigidbodies) or rigidbody.*(atoms belong to rigidbody) | |
155 | < | matchRigidAtoms("*", names[0], names[1], bs); |
156 | < | matchStuntDouble(names[0], names[1], bs); |
155 | > | |
156 | > | if (!isInteger(names[1])){ |
157 | > | matchRigidAtoms("*", names[0], names[1], bs); |
158 | > | matchStuntDouble(names[0], names[1], bs); |
159 | > | } else { |
160 | > | int internalIndex = lexi_cast<int>(names[1]); |
161 | > | if (internalIndex < 0) { |
162 | > | std::cerr << names[0] << ". " << names[1] << " is an invalid name" << std::endl; |
163 | > | } else { |
164 | > | matchInternalIndex(names[0], internalIndex, bs); |
165 | > | } |
166 | > | } |
167 | ||
168 | break; | |
169 | case 3: | |
# | Line 175 | Line 171 | bool NameFinder::match(const std::string& name, BitSet | |
171 | matchRigidAtoms(names[0], names[1], names[2], bs); | |
172 | break; | |
173 | default: | |
174 | < | hasError = true; |
174 | > | std::cerr << "invalid name: " << name << std::endl; |
175 | break; | |
176 | } | |
177 | ||
178 | < | return hasError; |
178 | > | return bs; |
179 | } | |
180 | ||
181 | void NameFinder::matchMolecule(const std::string& molName, BitSet& bs) { | |
# | Line 237 | Line 233 | bool NameFinder::isMatched(const std::string& str, con | |
233 | return Wildcard::wildcardfit (wildcard.c_str(), str.c_str()); | |
234 | } | |
235 | ||
236 | + | |
237 | + | void NameFinder::matchInternalIndex(const std::string& name, int internalIndex, BitSet& bs){ |
238 | + | |
239 | + | std::map<std::string, TreeNode*>::iterator foundIter; |
240 | + | SimInfo::MoleculeIterator mi; |
241 | + | Molecule* mol; |
242 | + | |
243 | + | for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) { |
244 | + | |
245 | + | if (isMatched(mol->getMoleculeName(), name) ) { |
246 | + | int natoms = mol->getNAtoms(); |
247 | + | int nrigidbodies = mol->getNRigidBodies(); |
248 | + | if (internalIndex >= natoms + nrigidbodies) { |
249 | + | continue; |
250 | + | } else if (internalIndex < natoms) { |
251 | + | bs.setBitOn(mol->getAtomAt(internalIndex)->getGlobalIndex()); |
252 | + | continue; |
253 | + | } else if ( internalIndex < natoms + nrigidbodies) { |
254 | + | bs.setBitOn(mol->getRigidBodyAt(internalIndex - natoms)->getGlobalIndex()); |
255 | + | } |
256 | + | } |
257 | + | |
258 | + | } |
259 | + | |
260 | } | |
261 | + | |
262 | + | bool NameFinder::isInteger(const std::string str) { |
263 | + | for(int i =0; i < str.size(); ++i){ |
264 | + | if (!std::isdigit(str[i])) { |
265 | + | return false; |
266 | + | } |
267 | + | } |
268 | + | |
269 | + | return true; |
270 | + | } |
271 | + | |
272 | + | } |
– | Removed lines |
+ | Added lines |
< | Changed lines |
> | Changed lines |