/*========================================================================= Program: bbtk Module: $RCSfile: bbtkExecuter.cxx,v $ $ Language: C++ Date: $Date: 2008/02/05 13:23:46 $ Version: $Revision: 1.7 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notices for more information. =========================================================================*/ /** * \file * \brief class Executer: level 0 of script execution (code) */ #include "bbtkExecuter.h" #include "bbtkMessageManager.h" #include "bbtkFactory.h" #include "bbtkUtilities.h" #include #ifdef _USE_WXWIDGETS_ #include #endif #include "bbtkWxBlackBox.h" #include "bbtkConfigurationFile.h" namespace bbtk { /** * */ Executer::Executer() : mPackage(0), mRoot(0), mNoExecMode(false), mDialogMode(NoDialog) { //VirtualExec(); bbtkDebugMessageInc("Kernel",9,"Executer::Executer()" <UnRegisterBlackBox("workspace"); delete mRoot; } if (mPackage) { GetGlobalFactory()->UnLoadPackage("user"); } bbtkDebugDecTab("Kernel",9); } /** * */ void Executer::Reset() { bbtkDebugMessageInc("Kernel",9,"Executer::Reset()" <UnRegisterBlackBox(mRoot->GetTypeName()); delete mRoot; } if (mPackage) { GetGlobalFactory()->UnLoadPackage("user"); } GetGlobalFactory()->Reset(); // Create user package mPackage = new Package("user","internal to bbi", "User defined black boxes", "", BBTK_STRINGIFY_SYMBOL(BBTK_VERSION)); // Create user workspace mRoot = new ComplexBlackBoxDescriptor("workspace"); //,f); mRoot->AddToAuthor("bbi (internal)"); mRoot->AddToDescription("User's workspace"); mOpenDefinition.push_back(CBBDefinition(mRoot,"user")); // Register it into the user package mPackage->RegisterBlackBox(mRoot); // Insert the user package in the factory InsertPackage(mPackage); mOpenPackage.push_back(mPackage); bbtkDebugDecTab("Kernel",9); } /// changes the workspace name void Executer::SetWorkspaceName( const std::string& n ) { mPackage->ChangeBlackBoxName( mRoot->GetTypeName(), n ); } void Executer::BeginPackage (const std::string &name) { bbtkDebugMessageInc("Kernel",9,"Executer::BeginPackage(\""<GetPackage(name); } catch (Exception e) { p = new Package(name, "", "", "", BBTK_STRINGIFY_SYMBOL(BBTK_VERSION)); InsertPackage(p); } mOpenPackage.push_back(p); } void Executer::EndPackage() { if (mOpenPackage.size()>1) mOpenPackage.pop_back(); } void Executer::Define (const std::string &name, const std::string &pack, const std::string &scriptfilename) { bbtkDebugMessageInc("Kernel",9,"Executer::Define(\""<SetScriptFileName(scriptfilename); mOpenDefinition.push_back( CBBDefinition( b, pack ) ); bbtkDebugDecTab("Kernel",9); } void Executer::EndDefine () { bbtkDebugMessageInc("Kernel",9,"Executer::EndDefine(\"" <GetTypeName()<<"\")" <0) { try { p = GetGlobalFactory()->GetPackage(pname); } catch (Exception e) { p = new Package(pname, "", "", "", BBTK_STRINGIFY_SYMBOL(BBTK_VERSION)); InsertPackage(p); } } else { p = mOpenPackage.back(); } p->RegisterBlackBox(Current()); mOpenDefinition.pop_back(); } void Executer::Create ( const std::string& nodeType, const std::string& nodeName) { Current()->Add(nodeType,nodeName); } /* void Executer::Remove (const std::string &nodeName) { // Current()->RemoveBlackBox(nodeName); } */ /** * */ void Executer::Connect (const std::string &nodeFrom, const std::string &outputLabel, const std::string &nodeTo, const std::string &inputLabel) { Current()->Connect(nodeFrom, outputLabel, nodeTo, inputLabel); } /** * */ void Executer::Update (const std::string &nodeName) // would 'Execute' be more meaningfull ? { // if in root if (Current()==mRoot) { if (!mNoExecMode) { Current()->GetPrototype()->bbGetBlackBox(nodeName)->bbExecute(true); } } else { Current()->AddToExecutionList(nodeName) ; } } /** * */ void Executer::DefineInput ( const std::string &name, const std::string &box, const std::string &input, const std::string& help) { // If the input is defined in the Root box if (Current()==mRoot) { // If the dialog mode is set to NoDialog // and the user passed the name in the Inputs map // then the associated value is set to the box.input // This is the way command line parameters are passed to the Root box if (mDialogMode == NoDialog) { // find if name is in mInputs std::map::iterator i; i = mInputs.find(name); if (i!=mInputs.end()) { Set(box,input,(*i).second); } } // If the dialog mode is set to TextDialog // The user is prompted for the value else if (mDialogMode == TextDialog) { std::cout << name << "="; std::string ans; std::cin >> ans; Set(box,input,ans); } #ifdef _USE_WXWIDGETS_ // If the dialog mode is set to GraphicalDialog // A dialog box is pop up else if (mDialogMode == GraphicalDialog) { std::string mess("Enter the value of '"); mess += name; mess += "' ("; mess += help; mess += ")"; std::string title(name); title += " ?"; std::string ans = wx2std ( wxGetTextFromUser( std2wx (mess), std2wx(title))); Set(box,input,ans); } #endif } Current()->DefineInput(name,box,input,help); } /** * */ void Executer::DefineOutput ( const std::string &name, const std::string &box, const std::string &output, const std::string& help) { Current()->DefineOutput(name,box,output,help); } /** * */ void Executer::Set (const std::string &box, const std::string &input, const std::string &value) { BlackBox* b = Current()->GetPrototype()->bbGetBlackBox(box); // Looks for the adaptor if ( b->bbGetInputType(input) != typeid(std::string) ) { BlackBox* a = /*mFactory->*/ NewAdaptor(typeid(std::string), b->bbGetInputType(input), "tmp"); if (!a) { bbtkError("No <"<< TypeName(b->bbGetInputType(input)) <<"> to found"); } std::string v(value); a->bbSetInput("In",v); a->bbExecute(); b->bbSetInput(input,a->bbGetOutput("Out")); a->bbDelete(); } else { std::string v(value); b->bbSetInput(input,v); } } /** * */ std::string Executer::Get(const std::string &box, const std::string &output) { BlackBox* b = Current()->GetPrototype()->bbGetBlackBox(box); // Looks for the adaptor if (b->bbGetOutputType(output) != typeid(std::string)) { BlackBox* a = /*mFactory->*/ NewAdaptor( b->bbGetOutputType(output), typeid(std::string), "tmp"); if (!a) { bbtkError("No <"<< TypeName(b->bbGetOutputType(output)) <<"> to found"); } b->bbExecute(); a->bbSetInput("In",b->bbGetOutput(output)); a->bbExecute(); std::string r = a->bbGetOutput("Out").unsafe_get(); //std::string v = *((std::string*)a->bbGetOutput("Out")) ; // std::cout << a->bbGetOutput("Out").unsafe_get() // << std::endl; //std::string v(value); //b->bbSetInput(input,a->bbGetOutput("Out")); a->bbDelete(); return r; } else { b->bbExecute(); return b->bbGetOutput(output).unsafe_get(); // std::string v = *((std::string*)b->bbGetOutput(output)) ; // std::cout << b->bbGetOutput("Out").unsafe_get() // << std::endl; // b->bbSetInput(input,&v); } } void Executer::Author(const std::string &authorName) { Current()->AddToAuthor(authorName,Current()==mRoot); } void Executer::Category(const std::string &category) { Current()->AddToCategory(category,Current()==mRoot); } void Executer::Description(const std::string &d) { Current()->AddToDescription(d,Current()==mRoot); } /// prints the list of the boxes of the current descriptor void Executer::PrintBoxes() { bbtkMessageInc("Help",1,"The black box descriptor \"" <GetTypeName()<<"\" contains : "<PrintBlackBoxes(); bbtkDecTab("Help",1); } std::string Executer::ShowGraph(const std::string &nameblackbox, const std::string &detailStr, const std::string &levelStr, const std::string &output_html, const std::string &custom_header, const std::string &custom_title, bool system_display ) { int detail = atoi(detailStr.c_str()); int level = atoi(levelStr.c_str()); std::string filename_rootHtml (output_html) ; std::string simplefilename_rootHtml ( Utilities::get_file_name(output_html)); bool relative_link = true; // No output provided : automatic generation if (output_html.length() == 0) { // Don't pollute the file store with "doc_tmp" directories ... std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_doc_tmp(); char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1]; std::string directory = default_doc_dir; if (c != '/' && c !='\\') directory = directory + "/"; directory = directory + "doc_tmp"; filename_rootHtml = directory + "/" + "User.html"; simplefilename_rootHtml = "User.html" ; // Creating directory std::string command0("mkdir \"" +directory + "\""); system( command0.c_str() ); relative_link = false; } Package* p; try { p = GetGlobalFactory()->GetPackage(nameblackbox); } catch (Exception e) { p = mPackage; } // Generating documentation-help of workspace p->SetDocURL(filename_rootHtml); p->SetDocRelativeURL(simplefilename_rootHtml); p->CreateHtmlPage(filename_rootHtml,"bbi","user package",custom_header,custom_title,detail,level,relative_link); std::string page = filename_rootHtml; /* try { ShowGraphTypes(nameblackbox); } catch (bbtk::Exception a) { std::cout <<"EXC"<GetPrototype(); } else { blackbox = Current()->GetPrototype()->bbFindBlackBox(nameblackbox); } std::string page; if (blackbox) { // Don't pollute the file store with "doc_tmp" directories ... std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_doc_tmp(); char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1]; std::string directory = default_doc_dir; if (c != '/' && c !='\\') directory = directory + "/"; directory = directory + "doc_tmp"; //std::string directory("doc_tmp"); std::string filename(directory + "/" + "bbtk_graph_pipeline"); std::string filename_html(filename+".html"); std::string command0("mkdir \""+directory + "\""); #if defined(_WIN32) std::string command2("start "); #else std::string command2("gnome-open "); #endif command2=command2+filename_html; page = filename_html; // 1. Generate Html Diagram std::ofstream s; s.open(filename_html.c_str()); if (s.good()) { s << "BBtk graph diagram\n"; s << " \n\n"; if ( blackbox->bbGetName()=="workspacePrototype" ) { s << "
Current workspace
"; } else { s << "
" << blackbox->bbGetName()<< "
"; } blackbox->bbInsertHTMLGraph( s, detail, level, true, directory, false ); s << "\n"; } s.close(); // 2. Starting Browser if (system_display) system( command2.c_str() ); } else { bbtkMessageInc("Help",1,"No black box: \"" <GetPrototype(); } else { blackbox = Current()->GetPrototype()->bbFindBlackBox(nameblackbox); } if (blackbox) { found=true; blackbox->bbShowRelations(blackbox,detail,level); //,mFactory); } if (!found) { bbtkError("Blackbox Name not found.. <" <"); } } /* /// sets the level of message void Executer::Message(const std::string &kind, const std::string& level) { int l; sscanf(level.c_str(),"%d",&l); bbtk::MessageManager::SetMessageLevel(kind,l); } */ }//namespace