FSMConstructor_for_Dot.cpp
Go to the documentation of this file.
00001 /*
00002  * FSMConstructor.cpp
00003  *
00004  *  Created on: Nov 29, 2013
00005  *      Author: dan
00006  */
00007 
00008 #include "FSMConstructor.h"
00009 #include "ParserExceptions.h"
00010 
00011 #include <fstream>
00012 
00013 #define XML( X ) out<<o.tab<<#X<<endl
00014 #define CML( X ) out<<o.tab<<X<<endl
00015 #define S( X ) <<#X
00016 
00017 
00018 namespace fsm_constructor{
00019 using namespace std;
00020 
00021 std::ostream& saveDot(std::ostream& out, const Raise& o){
00022         return out;
00023 }
00024 
00025 std::ostream& saveDot(std::ostream& out, const EventAction& o){
00026         return out;
00027 }
00028 
00029 
00030 
00031 std::ostream& saveDot(std::ostream& out, const OnEvent& o, const States& state, const Fsm& fsm){
00032 
00033         string target="";
00034         int next_counter=0;
00035         for(std::vector<EventAction>::const_iterator i=o.actions.begin();i!=o.actions.end();i++){
00036                 if(i->type=="next"){
00037                         target=o.id + "/" + i->text;
00038                         next_counter++;
00039                 }
00040         }
00041         if(next_counter>0){
00042                 string src="";
00043                 string ltail="";
00044                 string dst="";
00045                 string lhead="";
00046                 src = Container::map_id_to_number()[searchSimpleState(state)];
00047                 if(state.calls.empty()){
00048                         ltail= src;
00049                 }else{
00050                         ltail="cluster_"+Container::map_id_to_number()[state.getId()];
00051                 }
00052                 for(std::vector<States>::const_iterator i=fsm.states.begin();i!=fsm.states.end();i++){
00053                         if(i->getId()!=target) continue;
00054                         dst = Container::map_id_to_number()[searchSimpleState(*i)];
00055                         if(i->calls.empty()){
00056                                 lhead= dst;
00057                         }else{
00058                                 lhead="cluster_"+Container::map_id_to_number()[i->getId()];
00059                         }
00060                         break;
00061                 }
00062                 if(dst.size()>0){
00063                         Container::postData()<<o.tab
00064                                         << src
00065                                         <<" -> "
00066                                         << dst
00067                                         << "[ label=\"" << o.text <<"\" ltail=\""<< ltail <<"\" lhead=\""<< lhead << "\", fontsize=8 ]" <<endl;
00068                         ;
00069 //                      cout<<"// Edge " <<o.tab
00070 //                                      << src
00071 //                                      <<" -> "
00072 //                                      << dst
00073 //                                      << "[ label=\"" << o.text <<"\" ltail=\""<< ltail <<"\" lhead=\""<< lhead << "\" ]" <<endl;
00074 
00075                 }else{
00076                         out<<o.tab<<"// target for edge : "<<target<< " not found"<<endl;
00077                 }
00078         }
00079 
00080         return out;
00081 }
00082 
00083 std::ostream& saveDot(std::ostream& out, const States& o, const Fsm& fsm){
00084         if(o.calls.empty()){
00085 
00086                 out<<o.tab<<Container::map_id_to_number()[o.getId()] << "[ label=\"" << o.name <<"\" ]" <<" //state: "<<o.getId()<<endl;
00087                 if(not o.events.empty()){
00088                         for(std::vector<OnEvent>::const_iterator i=o.events.begin();i!=o.events.end();i++){
00089                                 i->lib=o.lib;
00090                                 i->tab=o.tab;
00091                                 i->id = o.id;
00092                                 saveDot(out, *i, o, fsm)<<endl;
00093                         }
00094                 }
00095         }else{
00096                 out<<o.tab<<"subgraph cluster_"<<Container::map_id_to_number()[o.getId()]<<"{ //state: "<<o.getId()<<endl;
00097                 out<<o.tab<<"   label=\""<<o.name<<"\""<<endl;
00098                 if(not o.calls.empty()){
00099                         string tab = o.tab+"   ";
00100                         for(std::vector<Call>::const_iterator i=o.calls.begin();i!=o.calls.end();i++){
00101                                 i->lib=o.lib;
00102                                 i->tab=tab;
00103                                 i->id = o.getId();
00104                                 saveDot(out, *i)<<endl;
00105                         }
00106                 }
00107 
00108                 if(not o.events.empty()){
00109                         for(std::vector<OnEvent>::const_iterator i=o.events.begin();i!=o.events.end();i++){
00110                                 i->lib=o.lib;
00111                                 i->tab=o.tab+"   ";
00112                                 i->id = o.id;
00113                                 saveDot(out, *i, o, fsm)<<endl;
00114                         }
00115                 }
00116 
00117                 out<<o.tab<<"}";
00118         }
00119         return out;
00120 }
00121 
00122 std::ostream& saveDot(std::ostream& out, const Call& o){
00123         string resolved = "";
00124         if(o.type == "fsm"){
00125                 bool isResolved = o.lib->contains_fsm(o.text);
00126                 if(isResolved){
00127                         o.lib->saveDot_fsm(out, o.tab, o.text, o.id);
00128                 }
00129                 return out;
00130         }
00131         if(o.type == "bt"){
00132                 bool isResolved = o.lib->contains_tree(o.text);
00133                 if(isResolved){
00134                         out<<o.tab<<"subgraph cluster_"<<Container::map_id_to_number()[o.getId()]<<"{ //call bt : "<<o.getId()<<endl;
00135                         out<<o.tab<<"   label=\"BT["<<o.text<<"]\""<<endl;
00136                         o.lib->saveDot_tree(out, o.tab+"    ", o.text, o.id);
00137                         out<<o.tab<<"}";
00138                 }
00139                 return out;
00140         }
00141         if(o.type == "task"){
00142                 string tab=o.tab;
00143                 out<<o.tab<<Container::map_id_to_number()[o.getId()]<<" [label=\""<<o.text<<"\"]  //call: "<<o.getId();
00144         }
00145         return out;
00146 }
00147 
00148 std::ostream& saveDot(std::ostream& out, const Fsm& o){
00149         string start_index = "str"+Container::map_id_to_number()[o.getId()];
00150         out<<o.tab<<"subgraph cluster_"<<Container::map_id_to_number()[o.getId()]<<"{ //fsm: "<<o.getId()<<endl;
00151         out<<o.tab<<"   label=\""<<o.name<<"\""<<endl;
00152         out<<o.tab<<"   "<<start_index<<" [shape=point]"<<endl;
00153         for(std::vector<States>::const_iterator i=o.states.begin();i!=o.states.end();i++){
00154                 i->lib=o.lib;
00155                 i->tab=o.tab+"   ";
00156                 i->id=o.getId();
00157                 saveDot(out, *i, o)<<endl;
00158         }
00159         {
00160                 string src=start_index;
00161                 string ltail=src;
00162                 string dst="";
00163                 string lhead="";
00164                 for(std::vector<States>::const_iterator i=o.states.begin();i!=o.states.end();i++){
00165                         if(i->getId()!=o.getId()+"/"+o.start) continue;
00166                         dst = Container::map_id_to_number()[searchSimpleState(*i)];
00167                         if(i->calls.empty()){
00168                                 lhead= dst;
00169                         }else{
00170                                 lhead="cluster_"+Container::map_id_to_number()[i->getId()];
00171                         }
00172                         break;
00173                 }
00174                 if(dst.size()>0){
00175                         Container::postData()<<o.tab
00176                                         << src
00177                                         <<" -> "
00178                                         << dst
00179                                         << "[ lhead=\""<< lhead << "\", fontsize=8 ] //start" <<endl;
00180                         ;
00181                 }
00182         }
00183         out<<o.tab<<"}";
00184         return out;
00185 }
00186 
00187 
00188 void printIds(){
00189 //      std::cout<<"\nPrint all ids map (FSM): {"<<endl;
00190 //      for(std::map<std::string, std::string>::const_iterator i=Container::map_id_to_number().begin();i!=Container::map_id_to_number().end();i++){
00191 //              std::cout<<"    "<< i->first <<" :=  "<< i->second <<std::endl;
00192 //      }
00193 //      std::cout<<"}"<<std::endl;
00194 }
00195 
00196 
00197 std::ostream& saveDot(std::ostream& out, const FSMConstructor& o){
00198         for(std::map<std::string, Fsm>::const_iterator i=o.fsms.begin();i!=o.fsms.end();i++){
00199                 i->second.lib = &o;
00200                 i->second.tab=o.tab+"   ";
00201                 map_ids(i->second); printIds();
00202                 Container::clear_postData();
00203                 out<<o.tab<<"digraph "<<i->second.name<<" { //fsm"<<endl;
00204                 out<<o.tab<<"   compound=\"true\""<<endl;
00205                 saveDot(out, i->second)<<endl;
00206                 out<<o.tab<<Container::postData().str()<<endl;
00207                 out<<o.tab<<"}";
00208         }
00209         return out;
00210 }
00211 
00212 void saveDot(std::string path_prefix, const FSMConstructor& o){
00213         for(std::map<std::string, Fsm>::const_iterator i=o.fsms.begin();i!=o.fsms.end();i++){
00214                 std::stringstream filename; filename << path_prefix << i->second.name <<DOT_FILE_EXT;
00215                 std::ofstream out(filename.str().c_str());
00216                 if(out.is_open()){
00217                         i->second.lib = &o;
00218                         i->second.tab=o.tab+"   ";
00219                         map_ids(i->second); printIds();
00220                         Container::clear_postData();
00221                         out<<"//from file: "<<o.filename<<" "<<endl;
00222                         out<<o.tab<<"digraph "<<i->second.name<<" { //fsm"<<endl;
00223                         out<<o.tab<<"   compound=\"true\""<<endl;
00224                         saveDot(out, i->second)<<endl;
00225                         out<<o.tab<<Container::postData().str()<<endl;
00226                         out<<o.tab<<"}"<<endl;
00227                         out.close();
00228                 }else{
00229                         throw PEFileNotCreated(filename.str());
00230                 }
00231         }
00232 }
00233 
00234 //----------------------------------
00235 
00236 void map_ids(const Call& o){
00237         Container::map_id_to_number()[o.getId()] = Container::get_id_counter();
00238         if(o.type == "fsm"){
00239                 bool isResolved = o.lib->contains_fsm(o.text);
00240                 if(isResolved) o.lib->map_ids_fsm(o.text, o.id);
00241         }
00242         if(o.type == "bt"){
00243                 bool isResolved = o.lib->contains_tree(o.text);
00244                 if(isResolved) o.lib->map_ids_tree(o.text, o.id);
00245         }
00246 }
00247 
00248 
00249 void map_ids(const States& o){
00250         Container::map_id_to_number()[o.getId()] = Container::get_id_counter();
00251         if(not o.calls.empty()){
00252                 for(std::vector<Call>::const_iterator i=o.calls.begin();i!=o.calls.end();i++){
00253                         i->lib=o.lib;
00254                         i->id = o.getId();
00255                         map_ids(*i);
00256                 }
00257         }
00258 }
00259 
00260 void map_ids(const Fsm& o){
00261         Container::map_id_to_number()[o.getId()] = Container::get_id_counter();
00262         for(std::vector<States>::const_iterator i=o.states.begin();i!=o.states.end();i++){
00263                 i->lib=o.lib;
00264                 i->id = o.getId();
00265                 map_ids(*i);
00266         }
00267 }
00268 
00269 void map_ids(const FSMConstructor& o){
00270         Container::reset_id_counter();
00271         Container::map_id_to_number().clear();
00272         for(std::map<std::string, Fsm>::const_iterator i=o.fsms.begin();i!=o.fsms.end();i++){
00273                 i->second.lib = &o;
00274                 map_ids(i->second);
00275         }
00276 }
00277 
00278 
00279 std::string searchSimpleState(const States& o){
00280         if(not o.calls.empty()){
00281                 for(std::vector<Call>::const_iterator i=o.calls.begin();i!=o.calls.end();i++){
00282                         if(i->type=="task"){
00283                                 return i->getId();
00284                         }
00285                         if(i->type=="bt"){
00286                                 return i->getId()+"/"+i->text;
00287                         }
00288                         if(i->type=="fsm"){
00289                                 bool isResolved = o.lib->contains_fsm(i->text);
00290                                 if(isResolved){
00291                                         const Fsm& fsm = o.lib->fsms.at(i->text);
00292                                         return searchSimpleState(fsm.states.front());
00293                                 }
00294                         }
00295                 }
00296         }else{
00297                 return o.getId();
00298         }
00299         return "NOT-FOUND";
00300 }
00301 std::string searchSimpleState(Container const* cnt_fsm, std::string name){
00302         FSMConstructor const* con = (FSMConstructor const*) cnt_fsm;
00303         const Fsm& fsm = con->fsms.at(name);
00304         return searchSimpleState(fsm.states.front());
00305 }
00306 
00307 
00308 }
00309 
00310 


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