| [49c059] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
|  | 4 | * Copyright (C)  2010 University of Bonn. All rights reserved. | 
|---|
|  | 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
|  | 8 | /* | 
|---|
|  | 9 | * DepthFirstSearchAnalysis.cpp | 
|---|
|  | 10 | * | 
|---|
|  | 11 | *  Created on: Feb 16, 2011 | 
|---|
|  | 12 | *      Author: heber | 
|---|
|  | 13 | */ | 
|---|
|  | 14 |  | 
|---|
|  | 15 | // include config.h | 
|---|
|  | 16 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 17 | #include <config.h> | 
|---|
|  | 18 | #endif | 
|---|
|  | 19 |  | 
|---|
|  | 20 | #include "CodePatterns/MemDebug.hpp" | 
|---|
|  | 21 |  | 
|---|
|  | 22 | #include "DepthFirstSearchAnalysis.hpp" | 
|---|
|  | 23 |  | 
|---|
|  | 24 | #include <algorithm> | 
|---|
|  | 25 | #include <functional> | 
|---|
|  | 26 |  | 
|---|
|  | 27 | #include "atom.hpp" | 
|---|
|  | 28 | #include "Bond/bond.hpp" | 
|---|
|  | 29 | #include "CodePatterns/Assert.hpp" | 
|---|
|  | 30 | #include "CodePatterns/Info.hpp" | 
|---|
|  | 31 | #include "CodePatterns/Log.hpp" | 
|---|
|  | 32 | #include "CodePatterns/Verbose.hpp" | 
|---|
|  | 33 | #include "Descriptors/AtomDescriptor.hpp" | 
|---|
|  | 34 | #include "molecule.hpp" | 
|---|
|  | 35 | #include "World.hpp" | 
|---|
|  | 36 |  | 
|---|
|  | 37 | DepthFirstSearchAnalysis::DepthFirstSearchAnalysis() : | 
|---|
|  | 38 | CurrentGraphNr(0), | 
|---|
|  | 39 | ComponentNumber(0), | 
|---|
|  | 40 | BackStepping(false) | 
|---|
|  | 41 | { | 
|---|
|  | 42 | ResetAllBondsToUnused(); | 
|---|
|  | 43 | } | 
|---|
|  | 44 |  | 
|---|
|  | 45 | DepthFirstSearchAnalysis::~DepthFirstSearchAnalysis() | 
|---|
|  | 46 | {} | 
|---|
|  | 47 |  | 
|---|
|  | 48 | void DepthFirstSearchAnalysis::Init() | 
|---|
|  | 49 | { | 
|---|
|  | 50 | CurrentGraphNr = 0; | 
|---|
|  | 51 | ComponentNumber = 0; | 
|---|
|  | 52 | BackStepping = false; | 
|---|
|  | 53 | std::for_each(World::getInstance().getAtomIter(),World::getInstance().atomEnd(), | 
|---|
|  | 54 | std::mem_fun(&atom::resetGraphNr)); | 
|---|
|  | 55 | std::for_each(World::getInstance().getAtomIter(),World::getInstance().atomEnd(), | 
|---|
|  | 56 | std::mem_fun(&atom::InitComponentNr)); | 
|---|
|  | 57 | } | 
|---|
|  | 58 |  | 
|---|
|  | 59 |  | 
|---|
|  | 60 | bond * DepthFirstSearchAnalysis::FindNextUnused(atom *vertex) const | 
|---|
|  | 61 | { | 
|---|
|  | 62 | const BondList& ListOfBonds = vertex->getListOfBonds(); | 
|---|
|  | 63 | for (BondList::const_iterator Runner = ListOfBonds.begin(); | 
|---|
|  | 64 | Runner != ListOfBonds.end(); | 
|---|
|  | 65 | ++Runner) | 
|---|
|  | 66 | if ((*Runner)->IsUsed() == GraphEdge::white) | 
|---|
|  | 67 | return ((*Runner)); | 
|---|
|  | 68 | return NULL; | 
|---|
|  | 69 | } | 
|---|
|  | 70 |  | 
|---|
|  | 71 |  | 
|---|
|  | 72 | void DepthFirstSearchAnalysis::ResetAllBondsToUnused() const | 
|---|
|  | 73 | { | 
|---|
|  | 74 | World::AtomComposite allatoms = World::getInstance().getAllAtoms(); | 
|---|
|  | 75 | for(World::AtomComposite::const_iterator AtomRunner = allatoms.begin(); | 
|---|
|  | 76 | AtomRunner != allatoms.end(); | 
|---|
|  | 77 | ++AtomRunner) { | 
|---|
|  | 78 | const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds(); | 
|---|
|  | 79 | for(BondList::const_iterator BondRunner = ListOfBonds.begin(); | 
|---|
|  | 80 | BondRunner != ListOfBonds.end(); | 
|---|
|  | 81 | ++BondRunner) | 
|---|
|  | 82 | if ((*BondRunner)->leftatom == *AtomRunner) | 
|---|
|  | 83 | (*BondRunner)->ResetUsed(); | 
|---|
|  | 84 | } | 
|---|
|  | 85 | } | 
|---|
|  | 86 |  | 
|---|
|  | 87 | void DepthFirstSearchAnalysis::SetNextComponentNumber(atom *vertex, int nr) const | 
|---|
|  | 88 | { | 
|---|
|  | 89 | size_t i = 0; | 
|---|
|  | 90 | ASSERT(vertex != NULL, | 
|---|
|  | 91 | "DepthFirstSearchAnalysis::SetNextComponentNumber() - Given vertex is NULL!"); | 
|---|
|  | 92 | const BondList& ListOfBonds = vertex->getListOfBonds(); | 
|---|
|  | 93 | for (; i < ListOfBonds.size(); i++) { | 
|---|
|  | 94 | if (vertex->ComponentNr[i] == -1) { // check if not yet used | 
|---|
|  | 95 | vertex->ComponentNr[i] = nr; | 
|---|
|  | 96 | break; | 
|---|
|  | 97 | } else if (vertex->ComponentNr[i] == nr) // if number is already present, don't add another time | 
|---|
|  | 98 | break; // breaking here will not cause error! | 
|---|
|  | 99 | } | 
|---|
|  | 100 | ASSERT(i < ListOfBonds.size(), | 
|---|
|  | 101 | "DepthFirstSearchAnalysis::SetNextComponentNumber() - All Component entries are already occupied!"); | 
|---|
|  | 102 | } | 
|---|
|  | 103 |  | 
|---|
|  | 104 |  | 
|---|
|  | 105 | bool DepthFirstSearchAnalysis::PickLocalBackEdges(atom **ListOfLocalAtoms, std::deque<bond *> *&LocalStack) const | 
|---|
|  | 106 | { | 
|---|
|  | 107 | bool status = true; | 
|---|
|  | 108 | if (BackEdgeStack.empty()) { | 
|---|
|  | 109 | ELOG(1, "Reference BackEdgeStack is empty!"); | 
|---|
|  | 110 | return false; | 
|---|
|  | 111 | } | 
|---|
|  | 112 | bond *Binder = BackEdgeStack.front(); | 
|---|
|  | 113 | bond *FirstBond = Binder; // mark the first bond, so that we don't loop through the stack indefinitely | 
|---|
|  | 114 | atom *Walker = NULL, *OtherAtom = NULL; | 
|---|
|  | 115 |  | 
|---|
|  | 116 | do { // go through all bonds and push local ones | 
|---|
|  | 117 | Walker = ListOfLocalAtoms[Binder->leftatom->getNr()]; // get one atom in the reference molecule | 
|---|
|  | 118 | if (Walker != NULL) { // if this Walker exists in the subgraph ... | 
|---|
|  | 119 | const BondList& ListOfBonds = Walker->getListOfBonds(); | 
|---|
|  | 120 | for (BondList::const_iterator Runner = ListOfBonds.begin(); | 
|---|
|  | 121 | Runner != ListOfBonds.end(); | 
|---|
|  | 122 | ++Runner) { | 
|---|
|  | 123 | OtherAtom = (*Runner)->GetOtherAtom(Walker); | 
|---|
|  | 124 | if (OtherAtom == ListOfLocalAtoms[(*Runner)->rightatom->getNr()]) { // found the bond | 
|---|
|  | 125 | LocalStack->push_front((*Runner)); | 
|---|
|  | 126 | LOG(3, "INFO: Found local edge " << *(*Runner) << "."); | 
|---|
|  | 127 | break; | 
|---|
|  | 128 | } | 
|---|
|  | 129 | } | 
|---|
|  | 130 | } | 
|---|
|  | 131 | ASSERT(!BackEdgeStack.empty(), "DepthFirstSearchAnalysis::PickLocalBackEdges() - ReferenceStack is empty!"); | 
|---|
|  | 132 | Binder = BackEdgeStack.front(); // loop the stack for next item | 
|---|
|  | 133 | LOG(3, "Current candidate edge " << Binder << "."); | 
|---|
|  | 134 | } while (FirstBond != Binder); | 
|---|
|  | 135 |  | 
|---|
|  | 136 | return status; | 
|---|
|  | 137 | } | 
|---|
|  | 138 |  | 
|---|
|  | 139 |  | 
|---|
|  | 140 |  | 
|---|
|  | 141 | void DepthFirstSearchAnalysis::OutputGraphInfoPerAtom() const | 
|---|
|  | 142 | { | 
|---|
|  | 143 | LOG(1, "Final graph info for each atom is:"); | 
|---|
|  | 144 | World::AtomComposite allatoms = World::getInstance().getAllAtoms(); | 
|---|
|  | 145 | for_each(allatoms.begin(),allatoms.end(),mem_fun(&atom::OutputGraphInfo)); | 
|---|
|  | 146 | } | 
|---|
|  | 147 |  | 
|---|
|  | 148 |  | 
|---|
|  | 149 | void DepthFirstSearchAnalysis::OutputGraphInfoPerBond() const | 
|---|
|  | 150 | { | 
|---|
|  | 151 | LOG(1, "Final graph info for each bond is:"); | 
|---|
|  | 152 | World::AtomComposite allatoms = World::getInstance().getAllAtoms(); | 
|---|
|  | 153 | for(World::AtomComposite::const_iterator AtomRunner = allatoms.begin(); | 
|---|
|  | 154 | AtomRunner != allatoms.end(); | 
|---|
|  | 155 | ++AtomRunner) { | 
|---|
|  | 156 | const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds(); | 
|---|
|  | 157 | for(BondList::const_iterator BondRunner = ListOfBonds.begin(); | 
|---|
|  | 158 | BondRunner != ListOfBonds.end(); | 
|---|
|  | 159 | ++BondRunner) | 
|---|
|  | 160 | if ((*BondRunner)->leftatom == *AtomRunner) { | 
|---|
|  | 161 | const bond *Binder = *BondRunner; | 
|---|
|  | 162 | if (DoLog(2)) { | 
|---|
|  | 163 | ostream &out = (Log() << Verbose(2)); | 
|---|
|  | 164 | out << ((Binder->Type == GraphEdge::TreeEdge) ? "TreeEdge " : "BackEdge ") << *Binder << ": <"; | 
|---|
|  | 165 | out << ((Binder->leftatom->SeparationVertex) ? "SP," : "") << "L" << Binder->leftatom->LowpointNr << " G" << Binder->leftatom->GraphNr << " Comp."; | 
|---|
|  | 166 | Binder->leftatom->OutputComponentNumber(&out); | 
|---|
|  | 167 | out << " ===  "; | 
|---|
|  | 168 | out << ((Binder->rightatom->SeparationVertex) ? "SP," : "") << "L" << Binder->rightatom->LowpointNr << " G" << Binder->rightatom->GraphNr << " Comp."; | 
|---|
|  | 169 | Binder->rightatom->OutputComponentNumber(&out); | 
|---|
|  | 170 | out << ">." << endl; | 
|---|
|  | 171 | } | 
|---|
|  | 172 | if (Binder->Cyclic) // cyclic ?? | 
|---|
|  | 173 | LOG(3, "Lowpoint at each side are equal: CYCLIC!"); | 
|---|
|  | 174 | } | 
|---|
|  | 175 | } | 
|---|
|  | 176 | } | 
|---|
|  | 177 |  | 
|---|
|  | 178 |  | 
|---|
|  | 179 | unsigned int DepthFirstSearchAnalysis::CyclicBondAnalysis() const | 
|---|
|  | 180 | { | 
|---|
|  | 181 | unsigned int NoCyclicBonds = 0; | 
|---|
|  | 182 | World::AtomComposite allatoms = World::getInstance().getAllAtoms(); | 
|---|
|  | 183 | for(World::AtomComposite::const_iterator AtomRunner = allatoms.begin(); | 
|---|
|  | 184 | AtomRunner != allatoms.end(); | 
|---|
|  | 185 | ++AtomRunner) { | 
|---|
|  | 186 | const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds(); | 
|---|
|  | 187 | for(BondList::const_iterator BondRunner = ListOfBonds.begin(); | 
|---|
|  | 188 | BondRunner != ListOfBonds.end(); | 
|---|
|  | 189 | ++BondRunner) | 
|---|
|  | 190 | if ((*BondRunner)->leftatom == *AtomRunner) | 
|---|
|  | 191 | if ((*BondRunner)->rightatom->LowpointNr == (*BondRunner)->leftatom->LowpointNr) { // cyclic ?? | 
|---|
|  | 192 | (*BondRunner)->Cyclic = true; | 
|---|
|  | 193 | NoCyclicBonds++; | 
|---|
|  | 194 | } | 
|---|
|  | 195 | } | 
|---|
|  | 196 | return NoCyclicBonds; | 
|---|
|  | 197 | } | 
|---|
|  | 198 |  | 
|---|
|  | 199 |  | 
|---|
|  | 200 | void DepthFirstSearchAnalysis::SetWalkersGraphNr(atom *&Walker) | 
|---|
|  | 201 | { | 
|---|
|  | 202 | if (!BackStepping) { // if we don't just return from (8) | 
|---|
|  | 203 | Walker->GraphNr = CurrentGraphNr; | 
|---|
|  | 204 | Walker->LowpointNr = CurrentGraphNr; | 
|---|
|  | 205 | LOG(1, "Setting Walker[" << Walker->getName() << "]'s number to " << Walker->GraphNr << " with Lowpoint " << Walker->LowpointNr << "."); | 
|---|
|  | 206 | AtomStack.push_front(Walker); | 
|---|
|  | 207 | CurrentGraphNr++; | 
|---|
|  | 208 | } | 
|---|
|  | 209 | } | 
|---|
|  | 210 |  | 
|---|
|  | 211 |  | 
|---|
|  | 212 | void DepthFirstSearchAnalysis::ProbeAlongUnusedBond(atom *&Walker, bond *&Binder) | 
|---|
|  | 213 | { | 
|---|
|  | 214 | atom *OtherAtom = NULL; | 
|---|
|  | 215 |  | 
|---|
|  | 216 | do { // (3) if Walker has no unused egdes, go to (5) | 
|---|
|  | 217 | BackStepping = false; // reset backstepping flag for (8) | 
|---|
|  | 218 | if (Binder == NULL) // if we don't just return from (11), Binder is already set to next unused | 
|---|
|  | 219 | Binder = FindNextUnused(Walker); | 
|---|
|  | 220 | if (Binder == NULL) | 
|---|
|  | 221 | break; | 
|---|
|  | 222 | LOG(2, "Current Unused Bond is " << *Binder << "."); | 
|---|
|  | 223 | // (4) Mark Binder used, ... | 
|---|
|  | 224 | Binder->MarkUsed(GraphEdge::black); | 
|---|
|  | 225 | OtherAtom = Binder->GetOtherAtom(Walker); | 
|---|
|  | 226 | LOG(2, "(4) OtherAtom is " << OtherAtom->getName() << "."); | 
|---|
|  | 227 | if (OtherAtom->GraphNr != -1) { | 
|---|
|  | 228 | // (4a) ... if "other" atom has been visited (GraphNr != 0), set lowpoint to minimum of both, go to (3) | 
|---|
|  | 229 | Binder->Type = GraphEdge::BackEdge; | 
|---|
|  | 230 | BackEdgeStack.push_front(Binder); | 
|---|
|  | 231 | Walker->LowpointNr = (Walker->LowpointNr < OtherAtom->GraphNr) ? Walker->LowpointNr : OtherAtom->GraphNr; | 
|---|
|  | 232 | LOG(3, "(4a) Visited: Setting Lowpoint of Walker[" << Walker->getName() << "] to " << Walker->LowpointNr << "."); | 
|---|
|  | 233 | } else { | 
|---|
|  | 234 | // (4b) ... otherwise set OtherAtom as Ancestor of Walker and Walker as OtherAtom, go to (2) | 
|---|
|  | 235 | Binder->Type = GraphEdge::TreeEdge; | 
|---|
|  | 236 | OtherAtom->Ancestor = Walker; | 
|---|
|  | 237 | Walker = OtherAtom; | 
|---|
|  | 238 | LOG(3, "(4b) Not Visited: OtherAtom[" << OtherAtom->getName() << "]'s Ancestor is now " << OtherAtom->Ancestor->getName() << ", Walker is OtherAtom " << OtherAtom->getName() << "."); | 
|---|
|  | 239 | break; | 
|---|
|  | 240 | } | 
|---|
|  | 241 | Binder = NULL; | 
|---|
|  | 242 | } while (1); // (3) | 
|---|
|  | 243 | } | 
|---|
|  | 244 |  | 
|---|
|  | 245 |  | 
|---|
|  | 246 | void DepthFirstSearchAnalysis::CheckForaNewComponent(atom *&Walker, ConnectedSubgraph &Subgraph) | 
|---|
|  | 247 | { | 
|---|
|  | 248 | atom *OtherAtom = NULL; | 
|---|
|  | 249 |  | 
|---|
|  | 250 | // (5) if Ancestor of Walker is ... | 
|---|
|  | 251 | LOG(1, "(5) Number of Walker[" << Walker->getName() << "]'s Ancestor[" << Walker->Ancestor->getName() << "] is " << Walker->Ancestor->GraphNr << "."); | 
|---|
|  | 252 |  | 
|---|
|  | 253 | if (Walker->Ancestor->GraphNr != Root->GraphNr) { | 
|---|
|  | 254 | // (6)  (Ancestor of Walker is not Root) | 
|---|
|  | 255 | if (Walker->LowpointNr < Walker->Ancestor->GraphNr) { | 
|---|
|  | 256 | // (6a) set Ancestor's Lowpoint number to minimum of of its Ancestor and itself, go to Step(8) | 
|---|
|  | 257 | Walker->Ancestor->LowpointNr = (Walker->Ancestor->LowpointNr < Walker->LowpointNr) ? Walker->Ancestor->LowpointNr : Walker->LowpointNr; | 
|---|
|  | 258 | LOG(2, "(6) Setting Walker[" << Walker->getName() << "]'s Ancestor[" << Walker->Ancestor->getName() << "]'s Lowpoint to " << Walker->Ancestor->LowpointNr << "."); | 
|---|
|  | 259 | } else { | 
|---|
|  | 260 | // (7) (Ancestor of Walker is a separating vertex, remove all from stack till Walker (including), these and Ancestor form a component | 
|---|
|  | 261 | Walker->Ancestor->SeparationVertex = true; | 
|---|
|  | 262 | LOG(2, "(7) Walker[" << Walker->getName() << "]'s Ancestor[" << Walker->Ancestor->getName() << "]'s is a separating vertex, creating component."); | 
|---|
|  | 263 | SetNextComponentNumber(Walker->Ancestor, ComponentNumber); | 
|---|
|  | 264 | LOG(3, "(7) Walker[" << Walker->getName() << "]'s Ancestor's Compont is " << ComponentNumber << "."); | 
|---|
|  | 265 | SetNextComponentNumber(Walker, ComponentNumber); | 
|---|
|  | 266 | LOG(3, "(7) Walker[" << Walker->getName() << "]'s Compont is " << ComponentNumber << "."); | 
|---|
|  | 267 | do { | 
|---|
|  | 268 | ASSERT(!AtomStack.empty(), "DepthFirstSearchAnalysis_CheckForaNewComponent() - AtomStack is empty!"); | 
|---|
|  | 269 | OtherAtom = AtomStack.front(); | 
|---|
|  | 270 | AtomStack.pop_front(); | 
|---|
|  | 271 | Subgraph.push_back(OtherAtom); | 
|---|
|  | 272 | SetNextComponentNumber(OtherAtom, ComponentNumber); | 
|---|
|  | 273 | LOG(3, "(7) Other[" << OtherAtom->getName() << "]'s Compont is " << ComponentNumber << "."); | 
|---|
|  | 274 | } while (OtherAtom != Walker); | 
|---|
|  | 275 | ComponentNumber++; | 
|---|
|  | 276 | } | 
|---|
|  | 277 | // (8) Walker becomes its Ancestor, go to (3) | 
|---|
|  | 278 | LOG(2, "(8) Walker[" << Walker->getName() << "] is now its Ancestor " << Walker->Ancestor->getName() << ", backstepping. "); | 
|---|
|  | 279 | Walker = Walker->Ancestor; | 
|---|
|  | 280 | BackStepping = true; | 
|---|
|  | 281 | } | 
|---|
|  | 282 | } | 
|---|
|  | 283 |  | 
|---|
|  | 284 |  | 
|---|
|  | 285 | void DepthFirstSearchAnalysis::CleanRootStackDownTillWalker(atom *&Walker, bond *&Binder, ConnectedSubgraph &Subgraph) | 
|---|
|  | 286 | { | 
|---|
|  | 287 | atom *OtherAtom = NULL; | 
|---|
|  | 288 |  | 
|---|
|  | 289 | if (!BackStepping) { // coming from (8) want to go to (3) | 
|---|
|  | 290 | // (9) remove all from stack till Walker (including), these and Root form a component | 
|---|
|  | 291 | //AtomStack.Output(out); | 
|---|
|  | 292 | SetNextComponentNumber(Root, ComponentNumber); | 
|---|
|  | 293 | LOG(3, "(9) Root[" << Root->getName() << "]'s Component is " << ComponentNumber << "."); | 
|---|
|  | 294 | SetNextComponentNumber(Walker, ComponentNumber); | 
|---|
|  | 295 | LOG(3, "(9) Walker[" << Walker->getName() << "]'s Component is " << ComponentNumber << "."); | 
|---|
|  | 296 | do { | 
|---|
|  | 297 | ASSERT(!AtomStack.empty(), "DepthFirstSearchAnalysis::CleanRootStackDownTillWalker() - AtomStack is empty!"); | 
|---|
|  | 298 | OtherAtom = AtomStack.front(); | 
|---|
|  | 299 | AtomStack.pop_front(); | 
|---|
|  | 300 | Subgraph.push_back(OtherAtom); | 
|---|
|  | 301 | SetNextComponentNumber(OtherAtom, ComponentNumber); | 
|---|
|  | 302 | LOG(3, "(7) Other[" << OtherAtom->getName() << "]'s Component is " << ComponentNumber << "."); | 
|---|
|  | 303 | } while (OtherAtom != Walker); | 
|---|
|  | 304 | ComponentNumber++; | 
|---|
|  | 305 |  | 
|---|
|  | 306 | // (11) Root is separation vertex,  set Walker to Root and go to (4) | 
|---|
|  | 307 | Walker = Root; | 
|---|
|  | 308 | Binder = FindNextUnused(Walker); | 
|---|
|  | 309 | if (Binder != NULL) { // Root is separation vertex | 
|---|
|  | 310 | LOG(1, "(10) Walker is Root[" << Root->getName() << "], next Unused Bond is " << *Binder << "."); | 
|---|
|  | 311 | LOG(1, "(11) Root is a separation vertex."); | 
|---|
|  | 312 | Walker->SeparationVertex = true; | 
|---|
|  | 313 | } else { | 
|---|
|  | 314 | LOG(1, "(10) Walker is Root[" << Root->getName() << "], no next Unused Bond."); | 
|---|
|  | 315 | } | 
|---|
|  | 316 | } | 
|---|
|  | 317 | } | 
|---|
|  | 318 |  | 
|---|
|  | 319 |  | 
|---|
|  | 320 | const std::deque<bond *>& DepthFirstSearchAnalysis::getBackEdgeStack() const | 
|---|
|  | 321 | { | 
|---|
|  | 322 | return BackEdgeStack; | 
|---|
|  | 323 | } | 
|---|
|  | 324 |  | 
|---|
|  | 325 |  | 
|---|
|  | 326 | void DepthFirstSearchAnalysis::operator()() | 
|---|
|  | 327 | { | 
|---|
|  | 328 | Info FunctionInfo("DepthFirstSearchAnalysis"); | 
|---|
|  | 329 | ListOfConnectedSubgraphs.clear(); | 
|---|
|  | 330 | int OldGraphNr = 0; | 
|---|
|  | 331 | atom *Walker = NULL; | 
|---|
|  | 332 | bond *Binder = NULL; | 
|---|
|  | 333 |  | 
|---|
|  | 334 | if (World::getInstance().numAtoms() == 0) | 
|---|
|  | 335 | return; | 
|---|
|  | 336 |  | 
|---|
|  | 337 | Init(); | 
|---|
|  | 338 |  | 
|---|
|  | 339 | LOG(0, "STATUS: Start walking the bond graph."); | 
|---|
|  | 340 | for(World::AtomIterator iter = World::getInstance().getAtomIter(); | 
|---|
|  | 341 | iter != World::getInstance().atomEnd();) { // don't advance, is done at the end | 
|---|
|  | 342 | Root = *iter; | 
|---|
|  | 343 | // (1) mark all edges unused, empty stack, set atom->GraphNr = -1 for all | 
|---|
|  | 344 | AtomStack.clear(); | 
|---|
|  | 345 |  | 
|---|
|  | 346 | // put into new subgraph molecule and add this to list of subgraphs | 
|---|
|  | 347 | ConnectedSubgraph CurrentSubgraph; | 
|---|
|  | 348 | CurrentSubgraph.push_back(Root); | 
|---|
|  | 349 |  | 
|---|
|  | 350 | OldGraphNr = CurrentGraphNr; | 
|---|
|  | 351 | Walker = Root; | 
|---|
|  | 352 | do { // (10) | 
|---|
|  | 353 | do { // (2) set number and Lowpoint of Atom to i, increase i, push current atom | 
|---|
|  | 354 | SetWalkersGraphNr(Walker); | 
|---|
|  | 355 |  | 
|---|
|  | 356 | ProbeAlongUnusedBond(Walker, Binder); | 
|---|
|  | 357 |  | 
|---|
|  | 358 | if (Binder == NULL) { | 
|---|
|  | 359 | LOG(2, "No more Unused Bonds."); | 
|---|
|  | 360 | break; | 
|---|
|  | 361 | } else | 
|---|
|  | 362 | Binder = NULL; | 
|---|
|  | 363 | } while (1); // (2) | 
|---|
|  | 364 |  | 
|---|
|  | 365 | // if we came from backstepping, yet there were no more unused bonds, we end up here with no Ancestor, because Walker is Root! Then we are finished! | 
|---|
|  | 366 | if ((Walker == Root) && (Binder == NULL)) | 
|---|
|  | 367 | break; | 
|---|
|  | 368 |  | 
|---|
|  | 369 | CheckForaNewComponent( Walker,  CurrentSubgraph); | 
|---|
|  | 370 |  | 
|---|
|  | 371 | CleanRootStackDownTillWalker(Walker, Binder,  CurrentSubgraph); | 
|---|
|  | 372 |  | 
|---|
|  | 373 | } while ((BackStepping) || (Binder != NULL)); // (10) halt only if Root has no unused edges | 
|---|
|  | 374 |  | 
|---|
|  | 375 | ListOfConnectedSubgraphs.push_back(CurrentSubgraph); | 
|---|
|  | 376 | // From OldGraphNr to CurrentGraphNr ranges an disconnected subgraph | 
|---|
|  | 377 | std::stringstream output; | 
|---|
|  | 378 | output << CurrentSubgraph; | 
|---|
|  | 379 | LOG(0, "STATUS: Disconnected subgraph ranges from " << OldGraphNr << " to " | 
|---|
|  | 380 | << CurrentGraphNr-1 << ": " << output.str()); | 
|---|
|  | 381 |  | 
|---|
|  | 382 | // step on to next root | 
|---|
|  | 383 | while (iter != World::getInstance().atomEnd()) { | 
|---|
|  | 384 | if ((*iter)->GraphNr != -1) { // if already discovered, step on | 
|---|
|  | 385 | iter++; | 
|---|
|  | 386 | } else { | 
|---|
|  | 387 | LOG(1,"Current next subgraph root candidate is " << (*iter)->getName() | 
|---|
|  | 388 | << " with GraphNr " << (*iter)->GraphNr << "."); | 
|---|
|  | 389 | break; | 
|---|
|  | 390 | } | 
|---|
|  | 391 | } | 
|---|
|  | 392 | } | 
|---|
|  | 393 | LOG(0, "STATUS: Done walking the bond graph."); | 
|---|
|  | 394 |  | 
|---|
|  | 395 | // set cyclic bond criterium on "same LP" basis | 
|---|
|  | 396 | CyclicBondAnalysis(); | 
|---|
|  | 397 |  | 
|---|
|  | 398 | OutputGraphInfoPerAtom(); | 
|---|
|  | 399 |  | 
|---|
|  | 400 | OutputGraphInfoPerBond(); | 
|---|
|  | 401 | } | 
|---|
|  | 402 |  | 
|---|
|  | 403 | void DepthFirstSearchAnalysis::UpdateMoleculeStructure() const | 
|---|
|  | 404 | { | 
|---|
|  | 405 | // remove all of World's molecules | 
|---|
|  | 406 | for (World::MoleculeIterator iter = World::getInstance().getMoleculeIter(); | 
|---|
|  | 407 | World::getInstance().getMoleculeIter() != World::getInstance().moleculeEnd(); | 
|---|
|  | 408 | iter = World::getInstance().getMoleculeIter()) { | 
|---|
|  | 409 | World::getInstance().getMolecules()->erase(*iter); | 
|---|
|  | 410 | World::getInstance().destroyMolecule(*iter); | 
|---|
|  | 411 | } | 
|---|
|  | 412 | // instantiate new molecules | 
|---|
|  | 413 | molecule *newmol = NULL; | 
|---|
|  | 414 | for (ConnectedSubgraphList::const_iterator iter = ListOfConnectedSubgraphs.begin(); | 
|---|
|  | 415 | iter != ListOfConnectedSubgraphs.end(); | 
|---|
|  | 416 | ++iter) { | 
|---|
|  | 417 | LOG(0, "STATUS: Creating new molecule:"); | 
|---|
|  | 418 | std::stringstream output; | 
|---|
|  | 419 | newmol = (*iter).getMolecule(); | 
|---|
|  | 420 | newmol->Output(&output); | 
|---|
|  | 421 | std::stringstream outstream(output.str()); | 
|---|
|  | 422 | std::string line; | 
|---|
|  | 423 | while (getline(outstream, line)) { | 
|---|
|  | 424 | LOG(0, "\t"+line); | 
|---|
|  | 425 | } | 
|---|
|  | 426 | } | 
|---|
|  | 427 | } | 
|---|
|  | 428 |  | 
|---|
|  | 429 | MoleculeLeafClass *DepthFirstSearchAnalysis::getMoleculeStructure() const | 
|---|
|  | 430 | { | 
|---|
|  | 431 | MoleculeLeafClass *Subgraphs = new MoleculeLeafClass(NULL); | 
|---|
|  | 432 | MoleculeLeafClass *MolecularWalker = Subgraphs; | 
|---|
|  | 433 | for (World::MoleculeIterator iter = World::getInstance().getMoleculeIter(); | 
|---|
|  | 434 | iter != World::getInstance().moleculeEnd(); | 
|---|
|  | 435 | ++iter) { | 
|---|
|  | 436 | // TODO: Remove the insertion into molecule when saving does not depend on them anymore. Also, remove molecule.hpp include | 
|---|
|  | 437 | MolecularWalker = new MoleculeLeafClass(MolecularWalker); | 
|---|
|  | 438 | MolecularWalker->Leaf = (*iter); | 
|---|
|  | 439 | } | 
|---|
|  | 440 | return Subgraphs; | 
|---|
|  | 441 | } | 
|---|
|  | 442 |  | 
|---|