/* * Project: MoleCuilder * Description: creates and alters molecular systems * Copyright (C) 2010-2011 University of Bonn. All rights reserved. * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. */ /* * SubgraphDissectionAction.cpp * * Created on: May 9, 2010 * Author: heber */ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include "CodePatterns/MemDebug.hpp" #include "Descriptors/AtomIdDescriptor.hpp" #include "Descriptors/MoleculeDescriptor.hpp" #include "atom.hpp" #include "Bond/bond.hpp" #include "CodePatterns/Log.hpp" #include "CodePatterns/Verbose.hpp" #include "Graph/BondGraph.hpp" #include "Graph/DepthFirstSearchAnalysis.hpp" #include "molecule.hpp" #include "MoleculeListClass.hpp" #include "World.hpp" #include #include typedef std::map< moleculeId_t, std::vector > MolAtomList; typedef std::map< atomId_t, atomId_t > AtomAtomList; #include "Actions/GraphAction/SubgraphDissectionAction.hpp" using namespace MoleCuilder; // and construct the stuff #include "SubgraphDissectionAction.def" #include "Action_impl_pre.hpp" /** =========== define the function ====================== */ Action::state_ptr GraphSubgraphDissectionAction::performCall() { // obtain information getParametersfromValueStorage(); // first create stuff for undo state LOG(0, "STATUS: Creating undo state."); MolAtomList moleculelist; vector allmolecules = World::getInstance().getAllMolecules(); for (vector::const_iterator moliter = allmolecules.begin(); moliter != allmolecules.end(); ++moliter) { std::vector atomlist; atomlist.resize((*moliter)->size()); for (molecule::const_iterator atomiter = (*moliter)->begin(); atomiter != (*moliter)->end(); ++atomiter) { atomlist.push_back((*atomiter)->getId()); } moleculelist.insert( std::pair< moleculeId_t, std::vector > ((*moliter)->getId(), atomlist)); } GraphSubgraphDissectionState *UndoState = new GraphSubgraphDissectionState(moleculelist, params); // 0a. remove all present molecules LOG(0, "STATUS: Removing all present molecules."); MoleculeListClass *molecules = World::getInstance().getMolecules(); for (vector::iterator MolRunner = allmolecules.begin(); MolRunner != allmolecules.end(); ++MolRunner) { molecules->erase(*MolRunner); World::getInstance().destroyMolecule(*MolRunner); } // 1. create the bond structure of the single molecule LOG(0, "STATUS: (Re-)constructing adjacency."); BondGraph *BG = World::getInstance().getBondGraph(); World::AtomComposite Set = World::getInstance().getAllAtoms(); BG->CreateAdjacency(Set); // 2. scan for connected subgraphs DepthFirstSearchAnalysis DFS; DFS(); DFS.UpdateMoleculeStructure(); if (!World::getInstance().numMolecules()) { //World::getInstance().destroyMolecule(mol); ELOG(1, "There are no molecules."); return Action::failure; } LOG(1, "I scanned " << World::getInstance().numMolecules() << " molecules."); return Action::state_ptr(UndoState); } Action::state_ptr GraphSubgraphDissectionAction::performUndo(Action::state_ptr _state) { GraphSubgraphDissectionState *state = assert_cast(_state.get()); { // remove all present molecules MoleculeListClass *molecules = World::getInstance().getMolecules(); vector allmolecules = World::getInstance().getAllMolecules(); for (vector::iterator MolRunner = allmolecules.begin(); MolRunner != allmolecules.end(); ++MolRunner) { molecules->erase(*MolRunner); World::getInstance().destroyMolecule(*MolRunner); } } { // construct the old state MoleculeListClass *molecules = World::getInstance().getMolecules(); molecule *mol = NULL; for (MolAtomList::const_iterator iter = state->moleculelist.begin(); iter != state->moleculelist.end(); ++iter) { mol = World::getInstance().createMolecule(); if (mol->getId() != (*iter).first) World::getInstance().changeMoleculeId(mol->getId(), (*iter).first); for (std::vector::const_iterator atomiter = (*iter).second.begin(); atomiter != (*iter).second.end(); ++atomiter) { atom *Walker = World::getInstance().getAtom(AtomById(*atomiter)); mol->AddAtom(Walker); } molecules->insert(mol); } } return Action::state_ptr(_state); } Action::state_ptr GraphSubgraphDissectionAction::performRedo(Action::state_ptr _state){ return performCall(); } bool GraphSubgraphDissectionAction::canUndo() { return true; } bool GraphSubgraphDissectionAction::shouldUndo() { return true; } /** =========== end of function ====================== */