1 /*=========================================================================
3 Module: $RCSfile: bbtkBlackBoxDescriptor.cxx,v $
5 Date: $Date: 2012/11/14 07:12:00 $
6 Version: $Revision: 1.23 $
7 =========================================================================*/
9 /* ---------------------------------------------------------------------
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
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.
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
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 * ------------------------------------------------------------------------ */
34 * \brief Class bbtk::BlackBoxDescriptor : (abstract) describes a BlackBox (name, description, author) and is able to create an instance of it.
36 #include "bbtkBlackBoxDescriptor.h"
37 #include "bbtkMessageManager.h"
38 #include "bbtkPackage.h"
39 #include "bbtkUtilities.h"
40 #include "bbtkAtomicBlackBoxDescriptor.h"
41 #include "bbtkWxBlackBox.h"
46 typedef Package::Pointer PackagePointer;
48 #define bbtkDMessage(key,level,mess) \
49 bbtkMessage(key,level,"["<<GetFullTypeName()<<"] "<<mess)
50 #define bbtkDDebugMessage(key,level,mess) \
51 bbtkDebugMessage(key,level,"["<<GetFullTypeName()<<"] "<<mess)
54 //=========================================================================
56 BlackBoxDescriptor::BlackBoxDescriptor()
57 : mTypeName("Unknown"),
62 mTypeOfScript(TS_BINARY),
65 bbtkDDebugMessage("object",4,
66 "==> BlackBoxDescriptor()"<<std::endl);
67 bbtkDDebugMessage("object",4,
68 "<== BlackBoxDescriptor()"<<std::endl);
70 //=========================================================================
72 //=========================================================================
74 BlackBoxDescriptor::BlackBoxDescriptor(const BlackBoxDescriptor&)
78 //=========================================================================
80 BlackBoxDescriptor::~BlackBoxDescriptor()
82 bbtkDDebugMessage("object",4,
83 "==> ~BlackBoxDescriptor()"
86 // deletes all I/O descriptors
87 InputDescriptorMapType::iterator i;
88 for (i=mInput.begin(); i!=mInput.end(); ++i) delete i->second;
89 OutputDescriptorMapType::iterator o;
90 for (o=mOutput.begin(); o!=mOutput.end(); ++o) delete o->second;
92 bbtkDDebugMessage("object",4,
93 "<== ~BlackBoxDescriptor()"
96 //=========================================================================
99 //=======================================================================
101 void BlackBoxDescriptor::Release(BlackBoxDescriptor::WeakPointer desc)
103 bbtkMessage("object",2,"==> BlackBoxDescriptor::Release('"
104 <<desc.lock()->GetTypeName()<<"')"<<std::endl);
105 long c = desc.use_count();
106 bbtkMessage("object",3," - ref count = "<<c<<std::endl);
108 if ((c == 1) && (desc.lock()->mPackage))
110 bbtkMessage("object",2," --> No more instance alive = releasing from package"<<std::endl);
112 Package::WeakPointer pack = desc.lock()->mPackage;
113 Package::ReleaseBlackBoxDescriptor(pack,desc);
117 bbtkMessage("object",2," --> Still some instances alive = Keeping it alive"<<std::endl);
119 bbtkMessage("object",2,"<== BlackBoxDescriptor::Release('"
120 <<desc.lock()->GetTypeName()<<"')"<<std::endl);
123 //=========================================================================
127 //=========================================================================
129 void BlackBoxDescriptor::UnReference()
131 bbtkDDebugMessageInc("kernel",1,
132 "BlackBoxDescriptor::UnReference() ["
133 <<mTypeName<<"] #"<<mRefCount-1<<std::endl);
137 bbtkDDebugMessage("kernel",1,"--> Destructing BlackBoxDescriptor ["<<mTypeName<<"]"<<std::endl);
141 //=========================================================================
143 //=========================================================================
145 void BlackBoxDescriptor::Check(bool) const
149 //=========================================================================
151 //=========================================================================
152 /// Adds the string to the BlackBox description
153 void BlackBoxDescriptor::AddToDescription( const std::string& s, bool clear)
155 bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::AddToDescription(\""
156 <<s<<"\")"<<std::endl);
157 if (clear) mDescription = s;
158 else mDescription += s;
160 //=========================================================================
162 //=========================================================================
163 /// Adds the string to the BlackBox author list
164 void BlackBoxDescriptor::AddToAuthor( const std::string& s, bool clear)
166 bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::AddToAuthor(\""
169 if (clear) mAuthor = s;
172 //=========================================================================
174 //=========================================================================
175 /// Adds the string to the BlackBox category list
176 void BlackBoxDescriptor::AddToCategory( const std::string& s, bool clear)
178 bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::AddToCategory(\""
181 if (clear) mCategory = s;
185 //=========================================================================
187 //=========================================================================
188 const BlackBoxDescriptor::InputDescriptor*
189 BlackBoxDescriptor::GetInputDescriptor(const std::string & name) const
191 bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::GetInputDescriptor('"
192 <<name<<"')"<<std::endl);
194 InputDescriptorMapType::const_iterator i;
195 i = mInput.find(name);
196 if ( i == mInput.end() )
198 bbtkError("input '"<<name<<"' does not exist");
203 //=========================================================================
205 //=========================================================================
206 const BlackBoxDescriptor::OutputDescriptor*
207 BlackBoxDescriptor::GetOutputDescriptor(const std::string & name) const
209 bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::GetOutputDescriptor('"
210 <<name<<"')"<<std::endl);
212 OutputDescriptorMapType::const_iterator i;
213 i = mOutput.find(name);
214 if ( i == mOutput.end() )
216 bbtkError("output '"<<name<<"' does not exist");
221 //=========================================================================
223 //=========================================================================
224 void BlackBoxDescriptor::GetHelp(bool full) const
226 bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::GetHelp()"
229 bbtkMessage("help",1,"Black Box <"<<GetFullTypeName()<<">"<<std::endl);
230 bbtkMessage("help",1," " <<GetDescription()<<std::endl);
231 bbtkMessage("help",1," By : " <<GetAuthor() <<std::endl);
232 bbtkMessage("help",1," Categories : " <<GetCategory() <<std::endl);
234 bbtkMessage("help",1," * Inputs : "<<std::endl);
236 bbtkMessage("help",1," * No inputs"<<std::endl);
237 InputDescriptorMapType::const_iterator i;
238 unsigned int namelmax = 0;
239 unsigned int typelmax = 0;
240 unsigned int natlmax = 0;
241 for ( i = mInput.begin(); i != mInput.end(); ++i )
243 if (i->second->GetName().size()>namelmax)
244 namelmax = i->second->GetName().size();
245 if (i->second->GetHumanTypeName().size()>typelmax)
246 typelmax = i->second->GetHumanTypeName().size();
247 if (i->second->GetNature().size()>natlmax)
248 natlmax = i->second->GetNature().size();
250 OutputDescriptorMapType::const_iterator o;
251 for ( o = mOutput.begin(); o != mOutput.end(); ++o )
253 if (o->second->GetName().size()>namelmax)
254 namelmax = o->second->GetName().size();
255 if (o->second->GetHumanTypeName().size()>typelmax)
256 typelmax = o->second->GetHumanTypeName().size();
257 if (o->second->GetNature().size()>natlmax)
258 natlmax = o->second->GetNature().size();
261 for ( i = mInput.begin(); i != mInput.end(); ++i )
263 std::string name(i->second->GetName());
265 name.append(1+namelmax-name.size(),' ');
266 std::string type(i->second->GetHumanTypeName());
268 type.append(1+typelmax-type.size(),' ');
269 std::string nature(i->second->GetNature());
271 nature.append(1+natlmax-nature.size(),' ');
272 bbtkMessage("help",1,
276 <<" : "<<i->second->GetDescription()<<std::endl);
279 bbtkMessage("help",1," * Outputs : "<<std::endl);
281 bbtkMessage("help",1," * No outputs"<<std::endl);
282 for ( o = mOutput.begin(); o != mOutput.end(); ++o )
284 std::string name(o->second->GetName());
286 name.append(1+namelmax-name.size(),' ');
287 std::string type(o->second->GetHumanTypeName());
289 type.append(1+typelmax-type.size(),' ');
290 std::string nature(o->second->GetNature());
292 nature.append(1+natlmax-nature.size(),' ');
293 bbtkMessage("help",1,
297 <<" : "<<o->second->GetDescription()<<std::endl);
302 //=========================================================================
304 //=========================================================================
305 /// Returns the full name of the **TYPE** of the black box (+package name)
306 std::string BlackBoxDescriptor::GetFullTypeName() const
308 if (GetPackage()!=0) return GetPackage()->GetName() + "::" + mTypeName;
309 return "::" + mTypeName;
311 //=========================================================================
313 //=========================================================================
314 void BlackBoxDescriptor::InsertHtmlHelp ( std::ofstream& s,
315 int detail, int level,
316 const std::string& output_dir,
319 bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::InsertHtmlHelp()"
324 std::string name = GetTypeName();
325 Utilities::html_format(name);
328 (s) << "<a name=\""<<name<<"\"></a>\n";
330 "<a rel=\"top\" accesskey=\"t\" href=\"#Top\">Top</a>\n";
331 // (s) << "Previous: <a rel="previous" accesskey="p" href="#dir">(dir)</a>,
332 // (s) << "Up: <a rel="up" accesskey="u" href="#dir">(dir)</a>
333 (s) << "<h2 class=\"section\">"<<name<<"</h2>\n";
336 std::string descr = GetDescription();
337 //Utilities::html_format(descr);
338 std::string author = GetAuthor();
339 Utilities::html_format(author);
341 std::vector<std::string> categories;
342 // Split the category string
343 std::string delimiters = ";,";
344 Utilities::SplitString(GetCategory(),
345 delimiters,categories);
348 (s) << "<p><TABLE cellspacing=0 cellpadding=3>\n";
349 (s) << "<TR><TD style='vertical-align: top;'><b> Description </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
350 << descr << "</TD></TR>\n";
351 (s) << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
352 << author << "</TD></TR>\n";
353 (s) << "<TR><TD style='vertical-align: top;'><b> Category(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> ";
354 std::vector<std::string>::iterator ci;
355 for (ci=categories.begin(); ci!=categories.end(); ++ci)
357 s << "<a href=\"../index-category.html#"<< *ci <<"\">" << *ci
362 (s) << "<TR><TD style='vertical-align: top;'><b> To use it </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> include "
363 << GetPackage()->GetName() << "</TD></TR>\n";
368 //i->second->InsertHTMLGraph( &s , detail,level,dir);
372 std::string titlecol("#BBBBFF");
373 std::string usercol("#FFFFFF");
374 std::string ubbcol("#DDFFFF");
375 std::string wxbbcol("#EEFFFF");
378 // (s) << "<h3 class=\"subsection\">Inputs</h3>\n";
379 (s) << "<p><TABLE border=1 cellspacing=0 cellpadding=3>\n";
380 (s) << "<TR><TD colspan=3 align=center bgcolor=\""<<titlecol
381 <<"\">Inputs</TD></TR>\n";
383 std::vector<std::string> user_defined;
384 std::vector<std::string> ubb_defined;
385 std::vector<std::string> wxbb_defined;
387 const BlackBoxDescriptor::InputDescriptorMapType& imap =
388 GetInputDescriptorMap();
389 InputDescriptorMapType::const_iterator in;
390 for ( in = imap.begin(); in != imap.end(); ++in )
392 // Skips system-defined inputs
393 std::string col(usercol);
395 if (in->second->GetCreatorTypeInfo() ==
396 typeid(AtomicBlackBoxDescriptor))
402 else if (in->second->GetCreatorTypeInfo() ==
403 typeid(WxBlackBoxDescriptor))
410 std::string name(in->second->GetName());
411 Utilities::html_format(name);
413 std::string type("<");
414 type += in->second->GetTypeName();
416 Utilities::html_format(type);
418 std::string descr(in->second->GetDescription());
419 //Utilities::html_format(descr);
423 "<TR><TD style='vertical-align: top;' bgcolor=\"" + col
424 +"\"><B><PRE> "+name+" </PRE></B></TD>"
425 + "<TD style='vertical-align: top;' bgcolor=\""+col
426 +"\"><I><PRE> "+type+" </PRE></I></TD>"
427 + "<TD style='vertical-align: top;' bgcolor=\""+col
428 +"\">"+descr+"</TD></TR>\n";
432 "<TR><TD style='vertical-align: top;' bgcolor=\"" + col
433 +"\"><B><PRE> "+name+" </PRE></B></TD>"
434 + "<TD style='vertical-align: top;' bgcolor=\""+col
435 +"\"><I><PRE> "+descr+" </PRE></I></TD>"
436 + "<TD style='vertical-align: top;' bgcolor=\""+col
437 +"\">"+type+"</TD></TR>\n";
439 if (iotype==0) user_defined.push_back(out);
440 else if (iotype==1) ubb_defined.push_back(out);
441 else if (iotype==2) wxbb_defined.push_back(out);
445 std::vector<std::string>::iterator hi;
446 for (hi=user_defined.begin();hi!=user_defined.end();++hi) s << *hi;
447 for (hi=wxbb_defined.begin();hi!=wxbb_defined.end();++hi) s << *hi;
448 for (hi=ubb_defined.begin();hi!=ubb_defined.end();++hi) s << *hi;
450 user_defined.clear();
452 wxbb_defined.clear();
456 // (s) << "<h3 class=\"subsection\">Outputs</h3>\n";
457 // (s) << "<TABLE border=1 cellspacing=0>\n";
458 (s) << "<TR><TD colspan=3 align=center bgcolor=\""<<titlecol
459 <<"\">Outputs</TD></TR>\n";
461 const BlackBoxDescriptor::OutputDescriptorMapType& omap =
462 GetOutputDescriptorMap();
464 BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
466 for ( o = omap.begin(); o != omap.end(); ++o )
468 std::string col(usercol);
470 if (o->second->GetCreatorTypeInfo() ==
471 typeid(AtomicBlackBoxDescriptor))
477 else if (o->second->GetCreatorTypeInfo() ==
478 typeid(WxBlackBoxDescriptor))
485 std::string name(o->second->GetName());
486 Utilities::html_format(name);
488 std::string type("<");
489 type += o->second->GetTypeName();
491 Utilities::html_format(type);
493 std::string descr(o->second->GetDescription());
494 //Utilities::html_format(descr);
498 "<TR><TD style='vertical-align: top;' bgcolor=\"" + col
499 +"\"><B><PRE> "+name+" </PRE></B></TD>"
500 + "<TD style='vertical-align: top;' bgcolor=\""+col
501 +"\"><I><PRE> "+type+" </PRE></I></TD>"
502 + "<TD style='vertical-align: top;' bgcolor=\""+col
503 +"\">"+descr+"</TD></TR>\n";
506 "<TR><TD style='vertical-align: top;' bgcolor=\"" + col
507 +"\"><B><PRE> "+name+" </PRE></B></TD>"
508 + "<TD style='vertical-align: top;' bgcolor=\""+col
509 +"\"><I><PRE> "+descr+" </PRE></I></TD>"
510 + "<TD style='vertical-align: top;' bgcolor=\""+col
511 +"\">"+type+"</TD></TR>\n";
513 if (iotype==0) user_defined.push_back(out);
514 else if (iotype==1) ubb_defined.push_back(out);
515 else if (iotype==2) wxbb_defined.push_back(out);
519 for (hi=user_defined.begin();hi!=user_defined.end();++hi) s << *hi;
520 for (hi=wxbb_defined.begin();hi!=wxbb_defined.end();++hi) s << *hi;
521 for (hi=ubb_defined.begin();hi!=ubb_defined.end();++hi) s << *hi;
530 //=========================================================================
533 //==========================================================================
534 void BlackBoxDescriptor::SetTypeOfScript_Application()
536 SetTypeOfScript(TS_SCRIPT_APPLICATION);
538 //==========================================================================
541 //==========================================================================
542 bool BlackBoxDescriptor::IsTypeOfScript_Application()
544 return (GetTypeOfScript()==TS_SCRIPT_APPLICATION);
546 //==========================================================================
549 //==========================================================================
550 bool BlackBoxDescriptor::IsTypeOfScript_ComplexBox()
552 return (GetTypeOfScript()==TS_SCRIPT_COMPLEXBOX);
554 //==========================================================================