]> Creatis software - bbtk.git/blob - kernel/src/bbtkComplexBlackBox.h
Feature #1774
[bbtk.git] / kernel / src / bbtkComplexBlackBox.h
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28 /*=========================================================================
29   Program:   bbtk
30   Module:    $RCSfile: bbtkComplexBlackBox.h,v $
31   Language:  C++
32   Date:      $Date: 2012/11/16 08:49:01 $
33   Version:   $Revision: 1.9 $
34 =========================================================================*/
35
36                                                                    
37
38
39
40 /**
41  *  \file 
42  *  \brief class bbtk::ComplexBlackBox : user defined complex black boxes
43  */
44
45 /**
46  *  \class bbtk::ComplexBlackBox
47  *  \brief Abstract user defined black boxes
48  */
49  
50 #ifndef __bbtkComplexBlackBox_h__
51 #define __bbtkComplexBlackBox_h__
52
53 #include "bbtkBlackBox.h"
54 #include "bbtkComplexBlackBoxDescriptor.h"
55 //#include "bbtkComplexBlackBoxInputDescriptor.h"
56 //#include "bbtkComplexBlackBoxOutputDescriptor.h"
57 #include "creaSystem.h"
58 #include <list>
59
60 namespace bbtk
61 {
62
63
64  
65   //==================================================================
66   class BBTK_EXPORT ComplexBlackBox : public bbtk::BlackBox
67   {
68     BBTK_OBJECT_INTERFACE(ComplexBlackBox);
69     friend class ComplexBlackBoxDescriptor;
70     typedef BlackBox Superclass;
71   public: 
72     //==================================================================
73     // PUBLIC PART : ACCESSIBLE TO THE END-USER
74     /// Returns the pointer on the box' descriptor
75     BlackBoxDescriptor::Pointer bbGetDescriptor() const { return mDescriptor.lock(); }
76     /// Returns a pointer on a new instance with name <name>
77     //BlackBox* bbNew(const std::string& name);
78     /// Returns a pointer on a clone of the box with name <name>
79     BlackBox::Pointer bbClone(const std::string& name);
80
81     /// Clear
82     void Clear();
83     /// Set as prototype
84     void SetAsPrototype() { mLockedDescriptor.reset(); }
85     /// Returns true if it is a prototype 
86     bool IsAPrototype() const { return (!mLockedDescriptor); }
87
88     std::string bbGetNameWithParent() const;
89
90     void bbExecute(bool force = false);
91   
92     Data bbGetOutput( const std::string &label );
93     Data bbGetInput ( const std::string &label );
94     void bbSetOutput( const std::string &name, Data data);
95     void bbSetInput ( const std::string &name, Data data,
96                       bool setModified = true);
97     void bbBruteForceSetInputPointer( const std::string &name, 
98                                       void* data, 
99                                       bool setModified = true);
100
101      /// Connects the input <name> to the connection c
102     virtual void bbConnectInput( const std::string& name, Connection* c);
103     /// Connects the output <name> to the connection c
104     virtual void bbConnectOutput( const std::string& name, Connection* c);
105
106     BlackBox::Pointer bbGetBlackBox( const std::string& name );
107
108     /// The type of map of black boxes
109     typedef std::map<std::string, BlackBox::Pointer> BlackBoxMapType;
110
111     const BlackBoxMapType& bbGetBlackBoxMap() { return mBlackBoxMap; }
112
113     void bbPrintBlackBoxes();
114
115     /// Generates a png image representing the pipeline graph of the 
116     /// complex box and writes html code to insert it 
117     /// into the output file stream. 
118     /// detail : 
119     /// level : depth of nested complex boxes graph development 
120     /// (0:only this box level 
121     /// output_dir is the directory in which to write the files 
122     void bbInsertHTMLGraph(  std::ofstream& s, 
123                              int detail, 
124                              int level,
125                              bool instanceOrtype,
126                              const std::string& output_dir,
127                              bool relative_link );
128
129     /// Writes Graphviz-dot description in file.
130     /// Generates own description and recursively calls itself 
131     /// on internal boxes with level-1 
132     virtual void bbWriteDotFileBlackBox(FILE *ff,
133                                         BlackBox::Pointer parentblackbox, 
134                                         int detail, int level,
135                                         bool instanceOrtype,
136                                         bool relative_link );
137     
138     virtual void bbWriteDotInputOutputName(FILE *ff,
139                                            bool inputoutput, 
140                                            int detail, int level);
141     
142     virtual BlackBox::Pointer bbFindBlackBox(const std::string &blackboxname);
143     
144
145     void Check(bool recursive=true);
146
147        
148
149   protected:
150     //==================================================================
151     // PROTECTED PART : ACCESSIBLE TO ComplexBlackBoxDescriptor
152     static ComplexBlackBox::Pointer 
153     New(const std::string &name,
154         ComplexBlackBoxDescriptor::Pointer desc);
155                                  
156   private:                       
157     ///  Constructor that takes the ComplexBlackBox name
158     ComplexBlackBox(const std::string &name,
159                     ComplexBlackBoxDescriptor::Pointer desc);
160     /// Constructor from an existing box (copy) with a new name 
161     ComplexBlackBox(ComplexBlackBox& from, const std::string &name);
162     
163   protected:
164   
165
166
167     void bbAddBlackBox( BlackBox::Pointer );
168     void bbAddToExecutionList( const std::string& name );
169     void bbUnsafeAddBlackBox( BlackBox::Pointer );
170     void bbRemoveBlackBox( const std::string& name, 
171                            bool remove_connections = true);
172     void bbAddConnection( Connection::Pointer );
173     
174     //    void RemoveConnection( );
175
176     BlackBox::Pointer bbUnsafeGetBlackBox( const std::string& name );
177
178     /// 
179     void bbAllocateConnectors();
180     void bbDesallocateConnectors();
181
182   private:
183     //==================================================================
184     // PRIVATE PART : MEMBERS AND USEFULL INTERNAL METHODS
185
186     /// Default constructor : derived classes must use the constructor with the ComplexBlackBox's name
187     //ComplexBlackBox() : BlackBox("") {}
188     
189     // true if the box is a prototype of a ComplexBlackBoxDescriptor
190     //    bool mIsAPrototype;
191
192     /// The descriptor pointer : is 0 if the box is a prototype 
193     ComplexBlackBoxDescriptor::Pointer mLockedDescriptor;
194     /// The descriptor pointer
195     ComplexBlackBoxDescriptor::WeakPointer mDescriptor;
196     
197     /// The map of black boxes
198     BlackBoxMapType mBlackBoxMap;
199     
200     
201     /// The type of list of connections
202     typedef std::list<Connection::Pointer> ConnectionListType;
203     /// The list of connections
204     ConnectionListType mConnectionList;
205
206 /// The execution list
207     std::vector<std::string> mExecutionList;
208     
209   };
210   // Class ComplexBlackBox
211   //===========================================================================
212
213
214
215 }
216 // namespace bbtk
217
218
219 //#include "bbtkComplexBlackBoxMacros.h"
220
221 #endif
222