source: src/Actions/ActionRegistry.cpp@ 4464ef

Action_Thermostats Add_AtomRandomPerturbation Add_RotateAroundBondAction Add_SelectAtomByNameAction Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_ChronosMutex Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion GeometryObjects Gui_displays_atomic_force_velocity IndependentFragmentGrids_IntegrationTest JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo StoppableMakroAction Subpackage_levmar Subpackage_vmg ThirdParty_MPQC_rebuilt_buildsystem TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps Ubuntu_1604_changes stable
Last change on this file since 4464ef was 10aee4, checked in by Frederik Heber <heber@…>, 9 years ago

Cleaned up ActionQueue's interface: Actions are always cloned.

  • Action's dstor needs to be public as we have a clone() function.
  • ActionRegistrys' (and AQ's) getActionByName now returns const ref. We must not return a ptr as it may not get deleted elsewhere. We are handing out prototypes.
  • queueAction(action*,...) is private to prevent leakage, queueAction(string, ...) is the public interface.
  • some other small functions are now private, too.
  • MakroActions now need to clone() the prototype on prepare() and delete them on unprepare().
  • ActionSequence deletes contained actions.
  • FIX: ActionSequenceUnitTest cannot check on not-called property of removed actions anymore as these are gone now (they are deleted on removal and they are cloned on insertion).
  • DOCU: Updated documentation on Actions.
  • Property mode set to 100644
