| [bcf653] | 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/>.
 | 
|---|
| [bcf653] | 22 |  */
 | 
|---|
 | 23 | 
 | 
|---|
| [cc04b7] | 24 | /*
 | 
|---|
| [b2d8d0] | 25 |  * Registry<Action>.cpp
 | 
|---|
| [cc04b7] | 26 |  *
 | 
|---|
 | 27 |  *  Created on: Jan 7, 2010
 | 
|---|
 | 28 |  *      Author: crueger
 | 
|---|
 | 29 |  */
 | 
|---|
 | 30 | 
 | 
|---|
| [bf3817] | 31 | // include config.h
 | 
|---|
 | 32 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 33 | #include <config.h>
 | 
|---|
 | 34 | #endif
 | 
|---|
 | 35 | 
 | 
|---|
| [ad011c] | 36 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| [112b09] | 37 | 
 | 
|---|
| [22b786] | 38 | #include "CodePatterns/Assert.hpp"
 | 
|---|
 | 39 | 
 | 
|---|
| [cc04b7] | 40 | #include "Actions/ActionRegistry.hpp"
 | 
|---|
| [36053c] | 41 | #include "Actions/OptionRegistry.hpp"
 | 
|---|
 | 42 | #include "Actions/OptionTrait.hpp"
 | 
|---|
| [ad011c] | 43 | #include "CodePatterns/Registry_impl.hpp"
 | 
|---|
| [cc04b7] | 44 | 
 | 
|---|
| [83e90c] | 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 | 
 | 
|---|
| [ce7fdc] | 53 | using namespace MoleCuilder;
 | 
|---|
 | 54 | 
 | 
|---|
| [2aad5a] | 55 | // static entities
 | 
|---|
 | 56 | bool ActionRegistry::completely_instatiated = false;
 | 
|---|
 | 57 | 
 | 
|---|
| [d0fbec] | 58 | /** Constructor for class ActionRegistry.
 | 
|---|
 | 59 |  */
 | 
|---|
| [cc04b7] | 60 | ActionRegistry::ActionRegistry()
 | 
|---|
| [83e90c] | 61 | {
 | 
|---|
 | 62 |   //std::cout << "ActionRegistry::ActionRegistry() called, instance is " << this << "." << std::endl;
 | 
|---|
 | 63 |   fillRegistry();
 | 
|---|
| [10aee4] | 64 |   prepareAllMakroActions();
 | 
|---|
| [36053c] | 65 |   fillOptionRegistry();
 | 
|---|
| [2aad5a] | 66 |   completely_instatiated = true;
 | 
|---|
| [83e90c] | 67 | }
 | 
|---|
| [cc04b7] | 68 | 
 | 
|---|
| [d0fbec] | 69 | /** Destructor for class ActionRegistry.
 | 
|---|
 | 70 |  */
 | 
|---|
| [cc04b7] | 71 | ActionRegistry::~ActionRegistry()
 | 
|---|
| [95c0ab] | 72 | {
 | 
|---|
| [36053c] | 73 |   // first unregister all their options
 | 
|---|
 | 74 |   clearOptionRegistry();
 | 
|---|
 | 75 | 
 | 
|---|
| [e5f61ba] | 76 |   // add all MakroActions: PLEASE adhere to the alphabetical ordering
 | 
|---|
| [39986b] | 77 |   {
 | 
|---|
| [10aee4] | 78 |     MakroAction & presentAction =
 | 
|---|
 | 79 |         const_cast<MakroAction &>(
 | 
|---|
 | 80 |         dynamic_cast<const MakroAction &>(getActionByName("molecular-dynamics")));
 | 
|---|
 | 81 |     presentAction.unprepare(*this);
 | 
|---|
| [39986b] | 82 |   }
 | 
|---|
 | 83 |   {
 | 
|---|
| [10aee4] | 84 |     MakroAction & presentAction =
 | 
|---|
 | 85 |         const_cast<MakroAction &>(
 | 
|---|
 | 86 |         dynamic_cast<const MakroAction &>(getActionByName("optimize-structure")));
 | 
|---|
 | 87 |     presentAction.unprepare(*this);
 | 
|---|
| [e5f61ba] | 88 |   }
 | 
|---|
 | 89 |   {
 | 
|---|
| [10aee4] | 90 |     MakroAction & presentAction =
 | 
|---|
 | 91 |         const_cast<MakroAction &>(
 | 
|---|
 | 92 |         dynamic_cast<const MakroAction &>(getActionByName("subgraph-dissection")));
 | 
|---|
 | 93 |     presentAction.unprepare(*this);
 | 
|---|
| [39986b] | 94 |   }
 | 
|---|
| [066442] | 95 |   {
 | 
|---|
| [10aee4] | 96 |     MakroAction & presentAction =
 | 
|---|
 | 97 |         const_cast<MakroAction &>(
 | 
|---|
 | 98 |         dynamic_cast<const MakroAction &>(getActionByName("translate-molecules")));
 | 
|---|
 | 99 |     presentAction.unprepare(*this);
 | 
|---|
| [066442] | 100 |   }
 | 
|---|
| [22b786] | 101 | 
 | 
|---|
| [83e90c] | 102 |   //std::cout << "ActionRegistry::~ActionRegistry() called, instance is " << this << "." << std::endl;
 | 
|---|
| [95c0ab] | 103 |   cleanup();
 | 
|---|
 | 104 | }
 | 
