1 /*=========================================================================
4 Module: $RCSfile: bbtkExecuter.cxx,v $ $
6 Date: $Date: 2008/01/30 09:28:15 $
7 Version: $Revision: 1.4 $
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 Executer: level 0 of script execution (code)
23 #include "bbtkExecuter.h"
24 #include "bbtkMessageManager.h"
25 #include "bbtkFactory.h"
26 #include "bbtkUtilities.h"
30 #ifdef _USE_WXWIDGETS_
31 #include <wx/textdlg.h>
34 #include "bbtkWxBlackBox.h"
36 #include "bbtkConfigurationFile.h"
51 bbtkDebugMessageInc("Core",9,"Executer::Executer()" <<std::endl);
53 bbtkDebugDecTab("Core",9);
61 bbtkDebugMessageInc("Core",9,"Executer::~Executer()" <<std::endl);
64 mPackage->UnRegisterBlackBox("workspace");
69 GetGlobalFactory()->UnLoadPackage("user");
71 bbtkDebugDecTab("Core",9);
78 void Executer::Reset()
80 bbtkDebugMessageInc("Core",9,"Executer::Reset()" <<std::endl);
82 // The 'user' package must be closed before all other
83 // because box destructors must not be unloaded when bb are deleted!
84 // Similarly, the 'workspace' CBB must be destroyed before
85 // all user defined CBB otherwise any instance
86 // of a user CBB that is in the 'workspace' would try to
87 // access a user CBB descriptor which has been previously freed
90 mPackage->UnRegisterBlackBox(mRoot->GetTypeName());
95 GetGlobalFactory()->UnLoadPackage("user");
97 GetGlobalFactory()->Reset();
98 // Create user package
99 mPackage = new Package("user","internal to bbi",
100 "User defined black boxes",
102 BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
103 // Create user workspace
104 mRoot = new ComplexBlackBoxDescriptor("workspace"); //,f);
105 mRoot->AddToAuthor("bbi (internal)");
106 mRoot->AddToDescription("User's workspace");
107 mOpenDefinition.push_back(CBBDefinition(mRoot,"user"));
108 // Register it into the user package
109 mPackage->RegisterBlackBox(mRoot);
110 // Insert the user package in the factory
111 InsertPackage(mPackage);
112 mOpenPackage.push_back(mPackage);
113 bbtkDebugDecTab("Core",9);
116 /// changes the workspace name
117 void Executer::SetWorkspaceName( const std::string& n )
119 mPackage->ChangeBlackBoxName( mRoot->GetTypeName(), n );
122 void Executer::BeginPackage (const std::string &name)
124 bbtkDebugMessageInc("Core",9,"Executer::BeginPackage(\""<<name<<"\")"
129 p = GetGlobalFactory()->GetPackage(name);
133 p = new Package(name,
137 BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
140 mOpenPackage.push_back(p);
143 void Executer::EndPackage()
145 if (mOpenPackage.size()>1) mOpenPackage.pop_back();
148 void Executer::Define (const std::string &name,
149 const std::string &pack,
150 const std::string &scriptfilename)
152 bbtkDebugMessageInc("Core",9,"Executer::Define(\""<<name<<
156 ComplexBlackBoxDescriptor* b = new ComplexBlackBoxDescriptor(name);
157 b->SetScriptFileName(scriptfilename);
158 mOpenDefinition.push_back( CBBDefinition( b, pack ) );
160 bbtkDebugDecTab("Core",9);
163 void Executer::EndDefine ()
165 bbtkDebugMessageInc("Core",9,"Executer::EndDefine(\""
166 <<Current()->GetTypeName()<<"\")"
168 // Does current package exist ?
171 std::string pname(mOpenDefinition.back().package);
176 p = GetGlobalFactory()->GetPackage(pname);
180 p = new Package(pname,
184 BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
190 p = mOpenPackage.back();
192 p->RegisterBlackBox(Current());
194 mOpenDefinition.pop_back();
198 void Executer::Create ( const std::string& nodeType,
199 const std::string& nodeName)
201 Current()->Add(nodeType,nodeName);
206 void Executer::Remove (const std::string &nodeName)
208 // Current()->RemoveBlackBox(nodeName);
215 void Executer::Connect (const std::string &nodeFrom,
216 const std::string &outputLabel,
217 const std::string &nodeTo,
218 const std::string &inputLabel)
220 Current()->Connect(nodeFrom, outputLabel, nodeTo, inputLabel);
226 void Executer::Update (const std::string &nodeName) // would 'Execute' be more meaningfull ?
229 if (Current()==mRoot)
233 Current()->GetPrototype()->bbGetBlackBox(nodeName)->bbExecute(true);
238 Current()->AddToExecutionList(nodeName) ;
245 void Executer::DefineInput ( const std::string &name,
246 const std::string &box,
247 const std::string &input,
248 const std::string& help)
250 // If the input is defined in the Root box
251 if (Current()==mRoot)
253 // If the dialog mode is set to NoDialog
254 // and the user passed the name in the Inputs map
255 // then the associated value is set to the box.input
256 // This is the way command line parameters are passed to the Root box
257 if (mDialogMode == NoDialog)
259 // find if name is in mInputs
260 std::map<std::string,std::string>::iterator i;
261 i = mInputs.find(name);
262 if (i!=mInputs.end()) {
263 Set(box,input,(*i).second);
266 // If the dialog mode is set to TextDialog
267 // The user is prompted for the value
268 else if (mDialogMode == TextDialog)
270 std::cout << name << "=";
275 #ifdef _USE_WXWIDGETS_
276 // If the dialog mode is set to GraphicalDialog
277 // A dialog box is pop up
278 else if (mDialogMode == GraphicalDialog)
280 std::string mess("Enter the value of '");
285 std::string title(name);
288 std::string ans = wx2std ( wxGetTextFromUser( std2wx (mess), std2wx(title)));
294 Current()->DefineInput(name,box,input,help);
301 void Executer::DefineOutput ( const std::string &name,
302 const std::string &box,
303 const std::string &output,
304 const std::string& help)
306 Current()->DefineOutput(name,box,output,help);
312 void Executer::Set (const std::string &box,
313 const std::string &input,
314 const std::string &value)
316 BlackBox* b = Current()->GetPrototype()->bbGetBlackBox(box);
317 // Looks for the adaptor
319 if ( b->bbGetInputType(input) != typeid(std::string) )
321 BlackBox* a = /*mFactory->*/
322 NewAdaptor(typeid(std::string),
323 b->bbGetInputType(input),
328 TypeName(b->bbGetInputType(input))
329 <<"> to <std::string> found");
331 std::string v(value);
332 a->bbSetInput("In",v);
334 b->bbSetInput(input,a->bbGetOutput("Out"));
339 std::string v(value);
340 b->bbSetInput(input,v);
347 std::string Executer::Get(const std::string &box,
348 const std::string &output)
350 BlackBox* b = Current()->GetPrototype()->bbGetBlackBox(box);
351 // Looks for the adaptor
352 if (b->bbGetOutputType(output) != typeid(std::string))
354 BlackBox* a = /*mFactory->*/
356 b->bbGetOutputType(output),
362 TypeName(b->bbGetOutputType(output))
363 <<"> to <std::string> found");
367 a->bbSetInput("In",b->bbGetOutput(output));
369 std::string r = a->bbGetOutput("Out").unsafe_get<std::string>();
370 //std::string v = *((std::string*)a->bbGetOutput("Out")) ;
371 // std::cout << a->bbGetOutput("Out").unsafe_get<std::string>()
373 //std::string v(value);
374 //b->bbSetInput(input,a->bbGetOutput("Out"));
381 return b->bbGetOutput(output).unsafe_get<std::string>();
382 // std::string v = *((std::string*)b->bbGetOutput(output)) ;
383 // std::cout << b->bbGetOutput("Out").unsafe_get<std::string>()
385 // b->bbSetInput(input,&v);
390 void Executer::Author(const std::string &authorName)
392 Current()->AddToAuthor(authorName,Current()==mRoot);
395 void Executer::Keyword(const std::string &keyword)
397 Current()->AddToKeyword(keyword,Current()==mRoot);
400 void Executer::Description(const std::string &d)
402 Current()->AddToDescription(d,Current()==mRoot);
406 /// prints the list of the boxes of the current descriptor
407 void Executer::PrintBoxes()
409 bbtkMessageInc("Help",1,"The black box descriptor \""
410 <<Current()->GetTypeName()<<"\" contains : "<<std::endl);
411 Current()->PrintBlackBoxes();
412 bbtkDecTab("Help",1);
416 std::string Executer::ShowGraph(const std::string &nameblackbox,
417 const std::string &detailStr,
418 const std::string &levelStr,
419 const std::string &output_html,
420 const std::string &custom_header,
421 const std::string &custom_title,
422 bool system_display )
425 int detail = atoi(detailStr.c_str());
426 int level = atoi(levelStr.c_str());
428 std::string filename_rootHtml (output_html) ;
429 std::string simplefilename_rootHtml ( Utilities::get_file_name(output_html));
431 bool relative_link = true;
433 // No output provided : automatic generation
434 if (output_html.length() == 0)
436 // Don't pollute the file store with "doc_tmp" directories ...
437 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_doc_tmp();
439 char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
441 std::string directory = default_doc_dir;
442 if (c != '/' && c !='\\') directory = directory + "/";
443 directory = directory + "doc_tmp";
445 filename_rootHtml = directory + "/" + "User.html";
446 simplefilename_rootHtml = "User.html" ;
448 // Creating directory
449 std::string command0("mkdir \"" +directory + "\"");
450 system( command0.c_str() );
452 relative_link = false;
459 p = GetGlobalFactory()->GetPackage(nameblackbox);
465 // Generating documentation-help of workspace
466 p->SetDocURL(filename_rootHtml);
467 p->SetDocRelativeURL(simplefilename_rootHtml);
469 p->CreateHtmlPage(filename_rootHtml,"bbi","user package",custom_header,custom_title,detail,level,relative_link);
471 std::string page = filename_rootHtml;
475 ShowGraphTypes(nameblackbox);
477 catch (bbtk::Exception a)
479 std::cout <<"EXC"<<std::endl;
480 page = ShowGraphInstances(nameblackbox,detail,level,system_display);
486 /// Generate a png file with the actual pipeline (Graphviz-dot needed)
487 std::string Executer::ShowGraphInstances(const std::string &nameblackbox, int detail, int level,
491 BlackBox* blackbox=NULL;
492 if (nameblackbox==".")
494 blackbox=Current()->GetPrototype();
498 blackbox = Current()->GetPrototype()->bbFindBlackBox(nameblackbox);
505 // Don't pollute the file store with "doc_tmp" directories ...
506 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_doc_tmp();
507 char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
509 std::string directory = default_doc_dir;
510 if (c != '/' && c !='\\') directory = directory + "/";
512 directory = directory + "doc_tmp";
514 //std::string directory("doc_tmp");
515 std::string filename(directory + "/" + "bbtk_graph_pipeline");
516 std::string filename_html(filename+".html");
517 std::string command0("mkdir \""+directory + "\"");
520 std::string command2("start ");
522 std::string command2("gnome-open ");
525 command2=command2+filename_html;
526 page = filename_html;
527 // 1. Generate Html Diagram
529 s.open(filename_html.c_str());
532 s << "<html><head><title>BBtk graph diagram</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"></head>\n";
533 s << "<body bgcolor=\"#FFFFFF\" text=\"#000000\"> \n\n";
534 if ( blackbox->bbGetName()=="workspacePrototype" )
536 s << "<center>Current workspace</center>";
538 s << "<center>" << blackbox->bbGetName()<< "</center>";
541 blackbox->bbInsertHTMLGraph( s, detail, level, true, directory, false );
542 s << "</body></html>\n";
546 // 2. Starting Browser
547 if (system_display) system( command2.c_str() );
551 bbtkMessageInc("Help",1,"No black box: \""
552 <<nameblackbox<<"\" " <<std::endl);
559 void Executer::ShowRelations(const std::string &nameblackbox, const std::string &detailStr, const std::string &levelStr)
563 int detail = atoi(detailStr.c_str());
564 int level = atoi(levelStr.c_str());
565 BlackBox* blackbox=NULL;
566 if (nameblackbox.compare(".")==0)
568 blackbox=Current()->GetPrototype();
570 blackbox = Current()->GetPrototype()->bbFindBlackBox(nameblackbox);
576 blackbox->bbShowRelations(blackbox,detail,level); //,mFactory);
581 bbtkError("Blackbox Name not found.. <" <<nameblackbox<<">");
586 /// sets the level of message
587 void Executer::Message(const std::string &category,
588 const std::string& level)
591 sscanf(level.c_str(),"%d",&l);
592 bbtk::MessageManager::SetMessageLevel(category,l);