]> Creatis software - bbtk.git/blob - kernel/src/bbtkComplexBlackBox.h
3a5d8fb7c5ccbfdddd83c1eaa89ed9e336b21cf6
[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/18 12:59:15 $
7   Version:   $Revision: 1.3 $
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, 
82                                  Connection::Pointer c);
83     /// Connects the output <name> to the connection c
84     virtual void bbConnectOutput( const std::string& name, 
85                                   Connection::Pointer c);
86
87     BlackBox::Pointer bbGetBlackBox( const std::string& name );
88
89     /// The type of map of black boxes
90     typedef std::map<std::string, BlackBox::Pointer> BlackBoxMapType;
91
92     const BlackBoxMapType& bbGetBlackBoxMap() { return mBlackBoxMap; }
93
94     void bbPrintBlackBoxes();
95
96     /// Generates a png image representing the pipeline graph of the 
97     /// complex box and writes html code to insert it 
98     /// into the output file stream. 
99     /// detail : 
100     /// level : depth of nested complex boxes graph development 
101     /// (0:only this box level 
102     /// output_dir is the directory in which to write the files 
103     void bbInsertHTMLGraph(  std::ofstream& s, 
104                              int detail, 
105                              int level,
106                              bool instanceOrtype,
107                              const std::string& output_dir,
108                              bool relative_link );
109
110     /// Writes Graphviz-dot description in file.
111     /// Generates own description and recursively calls itself 
112     /// on internal boxes with level-1 
113     virtual void bbWriteDotFileBlackBox(FILE *ff,
114                                         BlackBox::Pointer parentblackbox, 
115                                         int detail, int level,
116                                         bool instanceOrtype,
117                                         bool relative_link );
118     
119     virtual void bbWriteDotInputOutputName(FILE *ff,
120                                            bool inputoutput, 
121                                            int detail, int level);
122     
123     virtual BlackBox::Pointer bbFindBlackBox(const std::string &blackboxname);
124     
125
126     void Check(bool recursive=true);
127
128        
129
130   protected:
131     //==================================================================
132     // PROTECTED PART : ACCESSIBLE TO ComplexBlackBoxDescriptor
133     static ComplexBlackBox::Pointer 
134     New(const std::string &name,
135         ComplexBlackBoxDescriptor::Pointer desc);
136                                  
137   private:                       
138     ///  Constructor that takes the ComplexBlackBox name
139     ComplexBlackBox(const std::string &name,
140                     ComplexBlackBoxDescriptor::Pointer desc);
141     /// Constructor from an existing box (copy) with a new name 
142     ComplexBlackBox(ComplexBlackBox& from, const std::string &name);
143     
144   public:
145     IOStatus bbBackwardUpdate(Connection::Pointer caller);
146     //  void bbForwardUpdate(Connection* caller);
147     void bbSetModifiedStatus(BlackBoxInputConnector* c);
148
149   protected:
150   
151
152
153     void bbAddBlackBox( BlackBox::Pointer );
154     void bbAddToExecutionList( const std::string& name );
155     void bbUnsafeAddBlackBox( BlackBox::Pointer );
156     void bbRemoveBlackBox( const std::string& name, 
157                            bool remove_connections = true);
158     void bbAddConnection( Connection::Pointer );
159     
160     //    void RemoveConnection( );
161
162     BlackBox::Pointer bbUnsafeGetBlackBox( const std::string& name );
163
164     /// 
165     void bbAllocateConnectors();
166     void bbDesallocateConnectors();
167
168   private:
169     //==================================================================
170     // PRIVATE PART : MEMBERS AND USEFULL INTERNAL METHODS
171
172     /// Default constructor : derived classes must use the constructor with the ComplexBlackBox's name
173     //ComplexBlackBox() : BlackBox("") {}
174     
175     // true if the box is a prototype of a ComplexBlackBoxDescriptor
176     //    bool mIsAPrototype;
177
178     /// The descriptor pointer : is 0 if the box is a prototype 
179     ComplexBlackBoxDescriptor::Pointer mLockedDescriptor;
180     /// The descriptor pointer
181     ComplexBlackBoxDescriptor::WeakPointer mDescriptor;
182     
183     /// The map of black boxes
184     BlackBoxMapType mBlackBoxMap;
185     
186     
187     /// The type of list of connections
188     typedef std::list<Connection::Pointer> ConnectionListType;
189     /// The list of connections
190     ConnectionListType mConnectionList;
191
192 /// The execution list
193     std::vector<std::string> mExecutionList;
194     
195   };
196   // Class ComplexBlackBox
197   //===========================================================================
198
199
200
201 }
202 // namespace bbtk
203
204
205 //#include "bbtkComplexBlackBoxMacros.h"
206
207 #endif
208