[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 |
|
---|
[29b52b] | 57 | const Action* ActionQueue::_lastchangedaction = NULL;
|
---|
| 58 |
|
---|
[ed3944] | 59 | ActionQueue::ActionQueue() :
|
---|
[29b52b] | 60 | Observable("ActionQueue"),
|
---|
[6367dd] | 61 | AR(new ActionRegistry()),
|
---|
[af5384] | 62 | history(new ActionHistory),
|
---|
[a61dbb] | 63 | CurrentAction(0),
|
---|
[74459a] | 64 | #ifndef HAVE_ACTION_THREAD
|
---|
[a61dbb] | 65 | lastActionOk(true)
|
---|
[74459a] | 66 | #else
|
---|
[a61dbb] | 67 | lastActionOk(true),
|
---|
[415ddd] | 68 | run_thread(boost::bind(&ActionQueue::run, this)),
|
---|
| 69 | run_thread_isIdle(true)
|
---|
[74459a] | 70 | #endif
|
---|
[29b52b] | 71 | {
|
---|
| 72 | // channels of observable
|
---|
| 73 | Channels *OurChannel = new Channels;
|
---|
| 74 | NotificationChannels.insert( std::make_pair(static_cast<Observable *>(this), OurChannel) );
|
---|
| 75 | // add instance for each notification type
|
---|
| 76 | for (size_t type = 0; type < NotificationType_MAX; ++type)
|
---|
| 77 | OurChannel->addChannel(type);
|
---|
| 78 | }
|
---|
[628577] | 79 |
|
---|
| 80 | ActionQueue::~ActionQueue()
|
---|
[ed3944] | 81 | {
|
---|
[74459a] | 82 | #ifdef HAVE_ACTION_THREAD
|
---|
[415ddd] | 83 | stop();
|
---|
[74459a] | 84 | #endif
|
---|
[415ddd] | 85 |
|
---|
[7f1a1a] | 86 | clearQueue();
|
---|
[af5384] | 87 |
|
---|
[6367dd] | 88 | delete history;
|
---|
[ed3944] | 89 | delete AR;
|
---|
| 90 | }
|
---|
[628577] | 91 |
|
---|
[f54cda] | 92 | void ActionQueue::queueAction(const std::string &name, enum Action::QueryOptions state)
|
---|
[05c989] | 93 | {
|
---|
[7f1a1a] | 94 | const Action * const registryaction = AR->getActionByName(name);
|
---|
| 95 | queueAction(registryaction, state);
|
---|
[f54cda] | 96 | }
|
---|
| 97 |
|
---|
[7f1a1a] | 98 | void ActionQueue::queueAction(const Action * const _action, enum Action::QueryOptions state)
|
---|
[f54cda] | 99 | {
|
---|
[29b52b] | 100 | OBSERVE;
|
---|
| 101 | NOTIFY(ActionQueued);
|
---|
[af5384] | 102 | Action *newaction = _action->clone(state);
|
---|
| 103 | newaction->prepare(state);
|
---|
[74459a] | 104 | #ifdef HAVE_ACTION_THREAD
|
---|
[415ddd] | 105 | mtx_queue.lock();
|
---|
[74459a] | 106 | #endif
|
---|
[7fc447] | 107 | actionqueue.push_back( newaction );
|
---|
[74459a] | 108 | #ifndef HAVE_ACTION_THREAD
|
---|
| 109 | try {
|
---|
| 110 | newaction->call();
|
---|
[a61dbb] | 111 | lastActionOk = true;
|
---|
[74459a] | 112 | } catch(ActionFailureException &e) {
|
---|
| 113 | std::cerr << "Action " << *boost::get_error_info<ActionNameString>(e) << " has failed." << std::endl;
|
---|
| 114 | World::getInstance().setExitFlag(5);
|
---|
[7f1a1a] | 115 | clearQueue();
|
---|
[a61dbb] | 116 | lastActionOk = false;
|
---|
| 117 | std::cerr << "ActionQueue cleared." << std::endl;
|
---|
[11d433] | 118 | } catch (std::exception &e) {
|
---|
| 119 | pushStatus("FAIL: General exception caught, aborting.");
|
---|
| 120 | World::getInstance().setExitFlag(134);
|
---|
| 121 | clearQueue();
|
---|
| 122 | lastActionOk = false;
|
---|
| 123 | std::cerr << "ActionQueue cleared." << std::endl;
|
---|
[74459a] | 124 | }
|
---|
| 125 | #else
|
---|
[415ddd] | 126 | {
|
---|
| 127 | boost::lock_guard<boost::mutex> lock(mtx_idle);
|
---|
| 128 | run_thread_isIdle = (CurrentAction == actionqueue.size());
|
---|
[0d4168] | 129 | }
|
---|
[415ddd] | 130 | mtx_queue.unlock();
|
---|
[74459a] | 131 | #endif
|
---|
[29b52b] | 132 | _lastchangedaction = newaction;
|
---|
[05c989] | 133 | }
|
---|
| 134 |
|
---|
[975b83] | 135 | void ActionQueue::insertAction(Action *_action, enum Action::QueryOptions state)
|
---|
| 136 | {
|
---|
[74459a] | 137 | #ifndef HAVE_ACTION_THREAD
|
---|
| 138 | queueAction(_action, state);
|
---|
| 139 | #else
|
---|
[415ddd] | 140 | Action *newaction = _action->clone(state);
|
---|
| 141 | newaction->prepare(state);
|
---|
| 142 | mtx_queue.lock();
|
---|
| 143 | tempqueue.push_back( newaction );
|
---|
| 144 | {
|
---|
| 145 | boost::lock_guard<boost::mutex> lock(mtx_idle);
|
---|
| 146 | run_thread_isIdle = !((CurrentAction != actionqueue.size()) || !tempqueue.empty());
|
---|
| 147 | }
|
---|
| 148 | mtx_queue.unlock();
|
---|
[74459a] | 149 | #endif
|
---|
[415ddd] | 150 | }
|
---|
| 151 |
|
---|
[74459a] | 152 | #ifdef HAVE_ACTION_THREAD
|
---|
[415ddd] | 153 | void ActionQueue::run()
|
---|
| 154 | {
|
---|
| 155 | bool Interrupted = false;
|
---|
| 156 | do {
|
---|
| 157 | // sleep for some time and wait for queue to fill up again
|
---|
| 158 | try {
|
---|
| 159 | #if BOOST_VERSION < 105000
|
---|
| 160 | run_thread.sleep(boost::get_system_time() + boost::posix_time::milliseconds(100));
|
---|
| 161 | #else
|
---|
[d93b4b3] | 162 | boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
|
---|
[415ddd] | 163 | #endif
|
---|
| 164 | } catch(boost::thread_interrupted &e) {
|
---|
| 165 | LOG(2, "INFO: ActionQueue has received stop signal.");
|
---|
| 166 | Interrupted = true;
|
---|
| 167 | }
|
---|
| 168 | // LOG(1, "DEBUG: Start of ActionQueue's run() loop.");
|
---|
| 169 | // call all currently present Actions
|
---|
| 170 | mtx_queue.lock();
|
---|
| 171 | insertTempQueue();
|
---|
| 172 | bool status = (CurrentAction != actionqueue.size());
|
---|
| 173 | mtx_queue.unlock();
|
---|
| 174 | while (status) {
|
---|
| 175 | // boost::this_thread::disable_interruption di;
|
---|
| 176 | LOG(0, "Calling Action " << actionqueue[CurrentAction]->getName() << " ... ");
|
---|
| 177 | try {
|
---|
| 178 | actionqueue[CurrentAction]->call();
|
---|
[0b6b77] | 179 | pushStatus("SUCCESS: Action "+actionqueue[CurrentAction]->getName()+" successful.");
|
---|
[a61dbb] | 180 | lastActionOk = true;
|
---|
[415ddd] | 181 | } catch(ActionFailureException &e) {
|
---|
[0b6b77] | 182 | pushStatus("FAIL: Action "+*boost::get_error_info<ActionNameString>(e)+" has failed.");
|
---|
[415ddd] | 183 | World::getInstance().setExitFlag(5);
|
---|
[7f1a1a] | 184 | clearQueue();
|
---|
[a61dbb] | 185 | lastActionOk = false;
|
---|
| 186 | std::cerr << "ActionQueue cleared." << std::endl;
|
---|
| 187 | CurrentAction = (size_t)-1;
|
---|
[11d433] | 188 | } catch (std::exception &e) {
|
---|
| 189 | pushStatus("FAIL: General exception caught, aborting.");
|
---|
| 190 | World::getInstance().setExitFlag(134);
|
---|
| 191 | clearQueue();
|
---|
| 192 | std::cerr << "ActionQueue cleared." << std::endl;
|
---|
| 193 | CurrentAction = (size_t)-1;
|
---|
[415ddd] | 194 | }
|
---|
| 195 | // access actionqueue, hence using mutex
|
---|
| 196 | mtx_queue.lock();
|
---|
| 197 | // step on to next action and check for end
|
---|
| 198 | CurrentAction++;
|
---|
| 199 | // insert new actions (before [CurrentAction]) if they have been spawned
|
---|
| 200 | // we must have an extra vector for this, as we cannot change actionqueue
|
---|
| 201 | // while an action instance is "in-use"
|
---|
| 202 | insertTempQueue();
|
---|
| 203 | status = (CurrentAction != actionqueue.size());
|
---|
| 204 | mtx_queue.unlock();
|
---|
| 205 | }
|
---|
| 206 | {
|
---|
| 207 | boost::lock_guard<boost::mutex> lock(mtx_idle);
|
---|
| 208 | run_thread_isIdle = !((CurrentAction != actionqueue.size()) || !tempqueue.empty());
|
---|
| 209 | }
|
---|
| 210 | cond_idle.notify_one();
|
---|
| 211 | // LOG(1, "DEBUG: End of ActionQueue's run() loop.");
|
---|
| 212 | } while (!Interrupted);
|
---|
| 213 | }
|
---|
[74459a] | 214 | #endif
|
---|
[415ddd] | 215 |
|
---|
| 216 | void ActionQueue::insertTempQueue()
|
---|
| 217 | {
|
---|
| 218 | if (!tempqueue.empty()) {
|
---|
| 219 | ActionQueue_t::iterator InsertionIter = actionqueue.begin();
|
---|
| 220 | std::advance(InsertionIter, CurrentAction);
|
---|
| 221 | actionqueue.insert( InsertionIter, tempqueue.begin(), tempqueue.end() );
|
---|
| 222 | tempqueue.clear();
|
---|
| 223 | }
|
---|
| 224 | }
|
---|
| 225 |
|
---|
[74459a] | 226 | #ifdef HAVE_ACTION_THREAD
|
---|
[415ddd] | 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 |
|
---|
[7f1a1a] | 311 | void ActionQueue::clearQueue()
|
---|
| 312 | {
|
---|
| 313 | // free all actions contained in actionqueue
|
---|
| 314 | for (ActionQueue_t::iterator iter = actionqueue.begin();
|
---|
| 315 | !actionqueue.empty(); iter = actionqueue.begin()) {
|
---|
| 316 | delete *iter;
|
---|
| 317 | actionqueue.erase(iter);
|
---|
| 318 | }
|
---|
| 319 | // free all actions contained in tempqueue
|
---|
| 320 | for (ActionQueue_t::iterator iter = tempqueue.begin();
|
---|
| 321 | !tempqueue.empty(); iter = tempqueue.begin()) {
|
---|
| 322 | delete *iter;
|
---|
| 323 | tempqueue.erase(iter);
|
---|
| 324 | }
|
---|
[06b5df] | 325 | #ifdef HAVE_ACTION_THREAD
|
---|
| 326 | {
|
---|
| 327 | boost::unique_lock<boost::mutex> lock(mtx_idle);
|
---|
| 328 | run_thread_isIdle = true;
|
---|
| 329 | }
|
---|
| 330 | #endif
|
---|
[7f1a1a] | 331 | }
|
---|
[6367dd] | 332 |
|
---|
[690741] | 333 | const ActionQueue::ActionTokens_t ActionQueue::getListOfActions() const
|
---|
| 334 | {
|
---|
| 335 | ActionTokens_t returnlist;
|
---|
| 336 |
|
---|
| 337 | returnlist.insert(
|
---|
| 338 | returnlist.end(),
|
---|
[ed3944] | 339 | MapKeyConstIterator<ActionRegistry::const_iterator>(AR->getBeginIter()),
|
---|
| 340 | MapKeyConstIterator<ActionRegistry::const_iterator>(AR->getEndIter()));
|
---|
[690741] | 341 |
|
---|
| 342 | return returnlist;
|
---|
| 343 | }
|
---|
| 344 |
|
---|
[6367dd] | 345 | void ActionQueue::undoLast()
|
---|
| 346 | {
|
---|
| 347 | history->undoLast();
|
---|
| 348 | }
|
---|
| 349 |
|
---|
| 350 | void ActionQueue::redoLast()
|
---|
| 351 | {
|
---|
| 352 | history->redoLast();
|
---|
| 353 | }
|
---|
| 354 |
|
---|
| 355 |
|
---|
[628577] | 356 | CONSTRUCT_SINGLETON(ActionQueue)
|
---|