Changeset af5384 for src


Ignore:
Timestamp:
Apr 29, 2014, 12:42:44 PM (11 years ago)
Author:
Frederik Heber <heber@…>
Branches:
Action_Thermostats, Add_AtomRandomPerturbation, Add_FitFragmentPartialChargesAction, Add_RotateAroundBondAction, Add_SelectAtomByNameAction, Added_ParseSaveFragmentResults, AddingActions_SaveParseParticleParameters, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_ParticleName_to_Atom, Adding_StructOpt_integration_tests, AtomFragments, Automaking_mpqc_open, AutomationFragmentation_failures, Candidate_v1.5.4, Candidate_v1.6.0, Candidate_v1.6.1, ChangeBugEmailaddress, ChangingTestPorts, ChemicalSpaceEvaluator, CombiningParticlePotentialParsing, Combining_Subpackages, Debian_Package_split, Debian_package_split_molecuildergui_only, Disabling_MemDebug, Docu_Python_wait, EmpiricalPotential_contain_HomologyGraph, EmpiricalPotential_contain_HomologyGraph_documentation, Enable_parallel_make_install, Enhance_userguide, Enhanced_StructuralOptimization, Enhanced_StructuralOptimization_continued, Example_ManyWaysToTranslateAtom, Exclude_Hydrogens_annealWithBondGraph, FitPartialCharges_GlobalError, Fix_BoundInBox_CenterInBox_MoleculeActions, Fix_ChargeSampling_PBC, Fix_ChronosMutex, Fix_FitPartialCharges, Fix_FitPotential_needs_atomicnumbers, Fix_ForceAnnealing, Fix_IndependentFragmentGrids, Fix_ParseParticles, Fix_ParseParticles_split_forward_backward_Actions, Fix_PopActions, Fix_QtFragmentList_sorted_selection, Fix_Restrictedkeyset_FragmentMolecule, Fix_StatusMsg, Fix_StepWorldTime_single_argument, Fix_Verbose_Codepatterns, Fix_fitting_potentials, Fixes, ForceAnnealing_goodresults, ForceAnnealing_oldresults, ForceAnnealing_tocheck, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, FragmentAction_writes_AtomFragments, FragmentMolecule_checks_bonddegrees, GeometryObjects, Gui_Fixes, Gui_displays_atomic_force_velocity, ImplicitCharges, IndependentFragmentGrids, IndependentFragmentGrids_IndividualZeroInstances, IndependentFragmentGrids_IntegrationTest, IndependentFragmentGrids_Sole_NN_Calculation, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, JobMarket_unresolvable_hostname_fix, MoreRobust_FragmentAutomation, ODR_violation_mpqc_open, PartialCharges_OrthogonalSummation, PdbParser_setsAtomName, PythonUI_with_named_parameters, QtGui_reactivate_TimeChanged_changes, Recreated_GuiChecks, Rewrite_FitPartialCharges, RotateToPrincipalAxisSystem_UndoRedo, SaturateAtoms_findBestMatching, SaturateAtoms_singleDegree, StoppableMakroAction, Subpackage_CodePatterns, Subpackage_JobMarket, Subpackage_LinearAlgebra, Subpackage_levmar, Subpackage_mpqc_open, Subpackage_vmg, Switchable_LogView, ThirdParty_MPQC_rebuilt_buildsystem, TrajectoryDependenant_MaxOrder, TremoloParser_IncreasedPrecision, TremoloParser_MultipleTimesteps, TremoloParser_setsAtomName, Ubuntu_1604_changes, stable
Children:
7fc447
Parents:
36053c
git-author:
Frederik Heber <heber@…> (08/27/13 01:58:34)
git-committer:
Frederik Heber <heber@…> (04/29/14 12:42:44)
Message:

