charon-core  0.3.1
ParameteredGroupObject.cpp
1 /* This file is part of Charon.
2 
3  Charon is free software: you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as published by
5  the Free Software Foundation, either version 3 of the License, or
6  (at your option) any later version.
7 
8  Charon is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU Lesser General Public License for more details.
12 
13  You should have received a copy of the GNU Lesser General Public License
14  along with Charon. If not, see <http://www.gnu.org/licenses/>.
15 */
16 #include "../include/charon-core/ParameteredGroupObject.h"
17 #include "../include/charon-core/PluginManager.h"
18 
19 
20 ParameteredGroupObject::ParameteredGroupObject(const std::string &className, const std::string &name, const std::string &doc)
21  :ParameteredObject(className,name,doc)
22 {
23  _addParameter(pluginPaths,"pluginPaths","The paths where the plugins are stored.","string");
24  _addParameter<bool>(debugSuffix,"debugSuffix","Load debug plugins suffixed with '_d'",1);
25  _addParameter(workFlowFile,"workflowfile","The workflow contained in this group","FileOpen");
26  _pluginMan=0;
27  _inputs=0;
28  _outputs=0;
29  _setDynamic(true);
30 }
31 
33 {
34 }
35 
37 {
39 
40  if(_inputs)
41  {
42  std::vector<VirtualInputSlot*> vinput=_inputs->getSlotVector();
43  for(size_t i=0;i<vinput.size();i++)
44  {
45  VirtualInputSlot* in=vinput[i];
47  Parameter<int> *par=_loopOutputNumber[i];
48  _removeSomething("parameters",par->getName());
49  delete par;
50  breakLoop(i);
51  }
52  }
53  _loopOutputNumber.clear();
54 
55  if(_outputs)
56  {
57  std::vector<VirtualOutputSlot*> voutput=_outputs->getSlotVector();
58  for(size_t i=0;i<voutput.size();i++)
59  {
60  VirtualOutputSlot* out=voutput[i];
61  _removeOutputSlot(out->getName());
62  }
63  }
64 
65  _inputs=0;
66  _outputs=0;
67  if (_pluginMan!=0) {
68  _pluginMan->reset();
69  delete _pluginMan;
70  _pluginMan = 0;
71  }
72 
73  _pluginMan = new PluginManager(pluginPaths(),debugSuffix());
74  _pluginMan->loadParameterFile(workFlowFile());
75  const std::map<std::string, ParameteredObject*>& objs=_pluginMan->getObjectList();
76 
77  std::map<std::string, ParameteredObject *>::const_iterator it=objs.begin();
78  for(;it!=objs.end();it++)
79  {
80  ParameteredObject* obj=it->second;
81  InputSlotBundleIntf* tinputs=dynamic_cast<InputSlotBundleIntf*>(obj);
82  OutputSlotBundleIntf* toutputs=dynamic_cast<OutputSlotBundleIntf*>(obj);
83  if(tinputs)
84  _inputs=tinputs;
85  if(toutputs)
86  _outputs=toutputs;
87  }
88 
89  if(_inputs)
90  {
91  std::vector<VirtualInputSlot*> vinput=_inputs->getSlotVector();
92  for(size_t i=0;i<vinput.size();i++)
93  {
94  VirtualInputSlot* in=vinput[i];
95  onAddInputSlot(in);
96  Parameter<int> *par=new Parameter<int>(-1);
97  std::stringstream pname;
98  pname<<"loop_input_"<<i<<"_to_output";
99  std::stringstream pdoc;
100  pdoc<<"The input "<<i<<" gets the data of the given output after one iteration of a loop. To disable the connection, set to -1";
101  _addParameter(*par,pname.str(),pdoc.str(),"int");
102  _loopOutputNumber.push_back(par);
103  }
104  }
105  if(_outputs)
106  {
107  std::vector<VirtualOutputSlot*> voutput=_outputs->getSlotVector();
108  for(size_t i=0;i<voutput.size();i++)
109  {
110  VirtualOutputSlot* out=voutput[i];
111  onAddOutputSlot(out);
112  }
113  }
114 
115  initializeGroup();
116 
117  #pragma message ("need to load slotbundle connections")
118  //_inputs->loadConnection(ParameterFile(workFlowFile),_pluginMan);
119  //_outputs->loadConnection(ParameterFile(workFlowFile),_pluginMan);
120 }
121 
123 {
124  // Does nothing!
125 }
126 
128 {
129  executeGroup();
130 }
131 
133 {
134 
135  finalizeGroup();
136  if (_pluginMan) {
137  _pluginMan->reset();
138  delete _pluginMan;
139  _pluginMan=0;
140  }
142 }
143 
145 {
146  // Does nothing!
147 }
148 
150  if(_pluginMan!=0) {
151  _pluginMan->runWorkflow();
152  }
153 }
154 
155 
156 
157 
158 
159 
160 
161 
162 
163 
164 
165 
166 
167 
168 
169 
171 {
172  _pluginMan->saveParameterFile(workFlowFile());
173 }
174 
175 
176 
177 void ParameteredGroupObject::loopInputToOutput(int input, int output)
178 {
179  VirtualOutputSlot* out=_inputs->getInternalSlotVector()[input];
180  VirtualInputSlot* in=_outputs->getInternalSlotVector()[output];
181  out->setLoopPartner(in);
182  out->setLoop(false);
183  _loopedSlots.insert(std::pair<int,VirtualOutputSlot*>(input,out));
184 }
185 
187 {
188  std::map<int,VirtualOutputSlot*>::iterator it;
189  it=_loopedSlots.find(input);
190  if(it==_loopedSlots.end())
191  sout<<this->getName()<<": Input "<<input<<" is not looped!";
192 
193  VirtualOutputSlot* out=(*it).second;
194  out->setLoopPartner(0);
195  out->setLoop(false);
196  _loopedSlots.erase(it);
197 }
198 
200 {
201  std::map<int,VirtualOutputSlot*>::iterator it=_loopedSlots.begin();
202  for(;it!=_loopedSlots.end();it++)
203  {
204  VirtualOutputSlot* out=(*it).second;
205  out->setLoop(true);
206  }
207 }
208 
210 {
211  std::map<int,VirtualOutputSlot*>::iterator it=_loopedSlots.begin();
212  for(;it!=_loopedSlots.end();it++)
213  {
214  VirtualOutputSlot* out=(*it).second;
215  out->setLoop(false);
216  }
217 }
218 
220 {
221  workFlowFile.load(file);
222  pluginPaths.load(file);
223  initialize();
224 }
225 
227  const ParameterFile& pf, const PluginManagerInterface* /*man*/) {
228  for(size_t i=0; i<_loopOutputNumber.size(); i++) {
229  Parameter<int> *par=_loopOutputNumber[i];
230  par->load(pf);
231  if((*par)()>=0)
232  {
233  loopInputToOutput(i,(*par)());
234  }
235  }
236 }
237 
239 {
241  if(_pluginMan)
242  {
243  std::map<std::string, ParameteredObject *>::const_iterator iter;
244 
245  if (_pluginMan->getObjectList().empty()) {
247  "Could not reset executed flags in workflow:\n\t"
248  "No valid target point found.\n\t"
249  "Please check if all required plugins could be loaded,\n\t"
250  "then check if this is a valid parameter file.", "unknown",
252  }
253 
254  for (iter = _pluginMan->getObjectList().begin(); iter != _pluginMan->getObjectList().end(); iter++) {
255  // if(isGroup((*iter)))
256  // {
257  // AbstractBaseGroupIntf* grpIntf=dynamic_cast<AbstractBaseGroupIntf*>((*iter));
258  // grpIntf->loadWorkflow(AbstractPluginLoader::pluginPaths);
259  // }
260  (*iter).second->setExecuted(value);
261  }
262  }
263 
264 }
265 
266 void ParameteredGroupObject::onAddInputSlot(VirtualInputSlot *in)
267 {
268  _addInputSlot(*in,in->getName(),in->getDisplayName(),"",in->getType());
269 }
270 
271 void ParameteredGroupObject::onAddOutputSlot(VirtualOutputSlot *out)
272 {
273  _addOutputSlot(*out,out->getName(),out->getDisplayName(),"",out->getType());
274 }
275 
276 
277 
278 
279 
void saveParameterFile(ParameterFile &paramFile) const
save parameters and connections of all managed plugins
void loopInputToOutput(int input, int output)
LoopOutToInput.
virtual void finalize()
finalize plugin
Interface for a plugin manager.
virtual std::vector< VirtualOutputSlot * > & getInternalSlotVector()=0
get virtual inputs to be exposed in group workflow.
Manages ParameteredObject based plugins and their instances.
std::string getType() const
overloaded getType
Definition: Slots.cpp:318
This class serves to store parameters used within the Charon Project.
Definition: ParameterFile.h:68
ParameterList< std::string > pluginPaths
plugin search paths which are used when executing this loop
SplitStream sout
Dummy instance for usage in other files (for interface too).
void initialize()
Initialization.
bool _removeSomething(const std::string &extension, const std::string &name)
Remove something. Iverse of _addSomething.
Base class for serializable objects.
virtual void load(const ParameterFile &pf)
Load from ParameterFile.
Definition: Parameter.hxx:194
ParameteredGroupObject(const std::string &className="ParameteredGroupObject", const std::string &name="", const std::string &doc="")
Default constructor.
Interface for OutputSlotBundle.
void _setDynamic(bool v)
Specifies if the ParameteredObject is dynamic.
void _removeOutputSlot(std::string name)
Remove an output slot.
std::string getName() const
get the Name
Definition: Slots.cpp:425
std::string getType() const
overloaded getType
Definition: Slots.cpp:421
void _removeInputSlot(std::string name)
Remove an input slot.
void _addInputSlot(Slot &slot, const std::string &name, const std::string &doc, const std::string &type="")
These functions needs to be called by the derived class in order to register all objects which can be...
virtual void finalizeGroup()
Finalization.
Interface for InputSlotBundle.
void reset()
reset plugin manager
virtual void executeGroup()
Execution.
void enableLoopConnections()
enableLoopConnections
virtual void load(const ParameterFile &pf)
Load from ParameterFile.
Definition: Parameter.hxx:109
void _addParameter(AbstractParameter &param, const std::string &name, const std::string &doc, const std::string &type="")
Add parameters.
void runWorkflow()
run whole workflow
virtual void onSave(ParameterFile &pf) const
Custom save.
void _addOutputSlot(Slot &slot, const std::string &name, const std::string &doc, const std::string &type="")
Register output slot.
virtual std::vector< VirtualOutputSlot * > & getSlotVector()=0
get virtual inputs to be exposed in parent workflow.
virtual void initializeGroup()
Group Initialization.
virtual void setExecuted(bool value)
set property _executed
const std::map< std::string, ParameteredObject * > & getObjectList() const
Get names of existing instances.
virtual void initialize()
initialize plugin
const std::string & getName() const
instance name
virtual std::string getDisplayName() const
Get slot display name.
Definition: Slots.cpp:484
virtual ~ParameteredGroupObject()
Default deconstructor.
virtual void onLoad(const ParameterFile &pf, const PluginManagerInterface *man)
Custom load.
std::string getName() const
get the Name
Definition: Slots.cpp:322
void breakLoop(int input)
breakLoop
virtual std::vector< VirtualInputSlot * > & getInternalSlotVector()=0
get virtual inputs to be exposed in group workflow.
void disableLoopConnections()
disableLoopConnections
virtual void setExecuted(bool value)
set property _executed
virtual void prepareDynamicInterface(const ParameterFile &file)
Prepare the interface.
virtual std::vector< VirtualInputSlot * > & getSlotVector()=0
get virtual inputs to be exposed in parent workflow.
void loadParameterFile(const ParameterFile &pf)
Reads a parameter file.