]> Creatis software - bbtk.git/blob - kernel/src/bbtkComplexBlackBox.h
7be6b041e4e1f509daec099388550c29dc9a3d1c
[bbtk.git] / kernel / src / bbtkComplexBlackBox.h
1 /*=========================================================================
2                                                                                 
3   Program:   bbtk
4   Module:    $RCSfile: bbtkComplexBlackBox.h,v $
5   Language:  C++
6   Date:      $Date: 2008/04/22 14:30:25 $
7   Version:   $Revision: 1.4 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18
19
20 /**
21  *  \file 
22  *  \brief class bbtk::ComplexBlackBox : user defined complex black boxes
23  */
24
25 /**
26  *  \class bbtk::ComplexBlackBox
27  *  \brief Abstract user defined black boxes
28  */
29  
30 #ifndef __bbtkComplexBlackBox_h__
31 #define __bbtkComplexBlackBox_h__
32
33 #include "bbtkBlackBox.h"
34 #include "bbtkComplexBlackBoxDescriptor.h"
35 //#include "bbtkComplexBlackBoxInputDescriptor.h"
36 //#include "bbtkComplexBlackBoxOutputDescriptor.h"
37 #include <list>
38
39 namespace bbtk
40 {
41
42
43  
44   //==================================================================
45   class BBTK_EXPORT ComplexBlackBox : public bbtk::BlackBox
46   {
47     BBTK_OBJECT_INTERFACE(ComplexBlackBox);
48     friend class ComplexBlackBoxDescriptor;
49     typedef BlackBox Superclass;
50   public: 
51     //==================================================================
52     // PUBLIC PART : ACCESSIBLE TO THE END-USER
53     /// Returns the pointer on the box' descriptor
54     BlackBoxDescriptor::Pointer bbGetDescriptor() const { return mDescriptor.lock(); }
55     /// Returns a pointer on a new instance with name <name>
56     //BlackBox* bbNew(const std::string& name);
57     /// Returns a pointer on a clone of the box with name <name>
58     BlackBox::Pointer bbClone(const std::string& name);
59
60     /// Clear
61     void Clear();
62     /// Set as prototype
63     void SetAsPrototype() { mLockedDescriptor.reset(); }
64     /// Returns true if it is a prototype 
65     bool IsAPrototype() const { return (!mLockedDescriptor); }
66
67     std::string bbGetNameWithParent() const;
68
69     void bbExecute(bool force = false);
70   
71     Data bbGetOutput( const std::string &label );
72     Data bbGetInput ( const std::string &label );
73     void bbSetOutput( const std::string &name, Data data);
74     void bbSetInput ( const std::string &name, Data data,
75                       bool setModified = true);
76     void bbBruteForceSetInputPointer( const std::string &name, 
77                                       void* data, 
78                                       bool setModified = true);
79
80      /// Connects the input <name> to the connection c
81     virtual void bbConnectInput( const std::string& name, Connection* c);
82     /// Connects the output <name> to the connection c
83     virtual void bbConnectOutput( const std::string& name, Connection* c);
84
85     BlackBox::Pointer bbGetBlackBox( const std::string& name );
86
87     /// The type of map of black boxes
88     typedef std::map<std::string, BlackBox::Pointer> BlackBoxMapType;
89
90     const BlackBoxMapType& bbGetBlackBoxMap() { return mBlackBoxMap; }
91
92     void bbPrintBlackBoxes();
93
94     /// Generates a png image representing the pipeline graph of the 
95     /// complex box and writes html code to insert it 
96     /// into the output file stream. 
97     /// detail : 
98     /// level : depth of nested complex boxes graph development 
99     /// (0:only this box level 
100     /// output_dir is the directory in which to write the files 
101     void bbInsertHTMLGraph(  std::ofstream& s, 
102                              int detail, 
103                              int level,
104                              bool instanceOrtype,
105                              const std::string& output_dir,
106                              bool relative_link );
107
108     /// Writes Graphviz-dot description in file.
109     /// Generates own description and recursively calls itself 
110     /// on internal boxes with level-1 
111     virtual void bbWriteDotFileBlackBox(FILE *ff,
112                                         BlackBox::Pointer parentblackbox, 
113                                         int detail, int level,
114                                         bool instanceOrtype,
115                                         bool relative_link );
116     
117     virtual void bbWriteDotInputOutputName(FILE *ff,
118                                            bool inputoutput, 
119                                            int detail, int level);
120     
121     virtual BlackBox::Pointer bbFindBlackBox(const std::string &blackboxname);
122     
123
124     void Check(bool recursive=true);
125
126        
127
128   protected:
129     //==================================================================
130     // PROTECTED PART : ACCESSIBLE TO ComplexBlackBoxDescriptor
131     static ComplexBlackBox::Pointer 
132     New(const std::string &name,
133         ComplexBlackBoxDescriptor::Pointer desc);
134                                  
135   private:                       
136     ///  Constructor that takes the ComplexBlackBox name
137     ComplexBlackBox(const std::string &name,
138                     ComplexBlackBoxDescriptor::Pointer desc);
139     /// Constructor from an existing box (copy) with a new name 
140     ComplexBlackBox(ComplexBlackBox& from, const std::string &name);
141     
142   public:
143     IOStatus bbBackwardUpdate(Connection::Pointer caller);
144     //  void bbForwardUpdate(Connection* caller);
145     void bbSetModifiedStatus(BlackBoxInputConnector* c);
146
147   protected:
148   
149
150
151     void bbAddBlackBox( BlackBox::Pointer );
152     void bbAddToExecutionList( const std::string& name );
153     void bbUnsafeAddBlackBox( BlackBox::Pointer );
154     void bbRemoveBlackBox( const std::string& name, 
155                            bool remove_connections = true);
156     void bbAddConnection( Connection::Pointer );
157     
158     //    void RemoveConnection( );
159
160     BlackBox::Pointer bbUnsafeGetBlackBox( const std::string& name );
161
162     /// 
163     void bbAllocateConnectors();
164     void bbDesallocateConnectors();
165
166   private:
167     //==================================================================
168     // PRIVATE PART : MEMBERS AND USEFULL INTERNAL METHODS
169
170     /// Default constructor : derived classes must use the constructor with the ComplexBlackBox's name
171     //ComplexBlackBox() : BlackBox("") {}
172     
173     // true if the box is a prototype of a ComplexBlackBoxDescriptor
174     //    bool mIsAPrototype;
175
176     /// The descriptor pointer : is 0 if the box is a prototype 
177     ComplexBlackBoxDescriptor::Pointer mLockedDescriptor;
178     /// The descriptor pointer
179     ComplexBlackBoxDescriptor::WeakPointer mDescriptor;
180     
181     /// The map of black boxes
182     BlackBoxMapType mBlackBoxMap;
183     
184     
185     /// The type of list of connections
186     typedef std::list<Connection::Pointer> ConnectionListType;
187     /// The list of connections
188     ConnectionListType mConnectionList;
189
190 /// The execution list
191     std::vector<std::string> mExecutionList;
192     
193   };
194   // Class ComplexBlackBox
195   //===========================================================================
196
197
198
199 }
200 // namespace bbtk
201
202
203 //#include "bbtkComplexBlackBoxMacros.h"
204
205 #endif
206