| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2011 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | /*
 | 
|---|
| 9 |  * Action.cpp
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  *  Created on: Dec 8, 2009
 | 
|---|
| 12 |  *      Author: crueger
 | 
|---|
| 13 |  */
 | 
|---|
| 14 | 
 | 
|---|
| 15 | // include config.h
 | 
|---|
| 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 17 | #include <config.h>
 | 
|---|
| 18 | #endif
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include <iostream>
 | 
|---|
| 23 | #include <sstream>
 | 
|---|
| 24 | #include <string>
 | 
|---|
| 25 | 
 | 
|---|
| 26 | #include "Actions/Action.hpp"
 | 
|---|
| 27 | #include "Actions/ActionExceptions.hpp"
 | 
|---|
| 28 | #include "Actions/ActionRegistry.hpp"
 | 
|---|
| 29 | #include "Actions/ActionHistory.hpp"
 | 
|---|
| 30 | #include "Actions/OptionRegistry.hpp"
 | 
|---|
| 31 | #include "Actions/OptionTrait.hpp"
 | 
|---|
| 32 | //#include "Actions/ValueStorage.hpp"
 | 
|---|
| 33 | #include "UIElements/Dialog.hpp"
 | 
|---|
| 34 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 35 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 36 | #include "UIElements/UIFactory.hpp"
 | 
|---|
| 37 | 
 | 
|---|
| 38 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 39 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
| 40 | 
 | 
|---|
| 41 | using namespace MoleCuilder;
 | 
|---|
| 42 | 
 | 
|---|
| 43 | Action::state_ptr getEmptyState() {
 | 
|---|
| 44 |   return Action::state_ptr(Memory::ignore(new ActionState()));
 | 
|---|
| 45 | }
 | 
|---|
| 46 | 
 | 
|---|
| 47 | void Action::removeStaticStateEntities()
 | 
|---|
| 48 | {
 | 
|---|
| 49 |   Action::success.reset();
 | 
|---|
| 50 |   Action::failure.reset();
 | 
|---|
| 51 | }
 | 
|---|
| 52 | 
 | 
|---|
| 53 | void 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
 | 
|---|
| 60 | Action::state_ptr Action::success;
 | 
|---|
| 61 | Action::state_ptr Action::failure;
 | 
|---|
| 62 | 
 | 
|---|
| 63 | Action::Action(const ActionTrait &_Traits, bool _doRegister) :
 | 
|---|
| 64 |     Traits(_Traits)
 | 
|---|
| 65 | {
 | 
|---|
| 66 |   // register with ActionRegistry
 | 
|---|
| 67 |   if(_doRegister){
 | 
|---|
| 68 |     ActionRegistry::getInstance().registerInstance(this);
 | 
|---|
| 69 |   }
 | 
|---|
| 70 | 
 | 
|---|
| 71 |   // register with OptionRegistry
 | 
|---|
| 72 |   for (ActionTrait::options_const_iterator optioniter = Traits.getBeginIter();
 | 
|---|
| 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
 | 
|---|
| 79 | #ifndef NDEBUG
 | 
|---|
| 80 |       OptionTrait const * const PresentOption = OptionRegistry::getInstance().getOptionByName(optioniter->first);
 | 
|---|
| 81 | #endif
 | 
|---|
| 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())+
 | 
|---|
| 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()
 | 
|---|
| 107 |           )
 | 
|---|
| 108 |       );
 | 
|---|
| 109 |     }
 | 
|---|
| 110 |   }
 | 
|---|
| 111 | }
 | 
|---|
| 112 | 
 | 
|---|
| 113 | Action::~Action()
 | 
|---|
| 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 | 
 | 
|---|
| 120 |   for (ActionTrait::options_const_iterator optioniter = Traits.getBeginIter();
 | 
|---|
| 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 | }
 | 
|---|
| 130 | 
 | 
|---|
| 131 | const string Action::getName() const
 | 
|---|
| 132 | {
 | 
|---|
| 133 |   return Traits.getName();
 | 
|---|
| 134 | }
 | 
|---|
| 135 | 
 | 
|---|
| 136 | const 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;
 | 
|---|
| 145 |   for (ActionTrait::options_const_iterator iter = Traits.getBeginIter();
 | 
|---|
| 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 | 
 | 
|---|
| 163 | Dialog * Action::createDialog(){
 | 
|---|
| 164 |   Dialog *dialog = UIFactory::getInstance().makeDialog();
 | 
|---|
| 165 |   return fillDialog(dialog);
 | 
|---|
| 166 | }
 | 
|---|
| 167 | 
 | 
|---|
| 168 | void Action::call(enum QueryOptions flag){
 | 
|---|
| 169 |   if(!isActive()){
 | 
|---|
| 170 |     return;
 | 
|---|
| 171 |   }
 | 
|---|
| 172 |   // forward to private virtual
 | 
|---|
| 173 |   if (flag == Interactive) {
 | 
|---|
| 174 |     Dialog* dialog = createDialog();
 | 
|---|
| 175 |     if (dialog->hasQueries()) {
 | 
|---|
| 176 |       dialog->display();
 | 
|---|
| 177 |     }
 | 
|---|
| 178 |     delete(dialog);
 | 
|---|
| 179 |   }
 | 
|---|
| 180 |   state_ptr state = Action::failure;
 | 
|---|
| 181 | //  try {
 | 
|---|
| 182 |     startTimer();
 | 
|---|
| 183 |     state = performCall();
 | 
|---|
| 184 |     endTimer();
 | 
|---|
| 185 | //  } catch(MissingValueException&) {
 | 
|---|
| 186 | //    ELOG(0, "There is a value missing for action " << getName());
 | 
|---|
| 187 | //  };
 | 
|---|
| 188 | 
 | 
|---|
| 189 |   if(shouldUndo() && state != failure){
 | 
|---|
| 190 |     if(canUndo()){
 | 
|---|
| 191 |       ActionHistory::getInstance().addElement(this,state);
 | 
|---|
| 192 |     }
 | 
|---|
| 193 |     else{
 | 
|---|
| 194 |       ActionHistory::getInstance().clear();
 | 
|---|
| 195 |     }
 | 
|---|
| 196 |   }
 | 
|---|
| 197 | 
 | 
|---|
| 198 |   // throw an exception that can be caught in case of failure
 | 
|---|
| 199 |   if (state == Action::failure)
 | 
|---|
| 200 |     throw ActionFailureException() << ActionNameString(getName());
 | 
|---|
| 201 | }
 | 
|---|
| 202 | Action::state_ptr Action::undo(state_ptr _state) {
 | 
|---|
| 203 |   // forward to private virtual
 | 
|---|
| 204 |   return performUndo(_state);
 | 
|---|
| 205 | }
 | 
|---|
| 206 | Action::state_ptr Action::redo(state_ptr _state) {
 | 
|---|
| 207 |   // forward to private virtual
 | 
|---|
| 208 |   return performRedo(_state);
 | 
|---|
| 209 | }
 | 
|---|
| 210 | 
 | 
|---|
| 211 | 
 | 
|---|
| 212 | bool Action::isActive(){
 | 
|---|
| 213 |   return true;
 | 
|---|
| 214 | }
 | 
|---|
| 215 | 
 | 
|---|