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