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