| 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 | const Action* ActionQueue::_lastchangedaction = NULL;
 | 
|---|
| 58 | 
 | 
|---|
| 59 | ActionQueue::ActionQueue() :
 | 
|---|
| 60 |     Observable("ActionQueue"),
 | 
|---|
| 61 |     AR(new ActionRegistry()),
 | 
|---|
| 62 |     history(new ActionHistory),
 | 
|---|
| 63 |     CurrentAction(0),
 | 
|---|
| 64 | #ifndef HAVE_ACTION_THREAD
 | 
|---|
| 65 |     lastActionOk(true)
 | 
|---|
| 66 | #else
 | 
|---|
| 67 |     lastActionOk(true),
 | 
|---|
| 68 |     run_thread(boost::bind(&ActionQueue::run, this)),
 | 
|---|
| 69 |     run_thread_isIdle(true)
 | 
|---|
| 70 | #endif
 | 
|---|
| 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 | }
 | 
|---|
| 79 | 
 | 
|---|
| 80 | ActionQueue::~ActionQueue()
 | 
|---|
| 81 | {
 | 
|---|
| 82 | #ifdef HAVE_ACTION_THREAD
 | 
|---|
| 83 |   stop();
 | 
|---|
| 84 | #endif
 | 
|---|
| 85 | 
 | 
|---|
| 86 |   clearQueue();
 | 
|---|
| 87 | 
 | 
|---|
| 88 |   delete history;
 | 
|---|
| 89 |   delete AR;
 | 
|---|
| 90 | }
 | 
|---|
| 91 | 
 | 
|---|
| 92 | void ActionQueue::queueAction(const std::string &name, enum Action::QueryOptions state)
 | 
|---|
| 93 | {
 | 
|---|
| 94 |   const Action * const registryaction = AR->getActionByName(name);
 | 
|---|
| 95 |   queueAction(registryaction, state);
 | 
|---|
| 96 | }
 | 
|---|
| 97 | 
 | 
|---|
| 98 | void ActionQueue::queueAction(const Action * const _action, enum Action::QueryOptions state)
 | 
|---|
| 99 | {
 | 
|---|
| 100 |   Action *newaction = _action->clone(state);
 | 
|---|
| 101 |   newaction->prepare(state);
 | 
|---|
| 102 | #ifdef HAVE_ACTION_THREAD
 | 
|---|
| 103 |   mtx_queue.lock();
 | 
|---|
| 104 | #endif
 | 
|---|
| 105 |   actionqueue.push_back( newaction );
 | 
|---|
| 106 | #ifndef HAVE_ACTION_THREAD
 | 
|---|
| 107 |   try {
 | 
|---|
| 108 |     newaction->call();
 | 
|---|
| 109 |     lastActionOk = true;
 | 
|---|
| 110 |   } catch(ActionFailureException &e) {
 | 
|---|
| 111 |     std::cerr << "Action " << *boost::get_error_info<ActionNameString>(e) << " has failed." << std::endl;
 | 
|---|
| 112 |     World::getInstance().setExitFlag(5);
 | 
|---|
| 113 |     clearQueue();
 | 
|---|
| 114 |     lastActionOk = false;
 | 
|---|
| 115 |     std::cerr << "ActionQueue cleared." << std::endl;
 | 
|---|
| 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;
 | 
|---|
| 122 |   }
 | 
|---|
| 123 |   if (lastActionOk) {
 | 
|---|
| 124 |     OBSERVE;
 | 
|---|
| 125 |     NOTIFY(ActionQueued);
 | 
|---|
| 126 |     _lastchangedaction = newaction;
 | 
|---|
| 127 |   }
 | 
|---|
| 128 | #else
 | 
|---|
| 129 |   {
 | 
|---|
| 130 |     boost::lock_guard<boost::mutex> lock(mtx_idle);
 | 
|---|
| 131 |     run_thread_isIdle = (CurrentAction == actionqueue.size());
 | 
|---|
| 132 |   }
 | 
|---|
| 133 |   mtx_queue.unlock();
 | 
|---|
| 134 | #endif
 | 
|---|
| 135 | }
 | 
