| [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 | 
 | 
|---|
| [cc04b7] | 41 | using namespace std;
 | 
|---|
 | 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()
 | 
|---|
| [ef81b0] | 102 | {}
 | 
|---|
| [cc04b7] | 103 | 
 | 
|---|
| [13799e] | 104 | const string Action::getName() const
 | 
|---|
 | 105 | {
 | 
|---|
| [e4afb4] | 106 |   return Traits.getName();
 | 
|---|
| [cc04b7] | 107 | }
 | 
|---|
| [67e2b3] | 108 | 
 | 
|---|
| [e4b2f6] | 109 | const std::string Action::help() const
 | 
|---|
 | 110 | {
 | 
|---|
 | 111 |   std::stringstream outputstream;
 | 
|---|
 | 112 |   outputstream << "Description for Action '" << getName() << "': " << Traits.getDescription()
 | 
|---|
 | 113 |       << std::endl;
 | 
|---|
 | 114 |   if (!Traits.hasOption(getName())) {
 | 
|---|
 | 115 |     outputstream << "\tNote that this Action does not take an argument." << std::endl;
 | 
|---|
 | 116 |   }
 | 
|---|
 | 117 |   outputstream << "Options available for action '" << getName() << "':" << std::endl;
 | 
|---|
 | 118 |   for (ActionTraits::options_const_iterator iter = Traits.getBeginIter();
 | 
|---|
 | 119 |       iter != Traits.getEndIter();
 | 
|---|
 | 120 |       ++iter) {
 | 
|---|
 | 121 |     outputstream << "Option '" << iter->first << "':" << std::endl;
 | 
|---|
 | 122 |     outputstream << "\tDescription: " << iter->second->getDescription() << "." << std::endl;
 | 
|---|
 | 123 |     outputstream << "\tArgument's type: " << iter->second->getTypeName() << "." << std::endl;
 | 
|---|
 | 124 |     outputstream << "\tDefault value: ";
 | 
|---|
 | 125 |     if (iter->second->hasDefaultValue()) {
 | 
|---|
 | 126 |       outputstream << "Yes, is '" << iter->second->getDefaultValue() << "'";
 | 
|---|
 | 127 |     } else {
 | 
|---|
 | 128 |       outputstream << "None";
 | 
|---|
 | 129 |     }
 | 
|---|
 | 130 |     outputstream << "." << std::endl;
 | 
|---|
 | 131 |   }
 | 
|---|
 | 132 | 
 | 
|---|
 | 133 |   return outputstream.str();
 | 
|---|
 | 134 | }
 | 
|---|
 | 135 | 
 | 
|---|
| [41449c] | 136 | void Action::removeStaticStateEntities()
 | 
|---|
 | 137 | {
 | 
|---|
 | 138 |   success.reset();
 | 
|---|
 | 139 |   failure.reset();
 | 
|---|
 | 140 | }
 | 
|---|
 | 141 | 
 | 
|---|
| [047878] | 142 | Dialog * Action::createDialog(){
 | 
|---|
 | 143 |   Dialog *dialog = UIFactory::getInstance().makeDialog();
 | 
|---|
 | 144 |   return fillDialog(dialog);
 | 
|---|
 | 145 | }
 | 
|---|
 | 146 | 
 | 
|---|
| [4e145c] | 147 | void Action::call(enum QueryOptions flag){
 | 
|---|
| [2efa90] | 148 |   if(!isActive()){
 | 
|---|
 | 149 |     return;
 | 
|---|
 | 150 |   }
 | 
|---|
| [67e2b3] | 151 |   // forward to private virtual
 | 
|---|
| [4e145c] | 152 |   if (flag == Interactive) {
 | 
|---|
 | 153 |     Dialog* dialog = createDialog();
 | 
|---|
| [031f62] | 154 |     if (dialog->hasQueries()) {
 | 
|---|
| [4e145c] | 155 |       dialog->display();
 | 
|---|
 | 156 |     }
 | 
|---|
| [031f62] | 157 |     delete(dialog);
 | 
|---|
| [4e145c] | 158 |   }
 | 
|---|
 | 159 |   state_ptr state = Action::failure;
 | 
|---|
| [e2f3114] | 160 | //  try {
 | 
|---|
| [4e145c] | 161 |     state = performCall();
 | 
|---|
| [e2f3114] | 162 | //  } catch(MissingValueException&) {
 | 
|---|
 | 163 | //    DoeLog(0) && (eLog() << Verbose(0) << "There is a value missing for action " << getName() << endl);
 | 
|---|
 | 164 | //  };
 | 
|---|
| [4e145c] | 165 | 
 | 
|---|
| [d56640] | 166 |   if(shouldUndo() && state != failure){
 | 
|---|
 | 167 |     if(canUndo()){
 | 
|---|
 | 168 |       ActionHistory::getInstance().addElement(this,state);
 | 
|---|
 | 169 |     }
 | 
|---|
 | 170 |     else{
 | 
|---|
 | 171 |       ActionHistory::getInstance().clear();
 | 
|---|
 | 172 |     }
 | 
|---|
 | 173 |   }
 | 
|---|
| [e69c87] | 174 | 
 | 
|---|
 | 175 |   // throw an exception that can be caught in case of failure
 | 
|---|
 | 176 |   if (state == Action::failure)
 | 
|---|
 | 177 |     throw ActionFailureException() << ActionNameString(getName());
 | 
|---|
| [67e2b3] | 178 | }
 | 
|---|
| [5b0b98] | 179 | Action::state_ptr Action::undo(state_ptr _state) {
 | 
|---|
| [67e2b3] | 180 |   // forward to private virtual
 | 
|---|
 | 181 |   return performUndo(_state);
 | 
|---|
 | 182 | }
 | 
|---|
| [5b0b98] | 183 | Action::state_ptr Action::redo(state_ptr _state) {
 | 
|---|
| [67e2b3] | 184 |   // forward to private virtual
 | 
|---|
 | 185 |   return performRedo(_state);
 | 
|---|
 | 186 | }
 | 
|---|
| [f9352d] | 187 | 
 | 
|---|
 | 188 | 
 | 
|---|
 | 189 | bool Action::isActive(){
 | 
|---|
 | 190 |   return true;
 | 
|---|
 | 191 | }
 | 
|---|
| [df32ee] | 192 | 
 | 
|---|