File size: 7.9 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 * Registry<Action>.cpp
26 *
27 * Created on: Jan 7, 2010
28 * Author: crueger
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 "CodePatterns/Assert.hpp"
39
40#include "Actions/ActionRegistry.hpp"
41#include "Actions/OptionRegistry.hpp"
42#include "Actions/OptionTrait.hpp"
43#include "CodePatterns/Registry_impl.hpp"
44
45#include "GlobalListOfActions.hpp"
46
47#include "AllActionHeaders.hpp"
48
49#include <boost/preprocessor/iteration/local.hpp>
50#include <boost/preprocessor/seq/elem.hpp>
51#include <boost/preprocessor/seq/size.hpp>
52
53using namespace MoleCuilder;
54
55// static entities
56bool ActionRegistry::completely_instatiated = false;
57
58/** Constructor for class ActionRegistry.
59 */
60ActionRegistry::ActionRegistry()
61{
62 //std::cout << "ActionRegistry::ActionRegistry() called, instance is " << this << "." << std::endl;
63 fillRegistry();
64 prepareAllMakroActions();
65 fillOptionRegistry();
66 completely_instatiated = true;
67}
68
69/** Destructor for class ActionRegistry.
70 */
71ActionRegistry::~ActionRegistry()
72{
73 // first unregister all their options
74 clearOptionRegistry();
75
76 // add all MakroActions: PLEASE adhere to the alphabetical ordering
77 {
78 MakroAction & presentAction =
79 const_cast<MakroAction &>(
80 dynamic_cast<const MakroAction &>(getActionByName("molecular-dynamics")));
81 presentAction.unprepare(*this);
82 }
83 {
84 MakroAction & presentAction =
85 const_cast<MakroAction &>(
86 dynamic_cast<const MakroAction &>(getActionByName("optimize-structure")));
87 presentAction.unprepare(*this);
88 }
89 {
90 MakroAction & presentAction =
91 const_cast<MakroAction &>(
92 dynamic_cast<const MakroAction &>(getActionByName("subgraph-dissection")));
93 presentAction.unprepare(*this);
94 }
95 {
96 MakroAction & presentAction =
97 const_cast<MakroAction &>(
98 dynamic_cast<const MakroAction &>(getActionByName("translate-molecules")));
99 presentAction.unprepare(*this);
100 }
101
102 //std::cout << "ActionRegistry::~ActionRegistry() called, instance is " << this << "." << std::endl;
103 cleanup();
104}
105
106
107/** Instantiates each existing Action.
108 *
109 */
110void ActionRegistry::fillRegistry()
111{
112#define instance_print(z,n,list) \
113 { \
114 Action * presentAction = new \
115 BOOST_PP_CAT(BOOST_PP_SEQ_ELEM(n, list), Action) \
116 (); \
117 registerInstance(presentAction); \
118 }
119#define BOOST_PP_LOCAL_MACRO(n) instance_print(~, n, GLOBALLISTOFACTIONS)
120#define BOOST_PP_LOCAL_LIMITS (0, BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(GLOBALLISTOFACTIONS)))
121#include BOOST_PP_LOCAL_ITERATE()
122#undef instance_print
123}
124
125void ActionRegistry::prepareAllMakroActions()
126{
127 // now prepare each macro action that require presence of all primitive ones
128 {
129 MakroAction & presentAction =
130 const_cast<MakroAction &>(
131 dynamic_cast<const MakroAction &>(getActionByName("subgraph-dissection")));
132 presentAction.prepare(*this);
133 }
134 {
135 MakroAction & presentAction =
136 const_cast<MakroAction &>(
137 dynamic_cast<const MakroAction &>(getActionByName("molecular-dynamics")));
138 presentAction.prepare(*this);
139 }
140 {
141 MakroAction & presentAction =
142 const_cast<MakroAction &>(
143 dynamic_cast<const MakroAction &>(getActionByName("optimize-structure")));
144 presentAction.prepare(*this);
145 }
146 {
147 MakroAction & presentAction =
148 const_cast<MakroAction &>(
149 dynamic_cast<const MakroAction &>(getActionByName("translate-molecules")));
150 presentAction.prepare(*this);
151 }
152}
153
154/** Registers every existing Action's option with the OptionRegistry.
155 *
156 */
157void ActionRegistry::fillOptionRegistry() const
158{
159 for (const_iterator iter = getBeginIter(); iter != getEndIter(); ++iter) {
160 // register with OptionRegistry
161 Action &currenAction = *(iter->second);
162 for (ActionTrait::options_const_iterator optioniter = currenAction.Traits.getBeginIter();
163 optioniter != currenAction.Traits.getEndIter();
164 ++optioniter) {
165 // options may have been re-used by other Actions, so check beforehand whether adding is needed
166 if (!OptionRegistry::getInstance().isOptionPresentByName((optioniter->first))) {
167 OptionRegistry::getInstance().registerInstance(optioniter->second);
168 } else { // if present, ASSERT that types coincide
169#ifndef NDEBUG
170 OptionTrait const * const PresentOption = OptionRegistry::getInstance().getOptionByName(optioniter->first);
171#endif
172 ASSERT(PresentOption->getType() == optioniter->second->getType(),
173 ("Action::Action() - option to add "+
174 std::string(optioniter->first)+
175 " of Action "+
176 std::string(currenAction.getName())+
177 " is already present with different type: "
178 +toString(PresentOption->getType())+" != "+toString(optioniter->second->getType())
179 )
180 );
181 ASSERT(PresentOption->getDefaultValue() == optioniter->second->getDefaultValue(),
182 ("Action::Action() - option to add "+
183 std::string(optioniter->first)+
184 " of Action "+
185 std::string(currenAction.getName())+
186 " is already present with different default value: "
187 +PresentOption->getDefaultValue()+" != "+optioniter->second->getDefaultValue()
188 )
189 );
190 ASSERT(PresentOption->getShortForm() == optioniter->second->getShortForm(),
191 ("Action::Action() - option to add "+
192 std::string(optioniter->first)+
193 " of Action "+
194 std::string(currenAction.getName())+
195 " is already present with different short form: "
196 +PresentOption->getShortForm()+" != "+optioniter->second->getShortForm()
197 )
198 );
199 }
200 }
201 }
202}
203
204/** Unregisters every existing Action's option from OptionRegistry.
205 *
206 */
207void ActionRegistry::clearOptionRegistry() const
208{
209 for (const_iterator iter = getBeginIter(); iter != getEndIter(); ++iter) {
210 Action &currenAction = *(iter->second);
211 for (ActionTrait::options_const_iterator optioniter = currenAction.Traits.getBeginIter();
212 optioniter != currenAction.Traits.getEndIter();
213 ++optioniter) {
214 // unregister option if still registered
215 if(OptionRegistry::getInstance().isOptionPresentByName((optioniter->first)))
216 if (OptionRegistry::getInstance().getOptionByName((optioniter->first)) == optioniter->second) {
217 OptionRegistry::getInstance().unregisterInstance(optioniter->second);
218 }
219 }
220 }
221}
222
223/** Just passes on call to Registry<Action>::getByName().
224 * \param name name of Action
225 * \return const ref to Action
226 */
227const Action& ActionRegistry::getActionByName(const std::string &name)
228{
229 Action * const action = getByName(name);
230 ASSERT( action != NULL,
231 "ActionRegistry::getActionByName() - action "+name+" not present!");
232 return *action;
233}
234
235/** Just passes on call to Registry<Action>::isPresentByName().
236 * \param name name of Action
237 * \return true - Action instance present, false - not
238 */
239bool ActionRegistry::isActionPresentByName(const std::string &name) const
240{
241 return isPresentByName(name);
242}
243
244
245CONSTRUCT_REGISTRY(Action)
Note: See TracBrowser for help on using the repository browser.