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