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 |
|
---|
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 | // An empty state to indicate success
|
---|
48 | Action::state_ptr Action::success = getEmptyState();
|
---|
49 | Action::state_ptr Action::failure = getEmptyState();
|
---|
50 |
|
---|
51 | Action::Action(const ActionTraits &_Traits, bool _doRegister) :
|
---|
52 | Traits(_Traits)
|
---|
53 | {
|
---|
54 | // register with ActionRegistry
|
---|
55 | if(_doRegister){
|
---|
56 | ActionRegistry::getInstance().registerInstance(this);
|
---|
57 | }
|
---|
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
|
---|
67 | #ifndef NDEBUG
|
---|
68 | OptionTrait const * const PresentOption = OptionRegistry::getInstance().getOptionByName(optioniter->first);
|
---|
69 | #endif
|
---|
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())+
|
---|
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()
|
---|
95 | )
|
---|
96 | );
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | Action::~Action()
|
---|
102 | {}
|
---|
103 |
|
---|
104 | const string Action::getName() const
|
---|
105 | {
|
---|
106 | return Traits.getName();
|
---|
107 | }
|
---|
108 |
|
---|
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 |
|
---|
136 | void Action::removeStaticStateEntities()
|
---|
137 | {
|
---|
138 | success.reset();
|
---|
139 | failure.reset();
|
---|
140 | }
|
---|
141 |
|
---|
142 | Dialog * Action::createDialog(){
|
---|
143 | Dialog *dialog = UIFactory::getInstance().makeDialog();
|
---|
144 | return fillDialog(dialog);
|
---|
145 | }
|
---|
146 |
|
---|
147 | void Action::call(enum QueryOptions flag){
|
---|
148 | if(!isActive()){
|
---|
149 | return;
|
---|
150 | }
|
---|
151 | // forward to private virtual
|
---|
152 | if (flag == Interactive) {
|
---|
153 | Dialog* dialog = createDialog();
|
---|
154 | if (dialog->hasQueries()) {
|
---|
155 | dialog->display();
|
---|
156 | }
|
---|
157 | delete(dialog);
|
---|
158 | }
|
---|
159 | state_ptr state = Action::failure;
|
---|
160 | // try {
|
---|
161 | startTimer();
|
---|
162 | state = performCall();
|
---|
163 | endTimer();
|
---|
164 | // } catch(MissingValueException&) {
|
---|
165 | // ELOG(0, "There is a value missing for action " << getName());
|
---|
166 | // };
|
---|
167 |
|
---|
168 | if(shouldUndo() && state != failure){
|
---|
169 | if(canUndo()){
|
---|
170 | ActionHistory::getInstance().addElement(this,state);
|
---|
171 | }
|
---|
172 | else{
|
---|
173 | ActionHistory::getInstance().clear();
|
---|
174 | }
|
---|
175 | }
|
---|
176 |
|
---|
177 | // throw an exception that can be caught in case of failure
|
---|
178 | if (state == Action::failure)
|
---|
179 | throw ActionFailureException() << ActionNameString(getName());
|
---|
180 | }
|
---|
181 | Action::state_ptr Action::undo(state_ptr _state) {
|
---|
182 | // forward to private virtual
|
---|
183 | return performUndo(_state);
|
---|
184 | }
|
---|
185 | Action::state_ptr Action::redo(state_ptr _state) {
|
---|
186 | // forward to private virtual
|
---|
187 | return performRedo(_state);
|
---|
188 | }
|
---|
189 |
|
---|
190 |
|
---|
191 | bool Action::isActive(){
|
---|
192 | return true;
|
---|
193 | }
|
---|
194 |
|
---|