| [628577] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
|  | 4 | * Copyright (C)  2013 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 | * ActionQueue.cpp | 
|---|
|  | 25 | * | 
|---|
|  | 26 | *  Created on: Aug 16, 2013 | 
|---|
|  | 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 |  | 
|---|
| [1d3563] | 37 | #include "Actions/ActionQueue.hpp" | 
|---|
| [628577] | 38 |  | 
|---|
| [690741] | 39 | #include "CodePatterns/Assert.hpp" | 
|---|
|  | 40 | #include "CodePatterns/IteratorAdaptors.hpp" | 
|---|
| [46b181] | 41 | #include "CodePatterns/Log.hpp" | 
|---|
| [628577] | 42 | #include "CodePatterns/Singleton_impl.hpp" | 
|---|
|  | 43 |  | 
|---|
| [415ddd] | 44 | #include <boost/date_time/posix_time/posix_time.hpp> | 
|---|
|  | 45 | #include <boost/version.hpp> | 
|---|
| [46b181] | 46 | #include <string> | 
|---|
|  | 47 | #include <sstream> | 
|---|
| [690741] | 48 | #include <vector> | 
|---|
|  | 49 |  | 
|---|
| [0d4168] | 50 | #include "Actions/ActionExceptions.hpp" | 
|---|
| [6367dd] | 51 | #include "Actions/ActionHistory.hpp" | 
|---|
| [ed3944] | 52 | #include "Actions/ActionRegistry.hpp" | 
|---|
| [0d4168] | 53 | #include "World.hpp" | 
|---|
| [ed3944] | 54 |  | 
|---|
| [628577] | 55 | using namespace MoleCuilder; | 
|---|
|  | 56 |  | 
|---|
| [ed3944] | 57 | ActionQueue::ActionQueue() : | 
|---|
| [6367dd] | 58 | AR(new ActionRegistry()), | 
|---|
| [af5384] | 59 | history(new ActionHistory), | 
|---|
| [a61dbb] | 60 | CurrentAction(0), | 
|---|
| [74459a] | 61 | #ifndef HAVE_ACTION_THREAD | 
|---|
| [a61dbb] | 62 | lastActionOk(true) | 
|---|
| [74459a] | 63 | #else | 
|---|
| [a61dbb] | 64 | lastActionOk(true), | 
|---|
| [415ddd] | 65 | run_thread(boost::bind(&ActionQueue::run, this)), | 
|---|
|  | 66 | run_thread_isIdle(true) | 
|---|
| [74459a] | 67 | #endif | 
|---|
| [628577] | 68 | {} | 
|---|
|  | 69 |  | 
|---|
|  | 70 | ActionQueue::~ActionQueue() | 
|---|
| [ed3944] | 71 | { | 
|---|
| [74459a] | 72 | #ifdef HAVE_ACTION_THREAD | 
|---|
| [415ddd] | 73 | stop(); | 
|---|
| [74459a] | 74 | #endif | 
|---|
| [415ddd] | 75 |  | 
|---|
| [7fc447] | 76 | // free all actions contained in actionqueue | 
|---|
|  | 77 | for (ActionQueue_t::iterator iter = actionqueue.begin(); !actionqueue.empty(); iter = actionqueue.begin()) { | 
|---|
| [af5384] | 78 | delete *iter; | 
|---|
| [7fc447] | 79 | actionqueue.erase(iter); | 
|---|
| [af5384] | 80 | } | 
|---|
|  | 81 |  | 
|---|
| [6367dd] | 82 | delete history; | 
|---|
| [ed3944] | 83 | delete AR; | 
|---|
|  | 84 | } | 
|---|
| [628577] | 85 |  | 
|---|
| [f54cda] | 86 | void ActionQueue::queueAction(const std::string &name, enum Action::QueryOptions state) | 
|---|
| [05c989] | 87 | { | 
|---|
| [af5384] | 88 | queueAction(AR->getActionByName(name), state); | 
|---|
| [f54cda] | 89 | } | 
|---|
|  | 90 |  | 
|---|
|  | 91 | void ActionQueue::queueAction(Action *_action, enum Action::QueryOptions state) | 
|---|
|  | 92 | { | 
|---|
| [af5384] | 93 | Action *newaction = _action->clone(state); | 
|---|
|  | 94 | newaction->prepare(state); | 
|---|
| [74459a] | 95 | #ifdef HAVE_ACTION_THREAD | 
|---|
| [415ddd] | 96 | mtx_queue.lock(); | 
|---|
| [74459a] | 97 | #endif | 
|---|
| [7fc447] | 98 | actionqueue.push_back( newaction ); | 
|---|
| [74459a] | 99 | #ifndef HAVE_ACTION_THREAD | 
|---|
|  | 100 | try { | 
|---|
|  | 101 | newaction->call(); | 
|---|
| [a61dbb] | 102 | lastActionOk = true; | 
|---|
| [74459a] | 103 | } catch(ActionFailureException &e) { | 
|---|
|  | 104 | std::cerr << "Action " << *boost::get_error_info<ActionNameString>(e) << " has failed." << std::endl; | 
|---|
|  | 105 | World::getInstance().setExitFlag(5); | 
|---|
| [a61dbb] | 106 | actionqueue.clear(); | 
|---|
|  | 107 | tempqueue.clear(); | 
|---|
|  | 108 | lastActionOk = false; | 
|---|
|  | 109 | std::cerr << "ActionQueue cleared." << std::endl; | 
|---|
| [74459a] | 110 | } | 
|---|
|  | 111 | #else | 
|---|
| [415ddd] | 112 | { | 
|---|
|  | 113 | boost::lock_guard<boost::mutex> lock(mtx_idle); | 
|---|
|  | 114 | run_thread_isIdle = (CurrentAction == actionqueue.size()); | 
|---|
| [0d4168] | 115 | } | 
|---|
| [415ddd] | 116 | mtx_queue.unlock(); | 
|---|
| [74459a] | 117 | #endif | 
|---|
| [05c989] | 118 | } | 
|---|
|  | 119 |  | 
|---|
| [975b83] | 120 | void ActionQueue::insertAction(Action *_action, enum Action::QueryOptions state) | 
|---|
|  | 121 | { | 
|---|
| [74459a] | 122 | #ifndef HAVE_ACTION_THREAD | 
|---|
|  | 123 | queueAction(_action, state); | 
|---|
|  | 124 | #else | 
|---|
| [415ddd] | 125 | Action *newaction = _action->clone(state); | 
|---|
|  | 126 | newaction->prepare(state); | 
|---|
|  | 127 | mtx_queue.lock(); | 
|---|
|  | 128 | tempqueue.push_back( newaction ); | 
|---|
|  | 129 | { | 
|---|
|  | 130 | boost::lock_guard<boost::mutex> lock(mtx_idle); | 
|---|
|  | 131 | run_thread_isIdle = !((CurrentAction != actionqueue.size()) || !tempqueue.empty()); | 
|---|
|  | 132 | } | 
|---|
|  | 133 | mtx_queue.unlock(); | 
|---|
| [74459a] | 134 | #endif | 
|---|
| [415ddd] | 135 | } | 
|---|
|  | 136 |  | 
|---|
| [74459a] | 137 | #ifdef HAVE_ACTION_THREAD | 
|---|
| [415ddd] | 138 | void ActionQueue::run() | 
|---|
|  | 139 | { | 
|---|
|  | 140 | bool Interrupted = false; | 
|---|
|  | 141 | do { | 
|---|
|  | 142 | // sleep for some time and wait for queue to fill up again | 
|---|
|  | 143 | try { | 
|---|
|  | 144 | #if BOOST_VERSION < 105000 | 
|---|
|  | 145 | run_thread.sleep(boost::get_system_time() + boost::posix_time::milliseconds(100)); | 
|---|
|  | 146 | #else | 
|---|
|  | 147 | run_thread.sleep_for(100); | 
|---|
|  | 148 | #endif | 
|---|
|  | 149 | } catch(boost::thread_interrupted &e) { | 
|---|
|  | 150 | LOG(2, "INFO: ActionQueue has received stop signal."); | 
|---|
|  | 151 | Interrupted = true; | 
|---|
|  | 152 | } | 
|---|
|  | 153 | //    LOG(1, "DEBUG: Start of ActionQueue's run() loop."); | 
|---|
|  | 154 | // call all currently present Actions | 
|---|
|  | 155 | mtx_queue.lock(); | 
|---|
|  | 156 | insertTempQueue(); | 
|---|
|  | 157 | bool status = (CurrentAction != actionqueue.size()); | 
|---|
|  | 158 | mtx_queue.unlock(); | 
|---|
|  | 159 | while (status) { | 
|---|
|  | 160 | //      boost::this_thread::disable_interruption di; | 
|---|
|  | 161 | LOG(0, "Calling Action " << actionqueue[CurrentAction]->getName() << " ... "); | 
|---|
|  | 162 | try { | 
|---|
|  | 163 | actionqueue[CurrentAction]->call(); | 
|---|
| [0b6b77] | 164 | pushStatus("SUCCESS: Action "+actionqueue[CurrentAction]->getName()+" successful."); | 
|---|
| [a61dbb] | 165 | lastActionOk = true; | 
|---|
| [415ddd] | 166 | } catch(ActionFailureException &e) { | 
|---|
| [0b6b77] | 167 | pushStatus("FAIL: Action "+*boost::get_error_info<ActionNameString>(e)+" has failed."); | 
|---|
| [415ddd] | 168 | World::getInstance().setExitFlag(5); | 
|---|
| [a61dbb] | 169 | actionqueue.clear(); | 
|---|
|  | 170 | tempqueue.clear(); | 
|---|
|  | 171 | lastActionOk = false; | 
|---|
|  | 172 | std::cerr << "ActionQueue cleared." << std::endl; | 
|---|
|  | 173 | CurrentAction = (size_t)-1; | 
|---|
| [415ddd] | 174 | } | 
|---|
|  | 175 | // access actionqueue, hence using mutex | 
|---|
|  | 176 | mtx_queue.lock(); | 
|---|
|  | 177 | // step on to next action and check for end | 
|---|
|  | 178 | CurrentAction++; | 
|---|
|  | 179 | // insert new actions (before [CurrentAction]) if they have been spawned | 
|---|
|  | 180 | // we must have an extra vector for this, as we cannot change actionqueue | 
|---|
|  | 181 | // while an action instance is "in-use" | 
|---|
|  | 182 | insertTempQueue(); | 
|---|
|  | 183 | status = (CurrentAction != actionqueue.size()); | 
|---|
|  | 184 | mtx_queue.unlock(); | 
|---|
|  | 185 | } | 
|---|
|  | 186 | { | 
|---|
|  | 187 | boost::lock_guard<boost::mutex> lock(mtx_idle); | 
|---|
|  | 188 | run_thread_isIdle = !((CurrentAction != actionqueue.size()) || !tempqueue.empty()); | 
|---|
|  | 189 | } | 
|---|
|  | 190 | cond_idle.notify_one(); | 
|---|
|  | 191 | //    LOG(1, "DEBUG: End of ActionQueue's run() loop."); | 
|---|
|  | 192 | } while (!Interrupted); | 
|---|
|  | 193 | } | 
|---|
| [74459a] | 194 | #endif | 
|---|
| [415ddd] | 195 |  | 
|---|
|  | 196 | void ActionQueue::insertTempQueue() | 
|---|
|  | 197 | { | 
|---|
|  | 198 | if (!tempqueue.empty()) { | 
|---|
|  | 199 | ActionQueue_t::iterator InsertionIter = actionqueue.begin(); | 
|---|
|  | 200 | std::advance(InsertionIter, CurrentAction); | 
|---|
|  | 201 | actionqueue.insert( InsertionIter, tempqueue.begin(), tempqueue.end() ); | 
|---|
|  | 202 | tempqueue.clear(); | 
|---|
|  | 203 | } | 
|---|
|  | 204 | } | 
|---|
|  | 205 |  | 
|---|
| [74459a] | 206 | #ifdef HAVE_ACTION_THREAD | 
|---|
| [415ddd] | 207 | void ActionQueue::wait() | 
|---|
|  | 208 | { | 
|---|
|  | 209 | boost::unique_lock<boost::mutex> lock(mtx_idle); | 
|---|
|  | 210 | while(!run_thread_isIdle) | 
|---|
|  | 211 | { | 
|---|
|  | 212 | cond_idle.wait(lock); | 
|---|
|  | 213 | } | 
|---|
|  | 214 | } | 
|---|
| [74459a] | 215 | #endif | 
|---|
| [415ddd] | 216 |  | 
|---|
| [74459a] | 217 | #ifdef HAVE_ACTION_THREAD | 
|---|
| [415ddd] | 218 | void ActionQueue::stop() | 
|---|
|  | 219 | { | 
|---|
|  | 220 | // notify actionqueue thread that we wish to terminate | 
|---|
|  | 221 | run_thread.interrupt(); | 
|---|
|  | 222 | // wait till it ends | 
|---|
|  | 223 | run_thread.join(); | 
|---|
| [975b83] | 224 | } | 
|---|
| [74459a] | 225 | #endif | 
|---|
| [975b83] | 226 |  | 
|---|
| [a6ceab] | 227 | Action* ActionQueue::getActionByName(const std::string &name) | 
|---|
| [1d3563] | 228 | { | 
|---|
| [ed3944] | 229 | return AR->getActionByName(name); | 
|---|
| [1d3563] | 230 | } | 
|---|
|  | 231 |  | 
|---|
| [a6ceab] | 232 | bool ActionQueue::isActionKnownByName(const std::string &name) const | 
|---|
| [1d3563] | 233 | { | 
|---|
| [ed3944] | 234 | return AR->isActionPresentByName(name); | 
|---|
| [1d3563] | 235 | } | 
|---|
|  | 236 |  | 
|---|
| [126867] | 237 | void ActionQueue::registerAction(Action *_action) | 
|---|
|  | 238 | { | 
|---|
|  | 239 | AR->registerInstance(_action); | 
|---|
|  | 240 | } | 
|---|
|  | 241 |  | 
|---|
| [46b181] | 242 | void ActionQueue::outputAsCLI(std::ostream &output) const | 
|---|
|  | 243 | { | 
|---|
| [7fc447] | 244 | for (ActionQueue_t::const_iterator iter = actionqueue.begin(); | 
|---|
|  | 245 | iter != actionqueue.end(); | 
|---|
| [46b181] | 246 | ++iter) { | 
|---|
| [bad589] | 247 | // skip store-session in printed list | 
|---|
| [12d946] | 248 | if ( ((*iter)->getName() != std::string("store-session")) | 
|---|
|  | 249 | && ((*iter)->getName() != std::string("load-session"))) { | 
|---|
| [7fc447] | 250 | if (iter != actionqueue.begin()) | 
|---|
| [bad589] | 251 | output << " "; | 
|---|
|  | 252 | (*iter)->outputAsCLI(output); | 
|---|
|  | 253 | } | 
|---|
| [46b181] | 254 | } | 
|---|
|  | 255 | output << std::endl; | 
|---|
|  | 256 | } | 
|---|
|  | 257 |  | 
|---|
| [477012] | 258 | void ActionQueue::outputAsPython(std::ostream &output) const | 
|---|
|  | 259 | { | 
|---|
|  | 260 | const std::string prefix("pyMoleCuilder"); | 
|---|
|  | 261 | output << "import " << prefix << std::endl; | 
|---|
| [9e4655] | 262 | output << "# ========================== Stored Session BEGIN ==========================" << std::endl; | 
|---|
| [7fc447] | 263 | for (ActionQueue_t::const_iterator iter = actionqueue.begin(); | 
|---|
|  | 264 | iter != actionqueue.end(); | 
|---|
| [477012] | 265 | ++iter) { | 
|---|
|  | 266 | // skip store-session in printed list | 
|---|
| [12d946] | 267 | if ( ((*iter)->getName() != std::string("store-session")) | 
|---|
|  | 268 | && ((*iter)->getName() != std::string("load-session"))) | 
|---|
| [477012] | 269 | (*iter)->outputAsPython(output, prefix); | 
|---|
|  | 270 | } | 
|---|
| [9e4655] | 271 | output << "# =========================== Stored Session END ===========================" << std::endl; | 
|---|
| [477012] | 272 | } | 
|---|
|  | 273 |  | 
|---|
| [a6ceab] | 274 | const ActionTrait& ActionQueue::getActionsTrait(const std::string &name) const | 
|---|
| [690741] | 275 | { | 
|---|
|  | 276 | // this const_cast is just required as long as we have a non-const getActionByName | 
|---|
|  | 277 | const Action * const action = const_cast<ActionQueue *>(this)->getActionByName(name); | 
|---|
|  | 278 | return action->Traits; | 
|---|
|  | 279 | } | 
|---|
|  | 280 |  | 
|---|
| [6367dd] | 281 | void ActionQueue::addElement(Action* _Action,ActionState::ptr _state) | 
|---|
|  | 282 | { | 
|---|
|  | 283 | history->addElement(_Action, _state); | 
|---|
|  | 284 | } | 
|---|
|  | 285 |  | 
|---|
|  | 286 | void ActionQueue::clear() | 
|---|
|  | 287 | { | 
|---|
|  | 288 | history->clear(); | 
|---|
|  | 289 | } | 
|---|
|  | 290 |  | 
|---|
|  | 291 |  | 
|---|
| [690741] | 292 | const ActionQueue::ActionTokens_t ActionQueue::getListOfActions() const | 
|---|
|  | 293 | { | 
|---|
|  | 294 | ActionTokens_t returnlist; | 
|---|
|  | 295 |  | 
|---|
|  | 296 | returnlist.insert( | 
|---|
|  | 297 | returnlist.end(), | 
|---|
| [ed3944] | 298 | MapKeyConstIterator<ActionRegistry::const_iterator>(AR->getBeginIter()), | 
|---|
|  | 299 | MapKeyConstIterator<ActionRegistry::const_iterator>(AR->getEndIter())); | 
|---|
| [690741] | 300 |  | 
|---|
|  | 301 | return returnlist; | 
|---|
|  | 302 | } | 
|---|
|  | 303 |  | 
|---|
| [6367dd] | 304 | void ActionQueue::undoLast() | 
|---|
|  | 305 | { | 
|---|
|  | 306 | history->undoLast(); | 
|---|
|  | 307 | } | 
|---|
|  | 308 |  | 
|---|
|  | 309 | void ActionQueue::redoLast() | 
|---|
|  | 310 | { | 
|---|
|  | 311 | history->redoLast(); | 
|---|
|  | 312 | } | 
|---|
|  | 313 |  | 
|---|
|  | 314 |  | 
|---|
| [628577] | 315 | CONSTRUCT_SINGLETON(ActionQueue) | 
|---|