1 /*=========================================================================
4 Module: $RCSfile: bbtkExecuter.cxx,v $ $
6 Date: $Date: 2008/02/05 11:07:42 $
7 Version: $Revision: 1.5 $
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"
29 #ifdef _USE_WXWIDGETS_
30 #include <wx/textdlg.h>
33 #include "bbtkWxBlackBox.h"
35 #include "bbtkConfigurationFile.h"
50 bbtkDebugMessageInc("Core",9,"Executer::Executer()" <<std::endl);
52 bbtkDebugDecTab("Core",9);
60 bbtkDebugMessageInc("Core",9,"Executer::~Executer()" <<std::endl);
63 mPackage->UnRegisterBlackBox("workspace");
68 GetGlobalFactory()->UnLoadPackage("user");
70 bbtkDebugDecTab("Core",9);
77 void Executer::Reset()
79 bbtkDebugMessageInc("Core",9,"Executer::Reset()" <<std::endl);
81 // The 'user' package must be closed before all other
82 // because box destructors must not be unloaded when bb are deleted!
83 // Similarly, the 'workspace' CBB must be destroyed before
84 // all user defined CBB otherwise any instance
85 // of a user CBB that is in the 'workspace' would try to
86 // access a user CBB descriptor which has been previously freed
89 mPackage->UnRegisterBlackBox(mRoot->GetTypeName());
94 GetGlobalFactory()->UnLoadPackage("user");
96 GetGlobalFactory()->Reset();
97 // Create user package
98 mPackage = new Package("user","internal to bbi",
99 "User defined black boxes",
101 BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
102 // Create user workspace
103 mRoot = new ComplexBlackBoxDescriptor("workspace"); //,f);
104 mRoot->AddToAuthor("bbi (internal)");
105 mRoot->AddToDescription("User's workspace");
106 mOpenDefinition.push_back(CBBDefinition(mRoot,"user"));
107 // Register it into the user package
108 mPackage->RegisterBlackBox(mRoot);
109 // Insert the user package in the factory
110 InsertPackage(mPackage);
111 mOpenPackage.push_back(mPackage);
112 bbtkDebugDecTab("Core",9);
115 /// changes the workspace name
116 void Executer::SetWorkspaceName( const std::string& n )
118 mPackage->ChangeBlackBoxName( mRoot->GetTypeName(), n );
121 void Executer::BeginPackage (const std::string &name)
123 bbtkDebugMessageInc("Core",9,"Executer::BeginPackage(\""<<name<<"\")"
128 p = GetGlobalFactory()->GetPackage(name);
132 p = new Package(name,
136 BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
139 mOpenPackage.push_back(p);
142 void Executer::EndPackage()
144 if (mOpenPackage.size()>1) mOpenPackage.pop_back();
147 void Executer::Define (const std::string &name,
148 const std::string &pack,
149 const std::string &scriptfilename)
151 bbtkDebugMessageInc("Core",9,"Executer::Define(\""<<name<<
155 ComplexBlackBoxDescriptor* b = new ComplexBlackBoxDescriptor(name);
156 b->SetScriptFileName(scriptfilename);
157 mOpenDefinition.push_back( CBBDefinition( b, pack ) );
159 bbtkDebugDecTab("Core",9);
162 void Executer::EndDefine ()
164 bbtkDebugMessageInc("Core",9,"Executer::EndDefine(\""
165 <<Current()->GetTypeName()<<"\")"
167 // Does current package exist ?
169 std::string pname(mOpenDefinition.back().package);
174 p = GetGlobalFactory()->GetPackage(pname);
178 p = new Package(pname,
182 BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
188 p = mOpenPackage.back();
190 p->RegisterBlackBox(Current());
192 mOpenDefinition.pop_back();
196 void Executer::Create ( const std::string& nodeType,
197 const std::string& nodeName)
199 Current()->Add(nodeType,nodeName);
204 void Executer::Remove (const std::string &nodeName)
206 // Current()->RemoveBlackBox(nodeName);
213 void Executer::Connect (const std::string &nodeFrom,
214 const std::string &outputLabel,
215 const std::string &nodeTo,
216 const std::string &inputLabel)
218 Current()->Connect(nodeFrom, outputLabel, nodeTo, inputLabel);
224 void Executer::Update (const std::string &nodeName) // would 'Execute' be more meaningfull ?
227 if (Current()==mRoot)
231 Current()->GetPrototype()->bbGetBlackBox(nodeName)->bbExecute(true);
236 Current()->AddToExecutionList(nodeName) ;
243 void Executer::DefineInput ( const std::string &name,
244 const std::string &box,
245 const std::string &input,
246 const std::string& help)
248 // If the input is defined in the Root box
249 if (Current()==mRoot)
251 // If the dialog mode is set to NoDialog
252 // and the user passed the name in the Inputs map
253 // then the associated value is set to the box.input
254 // This is the way command line parameters are passed to the Root box
255 if (mDialogMode == NoDialog)
257 // find if name is in mInputs
258 std::map<std::string,std::string>::iterator i;
259 i = mInputs.find(name);
260 if (i!=mInputs.end()) {
261 Set(box,input,(*i).second);
264 // If the dialog mode is set to TextDialog
265 // The user is prompted for the value
266 else if (mDialogMode == TextDialog)
268 std::cout << name << "=";
273 #ifdef _USE_WXWIDGETS_
274 // If the dialog mode is set to GraphicalDialog
275 // A dialog box is pop up
276 else if (mDialogMode == GraphicalDialog)
278 std::string mess("Enter the value of '");
283 std::string title(name);
286 std::string ans = wx2std ( wxGetTextFromUser( std2wx (mess), std2wx(title)));
292 Current()->DefineInput(name,box,input,help);
299 void Executer::DefineOutput ( const std::string &name,
300 const std::string &box,
301 const std::string &output,
302 const std::string& help)
304 Current()->DefineOutput(name,box,output,help);
310 void Executer::Set (const std::string &box,
311 const std::string &input,
312 const std::string &value)
314 BlackBox* b = Current()->GetPrototype()->bbGetBlackBox(box);
315 // Looks for the adaptor
317 if ( b->bbGetInputType(input) != typeid(std::string) )
319 BlackBox* a = /*mFactory->*/
320 NewAdaptor(typeid(std::string),
321 b->bbGetInputType(input),
326 TypeName(b->bbGetInputType(input))
327 <<"> to <std::string> found");
329 std::string v(value);
330 a->bbSetInput("In",v);
332 b->bbSetInput(input,a->bbGetOutput("Out"));
337 std::string v(value);
338 b->bbSetInput(input,v);
345 std::string Executer::Get(const std::string &box,
346 const std::string &output)
348 BlackBox* b = Current()->GetPrototype()->bbGetBlackBox(box);
349 // Looks for the adaptor
350 if (b->bbGetOutputType(output) != typeid(std::string))
352 BlackBox* a = /*mFactory->*/
354 b->bbGetOutputType(output),
360 TypeName(b->bbGetOutputType(output))
361 <<"> to <std::string> found");
365 a->bbSetInput("In",b->bbGetOutput(output));
367 std::string r = a->bbGetOutput("Out").unsafe_get<std::string>();
368 //std::string v = *((std::string*)a->bbGetOutput("Out")) ;
369 // std::cout << a->bbGetOutput("Out").unsafe_get<std::string>()
371 //std::string v(value);
372 //b->bbSetInput(input,a->bbGetOutput("Out"));
379 return b->bbGetOutput(output).unsafe_get<std::string>();
380 // std::string v = *((std::string*)b->bbGetOutput(output)) ;
381 // std::cout << b->bbGetOutput("Out").unsafe_get<std::string>()
383 // b->bbSetInput(input,&v);
388 void Executer::Author(const std::string &authorName)
390 Current()->AddToAuthor(authorName,Current()==mRoot);
393 void Executer::Keyword(const std::string &keyword)
395 Current()->AddToKeyword(keyword,Current()==mRoot);
398 void Executer::Description(const std::string &d)
400 Current()->AddToDescription(d,Current()==mRoot);
404 /// prints the list of the boxes of the current descriptor
405 void Executer::PrintBoxes()
407 bbtkMessageInc("Help",1,"The black box descriptor \""
408 <<Current()->GetTypeName()<<"\" contains : "<<std::endl);
409 Current()->PrintBlackBoxes();
410 bbtkDecTab("Help",1);
413 std::string Executer::ShowGraph(const std::string &nameblackbox,
414 const std::string &detailStr,
415 const std::string &levelStr,
416 const std::string &output_html,
417 const std::string &custom_header,
418 const std::string &custom_title,
419 bool system_display )
421 int detail = atoi(detailStr.c_str());
422 int level = atoi(levelStr.c_str());
424 std::string filename_rootHtml (output_html) ;
425 std::string simplefilename_rootHtml ( Utilities::get_file_name(output_html));
427 bool relative_link = true;
429 // No output provided : automatic generation
430 if (output_html.length() == 0)
432 // Don't pollute the file store with "doc_tmp" directories ...
433 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_doc_tmp();
435 char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
437 std::string directory = default_doc_dir;
438 if (c != '/' && c !='\\') directory = directory + "/";
439 directory = directory + "doc_tmp";
441 filename_rootHtml = directory + "/" + "User.html";
442 simplefilename_rootHtml = "User.html" ;
444 // Creating directory
445 std::string command0("mkdir \"" +directory + "\"");
446 system( command0.c_str() );
448 relative_link = false;
455 p = GetGlobalFactory()->GetPackage(nameblackbox);
461 // Generating documentation-help of workspace
462 p->SetDocURL(filename_rootHtml);
463 p->SetDocRelativeURL(simplefilename_rootHtml);
465 p->CreateHtmlPage(filename_rootHtml,"bbi","user package",custom_header,custom_title,detail,level,relative_link);
467 std::string page = filename_rootHtml;
471 ShowGraphTypes(nameblackbox);
473 catch (bbtk::Exception a)
475 std::cout <<"EXC"<<std::endl;
476 page = ShowGraphInstances(nameblackbox,detail,level,system_display);
482 /// Generate a png file with the actual pipeline (Graphviz-dot needed)
483 std::string Executer::ShowGraphInstances(const std::string &nameblackbox, int detail, int level,
487 BlackBox* blackbox=NULL;
488 if (nameblackbox==".")
490 blackbox=Current()->GetPrototype();
494 blackbox = Current()->GetPrototype()->bbFindBlackBox(nameblackbox);
501 // Don't pollute the file store with "doc_tmp" directories ...
502 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_doc_tmp();
503 char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
505 std::string directory = default_doc_dir;
506 if (c != '/' && c !='\\') directory = directory + "/";
508 directory = directory + "doc_tmp";
510 //std::string directory("doc_tmp");
511 std::string filename(directory + "/" + "bbtk_graph_pipeline");
512 std::string filename_html(filename+".html");
513 std::string command0("mkdir \""+directory + "\"");
516 std::string command2("start ");
518 std::string command2("gnome-open ");
521 command2=command2+filename_html;
522 page = filename_html;
523 // 1. Generate Html Diagram
525 s.open(filename_html.c_str());
528 s << "<html><head><title>BBtk graph diagram</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"></head>\n";
529 s << "<body bgcolor=\"#FFFFFF\" text=\"#000000\"> \n\n";
530 if ( blackbox->bbGetName()=="workspacePrototype" )
532 s << "<center>Current workspace</center>";
534 s << "<center>" << blackbox->bbGetName()<< "</center>";
537 blackbox->bbInsertHTMLGraph( s, detail, level, true, directory, false );
538 s << "</body></html>\n";
542 // 2. Starting Browser
543 if (system_display) system( command2.c_str() );
547 bbtkMessageInc("Help",1,"No black box: \""
548 <<nameblackbox<<"\" " <<std::endl);
554 void Executer::ShowRelations(const std::string &nameblackbox, const std::string &detailStr, const std::string &levelStr)
558 int detail = atoi(detailStr.c_str());
559 int level = atoi(levelStr.c_str());
560 BlackBox* blackbox=NULL;
561 if (nameblackbox.compare(".")==0)
563 blackbox=Current()->GetPrototype();
565 blackbox = Current()->GetPrototype()->bbFindBlackBox(nameblackbox);
571 blackbox->bbShowRelations(blackbox,detail,level); //,mFactory);
576 bbtkError("Blackbox Name not found.. <" <<nameblackbox<<">");
581 /// sets the level of message
582 void Executer::Message(const std::string &kind,
583 const std::string& level)
586 sscanf(level.c_str(),"%d",&l);
587 bbtk::MessageManager::SetMessageLevel(kind,l);