| [53d6b2] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
| [0aa122] | 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| [94d5ac6] | 5 | * | 
|---|
|  | 6 | * | 
|---|
|  | 7 | *   This file is part of MoleCuilder. | 
|---|
|  | 8 | * | 
|---|
|  | 9 | *    MoleCuilder is free software: you can redistribute it and/or modify | 
|---|
|  | 10 | *    it under the terms of the GNU General Public License as published by | 
|---|
|  | 11 | *    the Free Software Foundation, either version 2 of the License, or | 
|---|
|  | 12 | *    (at your option) any later version. | 
|---|
|  | 13 | * | 
|---|
|  | 14 | *    MoleCuilder is distributed in the hope that it will be useful, | 
|---|
|  | 15 | *    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
|  | 16 | *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
|  | 17 | *    GNU General Public License for more details. | 
|---|
|  | 18 | * | 
|---|
|  | 19 | *    You should have received a copy of the GNU General Public License | 
|---|
|  | 20 | *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| [53d6b2] | 21 | */ | 
|---|
|  | 22 |  | 
|---|
|  | 23 | /* | 
|---|
|  | 24 | * BreadthFirstSearchAdd.cpp | 
|---|
|  | 25 | * | 
|---|
|  | 26 | *  Created on: Feb 16, 2011 | 
|---|
|  | 27 | *      Author: heber | 
|---|
|  | 28 | */ | 
|---|
|  | 29 |  | 
|---|
|  | 30 | // include config.h | 
|---|
|  | 31 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 32 | #include <config.h> | 
|---|
|  | 33 | #endif | 
|---|
|  | 34 |  | 
|---|
|  | 35 | #include "CodePatterns/MemDebug.hpp" | 
|---|
|  | 36 |  | 
|---|
|  | 37 | #include "BreadthFirstSearchAdd.hpp" | 
|---|
|  | 38 |  | 
|---|
| [47d041] | 39 | #include <sstream> | 
|---|
|  | 40 |  | 
|---|
| [6f0841] | 41 | #include "Atom/atom.hpp" | 
|---|
| [129204] | 42 | #include "Bond/bond.hpp" | 
|---|
| [53d6b2] | 43 | #include "CodePatterns/Assert.hpp" | 
|---|
|  | 44 | #include "CodePatterns/Info.hpp" | 
|---|
|  | 45 | #include "CodePatterns/Log.hpp" | 
|---|
|  | 46 | #include "CodePatterns/Verbose.hpp" | 
|---|
|  | 47 | #include "molecule.hpp" | 
|---|
|  | 48 | #include "World.hpp" | 
|---|
|  | 49 |  | 
|---|
|  | 50 |  | 
|---|
| [07a47e] | 51 | BreadthFirstSearchAdd::BreadthFirstSearchAdd(atom *&_Root, int _BondOrder, bool _IsAngstroem, const enum HydrogenSaturation _saturation) : | 
|---|
| [53d6b2] | 52 | BondOrder(_BondOrder), | 
|---|
|  | 53 | Root(_Root), | 
|---|
| [07a47e] | 54 | saturation(_saturation), | 
|---|
| [53d6b2] | 55 | IsAngstroem(_IsAngstroem) | 
|---|
|  | 56 | { | 
|---|
|  | 57 | BFSStack.push_front(Root); | 
|---|
|  | 58 | } | 
|---|
|  | 59 |  | 
|---|
|  | 60 |  | 
|---|
|  | 61 | BreadthFirstSearchAdd::~BreadthFirstSearchAdd() | 
|---|
|  | 62 | {} | 
|---|
|  | 63 |  | 
|---|
|  | 64 | void BreadthFirstSearchAdd::Init(atom *&_Root, int _BondOrder) | 
|---|
|  | 65 | { | 
|---|
|  | 66 | BondOrder = _BondOrder; | 
|---|
|  | 67 | Root = _Root; | 
|---|
|  | 68 | BFSStack.clear(); | 
|---|
|  | 69 | BFSStack.push_front(Root); | 
|---|
|  | 70 | } | 
|---|
|  | 71 |  | 
|---|
|  | 72 | void BreadthFirstSearchAdd::InitNode(atomId_t atom_id) | 
|---|
|  | 73 | { | 
|---|
|  | 74 | PredecessorList[atom_id] = NULL; | 
|---|
|  | 75 | ShortestPathList[atom_id] = -1; | 
|---|
|  | 76 | if (AddedAtomList.find(atom_id) != AddedAtomList.end()) // mark already present atoms (i.e. Root and maybe others) as visited | 
|---|
| [129204] | 77 | ColorList[atom_id] = GraphEdge::lightgray; | 
|---|
| [53d6b2] | 78 | else | 
|---|
| [129204] | 79 | ColorList[atom_id] = GraphEdge::white; | 
|---|
| [53d6b2] | 80 | } | 
|---|
|  | 81 |  | 
|---|
|  | 82 |  | 
|---|
|  | 83 | void BreadthFirstSearchAdd::UnvisitedNode(molecule *Mol, atom *&Walker, atom *&OtherAtom, bond *&Binder, bond *&Bond) | 
|---|
|  | 84 | { | 
|---|
| [129204] | 85 | if (Binder != Bond) // let other atom GraphEdge::white if it's via Root bond. In case it's cyclic it has to be reached again (yet Root is from OtherAtom already GraphEdge::black, thus no problem) | 
|---|
|  | 86 | ColorList[OtherAtom->getNr()] = GraphEdge::lightgray; | 
|---|
| [53d6b2] | 87 | PredecessorList[OtherAtom->getNr()] = Walker; // Walker is the predecessor | 
|---|
|  | 88 | ShortestPathList[OtherAtom->getNr()] = ShortestPathList[Walker->getNr()] + 1; | 
|---|
| [47d041] | 89 | LOG(2, "Coloring OtherAtom " << OtherAtom->getName() << " " << GraphEdge::getColorName(ColorList[OtherAtom->getNr()]) << ", its predecessor is " << Walker->getName() << " and its Shortest Path is " << ShortestPathList[OtherAtom->getNr()] << " egde(s) long."); | 
|---|
| [53d6b2] | 90 | if ((((ShortestPathList[OtherAtom->getNr()] < BondOrder) && (Binder != Bond)))) { // Check for maximum distance | 
|---|
| [47d041] | 91 | std::stringstream output; | 
|---|
| [53d6b2] | 92 | if (AddedAtomList[OtherAtom->getNr()] == NULL) { // add if it's not been so far | 
|---|
|  | 93 | AddedAtomList[OtherAtom->getNr()] = Mol->AddCopyAtom(OtherAtom); | 
|---|
| [47d041] | 94 | output << "Added OtherAtom " << OtherAtom->getName(); | 
|---|
| [efe516] | 95 | AddedBondList[Binder] = Mol->CopyBond(AddedAtomList[Walker->getNr()], AddedAtomList[OtherAtom->getNr()], Binder); | 
|---|
| [47d041] | 96 | output << " and bond " << *(AddedBondList[Binder]) << ", "; | 
|---|
| [129204] | 97 | } else { // this code should actually never come into play (all GraphEdge::white atoms are not yet present in BondMolecule, that's why they are GraphEdge::white in the first place) | 
|---|
| [47d041] | 98 | output << "Not adding OtherAtom " << OtherAtom->getName(); | 
|---|
| [efe516] | 99 | if (AddedBondList[Binder] == NULL) { | 
|---|
|  | 100 | AddedBondList[Binder] = Mol->CopyBond(AddedAtomList[Walker->getNr()], AddedAtomList[OtherAtom->getNr()], Binder); | 
|---|
| [47d041] | 101 | output << ", added Bond " << *(AddedBondList[Binder]); | 
|---|
| [53d6b2] | 102 | } else | 
|---|
| [47d041] | 103 | output << ", not added Bond "; | 
|---|
| [53d6b2] | 104 | } | 
|---|
| [47d041] | 105 | output << ", putting OtherAtom into queue."; | 
|---|
|  | 106 | LOG(0, output.str()); | 
|---|
| [53d6b2] | 107 | BFSStack.push_front(OtherAtom); | 
|---|
|  | 108 | } else { // out of bond order, then replace | 
|---|
|  | 109 | if ((AddedAtomList[OtherAtom->getNr()] == NULL) && (Binder->Cyclic)) | 
|---|
| [129204] | 110 | ColorList[OtherAtom->getNr()] = GraphEdge::white; // unmark if it has not been queued/added, to make it available via its other bonds (cyclic) | 
|---|
| [47d041] | 111 | { | 
|---|
|  | 112 | std::stringstream output; | 
|---|
|  | 113 | if (Binder == Bond) | 
|---|
|  | 114 | output << "Not Queueing, is the Root bond"; | 
|---|
|  | 115 | else if (ShortestPathList[OtherAtom->getNr()] >= BondOrder) | 
|---|
|  | 116 | output << "Not Queueing, is out of Bond Count of " << BondOrder; | 
|---|
|  | 117 | if (!Binder->Cyclic) | 
|---|
|  | 118 | output << ", is not part of a cyclic bond, saturating bond with Hydrogen."; | 
|---|
|  | 119 | LOG(3, output.str()); | 
|---|
|  | 120 | } | 
|---|
| [efe516] | 121 | if (AddedBondList[Binder] == NULL) { | 
|---|
| [53d6b2] | 122 | if ((AddedAtomList[OtherAtom->getNr()] != NULL)) { // .. whether we add or saturate | 
|---|
| [efe516] | 123 | AddedBondList[Binder] = Mol->CopyBond(AddedAtomList[Walker->getNr()], AddedAtomList[OtherAtom->getNr()], Binder); | 
|---|
| [53d6b2] | 124 | } else { | 
|---|
| [07a47e] | 125 | if (saturation == DoSaturate) | 
|---|
|  | 126 | if (!Mol->AddHydrogenReplacementAtom(Binder, AddedAtomList[Walker->getNr()], Walker, OtherAtom, IsAngstroem)) | 
|---|
|  | 127 | exit(1); | 
|---|
| [53d6b2] | 128 | } | 
|---|
|  | 129 | } | 
|---|
|  | 130 | } | 
|---|
|  | 131 | } | 
|---|
|  | 132 |  | 
|---|
|  | 133 |  | 
|---|
|  | 134 | void BreadthFirstSearchAdd::VisitedNode(molecule *Mol, atom *&Walker, atom *&OtherAtom, bond *&Binder, bond *&Bond) | 
|---|
|  | 135 | { | 
|---|
| [47d041] | 136 | LOG(3, "Not Adding, has already been visited."); | 
|---|
| [53d6b2] | 137 | // This has to be a cyclic bond, check whether it's present ... | 
|---|
| [efe516] | 138 | if (AddedBondList[Binder] == NULL) { | 
|---|
| [53d6b2] | 139 | if ((Binder != Bond) && (Binder->Cyclic) && (((ShortestPathList[Walker->getNr()] + 1) < BondOrder))) { | 
|---|
| [efe516] | 140 | AddedBondList[Binder] = Mol->CopyBond(AddedAtomList[Walker->getNr()], AddedAtomList[OtherAtom->getNr()], Binder); | 
|---|
| [53d6b2] | 141 | } else { // if it's root bond it has to broken (otherwise we would not create the fragments) | 
|---|
| [07a47e] | 142 | if (saturation == DoSaturate) | 
|---|
|  | 143 | if(!Mol->AddHydrogenReplacementAtom(Binder, AddedAtomList[Walker->getNr()], Walker, OtherAtom, IsAngstroem)) | 
|---|
|  | 144 | exit(1); | 
|---|
| [53d6b2] | 145 | } | 
|---|
|  | 146 | } | 
|---|
|  | 147 | } | 
|---|
|  | 148 |  | 
|---|
|  | 149 |  | 
|---|
|  | 150 | void BreadthFirstSearchAdd::operator()(molecule *Mol, atom *_Root, bond *Bond, int _BondOrder) | 
|---|
|  | 151 | { | 
|---|
|  | 152 | Info FunctionInfo("BreadthFirstSearchAdd"); | 
|---|
|  | 153 | atom *Walker = NULL, *OtherAtom = NULL; | 
|---|
|  | 154 | bond *Binder = NULL; | 
|---|
|  | 155 |  | 
|---|
|  | 156 | // add Root if not done yet | 
|---|
|  | 157 | if (AddedAtomList[_Root->getNr()] == NULL) // add Root if not yet present | 
|---|
|  | 158 | AddedAtomList[_Root->getNr()] = Mol->AddCopyAtom(_Root); | 
|---|
|  | 159 |  | 
|---|
|  | 160 | Init(_Root, _BondOrder); | 
|---|
|  | 161 |  | 
|---|
| [129204] | 162 | // and go on ... Queue always contains all GraphEdge::lightgray vertices | 
|---|
| [53d6b2] | 163 | while (!BFSStack.empty()) { | 
|---|
|  | 164 | // we have to pop the oldest atom from stack. This keeps the atoms on the stack always of the same ShortestPath distance. | 
|---|
|  | 165 | // e.g. if current atom is 2, push to end of stack are of length 3, but first all of length 2 would be popped. They again | 
|---|
|  | 166 | // append length of 3 (their neighbours). Thus on stack we have always atoms of a certain length n at bottom of stack and | 
|---|
|  | 167 | // followed by n+1 till top of stack. | 
|---|
|  | 168 | Walker = BFSStack.front(); // pop oldest added | 
|---|
|  | 169 | BFSStack.pop_front(); | 
|---|
|  | 170 | const BondList& ListOfBonds = Walker->getListOfBonds(); | 
|---|
| [47d041] | 171 | LOG(1, "Current Walker is: " << Walker->getName() << ", and has " << ListOfBonds.size() << " bonds."); | 
|---|
| [53d6b2] | 172 | for (BondList::const_iterator Runner = ListOfBonds.begin(); | 
|---|
|  | 173 | Runner != ListOfBonds.end(); | 
|---|
|  | 174 | ++Runner) { | 
|---|
|  | 175 | if ((*Runner) != NULL) { // don't look at bond equal NULL | 
|---|
|  | 176 | Binder = (*Runner); | 
|---|
|  | 177 | OtherAtom = (*Runner)->GetOtherAtom(Walker); | 
|---|
| [47d041] | 178 | LOG(2, "Current OtherAtom is: " << OtherAtom->getName() << " for bond " << *(*Runner) << "."); | 
|---|
| [129204] | 179 | if (ColorList[OtherAtom->getNr()] == GraphEdge::white) { | 
|---|
| [53d6b2] | 180 | UnvisitedNode(Mol, Walker, OtherAtom, Binder, Bond); | 
|---|
|  | 181 | } else { | 
|---|
|  | 182 | VisitedNode(Mol, Walker, OtherAtom, Binder, Bond); | 
|---|
|  | 183 | } | 
|---|
|  | 184 | } | 
|---|
|  | 185 | } | 
|---|
| [129204] | 186 | ColorList[Walker->getNr()] = GraphEdge::black; | 
|---|
| [47d041] | 187 | LOG(1, "Coloring Walker " << Walker->getName() << " GraphEdge::black."); | 
|---|
| [53d6b2] | 188 | } | 
|---|
|  | 189 | } | 
|---|
|  | 190 |  | 
|---|