|---|
| 136 | 
 | 
|---|
| 137 | void ActionQueue::insertAction(Action *_action, enum Action::QueryOptions state)
 | 
|---|
| 138 | {
 | 
|---|
| 139 | #ifndef HAVE_ACTION_THREAD
 | 
|---|
| 140 |   queueAction(_action, state);
 | 
|---|
| 141 | #else
 | 
|---|
| 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();
 | 
|---|
| 151 | #endif
 | 
|---|
| 152 | }
 | 
|---|
| 153 | 
 | 
|---|
| 154 | #ifdef HAVE_ACTION_THREAD
 | 
|---|
| 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
 | 
|---|
| 164 |       boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
 | 
|---|
| 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();
 | 
|---|
| 181 |         pushStatus("SUCCESS: Action "+actionqueue[CurrentAction]->getName()+" successful.");
 | 
|---|
| 182 |         lastActionOk = true;
 | 
|---|
| 183 |       } catch(ActionFailureException &e) {
 | 
|---|
| 184 |         pushStatus("FAIL: Action "+*boost::get_error_info<ActionNameString>(e)+" has failed.");
 | 
|---|
| 185 |         World::getInstance().setExitFlag(5);
 | 
|---|
| 186 |         clearQueue();
 | 
|---|
| 187 |         lastActionOk = false;
 | 
|---|
| 188 |         std::cerr << "ActionQueue cleared." << std::endl;
 | 
|---|
| 189 |         CurrentAction = (size_t)-1;
 | 
|---|
| 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;
 | 
|---|
| 196 |       }
 | 
|---|
| 197 |       if (lastActionOk) {
 | 
|---|
| 198 |         OBSERVE;
 | 
|---|
| 199 |         NOTIFY(ActionQueued);
 | 
|---|
| 200 |         _lastchangedaction = actionqueue[CurrentAction];
 | 
|---|
| 201 |       }
 | 
|---|
| 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 | }
 | 
|---|
| 221 | #endif
 | 
|---|
| 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 | 
 | 
|---|
| 233 | #ifdef HAVE_ACTION_THREAD
 | 
|---|
| 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 | }
 | 
|---|
| 242 | #endif
 | 
|---|
| 243 | 
 | 
|---|
| 244 | #ifdef HAVE_ACTION_THREAD
 | 
|---|
| 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();
 | 
|---|
| 251 | }
 | 
|---|
| 252 | #endif
 | 
|---|
| 253 | 
 | 
|---|
| 254 | Action* ActionQueue::getActionByName(const std::string &name)
 | 
|---|
| 255 | {
 | 
|---|
| 256 |   return AR->getActionByName(name);
 | 
|---|
| 257 | }
 | 
|---|
| 258 | 
 | 
|---|
| 259 | bool ActionQueue::isActionKnownByName(const std::string &name) const
 | 
|---|
| 260 | {
 | 
|---|
| 261 |   return AR->isActionPresentByName(name);
 | 
|---|
| 262 | }
 | 
|---|
| 263 | 
 | 
|---|
| 264 | void ActionQueue::registerAction(Action *_action)
 | 
|---|
| 265 | {
 | 
|---|
| 266 |   AR->registerInstance(_action);
 | 
|---|
| 267 | }
 | 
|---|
| 268 | 
 | 
|---|
| 269 | void ActionQueue::outputAsCLI(std::ostream &output) const
 | 