Actions can now clone() and split off prepare() from call().

  • ActionQueue now separates call phase into clone(), prepare(), and call().
  • ActionQueue's dstor releases Actions in deque.
  • CurrentAction is an index as push_back always invalidates iterator.
  • ActionQueue_t is now a simple vector of ptrs.
  • call() is now non-interactive, (maybe) interactive part is placed in prepare() where dialogs are used to fill parameters.
  • only fully prepared Actions are placed in ActionQueue.
  • ActionQueue obtains action instances from Registry, copies, prepares them, and places them in the queue.
  • Action::clone() gets QueryOptions as param. This lets us either clone without params or really copy the instance.
  • If action is called as COMMAND, params have been filled already.
  • Reactions require a ActionQueue::getLastAction() to actually obtain result of (cloned) Action.
  • FIX: Calculation now has const has...() and getResult().
Location:
src
Files:
27 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified src/Actions/Action.cpp

    r36053c raf5384  
    112112}
    113113
    114 Dialog * Action::createDialog(){
    115   Dialog *dialog = UIFactory::getInstance().makeDialog();
    116   return fillDialog(dialog);
    117 }
    118 
    119 void Action::call(enum QueryOptions flag){
    120   if(!isActive()){
    121     return;
    122   }
    123   // forward to private virtual
     114void Action::prepare(enum QueryOptions flag)
     115{
     116  // fill with
    124117  if (flag == Interactive) {
    125118    Dialog* dialog = createDialog();
     
    130123    }
    131124    delete(dialog);
     125  }
     126}
     127
     128Dialog * Action::createDialog(){
     129  Dialog *dialog = UIFactory::getInstance().makeDialog();
     130  return fillDialog(dialog);
     131}
     132
     133void Action::call(){
     134  if(!isActive()){
     135    return;
    132136  }
    133137  ActionState::ptr state = Action::failure;
  • TabularUnified src/Actions/Action.hpp

    r36053c raf5384  
    133133   * is thrown)
    134134   */
    135   void call(enum QueryOptions state = Interactive);
     135  void call();
    136136
    137137public:
     
    167167   */
    168168  const std::string help() const;
     169
     170  /** Clones the Action.
     171   *
     172   */
     173  virtual Action* clone(enum QueryOptions flag = Interactive) const=0;
     174
     175  /** Prepares the Action's parameters.
     176   *
     177   */
     178  virtual void prepare(enum QueryOptions flag = Interactive);
    169179
    170180  /** Prints what would be necessary to add the Action from the Command Line Interface.
  • TabularUnified src/Actions/ActionQueue.cpp

    r36053c raf5384  
    5353ActionQueue::ActionQueue() :
    5454    AR(new ActionRegistry()),
    55     history(new ActionHistory)
     55    history(new ActionHistory),
     56    CurrentAction(0)
    5657{}
    5758
    5859ActionQueue::~ActionQueue()
    5960{
    60   queue.clear();
     61  // free all actions contained in queue
     62  for (ActionQueue_t::iterator iter = queue.begin(); !queue.empty(); iter = queue.begin()) {
     63    delete *iter;
     64    queue.erase(iter);
     65  }
     66
    6167  delete history;
    6268  delete AR;
     
    6571void ActionQueue::queueAction(const std::string &name, enum Action::QueryOptions state)
    6672{
    67   Action* _action = AR->getActionByName(name);
    68   queue.push_back(_action);
    69   _action->call(state);
     73  queueAction(AR->getActionByName(name), state);
    7074}
    7175
    7276void ActionQueue::queueAction(Action *_action, enum Action::QueryOptions state)
    7377{
    74   queue.push_back(_action);
    75   _action->call(state);
     78  Action *newaction = _action->clone(state);
     79  newaction->prepare(state);
     80  queue.push_back( newaction );
     81  newaction->call();
    7682}
    7783
  • TabularUnified src/Actions/ActionQueue.hpp

    r36053c raf5384  
    1616#include "CodePatterns/Singleton.hpp"
    1717
    18 #include <list>
    1918#include <vector>
    2019
     
    3837public:
    3938  typedef std::vector<std::string> ActionTokens_t;
    40   typedef std::list<Action *> ActionQueue_t;
     39  typedef std::vector< Action * > ActionQueue_t;
    4140
    4241  /** Queues the Action with \a name to be called.
     
    104103  void outputAsPython(std::ostream &output) const;
    105104
    106   /** Undoes last called Action.
     105  /** Undoes last called Acfriend void ::cleanUp();tion.
    107106   *
    108107   */
     
    113112   */
    114113  void redoLast();
     114
     115  /** Getter for the last executed action.
     116   *
     117   * \note this is necessary for Reactions to actually have a chance of getting
     118   * the calculated value as Action's are always cloned.
     119   *
     120   * \return const ref to last action
     121   */
     122  const Action &getLastAction() const {
     123    return *(queue.back());
     124  }
    115125
    116126private:
     
    151161  //!> internal queue of actions
    152162  ActionQueue_t queue;
     163
     164  //!> point to current action in queue
     165  size_t CurrentAction;
    153166};
    154167
  • TabularUnified src/Actions/ActionSequence.cpp

    r36053c raf5384  
    4848    loop(1)
    4949{}
     50
     51ActionSequence::ActionSequence(const ActionSequence &_other) :
     52    loop(1)
     53{
     54  // we need to override copy cstor as we have pointer referenced objects
     55  for(actionSet::iterator it=actions.begin(); it!=actions.end(); it++){
     56    actions.push_back((*it)->clone());
     57  }
     58}
    5059
    5160ActionSequence::~ActionSequence()
  • TabularUnified src/Actions/ActionSequence.hpp

    r36053c raf5384  
    3232
    3333  ActionSequence();
     34  ActionSequence(const ActionSequence &_other);
    3435  virtual ~ActionSequence();
    3536
  • TabularUnified src/Actions/Action_impl_header.hpp

    r36053c raf5384  
    212212  bool shouldUndo();
    213213
     214  Action* clone(enum QueryOptions flag = Interactive) const;
     215
    214216  void outputAsCLI(std::ostream &ost) const;
    215217  void outputAsPython(std::ostream &ost, const std::string &prefix) const;
  • TabularUnified src/Actions/Action_impl_pre.hpp

    r36053c raf5384  
    292292    {}
    293293
     294// =========== clone Action ===========
     295Action* ACTION::clone(enum QueryOptions flag) const
     296{
     297  if (flag == Interactive)
     298    return new ACTION();
     299  else
     300    return new ACTION(*this);
     301}
     302
    294303// =========== fill a dialog ===========
    295304Dialog* ACTION::fillDialog(Dialog *dialog) {
  • TabularUnified src/Actions/AtomsCalculation.hpp

    r36053c raf5384  
    2828  virtual ~AtomsCalculation();
    2929
     30  Action* clone(enum Action::QueryOptions flag = Action::Interactive) const;
     31
    3032protected:
    3133  virtual std::vector<T>* doCalc();
  • TabularUnified src/Actions/AtomsCalculation_impl.hpp

    r36053c raf5384  
    5252
    5353template<typename T>
     54Action* AtomsCalculation<T>::clone(enum Action::QueryOptions flag) const
     55{
     56  if (flag == Action::Interactive)
     57    return new AtomsCalculation<T>(op, Action::Traits, descr);
     58  else
     59    return new AtomsCalculation<T>(*this);
     60}
     61
     62template<typename T>
    5463Dialog *AtomsCalculation<T>::fillDialog(Dialog *dialog){
    5564  ASSERT(dialog,"No Dialog given when filling action dialog");
  • TabularUnified src/Actions/Calculation.hpp

    r36053c raf5384  
    4242  virtual bool shouldUndo();
    4343
     44  virtual Action* clone(enum QueryOptions flag = Interactive) const=0;
     45
    4446  virtual void outputAsCLI(std::ostream &ost) const;
    4547  virtual void outputAsPython(std::ostream &ost, const std::string &prefix) const;
     
    5557   * Check if a cached result is available.
    5658   */
    57   virtual bool hasResult();
     59  virtual bool hasResult() const;
    5860
    5961  /**
     
    6163   * Fails if there is no cached result.
    6264   */
    63   virtual T getResult();
     65  virtual T getResult() const;
    6466
    6567  /**
  • TabularUnified src/Actions/Calculation_impl.hpp

    r36053c raf5384  
    8888
    8989template<typename T>
    90 bool Calculation<T>::hasResult(){
     90bool Calculation<T>::hasResult() const {
    9191  return done;
    9292}
    9393
    9494template<typename T>
    95 T Calculation<T>::getResult(){
     95T Calculation<T>::getResult() const {
    9696  assert(done && "No result calculated");
    9797  return *result;
  • TabularUnified src/Actions/MakroAction.cpp

    r36053c raf5384  
    102102
    103103ActionState::ptr MakroAction::performCall(){
    104 //  {
    105 //    // create and run our dialog and also of all members of the sequence
    106 //    Dialog* dialog = actions.fillAllDialogs(createDialog());
    107 //    if (dialog->hasQueries()) {
    108 //      if (!dialog->display())
    109 //        // dialog error or aborted -> throw exception
    110 //        throw ActionFailureException() << ActionNameString(getName());
    111 //    }
    112 //    delete(dialog);
    113 //  }
    114104  ActionSequence::stateSet states = actions.callAll(true);
    115105  return ActionState::ptr(new MakroActionState(states));
     
    138128}
    139129
     130Action* MakroAction::clone(enum QueryOptions flag) const
     131{
     132  if (flag == Interactive)
     133    return new MakroAction(Traits, actions);
     134  else
     135    return new MakroAction(*this);
     136}
     137
     138void MakroAction::prepare(enum QueryOptions flag)
     139{
     140  if (flag == Interactive) {
     141    // create and run our dialog and also of all members of the sequence
     142    Dialog* dialog = createDialog();
     143    if (dialog->hasQueries()) {
     144      if (!dialog->display())
     145        // dialog error or aborted -> throw exception
     146        throw ActionFailureException() << ActionNameString(getName());
     147    }
     148    delete(dialog);
     149  }
     150}
     151
    140152void MakroAction::outputAsCLI(std::ostream &ost) const
    141153{
  • TabularUnified src/Actions/MakroAction.hpp

    r36053c raf5384  
    3636  bool canUndo();
    3737  bool shouldUndo();
     38
     39  virtual Action* clone(enum QueryOptions flag = Interactive) const;
     40  void prepare(enum QueryOptions flag = Interactive);
    3841
    3942  virtual void outputAsCLI(std::ostream &ost) const;
  • TabularUnified src/Actions/MakroAction_impl_header.hpp

    r36053c raf5384  
    218218  bool shouldUndo();
    219219
     220  virtual Action* clone(enum QueryOptions flag = Interactive) const;
     221
    220222  void outputAsCLI(std::ostream &ost) const;
    221223  void outputAsPython(std::ostream &ost, const std::string &prefix) const;
  • TabularUnified src/Actions/MakroAction_impl_pre.hpp

    r36053c raf5384  
    291291    {}
    292292
     293// =========== clone Action ===========
     294Action* ACTION::clone(enum QueryOptions flag) const
     295{
     296  if (flag == Interactive)
     297    return new ACTION();
     298  else
     299    return new ACTION(*this);
     300}
     301
    293302// =========== fill a dialog ===========
    294303Dialog* ACTION::fillOwnDialog(Dialog *dialog) {
  • TabularUnified src/Actions/ManipulateAtomsProcess.cpp

    r36053c raf5384  
    103103  stop();
    104104}
     105
     106Action* ManipulateAtomsProcess::clone(enum QueryOptions flag) const
     107{
     108  if (flag == Interactive)
     109    return new ManipulateAtomsProcess(operation, descr, Traits);
     110  else
     111    return new ManipulateAtomsProcess(*this);
     112}
  • TabularUnified src/Actions/ManipulateAtomsProcess.hpp

    r36053c raf5384  
    3838  virtual bool shouldUndo();
    3939
     40  Action* clone(enum QueryOptions flag = Interactive) const;
     41
    4042  void outputAsCLI(std::ostream &ost) const;
    4143  void outputAsPython(std::ostream &ost, const std::string &prefix) const;
     
    4749   *
    4850   */
    49   void call(enum QueryOptions state = Interactive)
    50   { Process::call(state); }
     51  void call()
     52  { Process::call(); }
    5153
    5254protected:
  • TabularUnified src/Actions/MethodAction.cpp

    r36053c raf5384  
    8383}
    8484
     85Action* MethodAction::clone(enum QueryOptions flag) const
     86{
     87  if (flag == Interactive)
     88    return new MethodAction(Traits, executeMethod);
     89  else
     90    return new MethodAction(*this);
     91}
     92
    8593void MethodAction::outputAsCLI(std::ostream &ost) const
    8694{}
  • TabularUnified src/Actions/MethodAction.hpp

    r36053c raf5384  
    3434  virtual bool shouldUndo();
    3535
     36  virtual Action* clone(enum QueryOptions flag = Interactive) const;
     37
    3638  void outputAsCLI(std::ostream &ost) const;
    3739  void outputAsPython(std::ostream &ost, const std::string &prefix) const;
  • TabularUnified src/Actions/Reaction.hpp

    r36053c raf5384  
    3939   */
    4040  virtual bool canUndo();
     41  virtual bool shouldUndo();
    4142
    42   virtual bool shouldUndo();
     43  virtual Action* clone(enum QueryOptions flag = Interactive) const=0;
    4344
    4445  virtual void outputAsCLI(std::ostream &ost) const;
  • TabularUnified src/Actions/Reaction_impl_header.hpp

    r36053c raf5384  
    206206  virtual ~REACTION();
    207207
     208  Action* clone(enum QueryOptions flag = Interactive) const;
     209
    208210  void outputAsCLI(std::ostream &ost) const;
    209211  void outputAsPython(std::ostream &ost, const std::string &prefix) const;
  • TabularUnified src/Actions/Reaction_impl_pre.hpp

    r36053c raf5384  
    200200}
    201201
     202// =========== clone Action ===========
     203Action* REACTION::clone(enum QueryOptions flag) const
     204{
     205  if (flag == Interactive)
     206    return new REACTION();
     207  else
     208    return new REACTION(*this);
     209}
     210
    202211// =========== fill a dialog ===========
    203212Dialog* REACTION::fillDialog(Dialog *dialog) {
     
    267276#endif
    268277  ActionQueue::getInstance().queueAction( ToCall, Action::NonInteractive);
    269   Reaction< RETURNTYPE > * resultaction = static_cast<Reaction< RETURNTYPE > *>(ToCall);
    270   ASSERT( resultaction != NULL,
    271       "  COMMAND - static cast to Reaction type failed.");
    272   ASSERT( resultaction->hasResult (),
    273       " _COMMAND - result is not done yet?");
    274   return resultaction->getResult();
     278  RETURNTYPE result;
     279  try {
     280    const Reaction< RETURNTYPE > & resultaction =
     281        static_cast<const Reaction< RETURNTYPE > &>(ActionQueue::getInstance().getLastAction());
     282    ASSERT( resultaction.hasResult (),
     283        " _COMMAND - result is not done yet?");
     284    result = resultaction.getResult();
     285  } ASSERT_NOCATCH("  COMMAND - static cast to Reaction type failed.");
     286  return result;
    275287};
    276288
     
    290302#endif
    291303  ActionQueue::getInstance().queueAction( ToCall, Action::NonInteractive);
    292   Reaction< RETURNTYPE > * resultaction = static_cast<Reaction< RETURNTYPE > *>(ToCall);
    293   ASSERT( resultaction != NULL,
    294       "  COMMAND - static cast to Reaction type failed.");
    295   ASSERT( resultaction->hasResult (),
    296       " _COMMAND - result is not done yet?");
    297   return resultaction->getResult();
     304  RETURNTYPE result;
     305  try {
     306    const Reaction< RETURNTYPE > & resultaction =
     307        static_cast<const Reaction< RETURNTYPE > &>(ActionQueue::getInstance().getLastAction());
     308    ASSERT( resultaction.hasResult (),
     309        " _COMMAND - result is not done yet?");
     310    result = resultaction.getResult();
     311  } ASSERT_NOCATCH("  COMMAND - static cast to Reaction type failed.");
     312  return result;
    298313};
    299314
  • TabularUnified src/Actions/unittests/ActionSequenceUnitTest.cpp

    r36053c raf5384  
    8585    return true;
    8686  }
     87  Action* clone(enum QueryOptions flag = Interactive) const
     88  {
     89    return new canUndoActionStub(Traits);
     90  }
     91  void prepare(enum QueryOptions flag = Interactive)
     92  {}
    8793  void outputAsCLI(std::ostream &ost) const
    8894  {}
     
    118124   return true;
    119125  }
     126  Action* clone(enum QueryOptions flag = Interactive) const
     127  {
     128    return new cannotUndoActionStub(Traits);
     129  }
     130  void prepare(enum QueryOptions flag = Interactive)
     131  {}
    120132  void outputAsCLI(std::ostream &ost) const
    121133  {}
     
    154166    return true;
    155167  }
     168  Action* clone(enum QueryOptions flag = Interactive) const
     169  {
     170    return new wasCalledActionStub(Traits);
     171  }
     172  void prepare(enum QueryOptions flag = Interactive)
     173  {}
    156174  void outputAsCLI(std::ostream &ost) const
    157175  {}
  • TabularUnified src/UIElements/CommandLineUI/CommandLineWindow.cpp

    r36053c raf5384  
    6969
    7070  // go through all possible actions
    71   LOG(0, "Calling Actions ... ");
    7271  for (std::list<std::string>::iterator CommandRunner = CommandLineParser::getInstance().SequenceOfActions.begin(); CommandRunner != CommandLineParser::getInstance().SequenceOfActions.end(); ++CommandRunner) {
    7372    if (AQ.isActionKnownByName(*CommandRunner)) {
    7473//      LOG1, "INFO: Checking presence of " << *CommandRunner << ": " << "queuing.");
    75       LOG(1, "INFO: Queueing " << *CommandRunner << "...");
    7674      AQ.queueAction(*CommandRunner);
    7775    } else {
  • TabularUnified src/UIElements/Menu/TextMenu/TxMenuLeaveAction.cpp

    r36053c raf5384  
    7171}
    7272
     73Action* TxMenu::LeaveAction::clone(enum QueryOptions flag) const
     74{
     75  if (flag == Interactive)
     76    return new TxMenu::LeaveAction(menu, Traits);
     77  else
     78    return new TxMenu::LeaveAction(*this);
     79}
     80
    7381void TxMenu::LeaveAction::outputAsCLI(std::ostream &ost) const
    7482{}
  • TabularUnified src/UIElements/Menu/TextMenu/TxMenuLeaveAction.hpp

    r36053c raf5384  
    3333  bool shouldUndo();
    3434
     35  Action* clone(enum QueryOptions flag = Interactive) const;
     36
    3537  void outputAsCLI(std::ostream &ost) const;
    3638  void outputAsPython(std::ostream &ost, const std::string &prefix) const;
Note: See TracChangeset for help on using the changeset viewer.