[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] | 41 | using namespace MoleCuilder;
|
---|
[cc04b7] | 42 |
|
---|
[6d6b54] | 43 | Action::state_ptr getEmptyState() {
|
---|
| 44 | return Action::state_ptr(Memory::ignore(new ActionState()));
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[5b0b98] | 47 | // An empty state to indicate success
|
---|
[6d6b54] | 48 | Action::state_ptr Action::success = getEmptyState();
|
---|
| 49 | Action::state_ptr Action::failure = getEmptyState();
|
---|
[67e2b3] | 50 |
|
---|
[e4afb4] | 51 | Action::Action(const ActionTraits &_Traits, bool _doRegister) :
|
---|
| 52 | Traits(_Traits)
|
---|
[cc04b7] | 53 | {
|
---|
[e4afb4] | 54 | // register with ActionRegistry
|
---|
[cc04b7] | 55 | if(_doRegister){
|
---|
[b2d8d0] | 56 | ActionRegistry::getInstance().registerInstance(this);
|
---|
[cc04b7] | 57 | }
|
---|
[e4afb4] | 58 |
|
---|
| 59 | // register with OptionRegistry
|
---|
| 60 | for (ActionTraits::options_const_iterator optioniter = Traits.getBeginIter();
|
---|
| 61 | optioniter != Traits.getEndIter();
|
---|
| 62 | ++optioniter) {
|
---|
| 63 | // options may have been re-used by other Actions, so check beforehand whether adding is needed
|
---|
| 64 | if (!OptionRegistry::getInstance().isOptionPresentByName((optioniter->first))) {
|
---|
| 65 | OptionRegistry::getInstance().registerInstance(optioniter->second);
|
---|
| 66 | } else { // if present, ASSERT that types coincide
|
---|
[b2ae3b] | 67 | #ifndef NDEBUG
|
---|
[e4afb4] | 68 | OptionTrait const * const PresentOption = OptionRegistry::getInstance().getOptionByName(optioniter->first);
|
---|
[b2ae3b] | 69 | #endif
|
---|
[e4afb4] | 70 | ASSERT(PresentOption->getType() == optioniter->second->getType(),
|
---|
| 71 | ("Action::Action() - option to add "+
|
---|
| 72 | std::string(optioniter->first)+
|
---|
| 73 | " of Action "+
|
---|
| 74 | std::string(getName())+
|
---|
[23d7ff] | 75 | " is already present with different type: "
|
---|
| 76 | +toString(PresentOption->getType())+" != "+toString(optioniter->second->getType())
|
---|
| 77 | )
|
---|
| 78 | );
|
---|
| 79 | ASSERT(PresentOption->getDefaultValue() == optioniter->second->getDefaultValue(),
|
---|
| 80 | ("Action::Action() - option to add "+
|
---|
| 81 | std::string(optioniter->first)+
|
---|
| 82 | " of Action "+
|
---|
| 83 | std::string(getName())+
|
---|
| 84 | " is already present with different default value: "
|
---|
| 85 | +PresentOption->getDefaultValue()+" != "+optioniter->second->getDefaultValue()
|
---|
| 86 | )
|
---|
| 87 | );
|
---|
| 88 | ASSERT(PresentOption->getShortForm() == optioniter->second->getShortForm(),
|
---|
| 89 | ("Action::Action() - option to add "+
|
---|
| 90 | std::string(optioniter->first)+
|
---|
| 91 | " of Action "+
|
---|
| 92 | std::string(getName())+
|
---|
| 93 | " is already present with different short form: "
|
---|
| 94 | +PresentOption->getShortForm()+" != "+optioniter->second->getShortForm()
|
---|
[e4afb4] | 95 | )
|
---|
| 96 | );
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
[cc04b7] | 99 | }
|
---|
[65b6e0] | 100 |
|
---|
| 101 | Action::~Action()
|
---|
[d44d7b] | 102 | {
|
---|
| 103 | // unregister us if still registered
|
---|
| 104 | if(ActionRegistry::getInstance().isActionPresentByName(getName()))
|
---|
| 105 | if (ActionRegistry::getInstance().getActionByName(getName()) == this)
|
---|
| 106 | ActionRegistry::getInstance().unregisterInstance(this);
|
---|
| 107 |
|
---|
| 108 | for (ActionTraits::options_const_iterator optioniter = Traits.getBeginIter();
|
---|
| 109 | optioniter != Traits.getEndIter();
|
---|
| 110 | ++optioniter) {
|
---|
| 111 | // unregister option if still registered
|
---|
| 112 | if(OptionRegistry::getInstance().isOptionPresentByName((optioniter->first)))
|
---|
| 113 | if (OptionRegistry::getInstance().getOptionByName((optioniter->first)) == optioniter->second) {
|
---|
| 114 | OptionRegistry::getInstance().unregisterInstance(optioniter->second);
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
[cc04b7] | 118 |
|
---|
[13799e] | 119 | const string Action::getName() const
|
---|
| 120 | {
|
---|
[e4afb4] | 121 | return Traits.getName();
|
---|
[cc04b7] | 122 | }
|
---|
[67e2b3] | 123 |
|
---|
[e4b2f6] | 124 | const std::string Action::help() const
|
---|
| 125 | {
|
---|
| 126 | std::stringstream outputstream;
|
---|
| 127 | outputstream << "Description for Action '" << getName() << "': " << Traits.getDescription()
|
---|
| 128 | << std::endl;
|
---|
| 129 | if (!Traits.hasOption(getName())) {
|
---|
| 130 | outputstream << "\tNote that this Action does not take an argument." << std::endl;
|
---|
| 131 | }
|
---|
| 132 | outputstream << "Options available for action '" << getName() << "':" << std::endl;
|
---|
| 133 | for (ActionTraits::options_const_iterator iter = Traits.getBeginIter();
|
---|
| 134 | iter != Traits.getEndIter();
|
---|
| 135 | ++iter) {
|
---|
| 136 | outputstream << "Option '" << iter->first << "':" << std::endl;
|
---|
| 137 | outputstream << "\tDescription: " << iter->second->getDescription() << "." << std::endl;
|
---|
| 138 | outputstream << "\tArgument's type: " << iter->second->getTypeName() << "." << std::endl;
|
---|
| 139 | outputstream << "\tDefault value: ";
|
---|
| 140 | if (iter->second->hasDefaultValue()) {
|
---|
| 141 | outputstream << "Yes, is '" << iter->second->getDefaultValue() << "'";
|
---|
| 142 | } else {
|
---|
| 143 | outputstream << "None";
|
---|
| 144 | }
|
---|
| 145 | outputstream << "." << std::endl;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | return outputstream.str();
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[41449c] | 151 | void Action::removeStaticStateEntities()
|
---|
| 152 | {
|
---|
| 153 | success.reset();
|
---|
| 154 | failure.reset();
|
---|
| 155 | }
|
---|
| 156 |
|
---|
[047878] | 157 | Dialog * Action::createDialog(){
|
---|
| 158 | Dialog *dialog = UIFactory::getInstance().makeDialog();
|
---|
| 159 | return fillDialog(dialog);
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[4e145c] | 162 | void Action::call(enum QueryOptions flag){
|
---|
[2efa90] | 163 | if(!isActive()){
|
---|
| 164 | return;
|
---|
| 165 | }
|
---|
[67e2b3] | 166 | // forward to private virtual
|
---|
[4e145c] | 167 | if (flag == Interactive) {
|
---|
| 168 | Dialog* dialog = createDialog();
|
---|
[031f62] | 169 | if (dialog->hasQueries()) {
|
---|
[4e145c] | 170 | dialog->display();
|
---|
| 171 | }
|
---|
[031f62] | 172 | delete(dialog);
|
---|
[4e145c] | 173 | }
|
---|
| 174 | state_ptr state = Action::failure;
|
---|
[e2f3114] | 175 | // try {
|
---|
[862b6a] | 176 | startTimer();
|
---|
[4e145c] | 177 | state = performCall();
|
---|
[862b6a] | 178 | endTimer();
|
---|
[e2f3114] | 179 | // } catch(MissingValueException&) {
|
---|
[47d041] | 180 | // ELOG(0, "There is a value missing for action " << getName());
|
---|
[e2f3114] | 181 | // };
|
---|
[4e145c] | 182 |
|
---|
[d56640] | 183 | if(shouldUndo() && state != failure){
|
---|
| 184 | if(canUndo()){
|
---|
| 185 | ActionHistory::getInstance().addElement(this,state);
|
---|
| 186 | }
|
---|
| 187 | else{
|
---|
| 188 | ActionHistory::getInstance().clear();
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
[e69c87] | 191 |
|
---|
| 192 | // throw an exception that can be caught in case of failure
|
---|
| 193 | if (state == Action::failure)
|
---|
| 194 | throw ActionFailureException() << ActionNameString(getName());
|
---|
[67e2b3] | 195 | }
|
---|
[5b0b98] | 196 | Action::state_ptr Action::undo(state_ptr _state) {
|
---|
[67e2b3] | 197 | // forward to private virtual
|
---|
| 198 | return performUndo(_state);
|
---|
| 199 | }
|
---|
[5b0b98] | 200 | Action::state_ptr Action::redo(state_ptr _state) {
|
---|
[67e2b3] | 201 | // forward to private virtual
|
---|
| 202 | return performRedo(_state);
|
---|
| 203 | }
|
---|
[f9352d] | 204 |
|
---|
| 205 |
|
---|
| 206 | bool Action::isActive(){
|
---|
| 207 | return true;
|
---|
| 208 | }
|
---|
[df32ee] | 209 |
|
---|