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