|---|
| [cc04b7] | 105 | 
 | 
|---|
| [83e90c] | 106 | 
 | 
|---|
 | 107 | /** Instantiates each existing Action.
 | 
|---|
 | 108 |  *
 | 
|---|
 | 109 |  */
 | 
|---|
 | 110 | void 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  
 | 
|---|
| [10aee4] | 123 | }
 | 
|---|
| [22b786] | 124 | 
 | 
|---|
| [10aee4] | 125 | void ActionRegistry::prepareAllMakroActions()
 | 
|---|
 | 126 | {
 | 
|---|
| [22b786] | 127 |   // now prepare each macro action that require presence of all primitive ones
 | 
|---|
| [39986b] | 128 |   {
 | 
|---|
| [10aee4] | 129 |     MakroAction & presentAction =
 | 
|---|
 | 130 |         const_cast<MakroAction &>(
 | 
|---|
 | 131 |         dynamic_cast<const MakroAction &>(getActionByName("subgraph-dissection")));
 | 
|---|
 | 132 |     presentAction.prepare(*this);
 | 
|---|
| [39986b] | 133 |   }
 | 
|---|
 | 134 |   {
 | 
|---|
| [10aee4] | 135 |     MakroAction & presentAction =
 | 
|---|
 | 136 |         const_cast<MakroAction &>(
 | 
|---|
 | 137 |         dynamic_cast<const MakroAction &>(getActionByName("molecular-dynamics")));
 | 
|---|
 | 138 |     presentAction.prepare(*this);
 | 
|---|
| [39986b] | 139 |   }
 | 
|---|
| [e5f61ba] | 140 |   {
 | 
|---|
| [10aee4] | 141 |     MakroAction & presentAction =
 | 
|---|
 | 142 |         const_cast<MakroAction &>(
 | 
|---|
 | 143 |         dynamic_cast<const MakroAction &>(getActionByName("optimize-structure")));
 | 
|---|
 | 144 |     presentAction.prepare(*this);
 | 
|---|
| [e5f61ba] | 145 |   }
 | 
|---|
| [066442] | 146 |   {
 | 
|---|
| [10aee4] | 147 |     MakroAction & presentAction =
 | 
|---|
 | 148 |         const_cast<MakroAction &>(
 | 
|---|
 | 149 |         dynamic_cast<const MakroAction &>(getActionByName("translate-molecules")));
 | 
|---|
 | 150 |     presentAction.prepare(*this);
 | 
|---|
| [066442] | 151 |   }
 | 
|---|
| [83e90c] | 152 | }
 | 
|---|
 | 153 | 
 | 
|---|
| [36053c] | 154 | /** Registers every existing Action's option with the OptionRegistry.
 | 
|---|
 | 155 |  *
 | 
|---|
 | 156 |  */
 | 
|---|
 | 157 | void ActionRegistry::fillOptionRegistry() const
 | 
|---|
 | 158 | {
 | 
|---|
 | 159 |   for (const_iterator iter = getBeginIter(); iter != getEndIter(); ++iter) {
 | 
|---|
 | 160 |     // register with OptionRegistry
 | 
|---|
 | 161 |     Action ¤Action = *(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 |  */
 | 
|---|
 | 207 | void ActionRegistry::clearOptionRegistry() const
 | 
|---|
 | 208 | {
 | 
|---|
 | 209 |   for (const_iterator iter = getBeginIter(); iter != getEndIter(); ++iter) {
 | 
|---|
 | 210 |     Action ¤Action = *(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 | 
 | 
|---|
| [b2d8d0] | 223 | /** Just passes on call to Registry<Action>::getByName().
 | 
|---|
 | 224 |  * \param name name of Action
 | 
|---|
| [10aee4] | 225 |  * \return const ref to Action
 | 
|---|
| [d0fbec] | 226 |  */
 | 
|---|
| [10aee4] | 227 | const Action& ActionRegistry::getActionByName(const std::string &name)
 | 
|---|
| [d0fbec] | 228 | {
 | 
|---|
| [10aee4] | 229 |   Action * const action = getByName(name);
 | 
|---|
 | 230 |   ASSERT( action != NULL,
 | 
|---|
 | 231 |       "ActionRegistry::getActionByName() - action "+name+" not present!");
 | 
|---|
 | 232 |   return *action;
 | 
|---|
| [d0fbec] | 233 | }
 | 
|---|
 | 234 | 
 | 
|---|
| [b2d8d0] | 235 | /** Just passes on call to Registry<Action>::isPresentByName().
 | 
|---|
 | 236 |  * \param name name of Action
 | 
|---|
 | 237 |  * \return true - Action instance present, false - not
 | 
|---|
| [d0fbec] | 238 |  */
 | 
|---|
| [a6ceab] | 239 | bool ActionRegistry::isActionPresentByName(const std::string &name) const
 | 
|---|
| [d0fbec] | 240 | {
 | 
|---|
| [b2d8d0] | 241 |   return isPresentByName(name);
 | 
|---|
| [d0fbec] | 242 | }
 | 
|---|
 | 243 | 
 | 
|---|
| [b59da6] | 244 | 
 | 
|---|
| [b2d8d0] | 245 | CONSTRUCT_REGISTRY(Action)
 | 
|---|