1 /*=========================================================================
4 Module: $RCSfile: bbtkExecuter.cxx,v $ $
6 Date: $Date: 2008/03/07 11:15:15 $
7 Version: $Revision: 1.14 $
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);
51 mFactory = new Factory;
52 mFactory->SetExecuter(this);
54 bbtkDebugDecTab("Kernel",9);
62 //std::cout << "===================================== delete Executer\n";
63 bbtkDebugMessageInc("Kernel",9,"Executer::~Executer()" <<std::endl);
66 mRootPackage->UnRegisterBlackBox("workspace");
71 GetFactory()->UnLoadPackage("user");
75 bbtkDebugDecTab("Kernel",9);
82 void Executer::Reset()
84 bbtkDebugMessageInc("Kernel",9,"Executer::Reset()" <<std::endl);
86 // The 'user' package must be closed before all other
87 // because box destructors must not be unloaded when bb are deleted!
88 // Similarly, the 'workspace' CBB must be destroyed before
89 // all user defined CBB otherwise any instance
90 // of a user CBB that is in the 'workspace' would try to
91 // access a user CBB descriptor which has been previously freed
94 mRootPackage->UnRegisterBlackBox(mRootCBB->GetTypeName());
99 GetFactory()->UnLoadPackage("user");
101 GetFactory()->Reset();
103 // Create user package
104 mRootPackage = new Package("user","internal to bbi",
105 "User defined black boxes",
107 BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
108 // Create user workspace
109 mRootCBB = new ComplexBlackBoxDescriptor("workspace"); //,f);
110 mRootCBB->SetFactory(GetFactory());
111 mRootCBB->AddToAuthor("bbi (internal)");
112 mRootCBB->AddToDescription("User's workspace");
113 mOpenDefinition.push_back(CBBDefinition(mRootCBB,"user"));
114 // Register it into the user package
115 mRootPackage->RegisterBlackBox(mRootCBB);
116 // Insert the user package in the factory
117 GetFactory()->InsertPackage(mRootPackage);
118 // And in the list of open packages
119 mOpenPackage.push_back(mRootPackage);
121 bbtkDebugDecTab("Kernel",9);
124 /// changes the workspace name
125 void Executer::SetWorkspaceName( const std::string& n )
127 mRootPackage->ChangeBlackBoxName( mRootCBB->GetTypeName(), n );
130 void Executer::BeginPackage (const std::string &name)
132 bbtkDebugMessageInc("Kernel",9,"Executer::BeginPackage(\""<<name<<"\")"
137 p = GetFactory()->GetPackage(name);
141 p = new Package(name,
145 BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
146 GetFactory()->InsertPackage(p);
148 mOpenPackage.push_back(p);
151 void Executer::EndPackage()
153 if (mOpenPackage.size()>1) mOpenPackage.pop_back();
156 void Executer::Define (const std::string &name,
157 const std::string &pack,
158 const std::string &scriptfilename)
160 bbtkDebugMessageInc("Kernel",9,"Executer::Define(\""<<name<<
164 ComplexBlackBoxDescriptor* b = new ComplexBlackBoxDescriptor(name);
165 b->SetFactory(GetFactory());
166 b->SetScriptFileName(scriptfilename);
167 mOpenDefinition.push_back( CBBDefinition( b, pack ) );
169 bbtkDebugDecTab("Kernel",9);
172 /// Sets the file name to use for the current definition
173 /// (Used to set it after the Define command)
174 void Executer::SetCurrentFileName (const std::string &name )
176 mOpenDefinition.back().box->SetScriptFileName(name);
179 void Executer::EndDefine ()
181 bbtkDebugMessageInc("Kernel",9,"Executer::EndDefine(\""
182 <<Current()->GetTypeName()<<"\")"
184 // Does current package exist ?
186 std::string pname(mOpenDefinition.back().package);
191 p = GetFactory()->GetPackage(pname);
195 p = new Package(pname,
199 BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
200 GetFactory()->InsertPackage(p);
205 p = mOpenPackage.back();
207 p->RegisterBlackBox(Current());
209 mOpenDefinition.pop_back();
213 void Executer::Create ( const std::string& nodeType,
214 const std::string& nodeName)
216 Current()->Add(nodeType,nodeName);
220 void Executer::Remove (const std::string &nodeName)
222 // Current()->RemoveBlackBox(nodeName);
229 void Executer::Connect (const std::string &nodeFrom,
230 const std::string &outputLabel,
231 const std::string &nodeTo,
232 const std::string &inputLabel)
234 Current()->Connect(nodeFrom, outputLabel, nodeTo, inputLabel);
240 void Executer::Update (const std::string &nodeName) // would 'Execute' be more meaningfull ?
243 if (Current()==mRootCBB)
247 Current()->GetPrototype()->bbGetBlackBox(nodeName)->bbExecute(true);
252 Current()->AddToExecutionList(nodeName) ;
259 void Executer::DefineInput ( const std::string &name,
260 const std::string &box,
261 const std::string &input,
262 const std::string& help)
264 // If the input is defined in the Root box
265 if (Current()==mRootCBB)
267 // If the dialog mode is set to NoDialog
268 // and the user passed the name in the Inputs map
269 // then the associated value is set to the box.input
270 // This is the way command line parameters are passed to the Root box
271 if (mDialogMode == NoDialog)
273 // find if name is in mInputs
274 std::map<std::string,std::string>::iterator i;
275 i = mInputs.find(name);
276 if (i!=mInputs.end()) {
277 Set(box,input,(*i).second);
280 // If the dialog mode is set to TextDialog
281 // The user is prompted for the value
282 else if (mDialogMode == TextDialog)
284 std::cout << name << "=";
289 #ifdef _USE_WXWIDGETS_
290 // If the dialog mode is set to GraphicalDialog
291 // A dialog box is pop up
292 else if (mDialogMode == GraphicalDialog)
294 std::string mess("Enter the value of '");
299 std::string title(name);
301 std::string ans = wx2std ( wxGetTextFromUser( std2wx (mess), std2wx(title)));
307 Current()->DefineInput(name,box,input,help);
314 void Executer::DefineOutput ( const std::string &name,
315 const std::string &box,
316 const std::string &output,
317 const std::string& help)
319 Current()->DefineOutput(name,box,output,help);
325 void Executer::Set (const std::string &box,
326 const std::string &input,
327 const std::string &value)
329 BlackBox* b = Current()->GetPrototype()->bbGetBlackBox(box);
330 // Looks for the adaptor
332 if ( b->bbGetInputType(input) != typeid(std::string) )
335 GetFactory()->NewAdaptor(typeid(std::string),
336 b->bbGetInputType(input),
341 TypeName(b->bbGetInputType(input))
342 <<"> to <std::string> found");
344 std::string v(value);
345 a->bbSetInput("In",v);
347 b->bbSetInput(input,a->bbGetOutput("Out"));
352 std::string v(value);
353 b->bbSetInput(input,v);
360 std::string Executer::Get(const std::string &box,
361 const std::string &output)
363 BlackBox* b = Current()->GetPrototype()->bbGetBlackBox(box);
364 // Looks for the adaptor
365 if (b->bbGetOutputType(output) != typeid(std::string))
368 GetFactory()->NewAdaptor(
369 b->bbGetOutputType(output),
375 TypeName(b->bbGetOutputType(output))
376 <<"> to <std::string> found");
380 a->bbSetInput("In",b->bbGetOutput(output));
382 std::string r = a->bbGetOutput("Out").unsafe_get<std::string>();
383 //std::string v = *((std::string*)a->bbGetOutput("Out")) ;
384 // std::cout << a->bbGetOutput("Out").unsafe_get<std::string>()
386 //std::string v(value);
387 //b->bbSetInput(input,a->bbGetOutput("Out"));
394 return b->bbGetOutput(output).unsafe_get<std::string>();
395 // std::string v = *((std::string*)b->bbGetOutput(output)) ;
396 // std::cout << b->bbGetOutput("Out").unsafe_get<std::string>()
398 // b->bbSetInput(input,&v);
403 void Executer::Author(const std::string &authorName)
405 Current()->AddToAuthor(authorName,Current()==mRootCBB);
408 void Executer::Category(const std::string &category)
410 Current()->AddToCategory(category,Current()==mRootCBB);
413 void Executer::Description(const std::string &d)
415 Current()->AddToDescription(d,Current()==mRootCBB);
419 /// prints the list of the boxes of the current descriptor
420 void Executer::PrintBoxes()
422 bbtkMessageInc("Help",1,"The black box descriptor \""
423 <<Current()->GetTypeName()<<"\" contains : "<<std::endl);
424 Current()->PrintBlackBoxes();
425 bbtkDecTab("Help",1);
428 std::string Executer::ShowGraph(const std::string &nameblackbox,
429 const std::string &detailStr,
430 const std::string &levelStr,
431 const std::string &output_html,
432 const std::string &custom_header,
433 const std::string &custom_title,
434 bool system_display )
436 int detail = atoi(detailStr.c_str());
437 int level = atoi(levelStr.c_str());
439 std::string filename_rootHtml (output_html) ;
440 std::string simplefilename_rootHtml ( Utilities::get_file_name(output_html));
442 bool relative_link = true;
444 // No output provided : automatic generation
445 if (output_html.length() == 0)
447 // Don't pollute the file store with "temp_dir" directories ...
448 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
450 char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
452 std::string directory = default_doc_dir;
453 if (c != '/' && c !='\\') directory = directory + "/";
454 directory = directory + "temp_dir";
456 filename_rootHtml = directory + "/" + "User.html";
457 simplefilename_rootHtml = "User.html" ;
459 // Creating directory
460 std::string command0("mkdir \"" +directory + "\"");
461 system( command0.c_str() );
463 relative_link = false;
469 p = GetFactory()->GetPackage(nameblackbox);
475 // Generating documentation-help of workspace
476 p->SetDocURL(filename_rootHtml);
477 p->SetDocRelativeURL(simplefilename_rootHtml);
479 p->CreateHtmlPage(filename_rootHtml,"bbi","user package",custom_header,custom_title,detail,level,relative_link);
481 std::string page = filename_rootHtml;
485 ShowGraphTypes(nameblackbox);
487 catch (bbtk::Exception a)
489 std::cout <<"EXC"<<std::endl;
490 page = ShowGraphInstances(nameblackbox,detail,level,system_display);
496 /// Generate a png file with the actual pipeline (Graphviz-dot needed)
497 std::string Executer::ShowGraphInstances(const std::string &nameblackbox, int detail, int level,
501 BlackBox* blackbox=NULL;
502 if (nameblackbox==".")
504 blackbox=Current()->GetPrototype();
508 blackbox = Current()->GetPrototype()->bbFindBlackBox(nameblackbox);
515 // Don't pollute the file store with "temp_dir" directories ...
516 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
517 char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
519 std::string directory = default_doc_dir;
520 if (c != '/' && c !='\\') directory = directory + "/";
522 directory = directory + "temp_dir";
523 //std::string directory("temp_dir");
524 std::string filename(directory + "/" + "bbtk_graph_pipeline");
525 std::string filename_html(filename+".html");
526 std::string command0("mkdir \""+directory + "\"");
529 std::string command2("start ");
531 std::string command2("gnome-open ");
534 command2=command2+filename_html;
535 page = filename_html;
536 // 1. Generate Html Diagram
538 s.open(filename_html.c_str());
541 s << "<html><head><title>BBtk graph diagram</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"></head>\n";
542 s << "<body bgcolor=\"#FFFFFF\" text=\"#000000\"> \n\n";
543 if ( blackbox->bbGetName()=="workspacePrototype" )
545 s << "<center>Current workspace</center>";
547 s << "<center>" << blackbox->bbGetName()<< "</center>";
550 blackbox->bbInsertHTMLGraph( s, detail, level, true, directory, false );
551 s << "</body></html>\n";
555 // 2. Starting Browser
556 if (system_display) system( command2.c_str() );
560 bbtkMessageInc("Help",1,"No black box: \""
561 <<nameblackbox<<"\" " <<std::endl);
566 void Executer::ShowRelations(const std::string &nameblackbox, const std::string &detailStr, const std::string &levelStr)
570 int detail = atoi(detailStr.c_str());
571 int level = atoi(levelStr.c_str());
572 BlackBox* blackbox=NULL;
573 if (nameblackbox.compare(".")==0)
575 blackbox=Current()->GetPrototype();
577 blackbox = Current()->GetPrototype()->bbFindBlackBox(nameblackbox);
583 blackbox->bbShowRelations(blackbox,detail,level); //,mFactory);
588 bbtkError("Blackbox Name not found.. <" <<nameblackbox<<">");
593 /// sets the level of message
594 void Executer::Message(const std::string &kind,
595 const std::string& level)
598 sscanf(level.c_str(),"%d",&l);
599 bbtk::MessageManager::SetMessageLevel(kind,l);