FSMConstructor.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 void xml_version(std::ostream& out, std::string tab){
00019         out<<tab<<"<?xml version=\"1.0\"?>";
00020 }
00021 
00022 namespace fsm_constructor{
00023 using namespace std;
00024 
00025 void xml_scxml_bgn(std::ostream& out, std::string tab, std::string name, std::string start){
00026         string _name="";
00027         string _start="";
00028         if(name.size()>0) _name = " id=\""+name+"\"";
00029         if(start.size()>0)_start= " initialstate=\""+start+"\"";
00030         out<<tab<<"<scxml"<<_name<<_start<<">";
00031 }
00032 void xml_scxml_end(std::ostream& out, std::string tab){
00033         out<<tab<<"</scxml>";
00034 }
00035 void xml_state_bgn(std::ostream& out, std::string tab, std::string name, std::string start, std::string id){
00036         string _name="";
00037         string _start="";
00038         string _id="";
00039         if(name.size()>0) _name = " name=\""+name+"\"";
00040         if(start.size()>0)_start= " initialstate=\""+start+"\"";
00041         if(id.size()>0)_id= " id=\""+id+"\"";
00042         out<<tab<<"<state"<<_name<<_start<<_id<<">";
00043 }
00044 void xml_state_end(std::ostream& out, std::string tab){
00045         out<<tab<<"</state>";
00046 }
00047 void xml_transition_bgn(std::ostream& out, std::string tab, std::string event, std::string target){
00048         string _event="";
00049         string _target="";
00050         if(event.size()>0) _event = " event=\""+event+"\"";
00051         if(target.size()>0)_target= " target=\""+target+"\"";
00052         out<<tab<<"<transition"<<_event<<_target<<">";
00053 }
00054 void xml_transition_end(std::ostream& out, std::string tab){
00055         out<<tab<<"</transition>";
00056 }
00057 void xml_parallel_bgn(std::ostream& out, std::string tab){
00058         out<<tab<<"<parallel>";
00059 }
00060 void xml_parallel_end(std::ostream& out, std::string tab){
00061         out<<tab<<"</parallel>";
00062 }
00063 void xml_onentry_bgn(std::ostream& out, std::string tab){
00064         out<<tab<<"<onentry>";
00065 }
00066 void xml_onentry_end(std::ostream& out, std::string tab){
00067         out<<tab<<"</onentry>";
00068 }
00069 void xml_send(std::ostream& out, std::string tab, string event){
00070         out<<tab<<"<send event=\""<<  event <<"\" />";
00071 }
00072 void xml_call_task(std::ostream& out, std::string tab, std::string name, std::string id){
00073         string _name="";
00074         string _id="";
00075         if(id.size()>0) _id = " id=\""+id+"\"";
00076         if(name.size()>0) _name = " name=\"TASK["+name+"]\"";
00077         out<<tab<<"<invoke"<<_name<<_id<<" />";
00078 }
00079 
00080 
00081 std::ostream& saveXml(std::ostream& out, const Raise& o){
00082         xml_send(out, o.tab, o.text);
00083         return out;
00084 }
00085 
00086 std::ostream& saveXml(std::ostream& out, const EventAction& o){
00087         if(o.type=="raise"){
00088                 xml_send(out, o.tab, o.text);
00089         }
00090         return out;
00091 }
00092 
00093 std::ostream& saveXml(std::ostream& out, const OnEvent& o){
00094 
00095         string target="";
00096         int next_counter=0;
00097         for(std::vector<EventAction>::const_iterator i=o.actions.begin();i!=o.actions.end();i++){
00098                 if(i->type=="next"){
00099                         target=o.id + "/" + i->text;
00100                         next_counter++;
00101                 }
00102         }
00103         string tab="";
00104         xml_transition_bgn(out, o.tab, o.text, target);
00105         if(next_counter<(int)o.actions.size()){ out<<endl; tab=o.tab; }
00106         for(std::vector<EventAction>::const_iterator i=o.actions.begin();i!=o.actions.end();i++){
00107                 i->lib=o.lib;
00108                 i->tab=o.tab+"   ";
00109                 if(i->type!="next")
00110                         saveXml(out, *i)<<endl;
00111         }
00112         xml_transition_end(out, tab);
00113         return out;
00114 }
00115 
00116 std::ostream& saveXml(std::ostream& out, const States& o){
00117         //o.lib->name_to_id[o.name].push_back(o.getId());
00118         xml_state_bgn(out, o.tab, o.name, "", o.getId()); out<<endl;
00119 
00120         if(not o.raises.empty()){
00121                 string tab = o.tab+"   ";
00122                 xml_onentry_bgn(out, tab); out<<endl;
00123                 for(std::vector<Raise>::const_iterator i=o.raises.begin();i!=o.raises.end();i++){
00124                         i->lib=o.lib;
00125                         i->tab=tab+"   ";
00126                         saveXml(out, *i)<<endl;
00127                 }
00128                 xml_onentry_end(out, tab); out<<endl;
00129         }
00130 
00131         if(not o.calls.empty()){
00132                 string tab = o.tab+"   ";
00133                 if(o.calls.size()>1){ xml_parallel_bgn(out, tab); tab+="    ";  out<<endl; }
00134                 for(std::vector<Call>::const_iterator i=o.calls.begin();i!=o.calls.end();i++){
00135                         i->lib=o.lib;
00136                         i->tab=tab;
00137                         i->id = o.getId();
00138                         saveXml(out, *i)<<endl;
00139                 }
00140                 if(o.calls.size()>1){ xml_parallel_end(out, o.tab+"   "); out<<endl; }
00141         }
00142 
00143         if(not o.events.empty()){
00144                 for(std::vector<OnEvent>::const_iterator i=o.events.begin();i!=o.events.end();i++){
00145                         i->lib=o.lib;
00146                         i->tab=o.tab+"   ";
00147                         i->id = o.id;
00148                         saveXml(out, *i)<<endl;
00149                 }
00150         }
00151 
00152         xml_state_end(out, o.tab);
00153         //o.lib->name_to_id[o.name].pop_back();
00154         return out;
00155 }
00156 
00157 std::ostream& saveXml(std::ostream& out, const Call& o){
00158         string resolved = "";
00159         if(o.type == "fsm"){
00160                 bool isResolved = o.lib->contains_fsm(o.text);
00161                 if(not isResolved){
00162                         o.lib->errors<<"in "<<o.file<<":"<<o.line<<":"<<o.pos<<endl;
00163                         o.lib->errors<<"   FSM("<<o.text<<") definition does not found." <<endl;
00164                         out<<o.tab<<"<error>"<<endl;
00165                         out<<o.tab<<"   "<<"in "<<o.file<<":"<<o.line<<":"<<o.pos<<endl;
00166                         out<<o.tab<<"   "<<"   FSM("<<o.text<<") definition does not found." <<endl;
00167                         out<<o.tab<<"</error>";
00168                 }else{
00169                         o.lib->saveXml_fsm(out, o.tab, o.text, o.id);
00170                 }
00171                 return out;
00172         }
00173         if(o.type == "bt"){
00174                 bool isResolved = o.lib->contains_tree(o.text);
00175                 if(not isResolved){
00176                         o.lib->errors<<"in "<<o.file<<":"<<o.line<<":"<<o.pos<<endl;
00177                         o.lib->errors<<"   BT("<<o.text<<") definition does not found." <<endl;
00178                         out<<o.tab<<"<error>"<<endl;
00179                         out<<o.tab<<"   "<<"in "<<o.file<<":"<<o.line<<":"<<o.pos<<endl;
00180                         out<<o.tab<<"   "<<"   BT("<<o.text<<") definition does not found." <<endl;
00181                         out<<o.tab<<"</error>";
00182                 }else{
00183                         o.lib->saveXml_tree(out, o.tab, o.text, o.id);
00184                 }
00185                 return out;
00186         }
00187         if(o.type == "task"){
00188                 string tab=o.tab;
00189                 xml_call_task(out, tab, o.text, o.getId());
00190         }
00191         return out;
00192 }
00193 
00194 std::ostream& saveXml(std::ostream& out, const Fsm& o){
00195         //o.lib->name_to_id[o.name].push_back(o.getId());
00196         xml_state_bgn(out, o.tab, o.name, o.start, o.getId()); out<<endl;
00197         for(std::vector<States>::const_iterator i=o.states.begin();i!=o.states.end();i++){
00198                 i->lib=o.lib;
00199                 i->tab=o.tab+"   ";
00200                 i->id=o.getId();
00201                 saveXml(out, *i)<<endl;
00202         }
00203         xml_state_end(out, o.tab);
00204         //o.lib->name_to_id[o.name].pop_back();
00205         return out;
00206 }
00207 
00208 std::ostream& saveXml(std::ostream& out, const FSMConstructor& o){
00209 //      xml_version(out, o.tab); out<<endl;
00210         xml_scxml_bgn(out, o.tab, "", ""); out<<endl;
00211         for(std::map<std::string, Fsm>::const_iterator i=o.fsms.begin();i!=o.fsms.end();i++){
00212                 i->second.lib = &o;
00213                 i->second.tab=o.tab+"   ";
00214                 saveXml(out, i->second)<<endl;
00215         }
00216         xml_scxml_end(out, o.tab);
00217         return out;
00218 }
00219 
00220 void saveXml(std::string path_prefix, const FSMConstructor& o){
00221         for(std::map<std::string, Fsm>::const_iterator i=o.fsms.begin();i!=o.fsms.end();i++){
00222                 std::stringstream filename; filename << path_prefix << i->second.name <<".scxml";
00223                 std::ofstream out(filename.str().c_str());
00224                 if(out.is_open()){
00225                         xml_version(out, o.tab); out<<endl;
00226                         out<<"<!-- from file: "<<o.filename<<" -->"<<endl;
00227                         xml_scxml_bgn(out, o.tab, "", ""); out<<endl;
00228                         i->second.lib = &o;
00229                         i->second.tab=o.tab+"   ";
00230                         saveXml(out, i->second)<<endl;
00231                         xml_scxml_end(out, o.tab);
00232                         out.close();
00233                 }else{
00234                         throw PEFileNotCreated(filename.str());
00235                 }
00236         }
00237 }
00238 
00239 //==========================================================================================
00240 
00241 
00242 
00243 std::ostream& operator<<(std::ostream& out, const Raise& o){
00244         return
00245         out<<"            raise "<<" : "<<o.text;
00246 }
00247 
00248 std::ostream& operator<<(std::ostream& out, const EventAction& o){
00249         return
00250         out<<"               action "<<o.type<<" : "<<o.text;
00251 }
00252 
00253 std::ostream& operator<<(std::ostream& out, const OnEvent& o){
00254         out<<"            Event{ "<<endl;
00255         out<<"               type="<<o.type<<endl;
00256         out<<"               event="<<o.text<<endl;
00257         for(std::vector<EventAction>::const_iterator i=o.actions.begin();i!=o.actions.end();i++){
00258                 i->lib=o.lib;
00259                 out<<(*i)<<endl;
00260         }
00261         out<<"            }";
00262         return out;
00263 }
00264 
00265 std::ostream& operator<<(std::ostream& out, const Call& o);
00266 std::ostream& operator<<(std::ostream& out, const States& o){
00267         out<<"      State{ "<<endl;
00268         out<<"         name="<<o.name<<endl;
00269         out<<"         calls:"<<endl;
00270         for(std::vector<Call>::const_iterator i=o.calls.begin();i!=o.calls.end();i++){
00271                 i->lib=o.lib;
00272                 out<<(*i)<<endl;
00273         }
00274         out<<"         raises:"<<endl;
00275 
00276         for(std::vector<Raise>::const_iterator i=o.raises.begin();i!=o.raises.end();i++){
00277 
00278                 i->lib=o.lib;
00279                 out<<(*i)<<endl;
00280         }
00281         out<<"         events:"<<endl;
00282         for(std::vector<OnEvent>::const_iterator i=o.events.begin();i!=o.events.end();i++){
00283                 i->lib=o.lib;
00284                 out<<(*i)<<endl;
00285         }
00286         out<<"      }";
00287         return out;
00288 }
00289 
00290 std::ostream& operator<<(std::ostream& out, const Fsm& o){
00291         out<<"FSM{ "<<endl;
00292         out<<"   name="<<o.name<<endl;
00293         out<<"   start="<<o.start<<endl;
00294         out<<"   States"<<endl;
00295         for(std::vector<States>::const_iterator i=o.states.begin();i!=o.states.end();i++){
00296                 i->lib=o.lib;
00297                 out<<(*i)<<endl;
00298         }
00299         out<<"}";
00300         return out;
00301 }
00302 
00303 std::ostream& operator<<(std::ostream& out, const FSMConstructor& o){
00304         for(std::map<std::string, Fsm>::const_iterator i=o.fsms.begin();i!=o.fsms.end();i++){
00305                 i->second.lib = &o;
00306                 out<<i->second<<endl;
00307         }
00308         return out;
00309 }
00310 
00311 std::ostream& operator<<(std::ostream& out, const Call& o){
00312         string resolved = "";
00313         if(o.type == "fsm"){
00314                 bool isResolved = o.lib->contains_fsm(o.text);
00315                 resolved = isResolved?"resolved":"not found";
00316                 if(not isResolved){
00317                         o.lib->errors<<"in "<<o.file<<":"<<o.line<<":"<<o.pos<<endl;
00318                         o.lib->errors<<"   FSM("<<o.text<<") definition does not found." <<endl;
00319                 }
00320                 return
00321                         out<<"            call "<<o.type<<" : "<<o.text << " : " << resolved;
00322         }
00323         if(o.type == "bt"){
00324                 bool isResolved = o.lib->contains_tree(o.text);
00325                 resolved = isResolved?"resolved":"not found";
00326                 if(not isResolved){
00327                         o.lib->errors<<"in "<<o.file<<":"<<o.line<<":"<<o.pos<<endl;
00328                         o.lib->errors<<"   BT("<<o.text<<") definition does not found." <<endl;
00329                 }
00330                 return
00331                         out<<"            call "<<o.type<<" : "<<o.text << " : " << resolved;
00332         }
00333         if(o.type == "task"){
00334                 return
00335                         out<<"            call "<<o.type<<" : "<<o.text;
00336         }
00337         return out;
00338 }
00339 
00340 
00341 
00342 }
00343 
00344 


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