1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2016 Frederik Heber. All rights reserved.
|
---|
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/>.
|
---|
21 | */
|
---|
22 |
|
---|
23 | /*
|
---|
24 | * BondSetDegreeAction.cpp
|
---|
25 | *
|
---|
26 | * Created on: Oct 01, 2016
|
---|
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 "Atom/atom.hpp"
|
---|
38 | #include "Bond/bond.hpp"
|
---|
39 | #include "CodePatterns/Assert.hpp"
|
---|
40 | #include "CodePatterns/Log.hpp"
|
---|
41 | #include "CodePatterns/Verbose.hpp"
|
---|
42 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
43 | #include "World.hpp"
|
---|
44 | #include "WorldTime.hpp"
|
---|
45 |
|
---|
46 | #include <iostream>
|
---|
47 | #include <string>
|
---|
48 |
|
---|
49 | #include "Actions/BondAction/BondSetDegreeAction.hpp"
|
---|
50 |
|
---|
51 | using namespace MoleCuilder;
|
---|
52 |
|
---|
53 | // and construct the stuff
|
---|
54 | #include "BondSetDegreeAction.def"
|
---|
55 | #include "Action_impl_pre.hpp"
|
---|
56 | /** =========== define the function ====================== */
|
---|
57 | ActionState::ptr BondSetDegreeAction::performCall() {
|
---|
58 | // check preconditions
|
---|
59 | World& world = World::getInstance();
|
---|
60 | if (world.countSelectedAtoms() <= 1) {
|
---|
61 | ELOG(1, "There must be at least two atoms selected for BondSetDegreeAction.");
|
---|
62 | return Action::failure;
|
---|
63 | }
|
---|
64 | for (World::AtomSelectionConstIterator firstiter = world.beginAtomSelection();
|
---|
65 | firstiter != world.endAtomSelection(); ++firstiter) {
|
---|
66 | bool status = false;
|
---|
67 | for (World::AtomSelectionConstIterator seconditer = world.beginAtomSelection();
|
---|
68 | seconditer != world.endAtomSelection(); ++seconditer) {
|
---|
69 | if (firstiter == seconditer)
|
---|
70 | continue;
|
---|
71 | if ((firstiter->second)->IsBondedTo(WorldTime::getTime(), seconditer->second)) {
|
---|
72 | status = true;
|
---|
73 | }
|
---|
74 | }
|
---|
75 | if (!status) {
|
---|
76 | ELOG(1, "Selected atom #" << firstiter->second->getId() << " has no bonds.");
|
---|
77 | return Action::failure;
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | // store current degrees in vector
|
---|
82 | BondDegrees_t BondDegrees;
|
---|
83 | for (World::AtomSelectionConstIterator firstiter = world.beginAtomSelection();
|
---|
84 | firstiter != world.endAtomSelection(); ++firstiter) {
|
---|
85 | for (World::AtomSelectionConstIterator seconditer = firstiter;
|
---|
86 | seconditer != world.endAtomSelection(); ++seconditer) {
|
---|
87 | if (firstiter == seconditer)
|
---|
88 | continue;
|
---|
89 | if ((firstiter->second)->IsBondedTo(WorldTime::getTime(), seconditer->second)) {
|
---|
90 | bond::ptr bond = (firstiter->second)->getBond(seconditer->second);
|
---|
91 | BondInfo bondinfo(bond);
|
---|
92 | BondDegrees.push_back( bondinfo );
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 | if (BondDegrees.empty()) {
|
---|
97 | ELOG(1, "All bonds are already present.");
|
---|
98 | return Action::failure;
|
---|
99 | }
|
---|
100 |
|
---|
101 | // create undo state
|
---|
102 | BondSetDegreeState *UndoState = new BondSetDegreeState(BondDegrees, params);
|
---|
103 |
|
---|
104 | // execute action
|
---|
105 | for (BondDegrees_t::const_iterator iter = BondDegrees.begin();
|
---|
106 | iter != BondDegrees.end(); ++iter) {
|
---|
107 | atom *firstatom = world.getAtom(AtomById((*iter).leftid));
|
---|
108 | atom *secondatom = world.getAtom(AtomById((*iter).rightid));
|
---|
109 | ASSERT((firstatom != NULL) && (secondatom != NULL),
|
---|
110 | "BondSetDegreeAction::performCall() - at least one of the ids "
|
---|
111 | +toString((*iter).leftid)+" or "+toString((*iter).rightid)+" is not present.");
|
---|
112 | const bond::ptr bond = firstatom->getBond(secondatom);
|
---|
113 | bond->setDegree(params.degree.get());
|
---|
114 | }
|
---|
115 |
|
---|
116 | return ActionState::ptr(UndoState);
|
---|
117 | }
|
---|
118 |
|
---|
119 | ActionState::ptr BondSetDegreeAction::performUndo(ActionState::ptr _state) {
|
---|
120 | BondSetDegreeState *state = assert_cast<BondSetDegreeState*>(_state.get());
|
---|
121 |
|
---|
122 | // check whether bond already existed
|
---|
123 | World& world = World::getInstance();
|
---|
124 | for (BondDegrees_t::const_iterator iter = state->bonddegrees.begin();
|
---|
125 | iter != state->bonddegrees.end(); ++iter) {
|
---|
126 | atom *firstatom = world.getAtom(AtomById((*iter).leftid));
|
---|
127 | atom *secondatom = world.getAtom(AtomById((*iter).rightid));
|
---|
128 | ASSERT((firstatom != NULL) && (secondatom != NULL),
|
---|
129 | "BondSetDegreeAction::performUndo() - at least one of the ids "
|
---|
130 | +toString((*iter).leftid)+" or "+toString((*iter).rightid)+" is not present.");
|
---|
131 | if (firstatom->IsBondedTo(WorldTime::getTime(), secondatom)) {
|
---|
132 | const bond::ptr bond = firstatom->getBond(secondatom);
|
---|
133 | bond->setDegree((*iter).degree);
|
---|
134 | } else {
|
---|
135 | ELOG(2, "There is no bond in between "+toString((*iter).leftid)
|
---|
136 | +" and "+toString((*iter).rightid)+".");
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | return ActionState::ptr(_state);
|
---|
141 | }
|
---|
142 |
|
---|
143 | ActionState::ptr BondSetDegreeAction::performRedo(ActionState::ptr _state){
|
---|
144 | BondSetDegreeState *state = assert_cast<BondSetDegreeState*>(_state.get());
|
---|
145 |
|
---|
146 | // check whether bond already existed
|
---|
147 | World& world = World::getInstance();
|
---|
148 | for (BondDegrees_t::const_iterator iter = state->bonddegrees.begin();
|
---|
149 | iter != state->bonddegrees.end(); ++iter) {
|
---|
150 | atom * const firstatom = world.getAtom(AtomById((*iter).leftid));
|
---|
151 | atom * const secondatom = world.getAtom(AtomById((*iter).rightid));
|
---|
152 | ASSERT((firstatom != NULL) && (secondatom != NULL),
|
---|
153 | "BondSetDegreeAction::performRedo() - at least one of the ids "
|
---|
154 | +toString((*iter).leftid)+" or "+toString((*iter).rightid)+" is not present.");
|
---|
155 | if (firstatom->IsBondedTo(WorldTime::getTime(), secondatom)) {
|
---|
156 | const bond::ptr bond = firstatom->getBond(secondatom);
|
---|
157 | bond->setDegree(state->params.degree.get());
|
---|
158 | } else {
|
---|
159 | ELOG(2, "There is no bond in between "+toString((*iter).leftid)
|
---|
160 | +" and "+toString((*iter).rightid)+".");
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | return ActionState::ptr(_state);
|
---|
165 | }
|
---|
166 |
|
---|
167 | bool BondSetDegreeAction::canUndo() {
|
---|
168 | return true;
|
---|
169 | }
|
---|
170 |
|
---|
171 | bool BondSetDegreeAction::shouldUndo() {
|
---|
172 | return true;
|
---|
173 | }
|
---|
174 | /** =========== end of function ====================== */
|
---|