BTConstructor_for_Dot.cpp
Go to the documentation of this file.
00001 /*
00002  * BTConstructor.cpp
00003  *
00004  *  Created on: Nov 29, 2013
00005  *      Author: dan
00006  */
00007 
00008 #include "BTConstructor.h"
00009 #include <fstream>
00010 #include "ParserExceptions.h"
00011 
00012 namespace bt_constructor{
00013 using namespace std;
00014 
00015 
00016 #define FOREACH_VEC(TYPE, VEC) for(std::vector<TYPE>::const_iterator i=o.VEC.begin();i!=o.VEC.end();i++)
00017 #define FOREACH_MAP(TYPE, VEC) for(std::map<std::string, TYPE>::const_iterator i=o.VEC.begin();i!=o.VEC.end();i++)
00018 
00019 
00020 bool saveDot_call_node(std::ostream& out, const Node& o){
00021         string resolved = "";
00022         if(o.type == "bt"){
00023                 bool isResolved = o.lib->contains_tree(o.name);
00024                 if(isResolved){
00025                         o.lib->saveDot_tree(out, o.tab+"    ", o.name, o.getId()); out<<endl;
00026                         Container::postData()<<o.tab
00027                                         <<Container::map_id_to_number()[o.getId()]
00028                                         <<" -> "
00029                                         <<Container::map_id_to_number()[o.getId()+"/"+o.name]
00030                                         <<" // bt_call from "<<o.getId()<<" to "<<(o.getId()+"/"+o.name)<<endl;
00031                 }
00032                 return true;
00033         }
00034         if(o.type == "fsm"){
00035                 bool isResolved = o.lib->contains_fsm(o.name);
00036                 if(isResolved){
00037                         string tab = o.tab+"   ";
00038                         o.lib->saveDot_fsm(out, tab+"    ", o.name, o.getId()); out<<endl;
00039                 }
00040                 return true;
00041         }
00042         if(o.type == "rtask"){
00043                 out<<o.tab<<Container::map_id_to_number()[o.getId()]<<" [label=\""<<o.name<<"\"]";
00044                 return true;
00045         }
00046         return false;
00047 }
00048 
00049 std::ostream& saveDot(std::ostream& out, const Node& o){
00050         if(o.type!="fsm"){
00051                 if(o.type == "bt")
00052                         out<<o.tab<<Container::map_id_to_number()[o.getId()]<<" [label=\"BT["<<o.name<<"]\"]"<<" //node:"<<o.type<<":"<<o.getId()<<endl;
00053                 else
00054                         out<<o.tab<<Container::map_id_to_number()[o.getId()]<<" [label=\""<<o.name<<"\"]"<<" //node:"<<o.type<<":"<<o.getId()<<endl;
00055         }
00056         if(saveDot_call_node(out, o)) return out;
00057         if(o.type == "par")      ;
00058         if(o.type == "seq")      ;
00059         if(o.type == "sel")      ;
00060         if(o.type == "dec")      ;
00061         if(o.type == "task") ;
00062 
00063         FOREACH_VEC(Node, nodes){
00064                 out<<endl;
00065                 i->lib = o.lib;
00066                 i->tab = o.tab+"      ";
00067                 i->id = o.getId();
00068                 saveDot(out, *i);
00069                 out<<endl;
00070                 if(i->type!="fsm"){
00071                         Container::postData()<<o.tab<<Container::map_id_to_number()[o.getId()]<<" -> "<<Container::map_id_to_number()[i->getId()]<<" // from "<<o.getId()<<" to "<<i->getId()<<endl;
00072                 }else{
00073                         bool isResolved = o.lib->contains_fsm(i->name);
00074                         string dst = o.lib->search_simple_node(i->name);
00075                         Container::postData()<<o.tab<<Container::map_id_to_number()[o.getId()]<<" -> "<<Container::map_id_to_number()[dst]
00076                                   <<"[ ltail=\""<<Container::map_id_to_number()[o.getId()]<<"\", lhead=\"cluster_"<< Container::map_id_to_number()[i->getId()+"/"+i->name]<<"\" ]"
00077                                   <<" // fsm_call from "<<o.getId()<<" to "<<i->getId()<<endl;
00078                 }
00079         }
00080         out<<endl;
00081         if(o.type == "par") ;
00082         if(o.type == "seq")      ;
00083         if(o.type == "sel")      ;
00084         if(o.type == "dec")      ;
00085         if(o.type == "task") ;
00086         return out;
00087 }
00088 
00089 
00090 std::ostream& saveDot(std::ostream& out, const Tree& o){
00091         out<<o.tab<<Container::map_id_to_number()[o.getId()]<<" [label=\""<<o.name<<"\"]"<<" //root "<<o.getId()<<endl;
00092         FOREACH_VEC(Node, nodes){
00093                 out<<endl;
00094                 i->lib = o.lib;
00095                 i->tab = o.tab+"      ";
00096                 i->id = o.getId();
00097                 saveDot(out, *i);
00098                 out<<endl;
00099                 //cout<<"// Edge " <<o.tab<<Container::map_id_to_number()[o.getId()]<<" -> "<<Container::map_id_to_number()[i->getId()]<<" // from "<<o.getId()<<" to "<<i->getId()<<endl;
00100                 Container::postData()<<o.tab<<Container::map_id_to_number()[o.getId()]<<" -> "<<Container::map_id_to_number()[i->getId()]<<" // from "<<o.getId()<<" to "<<i->getId()<<endl;
00101         }
00102         out<<endl;
00103         return out;
00104 }
00105 
00106 void printIds(){
00107 //      std::cout<<"\nPrint all ids map (BT): {"<<endl;
00108 //      for(std::map<std::string, std::string>::const_iterator i=Container::map_id_to_number().begin();i!=Container::map_id_to_number().end();i++){
00109 //              std::cout<<"    "<< i->first <<" :=  "<< i->second <<std::endl;
00110 //      }
00111 //      std::cout<<"}"<<std::endl;
00112 }
00113 
00114 std::ostream& saveDot(std::ostream& out, const BTConstructor& o){
00115 
00116         FOREACH_MAP(Tree, trees){
00117                 i->second.lib = &o;
00118                 i->second.tab = "      ";
00119                 map_ids(i->second); printIds();
00120                 Container::clear_postData();
00121                 out<<o.tab<<"digraph "<<i->second.name<<" { //bt"<<endl;
00122                 out<<o.tab<<"      compound=\"true\""<<endl;
00123                 saveDot(out, i->second)<<endl;
00124                 out<<o.tab<<"     "<<Container::postData().str()<<endl;
00125                 out<<o.tab<<"}";
00126         }
00127         return out;
00128 }
00129 
00130 
00131 void saveDot(std::string path_prefix, const BTConstructor& o){
00132 
00133         FOREACH_MAP(Tree, trees){
00134                 std::stringstream filename; filename << path_prefix << i->second.name <<DOT_FILE_EXT;
00135                 std::ofstream out(filename.str().c_str());
00136                 if(out.is_open()){
00137                         i->second.lib = &o;
00138                         i->second.tab = "      ";
00139                         map_ids(i->second); printIds();
00140                         Container::clear_postData();
00141                         out<<o.tab<<"digraph "<<i->second.name<<" { //bt"<<endl;
00142                         out<<o.tab<<"      compound=\"true\""<<endl;
00143                         out<<o.tab<<"//from file: "<<o.filename<<" -->"; out<<endl;
00144                         saveDot(out, i->second)<<endl;
00145                         out<<o.tab<<"     "<<Container::postData().str()<<endl;
00146                         out<<o.tab<<"}";
00147                         out.close();
00148                 }else{
00149                         throw PEFileNotCreated(filename.str());
00150                 }
00151         }
00152 }
00153 
00154 void map_ids(const Node& o){
00155         Container::map_id_to_number()[o.getId()] = Container::get_id_counter();
00156         FOREACH_VEC(Node, nodes){
00157                 i->lib = o.lib;
00158                 i->id = o.getId();
00159                 if(i->nodes.empty()){
00160                         if(i->type == "bt"){
00161                                 bool isResolved = o.lib->contains_tree(i->name);
00162                                 if(isResolved){
00163                                         o.lib->map_ids_tree(i->name, i->getId());
00164                                 }
00165                         }else
00166                         if(i->type == "fsm"){
00167                                 bool isResolved = o.lib->contains_fsm(i->name);
00168                                 if(isResolved){
00169                                         o.lib->map_ids_fsm(i->name, i->getId());
00170                                 }
00171                         }//else
00172                         //map_ids(*i);
00173                 }//else
00174                 {
00175                         map_ids(*i);
00176                 }
00177         }
00178 }
00179 void map_ids(const Tree& o){
00180         Container::map_id_to_number()[o.getId()] = Container::get_id_counter();
00181         FOREACH_VEC(Node, nodes){
00182                 i->lib = o.lib;
00183                 i->id = o.getId();
00184                 map_ids(*i);
00185         }
00186 }
00187 void map_ids(const BTConstructor& o){
00188         Container::reset_id_counter();
00189         Container::map_id_to_number().clear();
00190         FOREACH_MAP(Tree, trees){
00191                 i->second.lib = &o;
00192                 map_ids(i->second);
00193         }
00194 }
00195 
00196 }
00197 
00198 


decision_making_parser
Author(s):
autogenerated on Wed Aug 26 2015 11:16:57