source: src/Actions/GraphAction/UpdateMoleculesAction.cpp@ d2be22

Candidate_v1.7.1 stable
Last change on this file since d2be22 was d2be22, checked in by Frederik Heber <frederik.heber@…>, 6 weeks ago

Actions relying on BondGraph fail if not bond table is loaded.

  • this is to ensure to not stumble over missing optimal bond lengths from the table, like with StretchBondAction.
  • TESTFIX: All regression tests that use these actions need to load the bond-table now.
  • Property mode set to 100644
File size: 4.8 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
5 * Copyright (C) 2013 Frederik Heber. All rights reserved.
6 *
7 *
8 * This file is part of MoleCuilder.
9 *
10 * MoleCuilder is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * MoleCuilder is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24/*
25 * UpdateMoleculesAction.cpp
26 *
27 * Created on: May 9, 2010
28 * Author: heber
29 */
30
31// include config.h
32#ifdef HAVE_CONFIG_H
33#include <config.h>
34#endif
35
36//#include "CodePatterns/MemDebug.hpp"
37
38#include "Descriptors/AtomIdDescriptor.hpp"
39#include "Descriptors/MoleculeDescriptor.hpp"
40
41#include "Atom/atom.hpp"
42#include "Bond/bond.hpp"
43#include "CodePatterns/Log.hpp"
44#include "CodePatterns/Verbose.hpp"
45#include "Graph/DepthFirstSearchAnalysis.hpp"
46#include "molecule.hpp"
47#include "World.hpp"
48
49#include <iostream>
50#include <string>
51
52typedef std::map< moleculeId_t, std::vector<atomId_t> > MolAtomList;
53typedef std::map< atomId_t, atomId_t > AtomAtomList;
54
55#include "Actions/GraphAction/UpdateMoleculesAction.hpp"
56
57using namespace MoleCuilder;
58
59// and construct the stuff
60#include "UpdateMoleculesAction.def"
61#include "Action_impl_pre.hpp"
62/** =========== define the function ====================== */
63ActionState::ptr GraphUpdateMoleculesAction::performCall() {
64 // first create stuff for undo state
65 LOG(0, "STATUS: Creating undo state.");
66 MolAtomList moleculelist;
67 {
68 vector<const molecule *> allmolecules = const_cast<const World &>(World::getInstance()).
69 getAllMolecules();
70 for (vector<const molecule *>::const_iterator moliter = allmolecules.begin(); moliter != allmolecules.end(); ++moliter) {
71 std::vector<atomId_t> atomlist;
72 atomlist.resize((*moliter)->size());
73 for (molecule::const_iterator atomiter = (*moliter)->begin(); atomiter != (*moliter)->end(); ++atomiter) {
74 atomlist.push_back((*atomiter)->getId());
75 }
76 moleculelist.insert( std::pair< moleculeId_t, std::vector<atomId_t> > ((*moliter)->getId(), atomlist));
77 }
78 }
79 GraphUpdateMoleculesState *UndoState = new GraphUpdateMoleculesState(moleculelist, params);
80
81 // 0a. remove all present molecules
82 LOG(0, "STATUS: Removing all present molecules.");
83 vector<molecule *> allmolecules = World::getInstance().getAllMolecules();
84 for (vector<molecule *>::iterator MolRunner = allmolecules.begin();
85 MolRunner != allmolecules.end();
86 ++MolRunner)
87 World::getInstance().destroyMolecule(*MolRunner);
88
89 // 2. scan for connected subgraphs
90 DepthFirstSearchAnalysis DFS;
91 DFS();
92 DFS.UpdateMoleculeStructure();
93 const size_t numMolecules =
94 const_cast<const World &>(World::getInstance()).numMolecules();
95 if (numMolecules == 0) {
96 //World::getInstance().destroyMolecule(mol);
97 STATUS("There are no molecules.");
98 return Action::failure;
99 }
100
101 LOG(1, "I scanned " << numMolecules << " molecules.");
102
103 return ActionState::ptr(UndoState);
104}
105
106ActionState::ptr GraphUpdateMoleculesAction::performUndo(ActionState::ptr _state) {
107 GraphUpdateMoleculesState *state = assert_cast<GraphUpdateMoleculesState*>(_state.get());
108
109 {
110 // remove all present molecules
111 vector<molecule *> allmolecules = World::getInstance().getAllMolecules();
112 for (vector<molecule *>::iterator MolRunner = allmolecules.begin();
113 MolRunner != allmolecules.end();
114 ++MolRunner)
115 World::getInstance().destroyMolecule(*MolRunner);
116 }
117
118 {
119 // construct the old state
120 molecule *mol = NULL;
121 for (MolAtomList::const_iterator iter = state->moleculelist.begin(); iter != state->moleculelist.end(); ++iter) {
122 mol = World::getInstance().createMolecule();
123 if (mol->getId() != (*iter).first)
124 World::getInstance().changeMoleculeId(mol->getId(), (*iter).first);
125 for (std::vector<atomId_t>::const_iterator atomiter = (*iter).second.begin(); atomiter != (*iter).second.end(); ++atomiter) {
126 atom *Walker = World::getInstance().getAtom(AtomById(*atomiter));
127 mol->AddAtom(Walker);
128 }
129 }
130 }
131
132 return ActionState::ptr(_state);
133}
134
135ActionState::ptr GraphUpdateMoleculesAction::performRedo(ActionState::ptr _state){
136 return performCall();
137}
138
139bool GraphUpdateMoleculesAction::canUndo() {
140 return true;
141}
142
143bool GraphUpdateMoleculesAction::shouldUndo() {
144 return true;
145}
146/** =========== end of function ====================== */
Note: See TracBrowser for help on using the repository browser.