1 /*=========================================================================
3 Module: $RCSfile: bbtkBlackBox.h,v $
5 Date: $Date: 2008/12/03 09:34:37 $
6 Version: $Revision: 1.15 $
7 =========================================================================*/
9 /* ---------------------------------------------------------------------
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
14 * This software is governed by the CeCILL-B license under French law and
15 * abiding by the rules of distribution of free software. You can use,
16 * modify and/ or redistribute the software under the terms of the CeCILL-B
17 * license as circulated by CEA, CNRS and INRIA at the following URL
18 * http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
19 * or in the file LICENSE.txt.
21 * As a counterpart to the access to the source code and rights to copy,
22 * modify and redistribute granted by the license, users are provided only
23 * with a limited warranty and the software's author, the holder of the
24 * economic rights, and the successive licensors have only limited
27 * The fact that you are presently reading this means that you have had
28 * knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */
35 * \brief Class bbtk::BlackBox : abstract black-box interface.
39 * \class bbtk::BlackBox
40 * \brief Abstract black-box interface
43 #ifndef __bbtkBlackBox_h__
44 #define __bbtkBlackBox_h__
46 #include "bbtkSystem.h"
47 #include "bbtkBlackBoxDescriptor.h"
48 #include "bbtkBlackBoxInputConnector.h"
49 #include "bbtkBlackBoxOutputConnector.h"
56 struct Void { Void(int = 0) {} };
61 class BBTK_EXPORT BlackBox : public Object
63 BBTK_ABSTRACT_OBJECT_INTERFACE(BlackBox);
65 //==================================================================
67 //==================================================================
69 /// The type of map of output connector pointers
70 typedef std::map<std::string, BlackBoxOutputConnector*>
71 OutputConnectorMapType;
72 /// The type of map of input connector pointers
73 typedef std::map<std::string, BlackBoxInputConnector*>
74 InputConnectorMapType;
76 /// Returns the pointer on the descriptor of the box
77 virtual BlackBoxDescriptor::Pointer bbGetDescriptor() const = 0;
79 /// Returns a pointer on a clone of the box with name <name>
80 virtual BlackBox::Pointer bbClone(const std::string& name) = 0;
82 /// User overloadable destruction method of a black box
83 virtual void bbUserDelete();
85 /// Returns the Name of the Type of the BlackBox
86 const std::string& bbGetTypeName() const
87 { return bbGetDescriptor()->GetTypeName(); }
90 /// Returns the name of the BlackBox (instance)
91 const std::string& bbGetName() const { return bbmName; }
93 /// Returns the full name of the BlackBox (instance+type)
94 virtual std::string bbGetFullName() const;
96 /// Returns the name with the name of the parent prepended if any
97 virtual std::string bbGetNameWithParent() const;
99 /// Returns the parent of the BlackBox, i.e the BlackBox that contains it (0 if none)
100 BlackBox::Pointer bbGetParent() const { return bbmParent.lock(); }
103 /// Main processing method of the box.
104 virtual void bbExecute(bool force = false);
106 /// Signals that the BlackBox has been modified through the input connector c
107 /// and propagates it downward
108 virtual void bbSetModifiedStatus(BlackBoxInputConnector* c = 0);
109 /// Signals that the BlackBox outputs have been modified
110 /// without marking the box as MODIFIED because its output state is ok.
111 /// This method should be used by widgets in response
112 /// to user interaction when **ALL** outputs have been modified
113 /// (after the outputs has been updated !)
114 virtual void bbSignalOutputModification(bool reaction = true);
115 /// Signals that the BlackBox output "output_name" have been modified
116 /// without marking the box as MODIFIED because its output state is ok.
117 /// This method should be used by widgets in response to user interaction
118 /// only when **ONE** output has been modified
119 /// (after the output has been updated !)
120 virtual void bbSignalOutputModification( const std::string& output_name,
121 bool reaction = true);
122 /// Signals that the BlackBox vector of outputs "output_name"
123 /// have been modified
124 /// without marking the box as MODIFIED because its output state is ok.
125 /// This method should be used by widgets in response to user interaction
126 /// When more than one output has been changed but not all
127 /// (after the outputs have been updated of course!)
128 virtual void bbSignalOutputModification( const std::vector<std::string>& output_name,
129 bool reaction = true);
131 /// Gets the status of the box
132 virtual const IOStatus& bbGetStatus() const { return bbmStatus; }
135 /// Returns true iff the BlackBox has an input of name label
136 virtual bool bbHasInput(const std::string& label) const;
137 /// Returns true iff the BlackBox has an output of name label
138 virtual bool bbHasOutput(const std::string& label) const;
140 /// Gets the input type of a given label
141 virtual TypeInfo bbGetInputType( const std::string &label ) const;
142 /// Gets the output type of a given label
143 virtual TypeInfo bbGetOutputType( const std::string &label ) const;
145 /// Gets the data of the input called <name>
146 virtual Data bbGetInput( const std::string &name ) = 0;
147 /// Gets the data of the output called <name>
148 virtual Data bbGetOutput( const std::string &name ) = 0;
150 /// Sets the data of the input called <name>.
151 /// If setModified is false then does not call bbSetModifiedStatus()
152 virtual void bbSetInput( const std::string &name, Data data,
153 bool setModified = true ) = 0;
154 virtual void bbBruteForceSetInputPointer( const std::string &name,
156 bool setModified = true) =0;
157 /// Sets the data of the output called <name>
158 virtual void bbSetOutput( const std::string &name, Data data) = 0;
161 /// Returns the input connectors map
162 InputConnectorMapType& bbGetInputConnectorMap()
163 { return mInputConnectorMap; }
164 /// Returns the output connectors map
165 OutputConnectorMapType& bbGetOutputConnectorMap()
166 { return mOutputConnectorMap; }
167 /// Returns the input connectors map (const)
168 const InputConnectorMapType& bbGetInputConnectorMap() const
169 { return mInputConnectorMap; }
170 /// Returns the output connectors map (const)
171 const OutputConnectorMapType& bbGetOutputConnectorMap() const
172 { return mOutputConnectorMap; }
175 /// Prints the Help on the BlackBox type
176 virtual void bbGetHelp(bool full=true) const;
179 //==================================================================
180 // Common inputs / outputs to all boxes
181 /// Returns the value of the input "BoxProcessMode"
182 std::string bbGetInputBoxProcessMode() { return bbmBoxProcessMode; }
183 /// Sets the value of the input "BoxProcessMode"
184 void bbSetInputBoxProcessMode(std::string a) { bbmBoxProcessMode = a; }
192 /// Returns the "decoded" value of the input "BoxProcessMode"
193 BoxProcessModeValue bbGetBoxProcessModeValue() const;
195 virtual bool bbBoxProcessModeIsReactive() const;
197 virtual bool bbBoxProcessModeIsAlways() const;
199 /// Returns the value of the input "Execute"
200 Void bbGetInputBoxExecute() { return Void(); }
201 /// Sets the value of the input "Execute"
202 void bbSetInputBoxExecute(Void = 0) {}
204 /// Returns the value of the output "Change"
205 Void bbGetOutputBoxChange() { return Void(); }
206 /// Sets the value of the output "Change" : signal a modification
207 void bbSetOutputBoxChange(Void = 0) { bbSetModifiedStatus(); }
209 //==================================================================
212 //==================================================================
214 /// Does nothing here : overloaded in ComplexBlackBox
215 void bbInsertHTMLGraph( std::ofstream& s,
219 const std::string& output_dir,
223 /// Write Graphviz-dot description in file.
224 /// Here dumps a single box description (i/o) but overloaded
225 /// in ComplexBlackBox to dump the internal pipeline representation
226 /// recursing into internal boxes descriptions if level>0.
227 /// detail = 1 : draw inputs and outputs (do not draw otherwise)
228 /// instanceOrtype = true : draw inputs and outputs VALUES
229 /// (uses bbGetInputAsString / bbGetOutputAsString which use adaptors)
230 /// If relative_link is true then creates relative hrefs
231 virtual void bbWriteDotFileBlackBox(FILE *ff,
232 BlackBox::Pointer parentblackbox,
233 int detail, int level,
235 bool relative_link );
236 /// Auxiliary method for bbWriteDotFileBlackBox
237 virtual void bbWriteDotInputOutputName(FILE *ff,
239 int detail, int level);
242 virtual void bbShowRelations(BlackBox::Pointer parentblackbox,
243 int detail, int level
246 std::string bbGetOutputAsString( const std::string &output ); //,Factory *factory);
247 std::string bbGetInputAsString( const std::string &input); //,Factory *factory);
248 virtual BlackBox::Pointer bbFindBlackBox(const std::string &blackboxname)
249 { return BlackBox::Pointer();}
251 virtual void Check(bool recursive = true);
253 virtual void bbUserOnShow() { }
254 void bbUserOnShowWidget(std::string nameInput);
257 //==================================================================
258 // PROTECTED PART : ACCESSIBLE TO THE BlackBox DEVELOPER
259 // (IN INHERITED CLASSES)
260 /// Constructor that take the BlackBox's name
261 BlackBox(const std::string &name);
262 /// Constructor from an existing box (copy) with a new name
263 BlackBox(BlackBox& from, const std::string &name);
264 //==================================================================
267 //==================================================================
268 /// Sets the status of the box
269 void bbSetStatus( IOStatus t) { bbmStatus = t; }
270 //==================================================================
273 friend class Connection;
274 friend class ComplexBlackBox;
276 /// Sets the parent of the BlackBox
277 void bbSetParent(BlackBox::Pointer p) { bbmParent = p; }
280 /// Connects the input <name> to the connection c
281 virtual void bbConnectInput( const std::string& name,
283 /// Connects the output <name> to the connection c
284 virtual void bbConnectOutput( const std::string& name,
286 /// Disconnects the input <name> from the connection c
287 virtual void bbDisconnectInput( const std::string& name,
289 /// Disconnects the output <name> from the connection c
290 virtual void bbDisconnectOutput( const std::string& name,
295 /// @name Pipeline processing methods
296 /// Methods which participate to (forward or backward) pipeline processing.
297 /// Some are pure virtual and prepare particular update mechanism which are implemented by descendents (e.g. ForwardUpdate and UpdateChildren are made for WxContainerBlackBox particular update mechanism which must recurse a piece of the pipeline top-down).
298 /// The main method is bbBackwardUpdate which is called by bbExecute and implemented in UserBlackBox and ComplexBlackBox.
302 //==================================================================
303 /// Recursive pipeline processing in backward direction
304 /// (recursion is in backward direction however execution always goes forward).
305 /// Pure virtual; defined in UserBlackBox and ComplexBlackBox
307 /// \returns The final status of the box (UPTODATE or MODIFIED)
308 /// \param caller : The connection which invoked the method; null if called by bbExecute
310 /// First checks that re-processing is needed (either Status==MODIFIED or InputProcessMode==Always)
312 /// - updates its inputs by calling bbUpdateInputs (which recursively calls bbBackwardUpdate on upstream boxes)
313 /// - calls bbCreateWidget
314 /// - calls bbProcess which is the user callback which does the actual processing
315 /// - calls bbUpdateChildren
316 /// - calls bbShowWidget which shows the widget associated to the box (if any)
318 virtual IOStatus bbBackwardUpdate(Connection::Pointer caller) = 0;
319 //==================================================================
321 //==================================================================
322 /// Recursive pipeline processing in forward direction along "Child"-"Parent" connections
323 /// Pure virtual; defined in UserBlackBox and ComplexBlackBox
325 /// \param caller : The connection which invoked the method
327 /// First checks that re-processing is needed (either Status==MODIFIED or InputProcessMode==Always)
329 /// - calls bbCreateWidget
330 /// - calls bbProcess which is the user callback which does the actual processing
331 /// - calls bbUpdateChildren which recursively calls bbForwardUpdate on connections attached the "Child" output
332 // virtual void bbForwardUpdate(Connection::Pointer caller) = 0;
333 //==================================================================
335 //==================================================================
336 /// Updates the BlackBox inputs and returns the final status of the inputs
337 /// (==UPTODATE iff all inputs are UPTODATE)
338 // If excludeParent == true then excludes the upstream box connected to input 'Parent' from recursive update
339 IOStatus bbUpdateInputs(bool excludeParent=false);
340 //==================================================================
342 //==================================================================
343 /// Updates the pipeline in upstream-downstream direction along the "Child"-"Parent" connections only.
344 /// Does nothing here. Overloaded in WxContainerBlackbox
345 //virtual void bbUpdateChildren( Connection::Pointer caller ) { }
346 //==================================================================
348 //==================================================================
349 /// Specific methods for window creation during pipeline execution
350 /// Creates the window associated to the box (called after bbUpdateInputs)
351 /// Does nothing here. Overloaded in WxBlackBox.
352 // virtual void bbCreateWindow() { }
353 /// Shows the window associated to the box
354 /// (called after bbProcess during bbExecute)
355 /// Does nothing here but overloaded in WxBlackBox and WxContainerBlackBox
356 virtual void bbShowWindow(Connection::Pointer caller) { }
358 virtual void bbHideWindow() {}
359 virtual void bbCloseWindow() { }
360 //==================================================================
365 static bool bbGlobalGetSomeBoxExecuting();
366 static void bbGlobalSetSomeBoxExecuting(bool b);
368 static void bbGlobalSetFreezeExecution(bool b);
369 static bool bbGlobalGetFreezeExecution();
371 /// Returns true if the box can "react",
372 /// which means execute in response to an input change
373 virtual bool bbCanReact() const;
375 /// Returns true iff the box is executing
376 bool bbGetExecuting() const { return bbmExecuting; }
379 static void bbGlobalAddToExecutionList( BlackBox::Pointer b );
380 static void bbGlobalProcessExecutionList();
381 /// Sets the bbmExecuting bool returned by bbGetExecuting
382 void bbSetExecuting(bool b) { bbmExecuting = b; }
384 //==================================================================
386 //==================================================================
387 /// Allocates the i/o connectors of the black box
388 virtual void bbAllocateConnectors();
389 /// Desallocates the i/o connectors of the black box
390 virtual void bbDesallocateConnectors();
391 /// Copies the values of the inputs/output from the BlackBox from
392 virtual void bbCopyIOValues(BlackBox& from);
393 //==================================================================
395 // Black box objects have a special deleter
396 // which must take care of releasing the descriptor
397 // **AFTER** the box is deleted
398 // (Releasing it in the destructor may cause dl close and crash)
399 /// Black box deleter
400 /// 1) Calls the user overloadable bbDelete method
401 /// 2) Releases the box descriptor
402 struct BBTK_EXPORT Deleter : public Object::Deleter
405 void Delete(Object* p);
409 static boost::shared_ptr<U> MakeBlackBoxPointer(U* s, bool lock = false)
411 return MakePointer(s,BlackBox::Deleter(),lock);
414 virtual void bbDelete() { delete this; }
418 //==================================================================
420 /// The status of the box
422 /// Is the box executing ?
424 /// The name of the black-box
426 /// The name of the package to which it belongs
427 std::string bbmPackageName;
429 /// 0 : "Pipeline" mode : bbBackwardUpdate() only calls Process if Status == MODIFIED (normal pipeline processing)
430 /// 1 : "Always" mode : bbUpdate() always calls Process
431 /// 2 : "Reactive" mode : bbSetModifiedStatus() calls bbUpdate()
432 std::string bbmBoxProcessMode;
434 /// The parent of the black box in the ComplexBlackBox hierarchy
435 BlackBox::WeakPointer bbmParent;
436 //==================================================================
439 //==================================================================
441 /// Map that contains the output connectors of the black box
442 OutputConnectorMapType mOutputConnectorMap;
443 /// Map that contains the input connectors of the black box
444 InputConnectorMapType mInputConnectorMap;
445 //==================================================================