1 /*=========================================================================
4 Module: $RCSfile: bbtkExecuter.cxx,v $ $
6 Date: $Date: 2008/02/14 11:38:58 $
7 Version: $Revision: 1.10 $
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("Kernel",9,"Executer::Executer()" <<std::endl);
52 bbtkDebugDecTab("Kernel",9);
60 bbtkDebugMessageInc("Kernel",9,"Executer::~Executer()" <<std::endl);
63 mPackage->UnRegisterBlackBox("workspace");
68 GetGlobalFactory()->UnLoadPackage("user");
70 bbtkDebugDecTab("Kernel",9);
77 void Executer::Reset()
79 bbtkDebugMessageInc("Kernel",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("Kernel",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("Kernel",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("Kernel",9,"Executer::Define(\""<<name<<
155 ComplexBlackBoxDescriptor* b = new ComplexBlackBoxDescriptor(name);
156 b->SetScriptFileName(scriptfilename);
157 mOpenDefinition.push_back( CBBDefinition( b, pack ) );
159 bbtkDebugDecTab("Kernel",9);
162 /// Sets the file name to use for the current definition
163 /// (Used to set it after the Define command)
164 void Executer::SetCurrentFileName (const std::string &name )
166 mOpenDefinition.back().box->SetScriptFileName(name);
169 void Executer::EndDefine ()
171 bbtkDebugMessageInc("Kernel",9,"Executer::EndDefine(\""
172 <<Current()->GetTypeName()<<"\")"
174 // Does current package exist ?
176 std::string pname(mOpenDefinition.back().package);
181 p = GetGlobalFactory()->GetPackage(pname);
185 p = new Package(pname,
189 BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
195 p = mOpenPackage.back();
197 p->RegisterBlackBox(Current());
199 mOpenDefinition.pop_back();
203 void Executer::Create ( const std::string& nodeType,
204 const std::string& nodeName)
206 Current()->Add(nodeType,nodeName);
210 void Executer::Remove (const std::string &nodeName)
212 // Current()->RemoveBlackBox(nodeName);
219 void Executer::Connect (const std::string &nodeFrom,
220 const std::string &outputLabel,
221 const std::string &nodeTo,
222 const std::string &inputLabel)
224 Current()->Connect(nodeFrom, outputLabel, nodeTo, inputLabel);
230 void Executer::Update (const std::string &nodeName) // would 'Execute' be more meaningfull ?
233 if (Current()==mRoot)
237 Current()->GetPrototype()->bbGetBlackBox(nodeName)->bbExecute(true);
242 Current()->AddToExecutionList(nodeName) ;
249 void Executer::DefineInput ( const std::string &name,
250 const std::string &box,
251 const std::string &input,
252 const std::string& help)
254 // If the input is defined in the Root box
255 if (Current()==mRoot)
257 // If the dialog mode is set to NoDialog
258 // and the user passed the name in the Inputs map
259 // then the associated value is set to the box.input
260 // This is the way command line parameters are passed to the Root box
261 if (mDialogMode == NoDialog)
263 // find if name is in mInputs
264 std::map<std::string,std::string>::iterator i;
265 i = mInputs.find(name);
266 if (i!=mInputs.end()) {
267 Set(box,input,(*i).second);
270 // If the dialog mode is set to TextDialog
271 // The user is prompted for the value
272 else if (mDialogMode == TextDialog)
274 std::cout << name << "=";
279 #ifdef _USE_WXWIDGETS_
280 // If the dialog mode is set to GraphicalDialog
281 // A dialog box is pop up
282 else if (mDialogMode == GraphicalDialog)
284 std::string mess("Enter the value of '");
289 std::string title(name);
291 std::string ans = wx2std ( wxGetTextFromUser( std2wx (mess), std2wx(title)));
297 Current()->DefineInput(name,box,input,help);
304 void Executer::DefineOutput ( const std::string &name,
305 const std::string &box,
306 const std::string &output,
307 const std::string& help)
309 Current()->DefineOutput(name,box,output,help);
315 void Executer::Set (const std::string &box,
316 const std::string &input,
317 const std::string &value)
319 BlackBox* b = Current()->GetPrototype()->bbGetBlackBox(box);
320 // Looks for the adaptor
322 if ( b->bbGetInputType(input) != typeid(std::string) )
324 BlackBox* a = /*mFactory->*/
325 NewAdaptor(typeid(std::string),
326 b->bbGetInputType(input),
331 TypeName(b->bbGetInputType(input))
332 <<"> to <std::string> found");
334 std::string v(value);
335 a->bbSetInput("In",v);
337 b->bbSetInput(input,a->bbGetOutput("Out"));
342 std::string v(value);
343 b->bbSetInput(input,v);
350 std::string Executer::Get(const std::string &box,
351 const std::string &output)
353 BlackBox* b = Current()->GetPrototype()->bbGetBlackBox(box);
354 // Looks for the adaptor
355 if (b->bbGetOutputType(output) != typeid(std::string))
357 BlackBox* a = /*mFactory->*/
359 b->bbGetOutputType(output),
365 TypeName(b->bbGetOutputType(output))
366 <<"> to <std::string> found");
370 a->bbSetInput("In",b->bbGetOutput(output));
372 std::string r = a->bbGetOutput("Out").unsafe_get<std::string>();
373 //std::string v = *((std::string*)a->bbGetOutput("Out")) ;
374 // std::cout << a->bbGetOutput("Out").unsafe_get<std::string>()
376 //std::string v(value);
377 //b->bbSetInput(input,a->bbGetOutput("Out"));
384 return b->bbGetOutput(output).unsafe_get<std::string>();
385 // std::string v = *((std::string*)b->bbGetOutput(output)) ;
386 // std::cout << b->bbGetOutput("Out").unsafe_get<std::string>()
388 // b->bbSetInput(input,&v);
393 void Executer::Author(const std::string &authorName)
395 Current()->AddToAuthor(authorName,Current()==mRoot);
398 void Executer::Category(const std::string &category)
400 Current()->AddToCategory(category,Current()==mRoot);
403 void Executer::Description(const std::string &d)
405 Current()->AddToDescription(d,Current()==mRoot);
409 /// prints the list of the boxes of the current descriptor
410 void Executer::PrintBoxes()
412 bbtkMessageInc("Help",1,"The black box descriptor \""
413 <<Current()->GetTypeName()<<"\" contains : "<<std::endl);
414 Current()->PrintBlackBoxes();
415 bbtkDecTab("Help",1);
418 std::string Executer::ShowGraph(const std::string &nameblackbox,
419 const std::string &detailStr,
420 const std::string &levelStr,
421 const std::string &output_html,
422 const std::string &custom_header,
423 const std::string &custom_title,
424 bool system_display )
426 int detail = atoi(detailStr.c_str());
427 int level = atoi(levelStr.c_str());
429 std::string filename_rootHtml (output_html) ;
430 std::string simplefilename_rootHtml ( Utilities::get_file_name(output_html));
432 bool relative_link = true;
434 // No output provided : automatic generation
435 if (output_html.length() == 0)
437 // Don't pollute the file store with "temp_dir" directories ...
438 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
440 char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
442 std::string directory = default_doc_dir;
443 if (c != '/' && c !='\\') directory = directory + "/";
444 directory = directory + "temp_dir";
446 filename_rootHtml = directory + "/" + "User.html";
447 simplefilename_rootHtml = "User.html" ;
449 // Creating directory
450 std::string command0("mkdir \"" +directory + "\"");
451 system( command0.c_str() );
453 relative_link = false;
460 p = GetGlobalFactory()->GetPackage(nameblackbox);
466 // Generating documentation-help of workspace
467 p->SetDocURL(filename_rootHtml);
468 p->SetDocRelativeURL(simplefilename_rootHtml);
470 p->CreateHtmlPage(filename_rootHtml,"bbi","user package",custom_header,custom_title,detail,level,relative_link);
472 std::string page = filename_rootHtml;
476 ShowGraphTypes(nameblackbox);
478 catch (bbtk::Exception a)
480 std::cout <<"EXC"<<std::endl;
481 page = ShowGraphInstances(nameblackbox,detail,level,system_display);
487 /// Generate a png file with the actual pipeline (Graphviz-dot needed)
488 std::string Executer::ShowGraphInstances(const std::string &nameblackbox, int detail, int level,
492 BlackBox* blackbox=NULL;
493 if (nameblackbox==".")
495 blackbox=Current()->GetPrototype();
499 blackbox = Current()->GetPrototype()->bbFindBlackBox(nameblackbox);
506 // Don't pollute the file store with "temp_dir" directories ...
507 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
508 char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
510 std::string directory = default_doc_dir;
511 if (c != '/' && c !='\\') directory = directory + "/";
513 directory = directory + "temp_dir";
514 //std::string directory("temp_dir");
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);
557 void Executer::ShowRelations(const std::string &nameblackbox, const std::string &detailStr, const std::string &levelStr)
561 int detail = atoi(detailStr.c_str());
562 int level = atoi(levelStr.c_str());
563 BlackBox* blackbox=NULL;
564 if (nameblackbox.compare(".")==0)
566 blackbox=Current()->GetPrototype();
568 blackbox = Current()->GetPrototype()->bbFindBlackBox(nameblackbox);
574 blackbox->bbShowRelations(blackbox,detail,level); //,mFactory);
579 bbtkError("Blackbox Name not found.. <" <<nameblackbox<<">");
584 /// sets the level of message
585 void Executer::Message(const std::string &kind,
586 const std::string& level)
589 sscanf(level.c_str(),"%d",&l);
590 bbtk::MessageManager::SetMessageLevel(kind,l);