1 /*=========================================================================
4 Module: $RCSfile: bbtkTranscriptor.cxx,v $ $
6 Date: $Date: 2008/02/14 11:38:58 $
7 Version: $Revision: 1.1 $
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);
284 Current()->AddToExecutionList(nodeName) ;
291 void Transcriptor::DefineInput ( const std::string &name,
292 const std::string &box,
293 const std::string &input,
294 const std::string& help)
296 // If the input is defined in the Root box
297 if (Current()==mRoot)
299 m_Fp << "// ==> Some extra work to do when Current()==mRoot" << std::endl;
302 m_Fp << "e->DefineInput("<< name << ", " << box << ", "
303 << input << ", " << help << ");" << std::endl;
307 // If the input is defined in the Root box
308 if (Current()==mRoot)
310 // If the dialog mode is set to NoDialog
311 // and the user passed the name in the Inputs map
312 // then the associated value is set to the box.input
313 // This is the way command line parameters are passed to the Root box
314 if (mDialogMode == NoDialog)
316 // find if name is in mInputs
317 std::map<std::string,std::string>::iterator i;
318 i = mInputs.find(name);
319 if (i!=mInputs.end()) {
320 Set(box,input,(*i).second);
323 // If the dialog mode is set to TextDialog
324 // The user is prompted for the value
325 else if (mDialogMode == TextDialog)
327 std::cout << name << "=";
332 #ifdef _USE_WXWIDGETS_
333 // If the dialog mode is set to GraphicalDialog
334 // A dialog box is pop up
335 else if (mDialogMode == GraphicalDialog)
337 std::string mess("Enter the value of '");
342 std::string title(name);
345 std::string ans = wx2std ( wxGetTextFromUser( std2wx (mess), std2wx(title)));
351 Current()->DefineInput(name,box,input,help);
359 void Transcriptor::DefineOutput ( const std::string &name,
360 const std::string &box,
361 const std::string &output,
362 const std::string& help)
364 // Current()->DefineOutput(name,box,output,help);
365 m_Fp << "e->DefineOutput("<< name << ", " << box << ", "
366 << output << ", " << help << ");" << std::endl;
372 void Transcriptor::Set (const std::string &box,
373 const std::string &input,
374 const std::string &value)
377 m_Fp << "// ==> Set() called here" << std::endl;
379 BlackBox* b = Current()->GetPrototype()->bbGetBlackBox(box);
380 // Looks for the adaptor
382 if ( b->bbGetInputType(input) != typeid(std::string) )
385 NewAdaptor(typeid(std::string),
386 b->bbGetInputType(input),
391 TypeName(b->bbGetInputType(input))
392 <<"> to <std::string> found");
394 std::string v(value);
395 a->bbSetInput("In",v);
397 b->bbSetInput(input,a->bbGetOutput("Out"));
402 std::string v(value);
403 b->bbSetInput(input,v);
411 std::string Transcriptor::Get(const std::string &box,
412 const std::string &output)
415 m_Fp << "// ==> Get() called here" << std::endl;
417 BlackBox* b = Current()->GetPrototype()->bbGetBlackBox(box);
418 // Looks for the adaptor
419 if (b->bbGetOutputType(output) != typeid(std::string))
423 b->bbGetOutputType(output),
429 TypeName(b->bbGetOutputType(output))
430 <<"> to <std::string> found");
434 a->bbSetInput("In",b->bbGetOutput(output));
436 std::string r = a->bbGetOutput("Out").unsafe_get<std::string>();
437 //std::string v = *((std::string*)a->bbGetOutput("Out")) ;
438 // std::cout << a->bbGetOutput("Out").unsafe_get<std::string>()
440 //std::string v(value);
441 //b->bbSetInput(input,a->bbGetOutput("Out"));
448 return b->bbGetOutput(output).unsafe_get<std::string>();
449 // std::string v = *((std::string*)b->bbGetOutput(output)) ;
450 // std::cout << b->bbGetOutput("Out").unsafe_get<std::string>()
452 // b->bbSetInput(input,&v);
458 void Transcriptor::Author(const std::string &authorName)
460 //Current()->AddToAuthor(authorName,Current()==mRoot);
461 if (Current()==mRoot)
462 m_Fp << "e->AddToAuthor(" << authorName << ", true)" << std::endl;
464 m_Fp << "e->AddToAuthor(" << authorName << ", false)" << std::endl;
467 void Transcriptor::Category(const std::string &category)
469 //Current()->AddToCategory(category,Current()==mRoot);
470 if (Current()==mRoot)
471 m_Fp << "e->AddToCategory(" << category << ", true)" << std::endl;
473 m_Fp << "e->AddToCategory(" << category << ", false)" << std::endl;
476 void Transcriptor::Description(const std::string &d)
478 // Current()->AddToDescription(d,Current()==mRoot);
479 if (Current()==mRoot)
480 m_Fp << "e->AddToDescription(" << d << ", true)" << std::endl;
482 m_Fp << "e->AddToDescription(" << d << ", false)" << std::endl;
486 /// prints the list of the boxes of the current descriptor
487 void Transcriptor::PrintBoxes()
489 m_Fp << "// ==> :PrintBoxes() called here" << std::endl;
491 bbtkMessageInc("Help",1,"The black box descriptor \""
492 <<Current()->GetTypeName()<<"\" contains : "<<std::endl);
493 Current()->PrintBlackBoxes();
494 bbtkDecTab("Help",1);
498 // =========================End of Battelfield ========================================================================
500 std::string Transcriptor::ShowGraph(const std::string &nameblackbox,
501 const std::string &detailStr,
502 const std::string &levelStr,
503 const std::string &output_html,
504 const std::string &custom_header,
505 const std::string &custom_title,
506 bool system_display )
508 int detail = atoi(detailStr.c_str());
509 int level = atoi(levelStr.c_str());
511 std::string filename_rootHtml (output_html) ;
512 std::string simplefilename_rootHtml ( Utilities::get_file_name(output_html));
514 bool relative_link = true;
516 // No output provided : automatic generation
517 if (output_html.length() == 0)
519 // Don't pollute the file store with "temp_dir" directories ...
520 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
522 char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
524 std::string directory = default_doc_dir;
525 if (c != '/' && c !='\\') directory = directory + "/";
526 directory = directory + "temp_dir";
528 filename_rootHtml = directory + "/" + "User.html";
529 simplefilename_rootHtml = "User.html" ;
531 // Creating directory
532 std::string command0("mkdir \"" +directory + "\"");
533 system( command0.c_str() );
535 relative_link = false;
541 p = GetGlobalFactory()->GetPackage(nameblackbox);
547 // Generating documentation-help of workspace
548 p->SetDocURL(filename_rootHtml);
549 p->SetDocRelativeURL(simplefilename_rootHtml);
551 p->CreateHtmlPage(filename_rootHtml,"bbi","user package",custom_header,custom_title,detail,level,relative_link);
553 std::string page = filename_rootHtml;
557 ShowGraphTypes(nameblackbox);
559 catch (bbtk::Exception a)
561 std::cout <<"EXC"<<std::endl;
562 page = ShowGraphInstances(nameblackbox,detail,level,system_display);
568 /// Generate a png file with the actual pipeline (Graphviz-dot needed)
569 std::string Transcriptor::ShowGraphInstances(const std::string &nameblackbox, int detail, int level,
573 BlackBox* blackbox=NULL;
574 if (nameblackbox==".")
576 blackbox=Current()->GetPrototype();
580 blackbox = Current()->GetPrototype()->bbFindBlackBox(nameblackbox);
587 // Don't pollute the file store with "temp_dir" directories ...
588 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
589 char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
591 std::string directory = default_doc_dir;
592 if (c != '/' && c !='\\') directory = directory + "/";
594 directory = directory + "temp_dir";
596 //std::string directory("temp_dir");
597 std::string filename(directory + "/" + "bbtk_graph_pipeline");
598 std::string filename_html(filename+".html");
599 std::string command0("mkdir \""+directory + "\"");
602 std::string command2("start ");
604 std::string command2("gnome-open ");
607 command2=command2+filename_html;
608 page = filename_html;
609 // 1. Generate Html Diagram
611 s.open(filename_html.c_str());
614 s << "<html><head><title>BBtk graph diagram</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"></head>\n";
615 s << "<body bgcolor=\"#FFFFFF\" text=\"#000000\"> \n\n";
616 if ( blackbox->bbGetName()=="workspacePrototype" )
618 s << "<center>Current workspace</center>";
620 s << "<center>" << blackbox->bbGetName()<< "</center>";
623 blackbox->bbInsertHTMLGraph( s, detail, level, true, directory, false );
624 s << "</body></html>\n";
628 // 2. Starting Browser
629 if (system_display) system( command2.c_str() );
633 bbtkMessageInc("Help",1,"No black box: \""
634 <<nameblackbox<<"\" " <<std::endl);
640 void Transcriptor::ShowRelations(const std::string &nameblackbox, const std::string &detailStr, const std::string &levelStr)
644 int detail = atoi(detailStr.c_str());
645 int level = atoi(levelStr.c_str());
646 BlackBox* blackbox=NULL;
647 if (nameblackbox.compare(".")==0)
649 blackbox=Current()->GetPrototype();
651 blackbox = Current()->GetPrototype()->bbFindBlackBox(nameblackbox);
657 blackbox->bbShowRelations(blackbox,detail,level); //,mFactory);
662 bbtkError("Blackbox Name not found.. <" <<nameblackbox<<">");
667 /// sets the level of message
668 void Transcriptor::Message(const std::string &kind,
669 const std::string& level)
672 sscanf(level.c_str(),"%d",&l);
673 bbtk::MessageManager::SetMessageLevel(kind,l);