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