Ignore:
Timestamp:
Apr 8, 2013, 11:56:08 AM (12 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:
560cbf
Parents:
f2fda6
git-author:
Frederik Heber <heber@…> (03/01/13 19:32:41)
git-committer:
Frederik Heber <heber@…> (04/08/13 11:56:08)
Message:

Added Undo/Redo functionality to CreateAdjacencyAction.

  • also added regression test on whole functionality.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Actions/GraphAction/CreateAdjacencyAction.cpp

    rf2fda6 r055ad7  
    3535#include "CodePatterns/MemDebug.hpp"
    3636
     37#include "CodePatterns/Assert.hpp"
    3738#include "CodePatterns/Log.hpp"
    3839
     
    5051using namespace MoleCuilder;
    5152
     53// Storing undo state
     54struct BondInfo_t {
     55  atomId_t leftatom;
     56  atomId_t rightatom;
     57  size_t degree;
     58};
     59
    5260// and construct the stuff
    5361#include "CreateAdjacencyAction.def"
     
    5866  ASSERT(BG != NULL, "GraphCreateAdjacencyAction: BondGraph is NULL.");
    5967
     68  // count all present bonds
    6069  World::AtomComposite Set = World::getInstance().getAllAtoms(AtomsBySelection());
     70  std::vector<BondInfo_t> bonds;
     71  {
     72    size_t count_bonds = 0;
     73    for (World::AtomComposite::const_iterator iter = Set.begin();
     74        iter != Set.end();
     75        ++iter) {
     76      const atom * const Walker = *iter;
     77      count_bonds += Walker->getListOfBonds().size();
     78    }
     79    bonds.reserve(count_bonds/2);
     80  }
     81  // Storing undo info
     82  for (World::AtomComposite::const_iterator iter = Set.begin();
     83      iter != Set.end();
     84      ++iter) {
     85    const atom * const Walker = *iter;
     86    const BondList& ListOfBonds = Walker->getListOfBonds();
     87    for (BondList::const_iterator bonditer = ListOfBonds.begin();
     88        bonditer != ListOfBonds.end();
     89        ++bonditer) {
     90      const bond::ptr &CurrentBond = *bonditer;
     91      // if both atoms are in selected set, we check ids otherwise not
     92      if (((!World::getInstance().isSelected(CurrentBond->leftatom))
     93          || (!World::getInstance().isSelected(CurrentBond->rightatom)))
     94          || (CurrentBond->leftatom->getId() < CurrentBond->rightatom->getId())) {
     95        BondInfo_t BondInfo;
     96        BondInfo.leftatom = CurrentBond->leftatom->getId();
     97        BondInfo.rightatom = CurrentBond->rightatom->getId();
     98        BondInfo.degree = CurrentBond->BondDegree;
     99        bonds.push_back(BondInfo);
     100      }
     101    }
     102  }
     103  GraphCreateAdjacencyState *UndoState = new GraphCreateAdjacencyState(bonds, params);
     104
     105  // now recreate adjacency
    61106  BG->CreateAdjacency(Set);
    62107
     108  // give info
    63109  size_t BondCount = 0;
    64110  std::vector<molecule *> molecules = World::getInstance().getAllMolecules();
     
    68114  LOG(0, "STATUS: Recognized " << BondCount << " bonds.");
    69115
    70   return Action::success;
     116  return Action::state_ptr(UndoState);
    71117}
    72118
    73119Action::state_ptr GraphCreateAdjacencyAction::performUndo(Action::state_ptr _state) {
    74 //  GraphCreateAdjacencyState *state = assert_cast<GraphCreateAdjacencyState*>(_state.get());
     120  GraphCreateAdjacencyState *state = assert_cast<GraphCreateAdjacencyState*>(_state.get());
    75121
    76   return Action::success;
     122  BondGraph *BG = World::getInstance().getBondGraph();
     123  ASSERT(BG != NULL, "GraphCreateAdjacencyAction: BondGraph is NULL.");
     124
     125  // remove all bonds in the set
     126  World::AtomComposite Set = World::getInstance().getAllAtoms(AtomsBySelection());
     127  BG->cleanAdjacencyList(Set);
     128
     129  // recreate from stored info
     130  const size_t CurrentTime = WorldTime::getTime();
     131  std::vector<BondInfo_t> &bonds = state->bonds;
     132  for(std::vector<BondInfo_t>::const_iterator iter = bonds.begin();
     133      iter != bonds.end(); ++iter) {
     134    atom * const Walker = World::getInstance().getAtom(AtomById(iter->leftatom));
     135    ASSERT( Walker != NULL,
     136        "GraphCreateAdjacencyAction::performUndo() - "+toString(iter->leftatom)+" missing.");
     137    atom * const OtherWalker = World::getInstance().getAtom(AtomById(iter->rightatom));
     138    ASSERT( OtherWalker != NULL,
     139        "GraphCreateAdjacencyAction::performUndo() - "+toString(iter->rightatom)+" missing.");
     140    bond::ptr CurrentBond = Walker->addBond(CurrentTime, OtherWalker);
     141    CurrentBond->BondDegree = iter->degree;
     142  }
     143  return Action::state_ptr(_state);
    77144}
    78145
    79146Action::state_ptr GraphCreateAdjacencyAction::performRedo(Action::state_ptr _state){
    80   return Action::success;
     147  BondGraph *BG = World::getInstance().getBondGraph();
     148  ASSERT(BG != NULL, "GraphCreateAdjacencyAction: BondGraph is NULL.");
     149
     150  // now recreate adjacency
     151  World::AtomComposite Set = World::getInstance().getAllAtoms(AtomsBySelection());
     152  BG->CreateAdjacency(Set);
     153
     154  return Action::state_ptr(_state);
    81155}
    82156
    83157bool GraphCreateAdjacencyAction::canUndo() {
    84   return false;
     158  return true;
    85159}
    86160
    87161bool GraphCreateAdjacencyAction::shouldUndo() {
    88   return false;
     162  return true;
    89163}
    90164/** =========== end of function ====================== */
Note: See TracChangeset for help on using the changeset viewer.