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