[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 | {
|
---|
[af5384] | 100 | Action *newaction = _action->clone(state);
|
---|
| 101 | newaction->prepare(state);
|
---|
[74459a] | 102 | #ifdef HAVE_ACTION_THREAD
|
---|
[415ddd] | 103 | mtx_queue.lock();
|
---|
[74459a] | 104 | #endif
|
---|
[7fc447] | 105 | actionqueue.push_back( newaction );
|
---|
[74459a] | 106 | #ifndef HAVE_ACTION_THREAD
|
---|
| 107 | try {
|
---|
| 108 | newaction->call();
|
---|
[a61dbb] | 109 | lastActionOk = true;
|
---|
[74459a] | 110 | } catch(ActionFailureException &e) {
|
---|
| 111 | std::cerr << "Action " << *boost::get_error_info<ActionNameString>(e) << " has failed." << std::endl;
|
---|
| 112 | World::getInstance().setExitFlag(5);
|
---|
[7f1a1a] | 113 | clearQueue();
|
---|
[a61dbb] | 114 | lastActionOk = false;
|
---|
| 115 | std::cerr << "ActionQueue cleared." << std::endl;
|
---|
[11d433] | 116 | } catch (std::exception &e) {
|
---|
| 117 | pushStatus("FAIL: General exception caught, aborting.");
|
---|
| 118 | World::getInstance().setExitFlag(134);
|
---|
| 119 | clearQueue();
|
---|
| 120 | lastActionOk = false;
|
---|
| 121 | std::cerr << "ActionQueue cleared." << std::endl;
|
---|
[74459a] | 122 | }
|
---|
[cfb9c5] | 123 | if (lastActionOk) {
|
---|
| 124 | OBSERVE;
|
---|
| 125 | NOTIFY(ActionQueued);
|
---|
| 126 | _lastchangedaction = newaction;
|
---|
| 127 | }
|
---|
[74459a] | 128 | #else
|
---|
[415ddd] | 129 | {
|
---|
| 130 | boost::lock_guard<boost::mutex> lock(mtx_idle);
|
---|
| 131 | run_thread_isIdle = (CurrentAction == actionqueue.size());
|
---|
[0d4168] | 132 | }
|
---|
[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 );
|
---|
| 146 | {
|
---|
| 147 | boost::lock_guard<boost::mutex> lock(mtx_idle);
|
---|
| 148 | run_thread_isIdle = !((CurrentAction != actionqueue.size()) || !tempqueue.empty());
|
---|
| 149 | }
|
---|
| 150 | mtx_queue.unlock();
|
---|
[74459a] | 151 | #endif
|
---|
[415ddd] | 152 | }
|
---|
| 153 |
|
---|
[74459a] | 154 | #ifdef HAVE_ACTION_THREAD
|
---|
[415ddd] | 155 | void ActionQueue::run()
|
---|
| 156 | {
|
---|
| 157 | bool Interrupted = false;
|
---|
| 158 | do {
|
---|
| 159 | // sleep for some time and wait for queue to fill up again
|
---|
| 160 | try {
|
---|
| 161 | #if BOOST_VERSION < 105000
|
---|
| 162 | run_thread.sleep(boost::get_system_time() + boost::posix_time::milliseconds(100));
|
---|
| 163 | #else
|
---|
[d93b4b3] | 164 | boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
|
---|
[415ddd] | 165 | #endif
|
---|
| 166 | } catch(boost::thread_interrupted &e) {
|
---|
| 167 | LOG(2, "INFO: ActionQueue has received stop signal.");
|
---|
| 168 | Interrupted = true;
|
---|
| 169 | }
|
---|
| 170 | // LOG(1, "DEBUG: Start of ActionQueue's run() loop.");
|
---|
| 171 | // call all currently present Actions
|
---|
| 172 | mtx_queue.lock();
|
---|
| 173 | insertTempQueue();
|
---|
| 174 | bool status = (CurrentAction != actionqueue.size());
|
---|
| 175 | mtx_queue.unlock();
|
---|
| 176 | while (status) {
|
---|
| 177 | // boost::this_thread::disable_interruption di;
|
---|
| 178 | LOG(0, "Calling Action " << actionqueue[CurrentAction]->getName() << " ... ");
|
---|
| 179 | try {
|
---|
| 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);
|
---|
[7f1a1a] | 186 | clearQueue();
|
---|
[a61dbb] | 187 | lastActionOk = false;
|
---|
| 188 | std::cerr << "ActionQueue cleared." << std::endl;
|
---|
| 189 | CurrentAction = (size_t)-1;
|
---|
[11d433] | 190 | } catch (std::exception &e) {
|
---|
| 191 | pushStatus("FAIL: General exception caught, aborting.");
|
---|
| 192 | World::getInstance().setExitFlag(134);
|
---|
| 193 | clearQueue();
|
---|
| 194 | std::cerr << "ActionQueue cleared." << std::endl;
|
---|
| 195 | CurrentAction = (size_t)-1;
|
---|
[415ddd] | 196 | }
|
---|
[cfb9c5] | 197 | if (lastActionOk) {
|
---|
| 198 | OBSERVE;
|
---|
| 199 | NOTIFY(ActionQueued);
|
---|
| 200 | _lastchangedaction = actionqueue[CurrentAction];
|
---|
| 201 | }
|
---|
[415ddd] | 202 | // access actionqueue, hence using mutex
|
---|
| 203 | mtx_queue.lock();
|
---|
| 204 | // step on to next action and check for end
|
---|
| 205 | CurrentAction++;
|
---|
| 206 | // insert new actions (before [CurrentAction]) if they have been spawned
|
---|
| 207 | // we must have an extra vector for this, as we cannot change actionqueue
|
---|
| 208 | // while an action instance is "in-use"
|
---|
| 209 | insertTempQueue();
|
---|
| 210 | status = (CurrentAction != actionqueue.size());
|
---|
| 211 | mtx_queue.unlock();
|
---|
| 212 | }
|
---|
| 213 | {
|
---|
| 214 | boost::lock_guard<boost::mutex> lock(mtx_idle);
|
---|
| 215 | run_thread_isIdle = !((CurrentAction != actionqueue.size()) || !tempqueue.empty());
|
---|
| 216 | }
|
---|
| 217 | cond_idle.notify_one();
|
---|
| 218 | // LOG(1, "DEBUG: End of ActionQueue's run() loop.");
|
---|
| 219 | } while (!Interrupted);
|
---|
| 220 | }
|
---|
[74459a] | 221 | #endif
|
---|
[415ddd] | 222 |
|
---|
| 223 | void ActionQueue::insertTempQueue()
|
---|
| 224 | {
|
---|
| 225 | if (!tempqueue.empty()) {
|
---|
| 226 | ActionQueue_t::iterator InsertionIter = actionqueue.begin();
|
---|
| 227 | std::advance(InsertionIter, CurrentAction);
|
---|
| 228 | actionqueue.insert( InsertionIter, tempqueue.begin(), tempqueue.end() );
|
---|
| 229 | tempqueue.clear();
|
---|
| 230 | }
|
---|
| 231 | }
|
---|
| 232 |
|
---|
[74459a] | 233 | #ifdef HAVE_ACTION_THREAD
|
---|
[415ddd] | 234 | void ActionQueue::wait()
|
---|
| 235 | {
|
---|
| 236 | boost::unique_lock<boost::mutex> lock(mtx_idle);
|
---|
| 237 | while(!run_thread_isIdle)
|
---|
| 238 | {
|
---|
| 239 | cond_idle.wait(lock);
|
---|
| 240 | }
|
---|
| 241 | }
|
---|
[74459a] | 242 | #endif
|
---|
[415ddd] | 243 |
|
---|
[74459a] | 244 | #ifdef HAVE_ACTION_THREAD
|
---|
[415ddd] | 245 | void ActionQueue::stop()
|
---|
| 246 | {
|
---|
| 247 | // notify actionqueue thread that we wish to terminate
|
---|
| 248 | run_thread.interrupt();
|
---|
| 249 | // wait till it ends
|
---|
| 250 | run_thread.join();
|
---|
[975b83] | 251 | }
|
---|
[74459a] | 252 | #endif
|
---|
[975b83] | 253 |
|
---|
[a6ceab] | 254 | Action* ActionQueue::getActionByName(const std::string &name)
|
---|
[1d3563] | 255 | {
|
---|
[ed3944] | 256 | return AR->getActionByName(name);
|
---|
[1d3563] | 257 | }
|
---|
| 258 |
|
---|
[a6ceab] | 259 | bool ActionQueue::isActionKnownByName(const std::string &name) const
|
---|
[1d3563] | 260 | {
|
---|
[ed3944] | 261 | return AR->isActionPresentByName(name);
|
---|
[1d3563] | 262 | }
|
---|
| 263 |
|
---|
[126867] | 264 | void ActionQueue::registerAction(Action *_action)
|
---|
| 265 | {
|
---|
| 266 | AR->registerInstance(_action);
|
---|
| 267 | }
|
---|
| 268 |
|
---|
[46b181] | 269 | void ActionQueue::outputAsCLI(std::ostream &output) const
|
---|
| 270 | {
|
---|
[7fc447] | 271 | for (ActionQueue_t::const_iterator iter = actionqueue.begin();
|
---|
| 272 | iter != actionqueue.end();
|
---|
[46b181] | 273 | ++iter) {
|
---|
[bad589] | 274 | // skip store-session in printed list
|
---|
[12d946] | 275 | if ( ((*iter)->getName() != std::string("store-session"))
|
---|
| 276 | && ((*iter)->getName() != std::string("load-session"))) {
|
---|
[7fc447] | 277 | if (iter != actionqueue.begin())
|
---|
[bad589] | 278 | output << " ";
|
---|
| 279 | (*iter)->outputAsCLI(output);
|
---|
| 280 | }
|
---|
[46b181] | 281 | }
|
---|
| 282 | output << std::endl;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
[477012] | 285 | void ActionQueue::outputAsPython(std::ostream &output) const
|
---|
| 286 | {
|
---|
| 287 | const std::string prefix("pyMoleCuilder");
|
---|
| 288 | output << "import " << prefix << std::endl;
|
---|
[9e4655] | 289 | output << "# ========================== Stored Session BEGIN ==========================" << std::endl;
|
---|
[7fc447] | 290 | for (ActionQueue_t::const_iterator iter = actionqueue.begin();
|
---|
| 291 | iter != actionqueue.end();
|
---|
[477012] | 292 | ++iter) {
|
---|
| 293 | // skip store-session in printed list
|
---|
[12d946] | 294 | if ( ((*iter)->getName() != std::string("store-session"))
|
---|
| 295 | && ((*iter)->getName() != std::string("load-session")))
|
---|
[477012] | 296 | (*iter)->outputAsPython(output, prefix);
|
---|
| 297 | }
|
---|
[9e4655] | 298 | output << "# =========================== Stored Session END ===========================" << std::endl;
|
---|
[477012] | 299 | }
|
---|
| 300 |
|
---|
[a6ceab] | 301 | const ActionTrait& ActionQueue::getActionsTrait(const std::string &name) const
|
---|
[690741] | 302 | {
|
---|
| 303 | // this const_cast is just required as long as we have a non-const getActionByName
|
---|
| 304 | const Action * const action = const_cast<ActionQueue *>(this)->getActionByName(name);
|
---|
| 305 | return action->Traits;
|
---|
| 306 | }
|
---|
| 307 |
|
---|
[6367dd] | 308 | void ActionQueue::addElement(Action* _Action,ActionState::ptr _state)
|
---|
| 309 | {
|
---|
| 310 | history->addElement(_Action, _state);
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | void ActionQueue::clear()
|
---|
| 314 | {
|
---|
| 315 | history->clear();
|
---|
| 316 | }
|
---|
| 317 |
|
---|
[7f1a1a] | 318 | void ActionQueue::clearQueue()
|
---|
| 319 | {
|
---|
| 320 | // free all actions contained in actionqueue
|
---|
| 321 | for (ActionQueue_t::iterator iter = actionqueue.begin();
|
---|
| 322 | !actionqueue.empty(); iter = actionqueue.begin()) {
|
---|
| 323 | delete *iter;
|
---|
| 324 | actionqueue.erase(iter);
|
---|
| 325 | }
|
---|
| 326 | // free all actions contained in tempqueue
|
---|
| 327 | for (ActionQueue_t::iterator iter = tempqueue.begin();
|
---|
| 328 | !tempqueue.empty(); iter = tempqueue.begin()) {
|
---|
| 329 | delete *iter;
|
---|
| 330 | tempqueue.erase(iter);
|
---|
| 331 | }
|
---|
[06b5df] | 332 | #ifdef HAVE_ACTION_THREAD
|
---|
| 333 | {
|
---|
| 334 | boost::unique_lock<boost::mutex> lock(mtx_idle);
|
---|
| 335 | run_thread_isIdle = true;
|
---|
| 336 | }
|
---|
| 337 | #endif
|
---|
[7f1a1a] | 338 | }
|
---|
[6367dd] | 339 |
|
---|
[690741] | 340 | const ActionQueue::ActionTokens_t ActionQueue::getListOfActions() const
|
---|
| 341 | {
|
---|
| 342 | ActionTokens_t returnlist;
|
---|
| 343 |
|
---|
| 344 | returnlist.insert(
|
---|
| 345 | returnlist.end(),
|
---|
[ed3944] | 346 | MapKeyConstIterator<ActionRegistry::const_iterator>(AR->getBeginIter()),
|
---|
| 347 | MapKeyConstIterator<ActionRegistry::const_iterator>(AR->getEndIter()));
|
---|
[690741] | 348 |
|
---|
| 349 | return returnlist;
|
---|
| 350 | }
|
---|
| 351 |
|
---|
[6367dd] | 352 | void ActionQueue::undoLast()
|
---|
| 353 | {
|
---|
| 354 | history->undoLast();
|
---|
| 355 | }
|
---|
| 356 |
|
---|
| 357 | void ActionQueue::redoLast()
|
---|
| 358 | {
|
---|
| 359 | history->redoLast();
|
---|
| 360 | }
|
---|
| 361 |
|
---|
| 362 |
|
---|
[628577] | 363 | CONSTRUCT_SINGLETON(ActionQueue)
|
---|