source: src/Actions/Action.cpp@ abae35

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
Last change on this file since abae35 was dfef3f, checked in by Frederik Heber <heber@…>, 13 years ago

FIX: empty states Action::success and ::failure are now managed by ActionHistory.

  • the python function reinit() runs into trouble because the static entities Action::success and ::failure are missing after a reinit() causing a std::exception to be thrown. Now we manage the static state instances via ActionHistory cstor and dstor. As ActionHistory::init() must be called before any action is called, this should always work well.
  • added static function Action::createStaticStateEntities().
  • Property mode set to 100644
File size: 6.5 KB
RevLine 
[bcf653]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010 University of Bonn. All rights reserved.
5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
[65b6e0]8/*
9 * Action.cpp
10 *
11 * Created on: Dec 8, 2009
12 * Author: crueger
13 */
14
[bf3817]15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
[ad011c]20#include "CodePatterns/MemDebug.hpp"
[112b09]21
[df32ee]22#include <iostream>
[e4b2f6]23#include <sstream>
[cc04b7]24#include <string>
[65b6e0]25
[cc04b7]26#include "Actions/Action.hpp"
[e69c87]27#include "Actions/ActionExceptions.hpp"
[cc04b7]28#include "Actions/ActionRegistry.hpp"
[d56640]29#include "Actions/ActionHistory.hpp"
[e4afb4]30#include "Actions/OptionRegistry.hpp"
31#include "Actions/OptionTrait.hpp"
[336e33]32//#include "Actions/ValueStorage.hpp"
[4e145c]33#include "UIElements/Dialog.hpp"
[ad011c]34#include "CodePatterns/Assert.hpp"
35#include "CodePatterns/MemDebug.hpp"
[047878]36#include "UIElements/UIFactory.hpp"
[cc04b7]37
[ad011c]38#include "CodePatterns/Log.hpp"
39#include "CodePatterns/Verbose.hpp"
[4e145c]40
[ce7fdc]41using namespace MoleCuilder;
[cc04b7]42
[6d6b54]43Action::state_ptr getEmptyState() {
44 return Action::state_ptr(Memory::ignore(new ActionState()));
45}
46
[dfef3f]47void Action::removeStaticStateEntities()
48{
49 Action::success.reset();
50 Action::failure.reset();
51}
52
53void Action::createStaticStateEntities()
54{
55 Action::success = getEmptyState();
56 Action::failure = getEmptyState();
57}
58
59// An empty state to indicate success, these are (de)initialized by ActionHistory
60Action::state_ptr Action::success;
61Action::state_ptr Action::failure;
[67e2b3]62
[3139b2]63Action::Action(const ActionTrait &_Traits, bool _doRegister) :
[e4afb4]64 Traits(_Traits)
[cc04b7]65{
[e4afb4]66 // register with ActionRegistry
[cc04b7]67 if(_doRegister){
[b2d8d0]68 ActionRegistry::getInstance().registerInstance(this);
[cc04b7]69 }
[e4afb4]70
71 // register with OptionRegistry
[3139b2]72 for (ActionTrait::options_const_iterator optioniter = Traits.getBeginIter();
[e4afb4]73 optioniter != Traits.getEndIter();
74 ++optioniter) {
75 // options may have been re-used by other Actions, so check beforehand whether adding is needed
76 if (!OptionRegistry::getInstance().isOptionPresentByName((optioniter->first))) {
77 OptionRegistry::getInstance().registerInstance(optioniter->second);
78 } else { // if present, ASSERT that types coincide
[b2ae3b]79#ifndef NDEBUG
[e4afb4]80 OptionTrait const * const PresentOption = OptionRegistry::getInstance().getOptionByName(optioniter->first);
[b2ae3b]81#endif
[e4afb4]82 ASSERT(PresentOption->getType() == optioniter->second->getType(),
83 ("Action::Action() - option to add "+
84 std::string(optioniter->first)+
85 " of Action "+
86 std::string(getName())+
[23d7ff]87 " is already present with different type: "
88 +toString(PresentOption->getType())+" != "+toString(optioniter->second->getType())
89 )
90 );
91 ASSERT(PresentOption->getDefaultValue() == optioniter->second->getDefaultValue(),
92 ("Action::Action() - option to add "+
93 std::string(optioniter->first)+
94 " of Action "+
95 std::string(getName())+
96 " is already present with different default value: "
97 +PresentOption->getDefaultValue()+" != "+optioniter->second->getDefaultValue()
98 )
99 );
100 ASSERT(PresentOption->getShortForm() == optioniter->second->getShortForm(),
101 ("Action::Action() - option to add "+
102 std::string(optioniter->first)+
103 " of Action "+
104 std::string(getName())+
105 " is already present with different short form: "
106 +PresentOption->getShortForm()+" != "+optioniter->second->getShortForm()
[e4afb4]107 )
108 );
109 }
110 }
[cc04b7]111}
[65b6e0]112
113Action::~Action()
[d44d7b]114{
115 // unregister us if still registered
116 if(ActionRegistry::getInstance().isActionPresentByName(getName()))
117 if (ActionRegistry::getInstance().getActionByName(getName()) == this)
118 ActionRegistry::getInstance().unregisterInstance(this);
119
[3139b2]120 for (ActionTrait::options_const_iterator optioniter = Traits.getBeginIter();
[d44d7b]121 optioniter != Traits.getEndIter();
122 ++optioniter) {
123 // unregister option if still registered
124 if(OptionRegistry::getInstance().isOptionPresentByName((optioniter->first)))
125 if (OptionRegistry::getInstance().getOptionByName((optioniter->first)) == optioniter->second) {
126 OptionRegistry::getInstance().unregisterInstance(optioniter->second);
127 }
128 }
129}
[cc04b7]130
[13799e]131const string Action::getName() const
132{
[e4afb4]133 return Traits.getName();
[cc04b7]134}
[67e2b3]135
[e4b2f6]136const std::string Action::help() const
137{
138 std::stringstream outputstream;
139 outputstream << "Description for Action '" << getName() << "': " << Traits.getDescription()
140 << std::endl;
141 if (!Traits.hasOption(getName())) {
142 outputstream << "\tNote that this Action does not take an argument." << std::endl;
143 }
144 outputstream << "Options available for action '" << getName() << "':" << std::endl;
[3139b2]145 for (ActionTrait::options_const_iterator iter = Traits.getBeginIter();
[e4b2f6]146 iter != Traits.getEndIter();
147 ++iter) {
148 outputstream << "Option '" << iter->first << "':" << std::endl;
149 outputstream << "\tDescription: " << iter->second->getDescription() << "." << std::endl;
150 outputstream << "\tArgument's type: " << iter->second->getTypeName() << "." << std::endl;
151 outputstream << "\tDefault value: ";
152 if (iter->second->hasDefaultValue()) {
153 outputstream << "Yes, is '" << iter->second->getDefaultValue() << "'";
154 } else {
155 outputstream << "None";
156 }
157 outputstream << "." << std::endl;
158 }
159
160 return outputstream.str();
161}
162
[047878]163Dialog * Action::createDialog(){
164 Dialog *dialog = UIFactory::getInstance().makeDialog();
165 return fillDialog(dialog);
166}
167
[4e145c]168void Action::call(enum QueryOptions flag){
[2efa90]169 if(!isActive()){
170 return;
171 }
[67e2b3]172 // forward to private virtual
[4e145c]173 if (flag == Interactive) {
174 Dialog* dialog = createDialog();
[031f62]175 if (dialog->hasQueries()) {
[4e145c]176 dialog->display();
177 }
[031f62]178 delete(dialog);
[4e145c]179 }
180 state_ptr state = Action::failure;
[e2f3114]181// try {
[862b6a]182 startTimer();
[4e145c]183 state = performCall();
[862b6a]184 endTimer();
[e2f3114]185// } catch(MissingValueException&) {
[47d041]186// ELOG(0, "There is a value missing for action " << getName());
[e2f3114]187// };
[4e145c]188
[d56640]189 if(shouldUndo() && state != failure){
190 if(canUndo()){
191 ActionHistory::getInstance().addElement(this,state);
192 }
193 else{
194 ActionHistory::getInstance().clear();
195 }
196 }
[e69c87]197
198 // throw an exception that can be caught in case of failure
199 if (state == Action::failure)
200 throw ActionFailureException() << ActionNameString(getName());
[67e2b3]201}
[5b0b98]202Action::state_ptr Action::undo(state_ptr _state) {
[67e2b3]203 // forward to private virtual
204 return performUndo(_state);
205}
[5b0b98]206Action::state_ptr Action::redo(state_ptr _state) {
[67e2b3]207 // forward to private virtual
208 return performRedo(_state);
209}
[f9352d]210
211
212bool Action::isActive(){
213 return true;
214}
[df32ee]215
Note: See TracBrowser for help on using the repository browser.