00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00036 #include "bbtkExecuter.h"
00037 #include "bbtkMessageManager.h"
00038 #include "bbtkFactory.h"
00039 #include "bbtkUtilities.h"
00040
00041 #include <fstream>
00042
00043 #ifdef USE_WXWIDGETS
00044 #include <wx/textdlg.h>
00045 #endif
00046
00047 #include "bbtkWxBlackBox.h"
00048
00049 #include "bbtkConfigurationFile.h"
00050
00051 namespace bbtk
00052 {
00053
00054 Executer::Pointer Executer::New()
00055 {
00056 bbtkDebugMessage("object",9,"Executer::New()"<<std::endl);
00057 return MakePointer(new Executer());
00058 }
00059
00060
00061
00062 Executer::Executer()
00063 :
00064 mFactory(),
00065 mRootPackage(),
00066 mRootCBB(),
00067 mNoExecMode(false),
00068 mDialogMode(NoDialog),
00069 mNoErrorMode(false)
00070 {
00071 bbtkDebugMessage("object",2,"==> Executer()" <<std::endl);
00072 mFactory = Factory::New();
00073
00074
00075
00076
00077
00078 mFactory->SetExecuter(MakePointer(this,true));
00079 Reset();
00080 bbtkDebugMessage("object",2,"<== Executer()" <<std::endl);
00081 }
00082
00083
00084
00085 Executer::~Executer()
00086 {
00087 bbtkDebugMessage("object",2,"==> ~Executer()" <<std::endl);
00088 mOpenDefinition.clear();
00089 mOpenPackage.clear();
00090 mFactory->Reset();
00091 mFactory.reset();
00092 bbtkDebugMessage("object",2,"<== ~Executer()" <<std::endl);
00093 }
00094
00095
00096
00098 void Executer::LoadPackage(const std::string &name )
00099 {
00100 GetFactory()->LoadPackage(name);
00101 }
00102
00103
00104
00106 void Executer::UnLoadPackage(const std::string &name )
00107 {
00108 GetFactory()->UnLoadPackage(name);
00109 }
00110
00111
00112
00113 void Executer::Reset()
00114 {
00115 bbtkDebugMessage("kernel",9,"==> Executer::Reset()" <<std::endl);
00116
00117
00118
00119 mOpenDefinition.clear();
00120 mOpenPackage.clear();
00121
00122
00123
00124 GetFactory()->Reset();
00125 Wx::ProcessPendingEvents();
00126
00127
00128 Package::Pointer p =
00129 Package::New("user","internal","User defined black boxes","");
00130
00131 GetFactory()->InsertPackage(p);
00132
00133 mOpenPackage.push_back(p);
00134 mRootPackage = p;
00135
00136
00137 ComplexBlackBoxDescriptor::Pointer r =
00138 ComplexBlackBoxDescriptor::New("workspace");
00139
00140 r->SetFactory(GetFactory());
00141 r->AddToAuthor("bbtk");
00142 r->AddToDescription("User's workspace");
00143 mOpenDefinition.push_back(CBBDefinition(r,"user"));
00144
00145 p->Register(r);
00146 mRootCBB = r;
00147
00148
00149
00150 bbtkDebugMessage("kernel",9,"<== Executer::Reset()" <<std::endl);
00151 }
00152
00153
00154
00155
00157 void Executer::SetWorkspaceName( const std::string& n )
00158 {
00159 GetUserPackage()->ChangeDescriptorName( GetWorkspace()->GetTypeName(), n );
00160 }
00161
00162
00163
00164 void Executer::BeginPackage (const std::string &name)
00165 {
00166 bbtkDebugMessage("kernel",9,"==> Executer::BeginPackage(\""<<name<<"\")"
00167 <<std::endl);
00168 Package::Pointer p;
00169 try
00170 {
00171 p = GetFactory()->GetPackage(name);
00172 }
00173 catch (Exception e)
00174 {
00175 p = Package::New(name,"","","");
00176 GetFactory()->InsertPackage(p);
00177 }
00178 mOpenPackage.push_back(p);
00179
00180 bbtkDebugMessage("kernel",9,"<== Executer::BeginPackage(\""<<name<<"\")"
00181 <<std::endl);
00182 }
00183
00184
00185
00186 void Executer::EndPackage()
00187 {
00188 if (mOpenPackage.size()>1) mOpenPackage.pop_back();
00189 }
00190
00191
00192
00193 void Executer::Define (const std::string &name,
00194 const std::string &pack,
00195 const std::string &scriptfilename)
00196 {
00197 bbtkDebugMessage("kernel",9,"==> Executer::Define(\""<<name<<
00198 ","<<pack<<"\")"
00199 <<std::endl);
00200
00201 ComplexBlackBoxDescriptor::Pointer b
00202 = ComplexBlackBoxDescriptor::New(name);
00203 b->SetFactory(GetFactory());
00204 b->SetScriptFileName(scriptfilename);
00205 mOpenDefinition.push_back( CBBDefinition( b, pack ) );
00206
00207 bbtkDebugMessage("kernel",9,"<== Executer::Define(\""<<name<<
00208 ","<<pack<<"\")"
00209 <<std::endl);
00210 }
00211
00212
00213
00216 void Executer::SetCurrentFileName (const std::string &name )
00217 {
00218 mOpenDefinition.back().box->SetScriptFileName(name);
00219 }
00220
00221
00222
00223 void Executer::Clear()
00224 {
00225 bbtkDebugMessage("kernel",9,"==> Executer::Clear()" <<std::endl);
00226 GetCurrentDescriptor()->GetPrototype()->Clear();
00227 bbtkDebugMessage("kernel",9,"<== Executer::Clear()" <<std::endl);
00228 }
00229
00230
00231
00232 void Executer::EndDefine ()
00233 {
00234 bbtkDebugMessage("kernel",9,"==> Executer::EndDefine(\""
00235 <<GetCurrentDescriptor()->GetTypeName()<<"\")"
00236 <<std::endl);
00237
00238 Package::Pointer p;
00239 std::string pname(mOpenDefinition.back().package);
00240 if (pname.size()>0)
00241 {
00242 try
00243 {
00244 p = GetFactory()->GetPackage(pname);
00245 }
00246 catch (Exception e)
00247 {
00248 p = Package::New(pname,"","","");
00249 GetFactory()->InsertPackage(p);
00250 }
00251 }
00252 else
00253 {
00254 p = mOpenPackage.back().lock();
00255 }
00256
00257 p->Register(GetCurrentDescriptor());
00258
00259 bbtkDebugMessage("kernel",9,"<== Executer::EndDefine(\""
00260 <<GetCurrentDescriptor()->GetTypeName()<<"\")"
00261 <<std::endl);
00262 mOpenDefinition.pop_back();
00263 }
00264
00265
00266
00267 void Executer::Kind(const std::string& kind)
00268 {
00269 if (kind=="ADAPTOR")
00270 {
00271 GetCurrentDescriptor()->AddToCategory("adaptor");
00272 GetCurrentDescriptor()->SetKind(bbtk::BlackBoxDescriptor::ADAPTOR);
00273 }
00274 else if (kind=="DEFAULT_ADAPTOR")
00275 {
00276 GetCurrentDescriptor()->AddToCategory("adaptor");
00277 GetCurrentDescriptor()->SetKind(bbtk::BlackBoxDescriptor::DEFAULT_ADAPTOR);
00278 }
00279 if (kind=="GUI")
00280 {
00281 GetCurrentDescriptor()->AddToCategory("gui");
00282 GetCurrentDescriptor()->SetKind(bbtk::BlackBoxDescriptor::GUI);
00283 }
00284 else if (kind=="DEFAULT_GUI")
00285 {
00286 GetCurrentDescriptor()->AddToCategory("gui");
00287 GetCurrentDescriptor()->SetKind(bbtk::BlackBoxDescriptor::DEFAULT_GUI);
00288 }
00289 else
00290 {
00291 bbtkError("Unknown box kind : '"<<kind<<"'. "
00292 <<"Valid kinds are 'ADAPTOR','DEFAULT_ADAPTOR',"
00293 <<"'GUI','DEFAULT_GUI'");
00294 }
00295 }
00296
00297
00298
00299 void Executer::Create ( const std::string& nodeType,
00300 const std::string& nodeName)
00301 {
00302 GetCurrentDescriptor()->Add(nodeType,nodeName);
00303 }
00304
00305
00306
00307 void Executer::Destroy(const std::string &boxName)
00308 {
00309 GetCurrentDescriptor()->Remove(boxName,true);
00310 }
00311
00312
00313
00314 void Executer::Connect (const std::string &nodeFrom,
00315 const std::string &outputLabel,
00316 const std::string &nodeTo,
00317 const std::string &inputLabel)
00318 {
00319 GetCurrentDescriptor()->Connect(nodeFrom, outputLabel, nodeTo, inputLabel);
00320 }
00321
00322
00323
00324 void Executer::Execute (const std::string &nodeName)
00325 {
00326
00327 if (GetCurrentDescriptor()==GetWorkspace())
00328 {
00329 if (!mNoExecMode)
00330 {
00331 GetCurrentDescriptor()->GetPrototype()->bbGetBlackBox(nodeName)->bbExecute(true);
00332 }
00333 }
00334 else
00335 {
00336 GetCurrentDescriptor()->AddToExecutionList(nodeName) ;
00337 }
00338 }
00339
00340
00341
00342 void Executer::DefineInput ( const std::string &name,
00343 const std::string &box,
00344 const std::string &input,
00345 const std::string& help)
00346 {
00347
00348 if (GetCurrentDescriptor()==GetWorkspace())
00349 {
00350
00351
00352
00353
00354 if (mDialogMode == NoDialog)
00355 {
00356
00357 std::map<std::string,std::string>::iterator i;
00358 i = mInputs.find(name);
00359 if (i!=mInputs.end()) {
00360 Set(box,input,(*i).second);
00361 }
00362 }
00363
00364
00365 else if (mDialogMode == TextDialog)
00366 {
00367 std::cout << name << "=";
00368 std::string ans;
00369 std::cin >> ans;
00370 Set(box,input,ans);
00371 }
00372 #ifdef USE_WXWIDGETS
00373
00374
00375 else if (mDialogMode == GraphicalDialog)
00376 {
00377 std::string mess("Enter the value of '");
00378 mess += name;
00379 mess += "' (";
00380 mess += help;
00381 mess += ")";
00382 std::string title(name);
00383 title += " ?";
00384 std::string ans = wx2std ( wxGetTextFromUser( std2wx (mess), std2wx(title)));
00385 Set(box,input,ans);
00386 }
00387 #endif
00388 }
00389
00390 GetCurrentDescriptor()->DefineInput(name,box,input,help);
00391
00392 }
00393
00394
00395
00396 void Executer::DefineOutput ( const std::string &name,
00397 const std::string &box,
00398 const std::string &output,
00399 const std::string& help)
00400 {
00401 GetCurrentDescriptor()->DefineOutput(name,box,output,help);
00402 }
00403
00404
00405
00406 void Executer::Set (const std::string &box,
00407 const std::string &input,
00408 const std::string &value)
00409 {
00410 BlackBox::Pointer b = GetCurrentDescriptor()->GetPrototype()->bbGetBlackBox(box);
00411
00412
00413 if ( ( b->bbGetInputType(input) != typeid(bbtk::any<bbtk::thing>) )&&
00414 ( b->bbGetInputType(input) != typeid(std::string) ) )
00415 {
00416 BlackBox::Pointer a =
00417 GetFactory()->NewAdaptor(typeid(std::string),
00418 b->bbGetInputType(input),
00419 "tmp");
00420 if (!a)
00421 {
00422 bbtkError("No <"<<
00423 TypeName(b->bbGetInputType(input))
00424 <<"> to <std::string> found");
00425 }
00426 std::string v(value);
00427 a->bbSetInput("In",v);
00428 a->bbExecute();
00429 b->bbSetInput(input,a->bbGetOutput("Out"));
00430
00431 }
00432 else
00433 {
00434 std::string v(value);
00435 b->bbSetInput(input,v);
00436 }
00437 }
00438
00439
00440
00441 std::string Executer::Get(const std::string &box,
00442 const std::string &output)
00443 {
00444 BlackBox::Pointer b = GetCurrentDescriptor()->GetPrototype()->bbGetBlackBox(box);
00445
00446 if (b->bbGetOutputType(output) != typeid(std::string))
00447 {
00448 BlackBox::Pointer a =
00449 GetFactory()->NewAdaptor(
00450 b->bbGetOutputType(output),
00451 typeid(std::string),
00452 "tmp");
00453 if (!a)
00454 {
00455 bbtkError("No <"<<
00456 TypeName(b->bbGetOutputType(output))
00457 <<"> to <std::string> found");
00458 }
00459 b->bbExecute();
00460
00461 a->bbSetInput("In",b->bbGetOutput(output));
00462 a->bbExecute();
00463 std::string r = a->bbGetOutput("Out").unsafe_get<std::string>();
00464
00465
00466
00467
00468
00469
00470 return r;
00471 }
00472 else
00473 {
00474 b->bbExecute();
00475 return b->bbGetOutput(output).unsafe_get<std::string>();
00476
00477
00478
00479
00480 }
00481 }
00482
00483
00484
00485 void Executer::Author(const std::string &authorName)
00486 {
00487 GetCurrentDescriptor()->AddToAuthor(authorName,GetCurrentDescriptor()==GetWorkspace());
00488 }
00489
00490
00491
00492 void Executer::Category(const std::string &category)
00493 {
00494 GetCurrentDescriptor()->AddToCategory(category,GetCurrentDescriptor()==GetWorkspace());
00495 }
00496
00497
00498
00499 void Executer::Description(const std::string &d)
00500 {
00501 GetCurrentDescriptor()->AddToDescription(d,GetCurrentDescriptor()==GetWorkspace());
00502 }
00503
00504
00505
00506
00507
00509 void Executer::PrintHelpListBoxes()
00510 {
00511 bbtkMessage("help",1,"The black box descriptor \""
00512 <<GetCurrentDescriptor()->GetTypeName()<<"\" contains : "<<std::endl);
00513 GetCurrentDescriptor()->PrintBlackBoxes();
00514 }
00515
00516
00517
00518 std::string Executer::ShowGraph(const std::string &nameblackbox,
00519 const std::string &detailStr,
00520 const std::string &levelStr,
00521 const std::string &output_html,
00522 const std::string &custom_header,
00523 const std::string &custom_title,
00524 bool system_display )
00525 {
00526 int detail = atoi(detailStr.c_str());
00527 int level = atoi(levelStr.c_str());
00528 bool relative_link = true;
00529
00530 Package::Pointer p;
00531 try
00532 {
00533 p = GetFactory()->GetPackage(nameblackbox);
00534 }
00535 catch (Exception e)
00536 {
00537 p = GetUserPackage();
00538 }
00539
00540 std::string doc_path = bbtk::ConfigurationFile::GetInstance().Get_doc_path();
00541 doc_path += bbtk::ConfigurationFile::GetInstance().Get_file_separator();
00542 doc_path += "bbdoc";
00543 doc_path += bbtk::ConfigurationFile::GetInstance().Get_file_separator();
00544
00545 std::string pack_name(p->GetName());
00546 std::string pack_path = doc_path + pack_name;
00547
00548 if ( ! bbtk::Utilities::FileExists(pack_path) )
00549 {
00550 std::string command("mkdir \"" +pack_path+ "\"");
00551 system( command.c_str() );
00552 }
00553 std::string pack_index(pack_path);
00554 pack_index += bbtk::ConfigurationFile::GetInstance().Get_file_separator();
00555 pack_index += "index.html";
00556
00557
00558
00559 p->SetDocURL(pack_index);
00560 p->SetDocRelativeURL("index.html");
00561 p->CreateHtmlPage(pack_index,"bbtk","user package",custom_header,custom_title,detail,level,relative_link);
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574 return pack_index;
00575 }
00576
00577
00578
00580 std::string Executer::ShowGraphInstances(const std::string &nameblackbox, int detail, int level,
00581 bool system_display)
00582 {
00583
00584 BlackBox::Pointer blackbox;
00585 if (nameblackbox==".")
00586 {
00587 blackbox = GetCurrentDescriptor()->GetPrototype();
00588 }
00589 else
00590 {
00591 blackbox = GetCurrentDescriptor()->GetPrototype()->bbFindBlackBox(nameblackbox);
00592 }
00593
00594 std::string page;
00595
00596 if (blackbox)
00597 {
00598
00599 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
00600 char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
00601
00602 std::string directory = default_doc_dir;
00603 if (c != '/' && c !='\\') directory = directory + "/";
00604
00605 directory = directory + "temp_dir";
00606
00607 std::string filename(directory + "/" + "bbtk_graph_pipeline");
00608 std::string filename_html(filename+".html");
00609 std::string command0("mkdir \""+directory + "\"");
00610
00611 #if defined(_WIN32)
00612 std::string command2("start ");
00613 #else
00614 std::string command2("gnome-open ");
00615 #endif
00616
00617 command2=command2+filename_html;
00618 page = filename_html;
00619
00620 std::ofstream s;
00621 s.open(filename_html.c_str());
00622 if (s.good())
00623 {
00624 s << "<html><head><title>BBtk graph diagram</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"></head>\n";
00625 s << "<body bgcolor=\"#FFFFFF\" text=\"#000000\"> \n\n";
00626 if ( blackbox->bbGetName()=="workspacePrototype" )
00627 {
00628 s << "<center>Current workspace</center>";
00629 } else {
00630 s << "<center>" << blackbox->bbGetName()<< "</center>";
00631 }
00632 blackbox->bbInsertHTMLGraph( s, detail, level, true, directory, false );
00633 s << "</body></html>\n";
00634 }
00635 s.close();
00636
00637
00638 if (system_display) system( command2.c_str() );
00639 }
00640 else
00641 {
00642 bbtkMessage("help",1,"No black box: \""
00643 <<nameblackbox<<"\" " <<std::endl);
00644 }
00645 return page;
00646 }
00647
00648
00649
00650 void Executer::PrintHelpBlackBox(const std::string &nameblackbox,
00651 const std::string &detailStr,
00652 const std::string &levelStr)
00653 {
00654 bool found=false;
00655
00656 int detail = atoi(detailStr.c_str());
00657 int level = atoi(levelStr.c_str());
00658 BlackBox::Pointer blackbox;
00659 if (nameblackbox.compare(".")==0)
00660 {
00661 blackbox=GetCurrentDescriptor()->GetPrototype();
00662 }
00663 else
00664 {
00665 blackbox = GetCurrentDescriptor()->GetPrototype()->bbFindBlackBox(nameblackbox);
00666 }
00667
00668 if (blackbox)
00669 {
00670 found=true;
00671 blackbox->bbPrintHelp(blackbox,detail,level);
00672 }
00673
00674 if (!found)
00675 {
00676 bbtkError("box with name '" <<nameblackbox<<"' unknown");
00677 }
00678 }
00679
00680
00681
00683 void Executer::SetMessageLevel(const std::string &kind,
00684 int level)
00685 {
00686 bbtk::MessageManager::SetMessageLevel(kind,level);
00687 }
00688
00689
00690
00692 void Executer::HelpMessages()
00693 {
00694 bbtk::MessageManager::PrintInfo();
00695 }
00696
00697
00698
00700 void Executer::Print(const std::string &str)
00701 {
00702 if (GetNoExecMode() && (GetCurrentDescriptor()==GetWorkspace()) ) return;
00703 if (GetCurrentDescriptor()!=GetWorkspace()) return;
00704
00705 bbtkDebugMessage("kernel",9,"Executer::Print(\""<<str<<"\")"<<std::endl);
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715 std::vector<std::string> chains;
00716 std::string delimiters("$");
00717
00718
00719 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
00720 bool is_text = true;
00721 if (lastPos>0) is_text = false;
00722
00723
00724 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
00725
00726 while (std::string::npos != pos || std::string::npos != lastPos)
00727 {
00728 if (is_text)
00729 {
00730
00731 chains.push_back(str.substr(lastPos, pos - lastPos));
00732
00733
00734
00735 }
00736 else
00737 {
00738
00739
00740 std::string tok,box,output;
00741 tok = str.substr(lastPos, pos - lastPos);
00742 Utilities::SplitAroundFirstDot(tok,box,output);
00743 chains.push_back( Get(box,output) );
00744
00745
00746
00747 }
00748
00749 lastPos = str.find_first_not_of(delimiters, pos);
00750
00751 pos = str.find_first_of(delimiters, lastPos);
00752
00753 is_text = !is_text;
00754
00755 }
00756
00757
00758
00759 std::vector<std::string>::iterator i;
00760 for (i= chains.begin(); i!=chains.end(); ++i)
00761 {
00762 Utilities::SubsBackslashN(*i);
00763 bbtkMessage("output",1,*i);
00764 }
00765 bbtkMessage("output",1,std::endl);
00766 }
00767
00768
00769
00770 std::string Executer::GetObjectName() const
00771 {
00772 return std::string("Executer");
00773 }
00774
00775
00776
00777 std::string Executer::GetObjectInfo() const
00778 {
00779 std::stringstream i;
00780 return i.str();
00781 }
00782
00783
00784 size_t Executer::GetObjectSize() const
00785 {
00786 size_t s = Superclass::GetObjectSize();
00787 s += Executer::GetObjectInternalSize();
00788 return s;
00789 }
00790
00791
00792 size_t Executer::GetObjectInternalSize() const
00793 {
00794 size_t s = sizeof(Executer);
00795 return s;
00796 }
00797
00798
00799 size_t Executer::GetObjectRecursiveSize() const
00800 {
00801 size_t s = Superclass::GetObjectRecursiveSize();
00802 s += Executer::GetObjectInternalSize();
00803 s += mFactory->GetObjectRecursiveSize();
00804 return s;
00805 }
00806
00807 }