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