/tags/rel-0-4-0/FreeSpeech/data-flow/src/UIDocument.cc

# · C++ · 906 lines · 653 code · 152 blank · 101 comment · 180 complexity · 878132498e1aa3f26f72ae2a38531ffc MD5 · raw file

  1. #include <tree.h>
  2. #include <parser.h>
  3. #include "UIDocument.h"
  4. #include "UINetwork.h"
  5. //#include "UINodeMenu.h"
  6. #include "Node.h"
  7. #include "Network.h"
  8. #include "ParameterSet.h"
  9. #include <sys/stat.h>
  10. #include <dlfcn.h>
  11. map<string, SubnetInfo *> UIDocument::externalDocInfo;
  12. UIDocument::UIDocument(string _name)
  13. : docName(_name)
  14. , untitled(true)
  15. , modified(false)
  16. {
  17. //cerr << "created doc with name " << docName << endl;
  18. //gnome_mdi_child_set_name (GNOME_MDI_CHILD(this), (gchar *)name.c_str());
  19. //create();
  20. }
  21. UIDocument::~UIDocument()
  22. {
  23. //cerr << "destroying UIDocument " << name << endl;
  24. for (int i=0;i<networks.size();i++)
  25. delete networks[i];
  26. //child.name = "destroyed document";
  27. }
  28. UINetwork *UIDocument::getNetworkNamed(const string &n)
  29. {
  30. //cerr << "UIDocument::getNetworkNamed\n";
  31. //cerr << n << endl;
  32. //cerr << "size = " << networks.size() << endl;
  33. for (int i=0;i<networks.size();i++)
  34. {
  35. //cerr << "ptr = " << &(networks[i]) << endl;
  36. //cerr << networks[i]->getName() << " == " << n << endl;
  37. if (networks[i]->getName() == n)
  38. return networks[i];
  39. }
  40. //cerr << "net not found in doc\n";
  41. return NULL;
  42. }
  43. vector<ItemInfo *> UIDocument::getNetInputs(const string &netName)
  44. {
  45. UINetwork *net = getNetworkNamed(netName);
  46. vector <ItemInfo *> inputs;
  47. if (net)
  48. {
  49. vector<string> tmp = net->getTerminals(UINetTerminal::INPUT);
  50. for (int i = 0; i < tmp.size(); i++)
  51. {
  52. ItemInfo *newInfo = new ItemInfo;
  53. newInfo->name = tmp[i];
  54. inputs.insert(inputs.end(), newInfo);
  55. }
  56. } else if (preloadInfo.find(netName) != preloadInfo.end()) {
  57. inputs = preloadInfo[netName]->inputs;
  58. } else if (externalDocInfo.find(netName) != externalDocInfo.end()) {
  59. inputs = externalDocInfo[netName]->inputs;
  60. }
  61. return inputs;
  62. }
  63. vector<ItemInfo *> UIDocument::getNetOutputs(const string &netName)
  64. {
  65. UINetwork *net = getNetworkNamed(netName);
  66. vector <ItemInfo *> outputs;
  67. if (net)
  68. {
  69. vector<string> tmp = net->getTerminals(UINetTerminal::OUTPUT);
  70. for (int i = 0; i < tmp.size(); i++)
  71. {
  72. ItemInfo *newInfo = new ItemInfo;
  73. newInfo->name = tmp[i];
  74. outputs.insert(outputs.end(), newInfo);
  75. }
  76. } else if (preloadInfo.find(netName) != preloadInfo.end()) {
  77. outputs = preloadInfo[netName]->outputs;
  78. } else if (externalDocInfo.find(netName) != externalDocInfo.end()) {
  79. outputs = externalDocInfo[netName]->outputs;
  80. }
  81. return outputs;
  82. }
  83. vector<ItemInfo *> UIDocument::getNetParams(const string &netName)
  84. {
  85. //cerr << "UIDocument::getNetParams for document: " << docName << "\n";
  86. UINetwork *net = getNetworkNamed(netName);
  87. //cerr << "got the network\n";
  88. vector <ItemInfo *> params;
  89. if (net)
  90. {
  91. //cerr << "net is already loaded...\n";
  92. /*vector<string> paramNames;
  93. paramNames.resize(preloadInfo[netName]->params.size());
  94. for (int i = 0; i < preloadInfo[netName]->params.size(); i++)
  95. paramNames[i] = preloadInfo[netName]->params[i]->name;*/
  96. net->insertNetParams(params);
  97. } else if (preloadInfo.find(netName) != preloadInfo.end()) {
  98. //cerr << "net is not loaded...\n";
  99. params = preloadInfo[netName]->params;
  100. } else if (externalDocInfo.find(netName) != externalDocInfo.end()) {
  101. //cerr << "net is not loaded...\n";
  102. params = externalDocInfo[netName]->params;
  103. }
  104. return params;
  105. }
  106. string UIDocument::getDescription(const string &type)
  107. {
  108. string descr;
  109. if (externalDocInfo.find(type) != externalDocInfo.end())
  110. descr = externalDocInfo[type]->description;
  111. else
  112. descr = "Description not available.";
  113. return descr;
  114. }
  115. void UIDocument::addParameterText(string name, string value, string type)
  116. {
  117. DocParameterDataText *textInfo = new DocParameterDataText;
  118. textInfo->name = name;
  119. textInfo->value = value;
  120. textInfo->type = type;
  121. textParams.insert(textParams.end(), textInfo);
  122. }
  123. void UIDocument::loadNetInfo(xmlNodePtr net, map<string, SubnetInfo *> &infoMap, string netName)
  124. {
  125. if (netName == "")
  126. netName = string((char *)xmlGetProp(net, (CHAR *)"name"));
  127. CHAR *category = xmlGetProp(net, (CHAR *)"category");
  128. if (infoMap.find(netName) != infoMap.end())
  129. {
  130. cerr << "error: net " << netName << " already existed\n";
  131. return;
  132. }
  133. //cerr << "new subnet info with name: " << netName << "\n";
  134. SubnetInfo *info = new SubnetInfo;
  135. infoMap[netName] = info;
  136. if (category)
  137. info->category = string((char *)category);
  138. //cerr << "scan all nodes\n";
  139. xmlNodePtr node = net->childs;
  140. while (node != NULL)
  141. {
  142. if (string((char*)node->name) == "Node")
  143. {
  144. xmlNodePtr par = node->childs;
  145. //cerr << "par = " << par << endl;
  146. while (par)
  147. {
  148. if (string((char*)par->name) == "Parameter")
  149. {
  150. string paramName = string ((char *) xmlGetProp(par, (CHAR *)"value"));
  151. string type = string ((char *) xmlGetProp(par, (CHAR *)"type"));
  152. if (type == "subnet_param")
  153. {
  154. bool alreadyPresent = false;
  155. for (int j=0;j<info->params.size();j++)
  156. if (info->params[j]->name == paramName)
  157. alreadyPresent=true;
  158. if (!alreadyPresent)
  159. {
  160. ItemInfo *newInfo = new ItemInfo;
  161. newInfo->name = paramName;
  162. newInfo->type = type;
  163. info->params.insert (info->params.end(), newInfo);
  164. }
  165. }
  166. }
  167. par = par->next;
  168. }
  169. //check for params
  170. }
  171. node = node->next;
  172. }
  173. //scan all net inputs/outputs
  174. node = net->childs;
  175. while (node != NULL)
  176. {
  177. if (string((char*)node->name) == "NetInput")
  178. {
  179. string termName = string((char *)xmlGetProp(node, (CHAR *)"name"));
  180. ItemInfo *newInfo = new ItemInfo;
  181. newInfo->name = termName;
  182. info->inputs.insert (info->inputs.end(), newInfo);
  183. } else if (string((char*)node->name) == "NetOutput")
  184. {
  185. string termName = string((char *)xmlGetProp(node, (CHAR *)"name"));
  186. ItemInfo *newInfo = new ItemInfo;
  187. newInfo->name = termName;
  188. info->outputs.insert (info->outputs.end(), newInfo);
  189. }
  190. node = node->next;
  191. }
  192. }
  193. void UIDocument::loadAllSubnetInfo(xmlNodePtr net)
  194. {
  195. //cerr << "UIDocument::loadNetInfo\n";
  196. //cerr << "this = " << this << endl;
  197. while (net != NULL)
  198. {
  199. if (string((char*)net->name) == "Network")
  200. {
  201. //cerr << "scanning a net\n";
  202. loadNetInfo(net, preloadInfo);
  203. }
  204. net = net->next;
  205. }
  206. }
  207. void UIDocument::load()
  208. {
  209. string fullpath=path+docName;
  210. xmlDocPtr doc = xmlParseFile(fullpath.c_str());
  211. if (!doc || !doc->root || !doc->root->name)
  212. {
  213. cerr << "load: error loading " << fullpath << "\n";
  214. xmlFreeDoc (doc);
  215. return;
  216. }
  217. xmlNodePtr root=doc->root;
  218. loadXML(root);
  219. xmlFreeDoc(doc);
  220. }
  221. void UIDocument::loadFromMemory(char *mem, int size)
  222. {
  223. xmlDocPtr doc = xmlParseMemory (mem, size);
  224. xmlNodePtr root=doc->root;
  225. if (!doc || !doc->root || !doc->root->name)
  226. {
  227. cerr << "load: error: cannot parse memory\n";
  228. xmlFreeDoc (doc);
  229. return;
  230. }
  231. loadXML(root);
  232. xmlFreeDoc(doc);
  233. }
  234. void UIDocument::loadXML(xmlNodePtr root)
  235. {
  236. loadAllSubnetInfo(root->childs);
  237. xmlNodePtr net = root->childs;
  238. //cerr << "parsing...\n";
  239. while (net != NULL)
  240. {
  241. //cerr << "start net\n";
  242. if (string((char*)net->name) == "Network")
  243. addNetwork (net);
  244. net = net->next;
  245. }
  246. vector<ItemInfo *> tmp = getNetParams("MAIN");
  247. //cerr << "Got " << tmp.size() << " params in GUIDocument::createParamDialog\n";
  248. //textParams.resize(tmp.size());
  249. for (int i=0;i<tmp.size();i++)
  250. {
  251. DocParameterDataText *newParam = new DocParameterDataText;
  252. newParam->name = string (tmp[i]->name);
  253. textParams.insert(textParams.end(), newParam);
  254. //textParams[i]->name=tmp[i];
  255. }
  256. //cerr << "--\n";
  257. xmlNodePtr par = root->childs;
  258. //cerr << "par = " << par << endl;
  259. while (par)
  260. {
  261. if (string((char*)par->name) == "Parameter")
  262. {
  263. string name = string ((char *) xmlGetProp(par, (CHAR *)"name"));
  264. string type = string ((char *) xmlGetProp(par, (CHAR *)"type"));
  265. string value = string ((char *) xmlGetProp(par, (CHAR *)"value"));
  266. for (int i=0;i<textParams.size();i++)
  267. {
  268. if (textParams[i]->name == name)
  269. {
  270. textParams[i]->type = type;
  271. textParams[i]->value = value;
  272. //insertLoadedParam(param, type, value);
  273. }
  274. }
  275. //cerr << "<param: " << name << ", " << type << ":" << value << ">\n";
  276. }
  277. par = par->next;
  278. }
  279. modified = false;
  280. }
  281. /***************************************************************************************************
  282. * *
  283. * The code starting from this point is a big mess, it would need to be rewritten almost completly *
  284. * *
  285. ***************************************************************************************************/
  286. static vector<string> envList(char *envName)
  287. {
  288. vector<string> list;
  289. list.insert(list.end(), INSTALL_PREFIX "/toolbox");
  290. list.insert(list.end(), INSTALL_PREFIX "/lib");
  291. char *strPath = getenv(envName);
  292. if (!strPath)
  293. return list;
  294. string path = strPath;
  295. int start = 0;
  296. int pos = 0;
  297. while (pos < path.length())
  298. {
  299. if (path[pos] == ':')
  300. {
  301. list.insert(list.end(), string(&(path[start]), &(path[pos])));
  302. start = pos+1;
  303. }
  304. pos++;
  305. }
  306. if (pos)
  307. list.insert(list.end(), string(&(path[start]), &(path[pos])));
  308. //cerr << pathList << endl;
  309. return list;
  310. }
  311. void UIDocument::loadNodeDefInfo(const string &path, const string &name)
  312. {
  313. string fullname = path + "/" + name;
  314. xmlDocPtr doc = xmlParseFile(fullname.c_str());
  315. if (!doc || !doc->root || !doc->root->name)
  316. {
  317. cerr << "loadNodeDefInfo: error loading " << fullname << "\n";
  318. xmlFreeDoc (doc);
  319. return;
  320. }
  321. xmlNodePtr root=doc->root;
  322. xmlNodePtr node = root->childs;
  323. while (node != NULL)
  324. {
  325. string nodeName;
  326. if (string((char*)node->name) == "NodeClass")
  327. {
  328. SubnetInfo *info = new SubnetInfo;
  329. info->category = string((char *)xmlGetProp(node, (CHAR *)"category"));
  330. nodeName = string((char *)xmlGetProp(node, (CHAR *)"name"));
  331. externalDocInfo[nodeName] = info;
  332. xmlNodePtr data = node->childs;
  333. while (data != NULL)
  334. {
  335. string kind = string((char*)data->name);
  336. if (kind == "Input")
  337. {
  338. xmlChar *tmp;
  339. ItemInfo *newInfo = new ItemInfo;
  340. newInfo->name = string((char *)xmlGetProp(data, (CHAR *)"name"));
  341. newInfo->type = string((char *)xmlGetProp(data, (CHAR *)"type"));
  342. tmp = xmlGetProp(data, (CHAR *)"value");
  343. if (tmp == NULL)
  344. newInfo->value = "";
  345. else
  346. newInfo->value = string((char *)tmp);
  347. tmp = xmlNodeListGetString(doc, data->childs, 1);
  348. if (tmp == NULL)
  349. newInfo->description = "No Description Available.";
  350. else
  351. newInfo->description = string((char *)tmp);
  352. info->inputs.insert(info->inputs.end(), newInfo);
  353. } else if (kind == "Output")
  354. {
  355. xmlChar *tmp;
  356. ItemInfo *newInfo = new ItemInfo;
  357. newInfo->name = string((char *)xmlGetProp(data, (CHAR *)"name"));
  358. newInfo->type = string((char *)xmlGetProp(data, (CHAR *)"type"));
  359. tmp = xmlGetProp(data, (CHAR *)"value");
  360. if (tmp == NULL)
  361. newInfo->value = "";
  362. else
  363. newInfo->value = string((char *)tmp);
  364. tmp = xmlNodeListGetString(doc, data->childs, 1);
  365. if (tmp == NULL)
  366. newInfo->description = "No Description Available.";
  367. else
  368. newInfo->description = string((char *)tmp);
  369. info->outputs.insert(info->outputs.end(), newInfo);
  370. } else if (kind == "Parameter")
  371. {
  372. xmlChar *tmp;
  373. ItemInfo *newInfo = new ItemInfo;
  374. newInfo->name = string((char *)xmlGetProp(data, (CHAR *)"name"));
  375. newInfo->type = string((char *)xmlGetProp(data, (CHAR *)"type"));
  376. tmp = xmlGetProp(data, (CHAR *)"value");
  377. if (tmp == NULL)
  378. newInfo->value = "";
  379. else
  380. newInfo->value = string((char *)tmp);
  381. tmp = xmlNodeListGetString(doc, data->childs, 1);
  382. if (tmp == NULL)
  383. newInfo->description = "No Description Available.";
  384. else
  385. newInfo->description = string((char *)tmp);
  386. info->params.insert(info->params.end(), newInfo);
  387. } else if (kind == "Description")
  388. {
  389. xmlChar *tmp;
  390. tmp = xmlNodeListGetString(doc, data->childs, 1);
  391. if (tmp == NULL)
  392. info->description = "No description available";
  393. else
  394. info->description = string((char *)tmp);
  395. } else
  396. {
  397. cerr << "other\n";
  398. }
  399. data = data->next;
  400. }
  401. }
  402. node = node->next;
  403. }
  404. xmlFreeDoc(doc);
  405. }
  406. void UIDocument::loadExtDocInfo(const string &path, const string &name)
  407. {
  408. string fullname = path + "/" + name;
  409. xmlDocPtr doc = xmlParseFile(fullname.c_str());
  410. if (!doc || !doc->root || !doc->root->name)
  411. {
  412. cerr << "ExtDoc: error loading " << fullname << "\n";
  413. xmlFreeDoc (doc);
  414. return;
  415. }
  416. xmlNodePtr root=doc->root;
  417. xmlNodePtr net = root->childs;
  418. while (net != NULL)
  419. {
  420. //cerr << "scanning networks...\n";
  421. if (string((char*)net->name) == "Network")
  422. {
  423. //cerr << "scanning a net\n";
  424. string netName = string((char *)xmlGetProp(net, (CHAR *)"name"));
  425. if (netName == "MAIN")
  426. {
  427. string basename = string(name.begin(), name.end()-2);
  428. loadNetInfo(net, externalDocInfo, basename);
  429. }
  430. }
  431. net = net->next;
  432. }
  433. xmlFreeDoc(doc);
  434. }
  435. #include <sys/types.h>
  436. #include <dirent.h>
  437. //
  438. //Fixed to load recursively all toolboxes starting with the directories included
  439. //In the VFLOW_PATH environment variable.
  440. //Dominic Letourneau Feb. 20 2001
  441. //
  442. void UIDocument::loadAllInfo()
  443. {
  444. vector<string> dirs = envList("VFLOW_PATH");
  445. for (int i=0;i<dirs.size();i++) {
  446. loadAllInfoRecursive(dirs[i]);
  447. }
  448. }
  449. void UIDocument::loadAllInfoRecursive(const string &path) {
  450. struct stat my_stat;
  451. DIR *my_directory = opendir (path.c_str());
  452. if (!my_directory) return;
  453. struct dirent *current_entry;
  454. for (current_entry = readdir(my_directory);
  455. current_entry != NULL; current_entry = readdir(my_directory)) {
  456. string name = current_entry->d_name;
  457. string fullpath = path + string("/") + name;
  458. if (stat(fullpath.c_str(), &my_stat) < 0) {
  459. cerr<<"stat error"<<endl;
  460. continue;
  461. }
  462. if (S_ISDIR(my_stat.st_mode)) {
  463. //it is a directory, let's doing it recursively
  464. if (name != string("..") && name != string(".")) {
  465. loadAllInfoRecursive(fullpath);
  466. }
  467. }
  468. else {
  469. //loading network
  470. if (strstr(current_entry->d_name, ".n") && !strstr(current_entry->d_name, ".nn")){
  471. //cout<<"Loading network : "<<fullpath<<endl;
  472. loadExtDocInfo(path, name);
  473. }
  474. //loading toolbox
  475. if (strstr(current_entry->d_name, ".def")) {
  476. //cout<<"Loading toolbox : "<<fullpath<<endl;
  477. loadNodeDefInfo(path, name);
  478. }
  479. }
  480. }
  481. closedir(my_directory);
  482. }
  483. void UIDocument::scanDL()
  484. {
  485. vector<string> dirs=envList("VFLOW_PATH");
  486. if (dirs.size() == 0)
  487. {
  488. cerr << "Cannot find any toolbox. Exiting\n";
  489. exit(1);
  490. }
  491. for (int i = 0; i<dirs.size();i++)
  492. {
  493. DIR *my_directory = opendir (dirs[i].c_str());
  494. if (!my_directory)
  495. continue;
  496. struct dirent *current_entry;
  497. for (current_entry = readdir(my_directory);
  498. current_entry != NULL; current_entry = readdir(my_directory))
  499. {
  500. if (!strstr(current_entry->d_name, ".tlb"))
  501. {
  502. //cerr << current_entry->d_name << " is not a shared library\n";
  503. continue;
  504. }
  505. string fullname = dirs[i] + "/" + current_entry->d_name;
  506. if (!dlopen(fullname.c_str(), RTLD_LAZY))
  507. cerr << "Toolbox load error: " << dlerror() << endl;
  508. }
  509. closedir(my_directory);
  510. }
  511. }
  512. UINetwork *UIDocument::newNetwork(UIDocument *_doc, const string &_name, UINetwork::Type type)
  513. {
  514. //cerr << "UIDocument::newNetwork\n";
  515. return new UINetwork(_doc, _name, type);
  516. }
  517. UINetwork *UIDocument::newNetwork(UIDocument *_doc, xmlNodePtr _net)
  518. {
  519. //cerr << "UIDocument::newNetwork\n";
  520. return new UINetwork(_doc, _net);
  521. }
  522. UINetwork *UIDocument::addNetwork(string name, UINetwork::Type type)
  523. {
  524. //cerr << "UIDocument::addNetwork (type = " << typeid(this).name() << ")" << endl;
  525. //UINetwork *newNet = new GUINetwork(this, name, iter);
  526. UINetwork *newNet = newNetwork(this, name, type);
  527. for (int i=0;i<networks.size();i++)
  528. {
  529. networks[i]->newNetNotify("Subnet",name);
  530. newNet->newNetNotify("Subnet",networks[i]->getName());
  531. }
  532. networks.insert(networks.end(), newNet);
  533. modified = true;
  534. return newNet;
  535. }
  536. UINetwork *UIDocument::addNetwork(xmlNodePtr xmlNet)
  537. {
  538. //cerr << "creating...\n";
  539. UINetwork *newNet = newNetwork(this, xmlNet);
  540. //cerr << "created\n";
  541. //cerr << "newNet = " << newNet << endl;
  542. //cerr << "network created in UIDocument::addNetwork\n";
  543. for (int i=0;i<networks.size();i++)
  544. {
  545. networks[i]->newNetNotify("Subnet",newNet->getName());
  546. newNet->newNetNotify("Subnet",networks[i]->getName());
  547. }
  548. //cerr << "newNet = " << newNet << endl;
  549. networks.insert(networks.end(), newNet);
  550. modified = true;
  551. return newNet;
  552. }
  553. void UIDocument::removeNetwork(UINetwork *toRemove)
  554. {
  555. //ANSI C++ fix
  556. vector<UINetwork *>::iterator i=networks.begin();
  557. while (i != networks.end())
  558. {
  559. if (*i == toRemove)
  560. {
  561. networks.erase(i);
  562. break;
  563. }
  564. ++i;
  565. }
  566. /*for (int i = 0; i < networks.size(); i++)
  567. if (networks[i] == toRemove)
  568. networks.erase(&networks[i]);
  569. */
  570. setModified();
  571. }
  572. void UIDocument::save()
  573. {
  574. xmlDocPtr doc;
  575. doc = xmlNewDoc((CHAR *)"1.0");
  576. doc->root = xmlNewDocNode(doc, NULL, (CHAR *)"Document", NULL);
  577. for (int i=0;i<networks.size();i++)
  578. {
  579. networks[i]->saveXML(doc->root);
  580. }
  581. for (int i=0;i<textParams.size();i++)
  582. {
  583. xmlNodePtr tree;
  584. tree = xmlNewChild(doc->root, NULL, (CHAR *)"Parameter", NULL);
  585. xmlSetProp(tree, (CHAR *)"name", (CHAR *)textParams[i]->name.c_str());
  586. xmlSetProp(tree, (CHAR *)"type", (CHAR *)textParams[i]->type.c_str());
  587. xmlSetProp(tree, (CHAR *)"value", (CHAR *)textParams[i]->value.c_str());
  588. }
  589. string fullname = path+docName;
  590. xmlSaveFile(fullname.c_str(), doc);
  591. xmlFreeDoc(doc);
  592. }
  593. void UIDocument::export2net()
  594. {
  595. string netName = path+docName+"et";
  596. ofstream out(netName.c_str());;
  597. for (int i=0;i<networks.size();i++)
  598. {
  599. networks[i]->export2net(out);
  600. }
  601. }
  602. #include <sys/stat.h>
  603. #include <unistd.h>
  604. Network *UIDocument::buildExternal(const string &type, const string &_name, const ParameterSet &params)
  605. {
  606. vector<string> pathlist = envList("VFLOW_PATH");
  607. Network *net = NULL;
  608. for (int i=0;i<pathlist.size();i++) {
  609. net = buildExternalRecursive(pathlist[i],type,_name, params);
  610. if (net) break;
  611. }
  612. return net;
  613. }
  614. Network *UIDocument::buildExternalRecursive(const string &path, const string &type,
  615. const string &_name, const ParameterSet &params) {
  616. Network *net = NULL;
  617. struct stat my_stat;
  618. DIR *my_directory = opendir (path.c_str());
  619. if (!my_directory) return NULL;
  620. struct dirent *current_entry;
  621. for (current_entry = readdir(my_directory);
  622. current_entry != NULL; current_entry = readdir(my_directory)) {
  623. string name = current_entry->d_name;
  624. string fullpath = path + string("/") + name;
  625. if (stat(fullpath.c_str(), &my_stat) < 0) {
  626. cerr<<"stat error"<<endl;
  627. continue;
  628. }
  629. if (S_ISDIR(my_stat.st_mode)) {
  630. //it is a directory, let's doing it recursively
  631. if (name != string("..") && name != string(".")) {
  632. Network *ret = buildExternalRecursive(fullpath,type, _name, params);
  633. if (ret) {
  634. closedir(my_directory);
  635. return ret;
  636. }
  637. }
  638. }
  639. else {
  640. //cerr << "is it " << name << " in " << path << endl;
  641. if (name == (type + string(".n"))) {
  642. //found the network
  643. UIDocument doc(fullpath);
  644. //cout<<"loading : "<<fullpath<<endl;
  645. doc.load();
  646. net = doc.getNetworkNamed("MAIN")->build(_name, params);
  647. closedir(my_directory);
  648. //cerr << "found net = " << net << endl;
  649. return net;
  650. }
  651. }
  652. }
  653. closedir(my_directory);
  654. //cerr << "returning " << net << " for " << type << endl;
  655. return net;
  656. }
  657. Network *UIDocument::build(const string &_name, const ParameterSet &params)
  658. {
  659. //cerr << "UIDocument::build\n";
  660. return getNetworkNamed("MAIN")->build(_name, params);
  661. }
  662. //Run without a GUI
  663. void UIDocument::run()
  664. {
  665. try {
  666. ParameterSet params;
  667. //cerr << "building net...\n";
  668. Network *net = build("MAIN", params);
  669. if (net->getInputNode())
  670. throw new GeneralException ("main network has input node", __FILE__, __LINE__);
  671. //cerr << "initializing...\n";
  672. net->initialize();
  673. //cerr << "running (UIDocument)...\n";
  674. for (int i = 0; ;i++) {
  675. if (!net->hasOutput(i)) break;
  676. cout << *net->getOutput(i,0);
  677. }
  678. }
  679. catch (BaseException &e) {
  680. e.print();
  681. }
  682. catch (BaseException *e) {
  683. e->print();
  684. }
  685. }
  686. void UIDocument::run(ParameterSet &p)
  687. {
  688. try {
  689. //cerr << "building net...\n";
  690. Network *net = build("MAIN", p);
  691. if (net->getInputNode())
  692. throw new GeneralException ("main network has input node", __FILE__, __LINE__);
  693. //cerr << "initializing...\n";
  694. net->initialize();
  695. //cerr << "running (UIDocument)...\n";
  696. for (int i = 0; ;i++)
  697. {
  698. if (!net->hasOutput(i))
  699. break;
  700. *net->getOutput(i,0);
  701. }
  702. }
  703. catch (BaseException &e) {
  704. e.print();
  705. }
  706. catch (BaseException *e) {
  707. e->print();
  708. }
  709. }
  710. void UIDocument::setFullPath(const string &fullpath)
  711. {
  712. //cerr << "fullpath is: \"" << fullpath << "\"" << endl;
  713. int slashpos = fullpath.rfind("/");
  714. //cerr << "slashpos = " << slashpos << endl;
  715. path="";
  716. path.append(fullpath,0,slashpos+1);
  717. docName=fullpath;
  718. docName.erase(0,slashpos+1);
  719. //cerr << "path is: \"" << path << "\"" << endl;
  720. //cerr << "name is: \"" << name << "\"" << endl;
  721. untitled=false;
  722. }
  723. /*
  724. * Hack to get the available nodes.
  725. * Returns a vector of strings, with each string having the following format:
  726. *
  727. * category***type
  728. * -> category is the category the node falls under (with subcategories
  729. * separated with :'s
  730. * -> type is the unique description of the node type
  731. */
  732. vector<string> UIDocument::getAvailableNodes()
  733. {
  734. vector<string> allNodes;
  735. string nextItem;
  736. // first look at the externalDocInfo
  737. map<string, SubnetInfo *>::iterator iter = externalDocInfo.begin();
  738. while (iter != externalDocInfo.end()) {
  739. nextItem = string((*iter).second->category) + "***" +
  740. string((*iter).first);
  741. allNodes.insert(allNodes.end(), nextItem);
  742. iter++;
  743. }
  744. // now look at the preloadInfo
  745. iter = preloadInfo.begin();
  746. while (iter != preloadInfo.end()) {
  747. nextItem = string((*iter).second->category) + "***" +
  748. string((*iter).first);
  749. allNodes.insert(allNodes.end(), nextItem);
  750. iter++;
  751. }
  752. return allNodes;
  753. }