|---|
| 270 | {
 | 
|---|
| 271 |   for (ActionQueue_t::const_iterator iter = actionqueue.begin();
 | 
|---|
| 272 |       iter != actionqueue.end();
 | 
|---|
| 273 |       ++iter) {
 | 
|---|
| 274 |     // skip store-session in printed list
 | 
|---|
| 275 |     if ( ((*iter)->getName() != std::string("store-session"))
 | 
|---|
| 276 |         && ((*iter)->getName() != std::string("load-session"))) {
 | 
|---|
| 277 |       if (iter != actionqueue.begin())
 | 
|---|
| 278 |         output << " ";
 | 
|---|
| 279 |       (*iter)->outputAsCLI(output);
 | 
|---|
| 280 |     }
 | 
|---|
| 281 |   }
 | 
|---|
| 282 |   output << std::endl;
 | 
|---|
| 283 | }
 | 
|---|
| 284 | 
 | 
|---|
| 285 | void ActionQueue::outputAsPython(std::ostream &output) const
 | 
|---|
| 286 | {
 | 
|---|
| 287 |   const std::string prefix("pyMoleCuilder");
 | 
|---|
| 288 |   output << "import " << prefix << std::endl;
 | 
|---|
| 289 |   output << "# ========================== Stored Session BEGIN ==========================" << std::endl;
 | 
|---|
| 290 |   for (ActionQueue_t::const_iterator iter = actionqueue.begin();
 | 
|---|
| 291 |       iter != actionqueue.end();
 | 
|---|
| 292 |       ++iter) {
 | 
|---|
| 293 |     // skip store-session in printed list
 | 
|---|
| 294 |     if ( ((*iter)->getName() != std::string("store-session"))
 | 
|---|
| 295 |         && ((*iter)->getName() != std::string("load-session")))
 | 
|---|
| 296 |       (*iter)->outputAsPython(output, prefix);
 | 
|---|
| 297 |   }
 | 
|---|
| 298 |   output << "# =========================== Stored Session END ===========================" << std::endl;
 | 
|---|
| 299 | }
 | 
|---|
| 300 | 
 | 
|---|
| 301 | const ActionTrait& ActionQueue::getActionsTrait(const std::string &name) const
 | 
|---|
| 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 | 
 | 
|---|
| 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 | 
 | 
|---|
| 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 |   }
 | 
|---|
| 332 | #ifdef HAVE_ACTION_THREAD
 | 
|---|
| 333 |   {
 | 
|---|
| 334 |     boost::unique_lock<boost::mutex> lock(mtx_idle);
 | 
|---|
| 335 |     run_thread_isIdle = true;
 | 
|---|
| 336 |   }
 | 
|---|
| 337 | #endif
 | 
|---|
| 338 | }
 | 
|---|
| 339 | 
 | 
|---|
| 340 | const ActionQueue::ActionTokens_t ActionQueue::getListOfActions() const
 | 
|---|
| 341 | {
 | 
|---|
| 342 |   ActionTokens_t returnlist;
 | 
|---|
| 343 | 
 | 
|---|
| 344 |   returnlist.insert(
 | 
|---|
| 345 |       returnlist.end(),
 | 
|---|
| 346 |       MapKeyConstIterator<ActionRegistry::const_iterator>(AR->getBeginIter()),
 | 
|---|
| 347 |       MapKeyConstIterator<ActionRegistry::const_iterator>(AR->getEndIter()));
 | 
|---|
| 348 | 
 | 
|---|
| 349 |   return returnlist;
 | 
|---|
| 350 | }
 | 
|---|
| 351 | 
 | 
|---|
| 352 | void ActionQueue::undoLast()
 | 
|---|
| 353 | {
 | 
|---|
| 354 |         history->undoLast();
 | 
|---|
| 355 | }
 | 
|---|
| 356 | 
 | 
|---|
| 357 | void ActionQueue::redoLast()
 | 
|---|
| 358 | {
 | 
|---|
| 359 |         history->redoLast();
 | 
|---|
| 360 | }
 | 
|---|
| 361 | 
 | 
|---|
| 362 | 
 | 
|---|
| 363 | CONSTRUCT_SINGLETON(ActionQueue)
 | 
|---|