/*========================================================================= Program: bbtk Module: $RCSfile: bbtkComplexBlackBox.cxx,v $ Language: C++ Date: $Date: 2008/01/22 15:02:00 $ Version: $Revision: 1.1.1.1 $ 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 bbtk::ComplexBlackBox : user defined complex black boxes */ #include "bbtkComplexBlackBox.h" #include "bbtkBlackBoxDescriptor.h" #include "bbtkFactory.h" #include "bbtkConfigurationFile.h" namespace bbtk { //======================================================================= /// Usefull constructor ComplexBlackBox::ComplexBlackBox(const std::string &name, ComplexBlackBoxDescriptor* desc) : BlackBox(name), mDescriptor(desc) { bbtkDebugMessageInc("Core",9, "ComplexBlackBox::ComplexBlackBox(\"" <first<<"\""<second->bbClone(i->second->bbGetName()); bbUnsafeAddBlackBox(B); bbtkDebugDecTab("Core",9); } bbtkDebugDecTab("Core",9); bbtkDebugMessageInc("Core",9,"* Cloning Connections"<GetFullName()<<"\""<GetBlackBoxFrom()->bbGetName() ); BlackBox* bbto = bbGetBlackBox( (*j)->GetBlackBoxTo()->bbGetName() ); Connection* c = /*mDescriptor->GetFactory()->Create*/ NewConnection( bbfrom, (*j)->GetBlackBoxFromOutput(), bbto, (*j)->GetBlackBoxToInput() ); bbAddConnection(c); bbtkDebugDecTab("Core",9); } bbtkDebugDecTab("Core",9); bbAllocateConnectors(); bbtkDebugDecTab("Core",9); } //======================================================================= //======================================================================= /// Destructor ComplexBlackBox::~ComplexBlackBox() { bbtkDebugMessageInc("Core",9, "ComplexBlackBox::~ComplexBlackBox() [" <GetFullName()<<"\""<first<<"\""<second->bbDelete(); bbtkDebugDecTab("Core",9); } bbtkDebugDecTab("Core",9); // bbtkDebugMessage("Core",9,"EO ComplexBlackBox::~ComplexBlackBox [" // <bbDesallocateConnectors(); bbtkDebugDecTab("Core",9); } //======================================================================= //========================================================================= /// Allocates the i/o connectors of the black box void ComplexBlackBox::bbAllocateConnectors() { bbtkDebugMessageInc("Core",8, "ComplexBlackBox::bbAllocateConnectors() [" <GetInputDescriptorMap(); BlackBoxDescriptor::InputDescriptorMapType::const_iterator i; for ( i = imap.begin(); i != imap.end(); ++i ) { bbtkDebugMessage("Core",8,"* Allocate \""<first<<"\""<second; // Get the internal box connector BlackBoxInputConnector* c = bbUnsafeGetBlackBox ( d->GetTarget() ) ->bbGetInputConnectorMap()[ d->GetInput() ]; bbGetInputConnectorMap()[i->second->GetName()] = c; //new BlackBoxInputConnector(); } // Output connectors const BlackBoxDescriptor::OutputDescriptorMapType& omap = bbGetDescriptor()->GetOutputDescriptorMap(); BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o; for ( o = omap.begin(); o != omap.end(); ++o ) { bbtkDebugMessage("Core",8,"* Allocate \""<first<<"\""<second; // Get the internal box connector BlackBoxOutputConnector* c = bbUnsafeGetBlackBox ( d->GetTarget() ) ->bbGetOutputConnectorMap()[ d->GetOutput() ]; bbGetOutputConnectorMap()[o->second->GetName()] = c; //new BlackBoxOutputConnector(); } bbtkDebugDecTab("Core",8); } //========================================================================= //========================================================================= /// Desallocates the i/o connectors of the black box void ComplexBlackBox::bbDesallocateConnectors() { bbtkDebugMessageInc("Core",8, "ComplexBlackBox::DesallocateConnectors()" <::const_iterator i; for (i=mExecutionList.begin(); i!=mExecutionList.end(); ++i) { bbtkDebugMessage("Process",2," -> Executing '"<<*i<<"'"<bbExecute(force); } } else { std::map::iterator i; for (i=mBlackBoxMap.begin(); i!=mBlackBoxMap.end(); ++i) { i->second->bbExecute(force); } } wx::EndBusyCursor(); bbtkDebugDecTab("Process",1); } //================================================================== //================================================================== void ComplexBlackBox::bbSetModifiedStatus(BlackBoxInputConnector* c) { bbtkDebugMessage("Process",3, "ComplexBlackBox::bbSetModifiedStatus(" <GetBlackBox()->bbSetModifiedStatus(c); bbtkDebugMessage("Process",3, "EO ComplexBlackBox::bbSetModifiedStatus(" <GetFullName()<<") [" <GetOutputDescriptorMap(); BlackBoxDescriptor::OutputDescriptorMapType::const_iterator i = omap.find(caller->GetBlackBoxFromOutput()); if (i!=omap.end()) { // Cast the BBOutputDescriptor into a ComplexBBOutputDescriptor ComplexBlackBoxOutputDescriptor* d = (ComplexBlackBoxOutputDescriptor*)i->second; // Get the internal box BlackBox* b = bbUnsafeGetBlackBox ( d->GetTarget() ); // Calls BackwardUpdate on it bbtkDebugMessageInc("Process",2,"Internal box connected to output : "<GetTarget()<bbBackwardUpdate(caller); // ??? STATUS OF CBBs ??? // ??? Here it is only the final status of the boxes connected to the output if (s1==MODIFIED) s=MODIFIED; bbtkDebugDecTab("Process",2); } else { bbtkError("Connection '"<GetFullName()<<"' does not point to a valid output of the complex box !"); } bbtkDebugDecTab("Process",1); return s; } //================================================================== /* //================================================================== void ComplexBlackBox::bbForwardUpdate(Connection* caller) { bbtkDebugMessageInc("Process",1, "ComplexBlackBox::bbForwardUpdate("<GetFullName()<<") [" <GetInputDescriptorMap(); BlackBoxDescriptor::InputDescriptorMapType::const_iterator i = imap.find(caller->GetBlackBoxToInput()); if (i!=imap.end()) { // Cast the BBOutputDescriptor into a ComplexBBOutputDescriptor ComplexBlackBoxInputDescriptor* d = (ComplexBlackBoxInputDescriptor*)i->second; // Get the internal box BlackBox* b = bbUnsafeGetBlackBox ( d->GetTarget() ); // Calls ForwardUpdate on it bbtkDebugMessage("Process",2,"-> Internal box connected to input : "<GetTarget()<bbForwardUpdate(caller); } else { bbtkError("Connection '"<GetFullName()<<"' does not point to a valid input of the complex box !"); } bbtkDebugDecTab("Process",1); } //================================================================== */ //================================================================== Data ComplexBlackBox::bbGetOutput( const std::string &name ) { bbtkDebugMessageInc("Data",7, "ComplexBlackBox::bbGetOutput(\""<GetOutputDescriptor(name); Data p = bbGetBlackBox(d->GetTarget())->bbGetOutput(d->GetOutput()); bbtkDebugDecTab("Data",7); return p; } //================================================================== //================================================================== /// Gets the input Data of a given name Data ComplexBlackBox::bbGetInput( const std::string &name ) { bbtkDebugMessageInc("Data",7, "ComplexBlackBox::bbGetInput(\""<GetInputDescriptor(name); Data p = bbGetBlackBox(d->GetTarget())->bbGetInput(d->GetInput()); bbtkDebugDecTab("Data",7); return p; } //================================================================== //================================================================== /// Sets the data of the output called void ComplexBlackBox::bbSetOutput( const std::string &name, Data data) { bbtkDebugMessageInc("Data",7, "ComplexBlackBox::bbSetOutput(\""<GetOutputDescriptor(name); bbGetBlackBox(d->GetTarget())->bbSetOutput(d->GetOutput(),data); bbtkDebugDecTab("Data",7); } //================================================================== //================================================================== /// Sets the data of the input called void ComplexBlackBox::bbSetInput( const std::string &name, Data data, bool setModified) { bbtkDebugMessageInc("Data",7, "ComplexBlackBox::bbSetInput(\""<GetInputDescriptor(name); bbGetBlackBox(d->GetTarget())->bbSetInput(d->GetInput(),data,setModified); bbtkDebugDecTab("Data",7); } //================================================================== //================================================================== /// Sets the data of the input called void ComplexBlackBox::bbBruteForceSetInputPointer( const std::string &name, void* data, bool setModified) { bbtkDebugMessageInc("Data",7, "ComplexBlackBox::bbBruteForceSetInputPointer('" <GetInputDescriptor(name); bbGetBlackBox(d->GetTarget())->bbBruteForceSetInputPointer(d->GetInput(), data, setModified); bbtkDebugDecTab("Data",7); } //================================================================== //================================================================== /// Adds the black box to the complex box void ComplexBlackBox::bbAddBlackBox( BlackBox* b) { bbtkDebugMessageInc("Core",7, "ComplexBlackBox::AddBlackBox(\""<bbGetName() <<"\") [" <bbGetName()) ) { bbtkError("a black box called \""<bbGetName() <<"\" already exists"); } b->bbSetParent(this); mBlackBoxMap[b->bbGetName()] = b; bbtkDebugDecTab("Core",7); } //================================================================== //================================================================== /// Adds the black box to the complex box (unsafe) void ComplexBlackBox::bbUnsafeAddBlackBox( BlackBox* b) { bbtkDebugMessageInc("Core",7, "ComplexBlackBox::UnsafeAddBlackBox(\""<bbGetName() <<"\") [" <bbSetParent(this); mBlackBoxMap[b->bbGetName()] = b; bbtkDebugDecTab("Core",7); } //================================================================== //================================================================== /// Removes the black box from the complex box void ComplexBlackBox::bbRemoveBlackBox( const std::string& name ) { bbtkDebugMessageInc("Core",7, "ComplexBlackBox::RemoveBlackBox(\""< BlackBox* ComplexBlackBox::bbGetBlackBox( const std::string& name ) { bbtkDebugMessageInc("Core",9, "ComplexBlackBox::GetBlackBox(\""<second; } //================================================================== //================================================================== /// Returns the black box with name : does not throw an exception /// if it does not exist but return a null pointer BlackBox* ComplexBlackBox::bbUnsafeGetBlackBox( const std::string& name ) { bbtkDebugMessageInc("Core",9, "ComplexBlackBox::UnsafeGetBlackBox(\""<second; } //================================================================== //================================================================== void ComplexBlackBox::bbPrintBlackBoxes() { bbtkDebugMessageInc("Core",9, "ComplexBlackBox::PrintBlackBoxes() [" <second->bbGetFullName()<"); // Skip delimiters at beginning. std::string::size_type lastPos = blackboxname.find_first_not_of(delimiters, 0); // Find first "non-delimiter". std::string::size_type pos = blackboxname.find_first_of(delimiters, lastPos); // Found a token, add it to the vector. subname = blackboxname.substr(lastPos, pos - lastPos); restname = blackboxname.substr(lastPos+pos - lastPos+1, 999); if (restname==subname) { restname=""; } BlackBoxMapType::iterator i = mBlackBoxMap.find(subname); if ( i == mBlackBoxMap.end() ) { blackbox = NULL; } else { blackbox = i->second; if (restname!="") { blackbox = blackbox->bbFindBlackBox(restname); } } return blackbox; } //========================================================================= //========================================================================= void ComplexBlackBox::bbInsertHTMLGraph( std::ofstream& s, int detail, int level, bool instanceOrtype, const std::string& output_dir, bool relative_link ) { std::string directory(output_dir); if (output_dir.length() == 0) { // Don't pollute the file store with "doc_tmp" directories ... std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_doc_tmp(); directory = default_doc_dir + "/" + "doc_tmp"; } std::string simplefilename (this->bbGetTypeName()+"_"+this->bbGetName()); std::string simplefilename_png (simplefilename+".png"); std::string filename (directory+"/"+simplefilename); std::string filename_png (filename+".png"); std::string filename_cmap (filename+".cmap"); std::string filename_dot (filename+".dot"); std::string filename_png2 ("\"" + filename_png + "\""); std::string filename_cmap2 ("\"" + filename_cmap + "\""); std::string filename_dot2 ("\"" + filename_dot + "\""); std::string command1 ("dot -T png -o " + filename_png2 + " " + filename_dot2); std::string command1a("dot -T cmap -o " + filename_cmap2 + " " + filename_dot2); // 1. Generating .dot file FILE *ff; ff = fopen(filename_dot.c_str(),"w"); fprintf(ff,"digraph bbtk_graph{\n"); fprintf(ff,"rankdir=LR%s\n",";"); fprintf(ff,"node [shape=record]%s\n",";"); this->bbWriteDotFileBlackBox(ff,this,detail,level, instanceOrtype, relative_link ); fprintf(ff,"}\n"); fclose(ff); // 2. Executing .dot file -> png system( command1.c_str() ); // 3. Executing .dot file -> cmap system( command1a.c_str() ); // 4. HTML code insertion // 4.1 image (s) << "
\"\"
\n"; // 4.2 map (s) << "\n"; FILE *ff2; char c; ff2=fopen(filename_cmap.c_str(),"r"); while (!feof(ff2)) { c=fgetc(ff2); if (c!=-1) { (s) << c; } } (s) << "\n\n"; // End } //========================================================================= //========================================================================= /// Write Graphviz-dot description in file void ComplexBlackBox::bbWriteDotFileBlackBox(FILE *ff, BlackBox *parentblackbox, int detail, int level, bool instanceOrtype, bool relative_link ) { std::string tmp1; std::string valueStr(""); Package *package = this->bbGetDescriptor()->GetPackage(); if (package!=NULL) { if (relative_link) tmp1 = this->bbGetDescriptor()->GetPackage()->GetDocRelativeURL(); else tmp1 = this->bbGetDescriptor()->GetPackage()->GetDocURL(); } else { tmp1 = "Caspitas"; } std::string tmp2=bbGetTypeName(); std::string url(tmp1 + "#" + tmp2 ); fprintf( ff , "subgraph cluster_%s_%p {\n",bbGetName().c_str(),this); if (!( (bbGetTypeName()=="workspace") && (bbGetName()=="workspacePrototype")) ) { fprintf( ff , " URL = \"%s\" %s",url.c_str(),";"); } std::string boxname="["+bbGetTypeName()+"]"; if (this!=parentblackbox) { if (detail==0) { boxname=bbGetName(); } else { boxname = bbGetName(); boxname = boxname + " [" +this->bbGetDescriptor()->GetPackage()->GetName()+"::"+ bbGetTypeName() + "]"; } } fprintf( ff , " label = \"%s\"%s\n", boxname.c_str() ,";"); // fprintf( ff , " style=filled%s\n",";"); // fprintf( ff , " color=grey%s\n",";"); fprintf( ff , " node [style=filled]%s\n",";"); fprintf( ff , " fillcolor=grey%s\n",";"); fprintf( ff , " edge [color=blue]%s\n",";"); // Labels Salida std::string labelStr1; std::string labelStr2; labelStr1 = boxname + "\\n(output)" ; labelStr2 = " | {{ "; bool tmp; tmp=false; OutputConnectorMapType::iterator i; const BlackBoxDescriptor::OutputDescriptorMapType& omap = this->bbGetDescriptor()->GetOutputDescriptorMap(); BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o; for ( o = omap.begin(); o != omap.end(); ++o ) { if (tmp==true) { labelStr2=labelStr2+" | "; } tmp=true; if (instanceOrtype==true) { valueStr = this->bbGetOutputAsString(o->second->GetName()/* ,factory*/) + " = "; } labelStr2=labelStr2+"<"+o->second->GetName().c_str()+"> " + valueStr + o->second->GetName().c_str(); } labelStr2 = labelStr2+ " } }"; if (detail==1) { labelStr1 = labelStr1 + labelStr2; } if (tmp){ fprintf(ff," " ); bbWriteDotInputOutputName(ff,false,detail,level); fprintf( ff , " [shape=record, style=filled,fillcolor=grey,color=red,label=\"%s\"]%s\n",labelStr1.c_str(),";" ); } // label Entrada labelStr1 = boxname + "\\n(input)" ; labelStr2 = " | {{ "; tmp=false; InputConnectorMapType::iterator ii; const BlackBoxDescriptor::InputDescriptorMapType& imap = this->bbGetDescriptor()->GetInputDescriptorMap(); BlackBoxDescriptor::InputDescriptorMapType::const_iterator iii; for ( iii = imap.begin(); iii != imap.end(); ++iii ) { if (tmp==true) { labelStr2=labelStr2+" | "; } tmp=true; if (instanceOrtype==true) { valueStr = this->bbGetInputAsString(iii->second->GetName()/*,factory*/) + " = "; } labelStr2=labelStr2+"<"+iii->second->GetName().c_str()+"> " + valueStr + iii->second->GetName().c_str(); } labelStr2 = labelStr2+ " } }"; if (detail==1) { labelStr1 = labelStr1 + labelStr2; } if (tmp){ fprintf(ff," " ); bbWriteDotInputOutputName(ff,true,detail,level); fprintf( ff , " [shape=record, style=filled,fillcolor=grey,color=red,label=\"%s\"]%s\n",labelStr1.c_str(),";" ); } // Body BlackBoxMapType::iterator j; for ( j = mBlackBoxMap.begin(); j != mBlackBoxMap.end(); ++j ) { if (level>-1) { j->second->bbWriteDotFileBlackBox(ff,parentblackbox,detail, level-1, instanceOrtype, relative_link); } } fprintf( ff , "}\n\n"); fprintf( ff , " edge[color=blue]%s\n",";"); if (level>-1) { // Relation Input with the inside BlackBox of the this ComplexBlackbox ComplexBlackBoxDescriptor::InputDescriptorMapType::iterator xx; ComplexBlackBoxDescriptor::InputDescriptorMapType idmt=bbGetDescriptor()->GetInputDescriptorMap(); for ( xx = idmt.begin(); xx != idmt.end(); ++xx ) { ComplexBlackBoxInputDescriptor *cbbid = (ComplexBlackBoxInputDescriptor*)xx->second; fprintf(ff," "); bbWriteDotInputOutputName(ff,true,detail,level); if (detail==1) { fprintf(ff,":%s",cbbid->GetName().c_str() ); } fprintf(ff,"->" ); BlackBox *bb = bbGetBlackBox( cbbid->GetTarget() ); bb->bbWriteDotInputOutputName(ff,true,detail,level); if (detail==1) { fprintf(ff,":%s \n", cbbid->GetInput().c_str() ); } }// for xx fprintf(ff,"\n \n"); // Relation Output ComplexBlackBox ComplexBlackBoxDescriptor::OutputDescriptorMapType::iterator yy; ComplexBlackBoxDescriptor::OutputDescriptorMapType odmt=bbGetDescriptor()->GetOutputDescriptorMap(); for ( yy = odmt.begin(); yy != odmt.end(); ++yy ) { ComplexBlackBoxOutputDescriptor *cbbod = (ComplexBlackBoxOutputDescriptor*)yy->second; fprintf(ff," "); BlackBox *bb = bbGetBlackBox( cbbod->GetTarget() ); bb->bbWriteDotInputOutputName(ff,false,detail,level); if (detail==1) { fprintf(ff,":%s", cbbod->GetOutput().c_str() ); } fprintf(ff,"->" ); bbWriteDotInputOutputName(ff,false,detail,level); if (detail==1) { fprintf(ff,":%s",cbbod->GetName().c_str() ); } fprintf(ff,"\n"); } // for yy } // if level // Relation from the out side of this ComplexBlackBox with its Inputs if (this!=parentblackbox) { for ( ii = bbGetInputConnectorMap().begin(); ii != bbGetInputConnectorMap().end(); ++ii ) { if (ii->second) { Connection* con = ii->second->GetConnection(); if (con!=NULL){ BlackBox *a=con->GetBlackBoxFrom(); BlackBox *b=con->GetBlackBoxTo(); fprintf(ff," "); a->bbWriteDotInputOutputName(ff,false,detail,level); if (detail==1) { fprintf(ff,":%s",con->GetBlackBoxFromOutput().c_str()); } fprintf(ff,"->"); b->bbWriteDotInputOutputName(ff,true,detail,level); if (detail==1) { fprintf(ff,":%s",con->GetBlackBoxToInput().c_str()); } fprintf(ff,"%s\n",";"); } // if con } // if second } // for } // if parentblackbox } //========================================================================= /* //======================================================================= /// Generates the list of the packages of which its depends /// (cause an internal box belongs to it) void ComplexBlackBox::GetPackagesDependencies(std::vector& deps) { deps.clear; BlackBoxMapType::iterator i; for ( i = mBlackBoxMap.begin(); i != mBlackBoxMap.end(); ++i ) { deps.push_back(i->second->bbGetDescriptor()->GetPackage()); } } //======================================================================= */ }