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