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