source: src/Actions/GraphAction/CreateAdjacencyAction.cpp@ 25aa214

Candidate_v1.7.1 stable
Last change on this file since 25aa214 was d2be22, checked in by Frederik Heber <frederik.heber@…>, 7 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: 5.8 KB
RevLine 
[c449d9]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
[0aa122]4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
[5aaa43]5 * Copyright (C) 2013 Frederik Heber. All rights reserved.
[94d5ac6]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/>.
[c449d9]22 */
23
24/*
[d4a44c]25 * CreateAdjacencyAction.cpp
[c449d9]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
[9eb71b3]36//#include "CodePatterns/MemDebug.hpp"
[c449d9]37
[055ad7]38#include "CodePatterns/Assert.hpp"
[ad011c]39#include "CodePatterns/Log.hpp"
[fa9d1d]40
41#include "Descriptors/AtomSelectionDescriptor.hpp"
[129204]42#include "Graph/BondGraph.hpp"
[c449d9]43#include "molecule.hpp"
44#include "World.hpp"
45
46#include <iostream>
[34c43a]47#include <list>
[c449d9]48#include <string>
49
[d09093]50#include "Actions/GraphAction/CreateAdjacencyAction.hpp"
[c449d9]51
[ce7fdc]52using namespace MoleCuilder;
53
[055ad7]54// Storing undo state
55struct BondInfo_t {
56 atomId_t leftatom;
57 atomId_t rightatom;
58 size_t degree;
59};
60
[c449d9]61// and construct the stuff
[d4a44c]62#include "CreateAdjacencyAction.def"
[c449d9]63#include "Action_impl_pre.hpp"
64/** =========== define the function ====================== */
[b5b01e]65ActionState::ptr GraphCreateAdjacencyAction::performCall() {
[f71baf]66 BondGraph *BG = World::getInstance().getBondGraph();
[d09093]67 ASSERT(BG != NULL, "GraphCreateAdjacencyAction: BondGraph is NULL.");
[fa9d1d]68
[d2be22]69 if (!BG->IsBondLengthTableLoaded()) {
70 STATUS("BondLength table has not been loaded.");
71 return Action::failure;
72 }
73
[055ad7]74 // count all present bonds
[a58c16]75 World::ConstAtomComposite Set = const_cast<const World &>(World::getInstance()).
76 getAllAtoms(AtomsBySelection());
[055ad7]77 std::vector<BondInfo_t> bonds;
78 {
79 size_t count_bonds = 0;
[a58c16]80 for (World::ConstAtomComposite::const_iterator iter = Set.begin();
[055ad7]81 iter != Set.end();
82 ++iter) {
83 const atom * const Walker = *iter;
84 count_bonds += Walker->getListOfBonds().size();
85 }
86 bonds.reserve(count_bonds/2);
87 }
88 // Storing undo info
[a58c16]89 for (World::ConstAtomComposite::const_iterator iter = Set.begin();
[055ad7]90 iter != Set.end();
91 ++iter) {
92 const atom * const Walker = *iter;
93 const BondList& ListOfBonds = Walker->getListOfBonds();
94 for (BondList::const_iterator bonditer = ListOfBonds.begin();
95 bonditer != ListOfBonds.end();
96 ++bonditer) {
97 const bond::ptr &CurrentBond = *bonditer;
98 // if both atoms are in selected set, we check ids otherwise not
99 if (((!World::getInstance().isSelected(CurrentBond->leftatom))
100 || (!World::getInstance().isSelected(CurrentBond->rightatom)))
101 || (CurrentBond->leftatom->getId() < CurrentBond->rightatom->getId())) {
102 BondInfo_t BondInfo;
103 BondInfo.leftatom = CurrentBond->leftatom->getId();
104 BondInfo.rightatom = CurrentBond->rightatom->getId();
[1f693d]105 BondInfo.degree = CurrentBond->getDegree();
[055ad7]106 bonds.push_back(BondInfo);
107 }
108 }
109 }
110 GraphCreateAdjacencyState *UndoState = new GraphCreateAdjacencyState(bonds, params);
111
112 // now recreate adjacency
[a58c16]113 {
114 World::AtomComposite Set = World::getInstance().getAllAtoms(AtomsBySelection());
115 BG->CreateAdjacency(Set);
116 }
[fa9d1d]117
[055ad7]118 // give info
[fa9d1d]119 size_t BondCount = 0;
[1259df]120 std::vector<const molecule *> molecules = const_cast<const World &>(World::getInstance()).
121 getAllMolecules();
122 for (std::vector<const molecule *>::const_iterator iter = molecules.begin();
[fa9d1d]123 iter != molecules.end(); ++iter)
124 BondCount += (*iter)->getBondCount();
125 LOG(0, "STATUS: Recognized " << BondCount << " bonds.");
[c449d9]126
[b5b01e]127 return ActionState::ptr(UndoState);
[c449d9]128}
129
[b5b01e]130ActionState::ptr GraphCreateAdjacencyAction::performUndo(ActionState::ptr _state) {
[055ad7]131 GraphCreateAdjacencyState *state = assert_cast<GraphCreateAdjacencyState*>(_state.get());
132
133 BondGraph *BG = World::getInstance().getBondGraph();
134 ASSERT(BG != NULL, "GraphCreateAdjacencyAction: BondGraph is NULL.");
[c449d9]135
[055ad7]136 // remove all bonds in the set
137 World::AtomComposite Set = World::getInstance().getAllAtoms(AtomsBySelection());
138 BG->cleanAdjacencyList(Set);
139
140 // recreate from stored info
141 const size_t CurrentTime = WorldTime::getTime();
142 std::vector<BondInfo_t> &bonds = state->bonds;
143 for(std::vector<BondInfo_t>::const_iterator iter = bonds.begin();
144 iter != bonds.end(); ++iter) {
145 atom * const Walker = World::getInstance().getAtom(AtomById(iter->leftatom));
146 ASSERT( Walker != NULL,
147 "GraphCreateAdjacencyAction::performUndo() - "+toString(iter->leftatom)+" missing.");
148 atom * const OtherWalker = World::getInstance().getAtom(AtomById(iter->rightatom));
149 ASSERT( OtherWalker != NULL,
150 "GraphCreateAdjacencyAction::performUndo() - "+toString(iter->rightatom)+" missing.");
151 bond::ptr CurrentBond = Walker->addBond(CurrentTime, OtherWalker);
[1f693d]152 CurrentBond->setDegree(iter->degree);
[055ad7]153 }
[b5b01e]154 return ActionState::ptr(_state);
[c449d9]155}
156
[b5b01e]157ActionState::ptr GraphCreateAdjacencyAction::performRedo(ActionState::ptr _state){
[055ad7]158 BondGraph *BG = World::getInstance().getBondGraph();
159 ASSERT(BG != NULL, "GraphCreateAdjacencyAction: BondGraph is NULL.");
160
161 // now recreate adjacency
162 World::AtomComposite Set = World::getInstance().getAllAtoms(AtomsBySelection());
163 BG->CreateAdjacency(Set);
164
[b5b01e]165 return ActionState::ptr(_state);
[c449d9]166}
167
[d09093]168bool GraphCreateAdjacencyAction::canUndo() {
[055ad7]169 return true;
[c449d9]170}
171
[d09093]172bool GraphCreateAdjacencyAction::shouldUndo() {
[055ad7]173 return true;
[c449d9]174}
175/** =========== end of function ====================== */
Note: See TracBrowser for help on using the repository browser.