[bcf653] | 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 |
|
---|
[97ebf8] | 8 | /*
|
---|
| 9 | * SubgraphDissectionAction.cpp
|
---|
| 10 | *
|
---|
| 11 | * Created on: May 9, 2010
|
---|
| 12 | * Author: heber
|
---|
| 13 | */
|
---|
| 14 |
|
---|
[bf3817] | 15 | // include config.h
|
---|
| 16 | #ifdef HAVE_CONFIG_H
|
---|
| 17 | #include <config.h>
|
---|
| 18 | #endif
|
---|
| 19 |
|
---|
[ad011c] | 20 | #include "CodePatterns/MemDebug.hpp"
|
---|
[112b09] | 21 |
|
---|
[faa1c9] | 22 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
[d74077] | 23 | #include "Descriptors/MoleculeDescriptor.hpp"
|
---|
| 24 |
|
---|
[97ebf8] | 25 | #include "atom.hpp"
|
---|
[129204] | 26 | #include "Bond/bond.hpp"
|
---|
[97ebf8] | 27 | #include "config.hpp"
|
---|
[ad011c] | 28 | #include "CodePatterns/Log.hpp"
|
---|
| 29 | #include "CodePatterns/Verbose.hpp"
|
---|
[129204] | 30 | #include "Graph/BondGraph.hpp"
|
---|
[49c059] | 31 | #include "Graph/DepthFirstSearchAnalysis.hpp"
|
---|
[97ebf8] | 32 | #include "molecule.hpp"
|
---|
| 33 | #include "World.hpp"
|
---|
| 34 |
|
---|
| 35 | #include <iostream>
|
---|
| 36 | #include <string>
|
---|
| 37 |
|
---|
[faa1c9] | 38 | typedef std::map< moleculeId_t, std::vector<atomId_t> > MolAtomList;
|
---|
[7d8342] | 39 | typedef std::map< atomId_t, atomId_t > AtomAtomList;
|
---|
[faa1c9] | 40 |
|
---|
[1fd675] | 41 | using namespace std;
|
---|
[1015fd] | 42 |
|
---|
[d09093] | 43 | #include "Actions/GraphAction/SubgraphDissectionAction.hpp"
|
---|
[1015fd] | 44 |
|
---|
[1fd675] | 45 | // and construct the stuff
|
---|
| 46 | #include "SubgraphDissectionAction.def"
|
---|
| 47 | #include "Action_impl_pre.hpp"
|
---|
| 48 | /** =========== define the function ====================== */
|
---|
[d09093] | 49 | Action::state_ptr GraphSubgraphDissectionAction::performCall() {
|
---|
[1fd675] | 50 | // obtain information
|
---|
| 51 | getParametersfromValueStorage();
|
---|
| 52 |
|
---|
[2b7d1b] | 53 |
|
---|
[7d8342] | 54 | // first create stuff for undo state
|
---|
[3738f0] | 55 | LOG(0, "STATUS: Creating undo state.");
|
---|
[faa1c9] | 56 | MolAtomList moleculelist;
|
---|
| 57 | vector<molecule *> allmolecules = World::getInstance().getAllMolecules();
|
---|
| 58 | for (vector<molecule *>::const_iterator moliter = allmolecules.begin(); moliter != allmolecules.end(); ++moliter) {
|
---|
| 59 | std::vector<atomId_t> atomlist;
|
---|
| 60 | atomlist.resize((*moliter)->size());
|
---|
| 61 | for (molecule::const_iterator atomiter = (*moliter)->begin(); atomiter != (*moliter)->end(); ++atomiter) {
|
---|
| 62 | atomlist.push_back((*atomiter)->getId());
|
---|
| 63 | }
|
---|
| 64 | moleculelist.insert( std::pair< moleculeId_t, std::vector<atomId_t> > ((*moliter)->getId(), atomlist));
|
---|
| 65 | }
|
---|
[d09093] | 66 | GraphSubgraphDissectionState *UndoState = new GraphSubgraphDissectionState(moleculelist, params);
|
---|
[faa1c9] | 67 |
|
---|
[d74077] | 68 | // 0a. remove all present molecules
|
---|
[3738f0] | 69 | LOG(0, "STATUS: Removing all present molecules.");
|
---|
[7d8342] | 70 | MoleculeListClass *molecules = World::getInstance().getMolecules();
|
---|
[d74077] | 71 | for (vector<molecule *>::iterator MolRunner = allmolecules.begin(); MolRunner != allmolecules.end(); ++MolRunner) {
|
---|
| 72 | molecules->erase(*MolRunner);
|
---|
| 73 | World::getInstance().destroyMolecule(*MolRunner);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | // 0b. remove all bonds and construct a molecule with all atoms
|
---|
| 77 | molecule *mol = World::getInstance().createMolecule();
|
---|
[faa1c9] | 78 | {
|
---|
| 79 | vector <atom *> allatoms = World::getInstance().getAllAtoms();
|
---|
| 80 | for(vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner) {
|
---|
[458c31] | 81 | // const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
|
---|
[9d83b6] | 82 | // for(BondList::iterator BondRunner = ListOfBonds.begin();
|
---|
| 83 | // !ListOfBonds.empty();
|
---|
| 84 | // BondRunner = ListOfBonds.begin()) {
|
---|
[7d8342] | 85 | // delete(*BondRunner);
|
---|
[9d83b6] | 86 | // }
|
---|
[faa1c9] | 87 | mol->AddAtom(*AtomRunner);
|
---|
| 88 | }
|
---|
[d74077] | 89 | }
|
---|
| 90 |
|
---|
| 91 | // 1. create the bond structure of the single molecule
|
---|
[3738f0] | 92 | LOG(0, "STATUS: (Re-)constructing adjacency.");
|
---|
[458c31] | 93 | if (mol->getBondCount() == 0) {
|
---|
[f71baf] | 94 | BondGraph *BG = World::getInstance().getBondGraph();
|
---|
[3738f0] | 95 | molecule::atomVector Set = mol->getAtomSet();
|
---|
| 96 | BG->CreateAdjacency(Set);
|
---|
| 97 | // if (mol->getBondCount() == 0) {
|
---|
| 98 | // World::getInstance().destroyMolecule(mol);
|
---|
| 99 | // ELOG(1, "There are no bonds.");
|
---|
| 100 | // return Action::failure;
|
---|
| 101 | // }
|
---|
[d74077] | 102 | }
|
---|
| 103 |
|
---|
| 104 | // 2. scan for connected subgraphs
|
---|
[49c059] | 105 | DepthFirstSearchAnalysis DFS;
|
---|
| 106 | DFS();
|
---|
| 107 | DFS.UpdateMoleculeStructure();
|
---|
| 108 | if (!World::getInstance().numMolecules()) {
|
---|
[faa1c9] | 109 | //World::getInstance().destroyMolecule(mol);
|
---|
[d74077] | 110 | DoeLog(1) && (eLog()<< Verbose(1) << "There are no atoms." << endl);
|
---|
| 111 | return Action::failure;
|
---|
| 112 | }
|
---|
[7d8342] | 113 |
|
---|
[49c059] | 114 | DoLog(1) && (Log() << Verbose(1) << "I scanned " << World::getInstance().numMolecules() << " molecules." << endl);
|
---|
[d74077] | 115 |
|
---|
[faa1c9] | 116 | return Action::state_ptr(UndoState);
|
---|
[97ebf8] | 117 | }
|
---|
| 118 |
|
---|
[d09093] | 119 | Action::state_ptr GraphSubgraphDissectionAction::performUndo(Action::state_ptr _state) {
|
---|
| 120 | GraphSubgraphDissectionState *state = assert_cast<GraphSubgraphDissectionState*>(_state.get());
|
---|
[faa1c9] | 121 |
|
---|
| 122 | {
|
---|
| 123 | // remove all present molecules
|
---|
| 124 | MoleculeListClass *molecules = World::getInstance().getMolecules();
|
---|
| 125 | vector<molecule *> allmolecules = World::getInstance().getAllMolecules();
|
---|
| 126 | for (vector<molecule *>::iterator MolRunner = allmolecules.begin(); MolRunner != allmolecules.end(); ++MolRunner) {
|
---|
| 127 | molecules->erase(*MolRunner);
|
---|
| 128 | World::getInstance().destroyMolecule(*MolRunner);
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | {
|
---|
| 133 | // construct the old state
|
---|
[7d8342] | 134 | MoleculeListClass *molecules = World::getInstance().getMolecules();
|
---|
[faa1c9] | 135 | molecule *mol = NULL;
|
---|
| 136 | for (MolAtomList::const_iterator iter = state->moleculelist.begin(); iter != state->moleculelist.end(); ++iter) {
|
---|
| 137 | mol = World::getInstance().createMolecule();
|
---|
| 138 | if (mol->getId() != (*iter).first)
|
---|
| 139 | World::getInstance().changeMoleculeId(mol->getId(), (*iter).first);
|
---|
[7d8342] | 140 | for (std::vector<atomId_t>::const_iterator atomiter = (*iter).second.begin(); atomiter != (*iter).second.end(); ++atomiter) {
|
---|
| 141 | atom *Walker = World::getInstance().getAtom(AtomById(*atomiter));
|
---|
| 142 | mol->AddAtom(Walker);
|
---|
| 143 | }
|
---|
| 144 | molecules->insert(mol);
|
---|
[faa1c9] | 145 | }
|
---|
| 146 | }
|
---|
[97ebf8] | 147 |
|
---|
[faa1c9] | 148 | return Action::state_ptr(_state);
|
---|
[97ebf8] | 149 | }
|
---|
| 150 |
|
---|
[d09093] | 151 | Action::state_ptr GraphSubgraphDissectionAction::performRedo(Action::state_ptr _state){
|
---|
[faa1c9] | 152 | return performCall();
|
---|
[97ebf8] | 153 | }
|
---|
| 154 |
|
---|
[d09093] | 155 | bool GraphSubgraphDissectionAction::canUndo() {
|
---|
[faa1c9] | 156 | return true;
|
---|
[97ebf8] | 157 | }
|
---|
| 158 |
|
---|
[d09093] | 159 | bool GraphSubgraphDissectionAction::shouldUndo() {
|
---|
[faa1c9] | 160 | return true;
|
---|
[97ebf8] | 161 | }
|
---|
[1fd675] | 162 | /** =========== end of function ====================== */
|
---|