1 /*=========================================================================
4 Module: $RCSfile: bbtkTranscriptor.cxx,v $ $
6 Date: $Date: 2008/02/14 13:48:13 $
7 Version: $Revision: 1.2 $
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.
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.
17 =========================================================================*/
20 * \brief class Transcriptor: level 0 of script execution (code)
23 #include "bbtkTranscriptor.h"
24 #include "bbtkMessageManager.h"
25 #include "bbtkFactory.h"
26 #include "bbtkUtilities.h"
29 //#ifdef _USE_WXWIDGETS_
30 //#include <wx/textdlg.h>
33 //#include "bbtkWxBlackBox.h"
35 #include "bbtkConfigurationFile.h"
42 Transcriptor::Transcriptor(std::string filename)
49 bbtkDebugMessageInc("Kernel",9,"Transcriptor::Transcriptor()" <<std::endl);
51 bbtkDebugDecTab("Kernel",9);
53 //std::ofstream *m_Fp = new std::ofstream();
54 m_Fp.open (filename.c_str(), std::ios::out );
56 m_Fp << "#include \"bbtkExecuter.h\"" << std::endl;
57 m_Fp << "main(int argc, char *argv[]) {" << std::endl;
58 m_Fp << " bbtk::Executer *e = new bbtk::Executer();" << std::endl;
64 Transcriptor::~Transcriptor()
66 bbtkDebugMessageInc("Kernel",9,"Transcriptor::~Transcriptor()" <<std::endl);
69 mPackage->UnRegisterBlackBox("workspace");
74 GetGlobalFactory()->UnLoadPackage("user");
79 bbtkDebugDecTab("Kernel",9);
80 m_Fp << " }" << std::endl;
87 // ========================= Begin of Battelfield ========================================================================
93 void Transcriptor::Reset()
96 m_Fp << "e->Reset( );" << std::endl;
98 bbtkDebugMessageInc("Kernel",9,"Transcriptor::Reset()" <<std::endl);
100 // The 'user' package must be closed before all other
101 // because box destructors must not be unloaded when bb are deleted!
102 // Similarly, the 'workspace' CBB must be destroyed before
103 // all user defined CBB otherwise any instance
104 // of a user CBB that is in the 'workspace' would try to
105 // access a user CBB descriptor which has been previously freed
108 mPackage->UnRegisterBlackBox(mRoot->GetTypeName());
113 GetGlobalFactory()->UnLoadPackage("user");
115 GetGlobalFactory()->Reset();
116 // Create user package
117 mPackage = new Package("user","internal to bbi",
118 "User defined black boxes",
120 BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
121 // Create user workspace
122 mRoot = new ComplexBlackBoxDescriptor("workspace"); //,f);
123 mRoot->AddToAuthor("bbi (internal)");
124 mRoot->AddToDescription("User's workspace");
125 mOpenDefinition.push_back(CBBDefinition(mRoot,"user"));
126 // Register it into the user package
127 mPackage->RegisterBlackBox(mRoot);
128 // Insert the user package in the factory
129 InsertPackage(mPackage);
130 mOpenPackage.push_back(mPackage);
131 bbtkDebugDecTab("Kernel",9);
135 /// changes the workspace name
136 void Transcriptor::SetWorkspaceName( const std::string& n )
139 m_Fp << "// ==> SetWorkspaceName() called here" << std::endl;
140 // mPackage->ChangeBlackBoxName( mRoot->GetTypeName(), n );
143 void Transcriptor::BeginPackage (const std::string &name)
145 m_Fp << "// ==> BeginPackage() called here" << std::endl;
147 bbtkDebugMessageInc("Kernel",9,"Executer::BeginPackage(\""<<name<<"\")"
152 p = GetGlobalFactory()->GetPackage(name);
156 p = new Package(name,
160 BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
163 mOpenPackage.push_back(p);
167 void Transcriptor::EndPackage()
169 m_Fp << "// ==> EndPackage() called here" << std::endl;
170 // if (mOpenPackage.size()>1) mOpenPackage.pop_back();
173 void Transcriptor::Define (const std::string &name,
174 const std::string &pack,
175 const std::string &scriptfilename)
177 m_Fp << "// on Define, Define() called here" << std::endl;
179 bbtkDebugMessageInc("Kernel",9,"Executer::Define(\""<<name<<
183 ComplexBlackBoxDescriptor* b = new ComplexBlackBoxDescriptor(name);
184 b->SetScriptFileName(scriptfilename);
185 mOpenDefinition.push_back( CBBDefinition( b, pack ) );
187 bbtkDebugDecTab("Kernel",9);
191 /// Sets the file name to use for the current definition
192 /// (Used to set it after the Define command)
193 void Transcriptor::SetCurrentFileName (const std::string &name )
196 m_Fp << "// ==>on Include : SetCurrentFileName( ) called here" << std::endl;
197 // mOpenDefinition.back().box->SetScriptFileName(name);
200 void Transcriptor::EndDefine ()
202 m_Fp << "// ==> on EndDefine, EndDefine( ); called here " << std::endl;
205 bbtkDebugMessageInc("Kernel",9,"Executer::EndDefine(\""
206 <<Current()->GetTypeName()<<"\")"
208 // Does current package exist ?
210 std::string pname(mOpenDefinition.back().package);
215 p = GetGlobalFactory()->GetPackage(pname);
219 p = new Package(pname,
223 BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
229 p = mOpenPackage.back();
231 p->RegisterBlackBox(Current());
233 mOpenDefinition.pop_back();
239 void Transcriptor::Create ( const std::string& nodeType,
240 const std::string& nodeName)
242 // Current()->Add(nodeType,nodeName);\"
243 m_Fp << "e->Add("\" << nodeType << "\", \"" << nodeName << "\");" << std::endl;
248 void Transcriptor::Remove (const std::string &nodeName)
251 // Current()->RemoveBlackBox(nodeName);
252 // m_Fp << "e->Remove(" << nodeName << ");" << std::endl;
259 void Transcriptor::Connect (const std::string &nodeFrom,
260 const std::string &outputLabel,
261 const std::string &nodeTo,
262 const std::string &inputLabel)
264 //Current()->Connect(nodeFrom, outputLabel, nodeTo, inputLabel);
265 m_Fp << "e->Connect("<< nodeFrom << "\", \"" << outputLabel << "\", \""
266 << nodeTo << "\", \"" << inputLabel<< "\");" << std::endl;
272 void Transcriptor::Update (const std::string &nodeName) // would 'Execute' be more meaningfull ?
275 if (Current()==mRoot)
279 //Current()->GetPrototype()->bbGetBlackBox(nodeName)->bbExecute(true);
280 m_Fp << "e->GetPrototype()->bbGetBlackBox("\" << nodeName << "\", true );" << std::endl;
285 //Current()->AddToExecutionList(nodeName) ;
286 m_Fp << "e->AddToExecutionList("\" << nodeName << "\");" << std::endl;
293 void Transcriptor::DefineInput ( const std::string &name,
294 const std::string &box,
295 const std::string &input,
296 const std::string& help)
298 // If the input is defined in the Root box
299 if (Current()==mRoot)
301 m_Fp << "// ==> Some extra work to do when Current()==mRoot" << std::endl;
304 m_Fp << "e->DefineInput(\""<< name << "\", " << box << ", "
305 << input << ", \"" << help << "\");" << std::endl;
309 // If the input is defined in the Root box
310 if (Current()==mRoot)
312 // If the dialog mode is set to NoDialog
313 // and the user passed the name in the Inputs map
314 // then the associated value is set to the box.input
315 // This is the way command line parameters are passed to the Root box
316 if (mDialogMode == NoDialog)
318 // find if name is in mInputs
319 std::map<std::string,std::string>::iterator i;
320 i = mInputs.find(name);
321 if (i!=mInputs.end()) {
322 Set(box,input,(*i).second);
325 // If the dialog mode is set to TextDialog
326 // The user is prompted for the value
327 else if (mDialogMode == TextDialog)
329 std::cout << name << "=";
334 #ifdef _USE_WXWIDGETS_
335 // If the dialog mode is set to GraphicalDialog
336 // A dialog box is pop up
337 else if (mDialogMode == GraphicalDialog)
339 std::string mess("Enter the value of '");
344 std::string title(name);
347 std::string ans = wx2std ( wxGetTextFromUser( std2wx (mess), std2wx(title)));
353 Current()->DefineInput(name,box,input,help);
361 void Transcriptor::DefineOutput ( const std::string &name,
362 const std::string &box,
363 const std::string &output,
364 const std::string& help)
366 // Current()->DefineOutput(name,box,output,help);
367 m_Fp << "e->DefineOutput(\""<< name << "\", \"" << box << "\", \""
368 << output << "\", \"" << help << "\");" << std::endl;
374 void Transcriptor::Set (const std::string &box,
375 const std::string &input,
376 const std::string &value)
379 m_Fp << "// ==> Set() called here" << std::endl;
381 BlackBox* b = Current()->GetPrototype()->bbGetBlackBox(box);
382 // Looks for the adaptor
384 if ( b->bbGetInputType(input) != typeid(std::string) )
387 NewAdaptor(typeid(std::string),
388 b->bbGetInputType(input),
393 TypeName(b->bbGetInputType(input))
394 <<"> to <std::string> found");
396 std::string v(value);
397 a->bbSetInput("In",v);
399 b->bbSetInput(input,a->bbGetOutput("Out"));
404 std::string v(value);
405 b->bbSetInput(input,v);
413 std::string Transcriptor::Get(const std::string &box,
414 const std::string &output)
417 m_Fp << "// ==> Get() called here" << std::endl;
419 BlackBox* b = Current()->GetPrototype()->bbGetBlackBox(box);
420 // Looks for the adaptor
421 if (b->bbGetOutputType(output) != typeid(std::string))
425 b->bbGetOutputType(output),
431 TypeName(b->bbGetOutputType(output))
432 <<"> to <std::string> found");
436 a->bbSetInput("In",b->bbGetOutput(output));
438 std::string r = a->bbGetOutput("Out").unsafe_get<std::string>();
439 //std::string v = *((std::string*)a->bbGetOutput("Out")) ;
440 // std::cout << a->bbGetOutput("Out").unsafe_get<std::string>()
442 //std::string v(value);
443 //b->bbSetInput(input,a->bbGetOutput("Out"));
450 return b->bbGetOutput(output).unsafe_get<std::string>();
451 // std::string v = *((std::string*)b->bbGetOutput(output)) ;
452 // std::cout << b->bbGetOutput("Out").unsafe_get<std::string>()
454 // b->bbSetInput(input,&v);
460 void Transcriptor::Author(const std::string &authorName)
462 //Current()->AddToAuthor(authorName,Current()==mRoot);
463 if (Current()==mRoot)
464 m_Fp << "e->AddToAuthor(\"" << authorName << "\", true)" << std::endl;
466 m_Fp << "e->AddToAuthor(\"" << authorName << "\", false)" << std::endl;
469 void Transcriptor::Category(const std::string &category)
471 //Current()->AddToCategory(category,Current()==mRoot);
472 if (Current()==mRoot)
473 m_Fp << "e->AddToCategory(\"" << category << "\", true)" << std::endl;
475 m_Fp << "e->AddToCategory(\"" << category << "\", false)" << std::endl;
478 void Transcriptor::Description(const std::string &d)
480 // Current()->AddToDescription(d,Current()==mRoot);
481 if (Current()==mRoot)
482 m_Fp << "e->AddToDescription(\"" << d << "\", true)" << std::endl;
484 m_Fp << "e->AddToDescription(\"" << d << "\", false)" << std::endl;
488 /// prints the list of the boxes of the current descriptor
489 void Transcriptor::PrintBoxes()
491 m_Fp << "// ==> :PrintBoxes() called here" << std::endl;
493 bbtkMessageInc("Help",1,"The black box descriptor \""
494 <<Current()->GetTypeName()<<"\" contains : "<<std::endl);
495 Current()->PrintBlackBoxes();
496 bbtkDecTab("Help",1);
500 // =========================End of Battelfield ========================================================================
502 std::string Transcriptor::ShowGraph(const std::string &nameblackbox,
503 const std::string &detailStr,
504 const std::string &levelStr,
505 const std::string &output_html,
506 const std::string &custom_header,
507 const std::string &custom_title,
508 bool system_display )
510 int detail = atoi(detailStr.c_str());
511 int level = atoi(levelStr.c_str());
513 std::string filename_rootHtml (output_html) ;
514 std::string simplefilename_rootHtml ( Utilities::get_file_name(output_html));
516 bool relative_link = true;
518 // No output provided : automatic generation
519 if (output_html.length() == 0)
521 // Don't pollute the file store with "temp_dir" directories ...
522 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
524 char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
526 std::string directory = default_doc_dir;
527 if (c != '/' && c !='\\') directory = directory + "/";
528 directory = directory + "temp_dir";
530 filename_rootHtml = directory + "/" + "User.html";
531 simplefilename_rootHtml = "User.html" ;
533 // Creating directory
534 std::string command0("mkdir \"" +directory + "\"");
535 system( command0.c_str() );
537 relative_link = false;
543 p = GetGlobalFactory()->GetPackage(nameblackbox);
549 // Generating documentation-help of workspace
550 p->SetDocURL(filename_rootHtml);
551 p->SetDocRelativeURL(simplefilename_rootHtml);
553 p->CreateHtmlPage(filename_rootHtml,"bbi","user package",custom_header,custom_title,detail,level,relative_link);
555 std::string page = filename_rootHtml;
559 ShowGraphTypes(nameblackbox);
561 catch (bbtk::Exception a)
563 std::cout <<"EXC"<<std::endl;
564 page = ShowGraphInstances(nameblackbox,detail,level,system_display);
570 /// Generate a png file with the actual pipeline (Graphviz-dot needed)
571 std::string Transcriptor::ShowGraphInstances(const std::string &nameblackbox, int detail, int level,
575 BlackBox* blackbox=NULL;
576 if (nameblackbox==".")
578 blackbox=Current()->GetPrototype();
582 blackbox = Current()->GetPrototype()->bbFindBlackBox(nameblackbox);
589 // Don't pollute the file store with "temp_dir" directories ...
590 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
591 char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
593 std::string directory = default_doc_dir;
594 if (c != '/' && c !='\\') directory = directory + "/";
596 directory = directory + "temp_dir";
598 //std::string directory("temp_dir");
599 std::string filename(directory + "/" + "bbtk_graph_pipeline");
600 std::string filename_html(filename+".html");
601 std::string command0("mkdir \""+directory + "\"");
604 std::string command2("start ");
606 std::string command2("gnome-open ");
609 command2=command2+filename_html;
610 page = filename_html;
611 // 1. Generate Html Diagram
613 s.open(filename_html.c_str());
616 s << "<html><head><title>BBtk graph diagram</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"></head>\n";
617 s << "<body bgcolor=\"#FFFFFF\" text=\"#000000\"> \n\n";
618 if ( blackbox->bbGetName()=="workspacePrototype" )
620 s << "<center>Current workspace</center>";
622 s << "<center>" << blackbox->bbGetName()<< "</center>";
625 blackbox->bbInsertHTMLGraph( s, detail, level, true, directory, false );
626 s << "</body></html>\n";
630 // 2. Starting Browser
631 if (system_display) system( command2.c_str() );
635 bbtkMessageInc("Help",1,"No black box: \""
636 <<nameblackbox<<"\" " <<std::endl);
642 void Transcriptor::ShowRelations(const std::string &nameblackbox, const std::string &detailStr, const std::string &levelStr)
646 int detail = atoi(detailStr.c_str());
647 int level = atoi(levelStr.c_str());
648 BlackBox* blackbox=NULL;
649 if (nameblackbox.compare(".")==0)
651 blackbox=Current()->GetPrototype();
653 blackbox = Current()->GetPrototype()->bbFindBlackBox(nameblackbox);
659 blackbox->bbShowRelations(blackbox,detail,level); //,mFactory);
664 bbtkError("Blackbox Name not found.. <" <<nameblackbox<<">");
669 /// sets the level of message
670 void Transcriptor::Message(const std::string &kind,
671 const std::string& level)
674 sscanf(level.c_str(),"%d",&l);
675 bbtk::MessageManager::SetMessageLevel(kind,l);