[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>
|
---|
[601ef8] | 46 | #include <iterator>
|
---|
[46b181] | 47 | #include <string>
|
---|
| 48 | #include <sstream>
|
---|
[690741] | 49 | #include <vector>
|
---|
| 50 |
|
---|
[0d4168] | 51 | #include "Actions/ActionExceptions.hpp"
|
---|
[6367dd] | 52 | #include "Actions/ActionHistory.hpp"
|
---|
[ed3944] | 53 | #include "Actions/ActionRegistry.hpp"
|
---|
[0d4168] | 54 | #include "World.hpp"
|
---|
[ed3944] | 55 |
|
---|
[628577] | 56 | using namespace MoleCuilder;
|
---|
| 57 |
|
---|
[29b52b] | 58 | const Action* ActionQueue::_lastchangedaction = NULL;
|
---|
| 59 |
|
---|
[ed3944] | 60 | ActionQueue::ActionQueue() :
|
---|
[29b52b] | 61 | Observable("ActionQueue"),
|
---|
[6367dd] | 62 | AR(new ActionRegistry()),
|
---|
[af5384] | 63 | history(new ActionHistory),
|
---|
[74459a] | 64 | #ifndef HAVE_ACTION_THREAD
|
---|
[a61dbb] | 65 | lastActionOk(true)
|
---|
[74459a] | 66 | #else
|
---|
[601ef8] | 67 | CurrentAction(0),
|
---|
[a61dbb] | 68 | lastActionOk(true),
|
---|
[415ddd] | 69 | run_thread(boost::bind(&ActionQueue::run, this)),
|
---|
| 70 | run_thread_isIdle(true)
|
---|
[74459a] | 71 | #endif
|
---|
[29b52b] | 72 | {
|
---|
| 73 | // channels of observable
|
---|
| 74 | Channels *OurChannel = new Channels;
|
---|
| 75 | NotificationChannels.insert( std::make_pair(static_cast<Observable *>(this), OurChannel) );
|
---|
| 76 | // add instance for each notification type
|
---|
| 77 | for (size_t type = 0; type < NotificationType_MAX; ++type)
|
---|
| 78 | OurChannel->addChannel(type);
|
---|
| 79 | }
|
---|
[628577] | 80 |
|
---|
| 81 | ActionQueue::~ActionQueue()
|
---|
[ed3944] | 82 | {
|
---|
[74459a] | 83 | #ifdef HAVE_ACTION_THREAD
|
---|
[415ddd] | 84 | stop();
|
---|
[601ef8] | 85 |
|
---|
| 86 | clearTempQueue();
|
---|
[74459a] | 87 | #endif
|
---|
[415ddd] | 88 |
|
---|
[7f1a1a] | 89 | clearQueue();
|
---|
[af5384] | 90 |
|
---|
[6367dd] | 91 | delete history;
|
---|
[ed3944] | 92 | delete AR;
|
---|
| 93 | }
|
---|
[628577] | 94 |
|
---|
[f54cda] | 95 | void ActionQueue::queueAction(const std::string &name, enum Action::QueryOptions state)
|
---|
[05c989] | 96 | {
|
---|
[7f1a1a] | 97 | const Action * const registryaction = AR->getActionByName(name);
|
---|
| 98 | queueAction(registryaction, state);
|
---|
[f54cda] | 99 | }
|
---|
| 100 |
|
---|
[7f1a1a] | 101 | void ActionQueue::queueAction(const Action * const _action, enum Action::QueryOptions state)
|
---|
[f54cda] | 102 | {
|
---|
[af5384] | 103 | Action *newaction = _action->clone(state);
|
---|
| 104 | newaction->prepare(state);
|
---|
[74459a] | 105 | #ifdef HAVE_ACTION_THREAD
|
---|
[415ddd] | 106 | mtx_queue.lock();
|
---|
[74459a] | 107 | #endif
|
---|
[7fc447] | 108 | actionqueue.push_back( newaction );
|
---|
[74459a] | 109 | #ifndef HAVE_ACTION_THREAD
|
---|
| 110 | try {
|
---|
| 111 | newaction->call();
|
---|
[a61dbb] | 112 | lastActionOk = true;
|
---|
[74459a] | 113 | } catch(ActionFailureException &e) {
|
---|
| 114 | std::cerr << "Action " << *boost::get_error_info<ActionNameString>(e) << " has failed." << std::endl;
|
---|
| 115 | World::getInstance().setExitFlag(5);
|
---|
[601ef8] | 116 | clearQueue(actionqueue.size()-1);
|
---|
[a61dbb] | 117 | lastActionOk = false;
|
---|
[601ef8] | 118 | std::cerr << "Remaining Actions cleared from queue." << std::endl;
|
---|
[11d433] | 119 | } catch (std::exception &e) {
|
---|
| 120 | pushStatus("FAIL: General exception caught, aborting.");
|
---|
| 121 | World::getInstance().setExitFlag(134);
|
---|
[601ef8] | 122 | clearQueue(actionqueue.size()-1);
|
---|
[11d433] | 123 | lastActionOk = false;
|
---|
[601ef8] | 124 | std::cerr << "Remaining Actions cleared from queue." << std::endl;
|
---|
[74459a] | 125 | }
|
---|
[cfb9c5] | 126 | if (lastActionOk) {
|
---|
| 127 | OBSERVE;
|
---|
| 128 | NOTIFY(ActionQueued);
|
---|
| 129 | _lastchangedaction = newaction;
|
---|
| 130 | }
|
---|
[74459a] | 131 | #else
|
---|
[601ef8] | 132 | setRunThreadIdle(CurrentAction == actionqueue.size());
|
---|
[415ddd] | 133 | mtx_queue.unlock();
|
---|
[74459a] | 134 | #endif
|
---|
[05c989] | 135 | }
|
---|
| 136 |
|
---|
[975b83] | 137 | void ActionQueue::insertAction(Action *_action, enum Action::QueryOptions state)
|
---|
| 138 | {
|
---|
[74459a] | 139 | #ifndef HAVE_ACTION_THREAD
|
---|
| 140 | queueAction(_action, state);
|
---|
| 141 | #else
|
---|
[415ddd] | 142 | Action *newaction = _action->clone(state);
|
---|
| 143 | newaction->prepare(state);
|
---|
| 144 | mtx_queue.lock();
|
---|
| 145 | tempqueue.push_back( newaction );
|
---|
[601ef8] | 146 | setRunThreadIdle( !((CurrentAction != actionqueue.size()) || !tempqueue.empty()) );
|
---|
[415ddd] | 147 | mtx_queue.unlock();
|
---|
[74459a] | 148 | #endif
|
---|
[415ddd] | 149 | }
|
---|
| 150 |
|
---|
[74459a] | 151 | #ifdef HAVE_ACTION_THREAD
|
---|
[415ddd] | 152 | void ActionQueue::run()
|
---|
| 153 | {
|
---|
| 154 | bool Interrupted = false;
|
---|
| 155 | do {
|
---|
| 156 | // sleep for some time and wait for queue to fill up again
|
---|
| 157 | try {
|
---|
| 158 | #if BOOST_VERSION < 105000
|
---|
| 159 | run_thread.sleep(boost::get_system_time() + boost::posix_time::milliseconds(100));
|
---|
| 160 | #else
|
---|
[d93b4b3] | 161 | boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
|
---|
[415ddd] | 162 | #endif
|
---|
| 163 | } catch(boost::thread_interrupted &e) {
|
---|
| 164 | LOG(2, "INFO: ActionQueue has received stop signal.");
|
---|
| 165 | Interrupted = true;
|
---|
| 166 | }
|
---|
| 167 | // LOG(1, "DEBUG: Start of ActionQueue's run() loop.");
|
---|
| 168 | // call all currently present Actions
|
---|
| 169 | mtx_queue.lock();
|
---|
| 170 | insertTempQueue();
|
---|
| 171 | bool status = (CurrentAction != actionqueue.size());
|
---|
| 172 | mtx_queue.unlock();
|
---|
| 173 | while (status) {
|
---|
| 174 | // boost::this_thread::disable_interruption di;
|
---|
| 175 | LOG(0, "Calling Action " << actionqueue[CurrentAction]->getName() << " ... ");
|
---|
| 176 | try {
|
---|
| 177 | actionqueue[CurrentAction]->call();
|
---|
[0b6b77] | 178 | pushStatus("SUCCESS: Action "+actionqueue[CurrentAction]->getName()+" successful.");
|
---|
[a61dbb] | 179 | lastActionOk = true;
|
---|
[415ddd] | 180 | } catch(ActionFailureException &e) {
|
---|
[0b6b77] | 181 | pushStatus("FAIL: Action "+*boost::get_error_info<ActionNameString>(e)+" has failed.");
|
---|
[415ddd] | 182 | World::getInstance().setExitFlag(5);
|
---|
[601ef8] | 183 | clearQueue(CurrentAction);
|
---|
| 184 | clearTempQueue();
|
---|
[a61dbb] | 185 | lastActionOk = false;
|
---|
[601ef8] | 186 | std::cerr << "Remaining Actions cleared from queue." << std::endl;
|
---|
[11d433] | 187 | } catch (std::exception &e) {
|
---|
| 188 | pushStatus("FAIL: General exception caught, aborting.");
|
---|
| 189 | World::getInstance().setExitFlag(134);
|
---|
[601ef8] | 190 | clearQueue(CurrentAction);
|
---|
| 191 | clearTempQueue();
|
---|
| 192 | std::cerr << "Remaining Actions cleared from queue." << std::endl;
|
---|
[415ddd] | 193 | }
|
---|
[cfb9c5] | 194 | if (lastActionOk) {
|
---|
| 195 | OBSERVE;
|
---|
| 196 | NOTIFY(ActionQueued);
|
---|
| 197 | _lastchangedaction = actionqueue[CurrentAction];
|
---|
[601ef8] | 198 | mtx_queue.lock();
|
---|
| 199 | CurrentAction++;
|
---|
| 200 | mtx_queue.unlock();
|
---|
[cfb9c5] | 201 | }
|
---|
[415ddd] | 202 | // access actionqueue, hence using mutex
|
---|
| 203 | mtx_queue.lock();
|
---|
| 204 | // insert new actions (before [CurrentAction]) if they have been spawned
|
---|
| 205 | // we must have an extra vector for this, as we cannot change actionqueue
|
---|
| 206 | // while an action instance is "in-use"
|
---|
| 207 | insertTempQueue();
|
---|
| 208 | status = (CurrentAction != actionqueue.size());
|
---|
| 209 | mtx_queue.unlock();
|
---|
| 210 | }
|
---|
[601ef8] | 211 | setRunThreadIdle( !((CurrentAction != actionqueue.size()) || !tempqueue.empty()) );
|
---|
[415ddd] | 212 | cond_idle.notify_one();
|
---|
| 213 | // LOG(1, "DEBUG: End of ActionQueue's run() loop.");
|
---|
| 214 | } while (!Interrupted);
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | void ActionQueue::insertTempQueue()
|
---|
| 218 | {
|
---|
| 219 | if (!tempqueue.empty()) {
|
---|
| 220 | ActionQueue_t::iterator InsertionIter = actionqueue.begin();
|
---|
| 221 | std::advance(InsertionIter, CurrentAction);
|
---|
| 222 | actionqueue.insert( InsertionIter, tempqueue.begin(), tempqueue.end() );
|
---|
| 223 | tempqueue.clear();
|
---|
| 224 | }
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | void ActionQueue::wait()
|
---|
| 228 | {
|
---|
| 229 | boost::unique_lock<boost::mutex> lock(mtx_idle);
|
---|
| 230 | while(!run_thread_isIdle)
|
---|
| 231 | {
|
---|
| 232 | cond_idle.wait(lock);
|
---|
| 233 | }
|
---|
| 234 | }
|
---|
[74459a] | 235 | #endif
|
---|
[415ddd] | 236 |
|
---|
[74459a] | 237 | #ifdef HAVE_ACTION_THREAD
|
---|
[415ddd] | 238 | void ActionQueue::stop()
|
---|
| 239 | {
|
---|
| 240 | // notify actionqueue thread that we wish to terminate
|
---|
| 241 | run_thread.interrupt();
|
---|
| 242 | // wait till it ends
|
---|
| 243 | run_thread.join();
|
---|
[975b83] | 244 | }
|
---|
[74459a] | 245 | #endif
|
---|
[975b83] | 246 |
|
---|
[a6ceab] | 247 | Action* ActionQueue::getActionByName(const std::string &name)
|
---|
[1d3563] | 248 | {
|
---|
[ed3944] | 249 | return AR->getActionByName(name);
|
---|
[1d3563] | 250 | }
|
---|
| 251 |
|
---|
[a6ceab] | 252 | bool ActionQueue::isActionKnownByName(const std::string &name) const
|
---|
[1d3563] | 253 | {
|
---|
[ed3944] | 254 | return AR->isActionPresentByName(name);
|
---|
[1d3563] | 255 | }
|
---|
| 256 |
|
---|
[126867] | 257 | void ActionQueue::registerAction(Action *_action)
|
---|
| 258 | {
|
---|
| 259 | AR->registerInstance(_action);
|
---|
| 260 | }
|
---|
| 261 |
|
---|
[46b181] | 262 | void ActionQueue::outputAsCLI(std::ostream &output) const
|
---|
| 263 | {
|
---|
[7fc447] | 264 | for (ActionQueue_t::const_iterator iter = actionqueue.begin();
|
---|
| 265 | iter != actionqueue.end();
|
---|
[46b181] | 266 | ++iter) {
|
---|
[bad589] | 267 | // skip store-session in printed list
|
---|
[12d946] | 268 | if ( ((*iter)->getName() != std::string("store-session"))
|
---|
| 269 | && ((*iter)->getName() != std::string("load-session"))) {
|
---|
[7fc447] | 270 | if (iter != actionqueue.begin())
|
---|
[bad589] | 271 | output << " ";
|
---|
| 272 | (*iter)->outputAsCLI(output);
|
---|
| 273 | }
|
---|
[46b181] | 274 | }
|
---|
| 275 | output << std::endl;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
[477012] | 278 | void ActionQueue::outputAsPython(std::ostream &output) const
|
---|
| 279 | {
|
---|
| 280 | const std::string prefix("pyMoleCuilder");
|
---|
| 281 | output << "import " << prefix << std::endl;
|
---|
[9e4655] | 282 | output << "# ========================== Stored Session BEGIN ==========================" << std::endl;
|
---|
[7fc447] | 283 | for (ActionQueue_t::const_iterator iter = actionqueue.begin();
|
---|
| 284 | iter != actionqueue.end();
|
---|
[477012] | 285 | ++iter) {
|
---|
| 286 | // skip store-session in printed list
|
---|
[12d946] | 287 | if ( ((*iter)->getName() != std::string("store-session"))
|
---|
| 288 | && ((*iter)->getName() != std::string("load-session")))
|
---|
[477012] | 289 | (*iter)->outputAsPython(output, prefix);
|
---|
| 290 | }
|
---|
[9e4655] | 291 | output << "# =========================== Stored Session END ===========================" << std::endl;
|
---|
[477012] | 292 | }
|
---|
| 293 |
|
---|
[a6ceab] | 294 | const ActionTrait& ActionQueue::getActionsTrait(const std::string &name) const
|
---|
[690741] | 295 | {
|
---|
| 296 | // this const_cast is just required as long as we have a non-const getActionByName
|
---|
| 297 | const Action * const action = const_cast<ActionQueue *>(this)->getActionByName(name);
|
---|
| 298 | return action->Traits;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
[6367dd] | 301 | void ActionQueue::addElement(Action* _Action,ActionState::ptr _state)
|
---|
| 302 | {
|
---|
| 303 | history->addElement(_Action, _state);
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | void ActionQueue::clear()
|
---|
| 307 | {
|
---|
| 308 | history->clear();
|
---|
| 309 | }
|
---|
| 310 |
|
---|
[601ef8] | 311 | void ActionQueue::clearQueue(const size_t _fromAction)
|
---|
[7f1a1a] | 312 | {
|
---|
[601ef8] | 313 | #ifdef HAVE_ACTION_THREAD
|
---|
| 314 | mtx_queue.lock();
|
---|
| 315 | #endif
|
---|
| 316 | LOG(1, "Removing all Actions from position " << _fromAction << " onward.");
|
---|
| 317 | // free all actions still to be called contained in actionqueue
|
---|
| 318 | ActionQueue_t::iterator inititer = actionqueue.begin();
|
---|
| 319 | std::advance(inititer, _fromAction);
|
---|
| 320 | for (ActionQueue_t::iterator iter = inititer; iter != actionqueue.end(); ++iter)
|
---|
[7f1a1a] | 321 | delete *iter;
|
---|
[601ef8] | 322 | actionqueue.erase(inititer, actionqueue.end());
|
---|
| 323 | LOG(1, "There are " << actionqueue.size() << " remaining Actions.");
|
---|
| 324 | #ifdef HAVE_ACTION_THREAD
|
---|
| 325 | CurrentAction = actionqueue.size();
|
---|
| 326 | mtx_queue.unlock();
|
---|
| 327 | #endif
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | #ifdef HAVE_ACTION_THREAD
|
---|
| 331 | void ActionQueue::clearTempQueue()
|
---|
| 332 | {
|
---|
[7f1a1a] | 333 | // free all actions contained in tempqueue
|
---|
| 334 | for (ActionQueue_t::iterator iter = tempqueue.begin();
|
---|
| 335 | !tempqueue.empty(); iter = tempqueue.begin()) {
|
---|
| 336 | delete *iter;
|
---|
| 337 | tempqueue.erase(iter);
|
---|
| 338 | }
|
---|
[601ef8] | 339 | }
|
---|
| 340 |
|
---|
| 341 | void ActionQueue::setRunThreadIdle(const bool _flag)
|
---|
| 342 | {
|
---|
[06b5df] | 343 | {
|
---|
| 344 | boost::unique_lock<boost::mutex> lock(mtx_idle);
|
---|
[601ef8] | 345 | run_thread_isIdle = _flag;
|
---|
[06b5df] | 346 | }
|
---|
[7f1a1a] | 347 | }
|
---|
[601ef8] | 348 | #endif
|
---|
[6367dd] | 349 |
|
---|
[690741] | 350 | const ActionQueue::ActionTokens_t ActionQueue::getListOfActions() const
|
---|
| 351 | {
|
---|
| 352 | ActionTokens_t returnlist;
|
---|
| 353 |
|
---|
| 354 | returnlist.insert(
|
---|
| 355 | returnlist.end(),
|
---|
[ed3944] | 356 | MapKeyConstIterator<ActionRegistry::const_iterator>(AR->getBeginIter()),
|
---|
| 357 | MapKeyConstIterator<ActionRegistry::const_iterator>(AR->getEndIter()));
|
---|
[690741] | 358 |
|
---|
| 359 | return returnlist;
|
---|
| 360 | }
|
---|
| 361 |
|
---|
[6367dd] | 362 | void ActionQueue::undoLast()
|
---|
| 363 | {
|
---|
| 364 | history->undoLast();
|
---|
| 365 | }
|
---|
| 366 |
|
---|
[c01fec] | 367 | bool ActionQueue::canUndo() const
|
---|
| 368 | {
|
---|
| 369 | return history->hasUndo();
|
---|
| 370 | }
|
---|
| 371 |
|
---|
[6367dd] | 372 | void ActionQueue::redoLast()
|
---|
| 373 | {
|
---|
| 374 | history->redoLast();
|
---|
| 375 | }
|
---|
| 376 |
|
---|
[c01fec] | 377 | bool ActionQueue::canRedo() const
|
---|
| 378 | {
|
---|
| 379 | return history->hasRedo();
|
---|
| 380 | }
|
---|
| 381 |
|
---|
[6367dd] | 382 |
|
---|
[628577] | 383 | CONSTRUCT_SINGLETON(ActionQueue)
|
---|