00001
00002
00003
00004
00005
00006
00007
00008 #ifndef FSMCONSTRUCTOR_H_
00009 #define FSMCONSTRUCTOR_H_
00010
00011 #include <deque>
00012 #include <map>
00013 #include <sstream>
00014 #include <vector>
00015 #include <iostream>
00016 #include "Container.h"
00017 #include <deque>
00018
00019 namespace fsm_constructor{
00020 using namespace std;
00021
00022 class FSMConstructor;
00023 class Element{
00024 public:
00025 mutable FSMConstructor const* lib;
00026 mutable string tab;
00027 Element():lib(0){}
00028 };
00029
00030 class Raise:public Element{
00031 public:
00032 std::string text;
00033 };
00034
00035 class EventAction:public Element{
00036 public:
00037 std::string type;
00038 std::string text;
00039 ~EventAction(){}
00040 };
00041
00042 class OnEvent:public Element{
00043 public:
00044 mutable std::string id;
00045 std::string type;
00046 std::string text;
00047 std::vector<EventAction> actions;
00048 };
00049
00050 class Call:public Element{
00051 public:
00052 mutable std::string id;
00053 std::string type;
00054 std::string text;
00055 int line, pos;
00056 std::string file;
00057
00058 string getId()const{ return id+"/"+text; }
00059 };
00060
00061 class States:public Element{
00062 public:
00063 mutable std::string id;
00064 std::string name;
00065 std::vector<Call> calls;
00066
00067 std::vector<Raise> raises;
00068
00069 std::vector<OnEvent> events;
00070
00071 std::deque<OnEvent> stack_events;
00072
00073 void create_event(){ stack_events.push_back(OnEvent()); }
00074 void add_event(){ events.push_back(event()); drop_event(); }
00075 void drop_event(){ stack_events.pop_back(); }
00076 OnEvent& event(){ return stack_events.back(); }
00077
00078 std::string getId()const{ return id+"/"+name; }
00079 };
00080
00081 class Fsm:public Element{
00082 public:
00083 mutable std::string id;
00084 std::string name;
00085 std::string start;
00086 std::vector<States> states;
00087 std::deque<States> stack;
00088
00089 void create(){
00090 States st;
00091 if(stack.empty()){
00092 st.id = getId();
00093 }else{
00094 st.id = stack.back().getId();
00095 }
00096 stack.push_back(st);
00097 }
00098 void add(){ states.push_back(state()); drop(); }
00099 void drop(){ stack.pop_back(); }
00100 States& state(){ return stack.back(); }
00101
00102 std::string getId()const{ return id+"/"+name; }
00103 };
00104
00105 class FSMConstructor : public Element, public Container {
00106 public:
00107 std::map<std::string, Fsm> fsms;
00108 std::deque<Fsm> stack;
00109 Container* trees;
00110 std::stringstream& errors;
00111 std::string filename;
00112
00113
00114 FSMConstructor(std::stringstream& errors, std::string filename):trees(0), errors(errors), filename(filename){}
00115
00116 void create(){
00117 stack.push_back(Fsm());
00118 }
00119 void drop(){
00120 stack.pop_back();
00121 }
00122 void add(){
00123 fsms[fsm().name] = fsm();
00124 drop();
00125 }
00126 Fsm& fsm(){ return stack.back(); }
00127
00128 bool contains_fsm(string name)const{
00129 return contains(name);
00130 }
00131 std::string copy_fsm(std::string name)const{
00132 return copy(name);
00133 }
00134
00135 bool contains_tree(string name)const{
00136 return trees->contains(name);
00137 }
00138
00139 std::string copy_tree(std::string name)const{
00140 return trees->copy(name);
00141 }
00142
00143 void saveXml_fsm(std::ostream& out, std::string tab, std::string name, std::string id)const{
00144 saveXml(out, tab, name, id);
00145 }
00146 void saveXml_tree(std::ostream& out, std::string tab, std::string name, std::string id)const{
00147 trees->saveXml(out, tab, name, id);
00148 }
00149
00150
00151 bool contains(std::string name)const{ return fsms.find(name)!=fsms.end(); }
00152 std::string copy(std::string name)const;
00153 void saveXml(std::ostream& out, std::string tab, std::string name, std::string id)const;
00154
00155
00156
00157 void saveDot_fsm(std::ostream& out, std::string tab, std::string name, std::string id)const{
00158 saveDot(out, tab, name, id);
00159 }
00160 void saveDot_tree(std::ostream& out, std::string tab, std::string name, std::string id)const{
00161 trees->saveDot(out, tab, name, id);
00162 }
00163
00164 void saveDot(std::ostream& out, std::string tab, std::string name, std::string id)const;
00165
00166 void map_ids_tree(std::string name, std::string id)const{trees->map_ids(name, id);}
00167 void map_ids_fsm(std::string name, std::string id)const{map_ids(name, id);}
00168 void map_ids(std::string name, std::string id)const;
00169 };
00170
00171
00172
00173 std::ostream& operator<<(std::ostream& out, const Raise& o);
00174
00175 std::ostream& operator<<(std::ostream& out, const EventAction& o);
00176
00177 std::ostream& operator<<(std::ostream& out, const OnEvent& o);
00178
00179 std::ostream& operator<<(std::ostream& out, const Call& o);
00180 std::ostream& operator<<(std::ostream& out, const States& o);
00181
00182 std::ostream& operator<<(std::ostream& out, const Fsm& o);
00183
00184 std::ostream& operator<<(std::ostream& out, const FSMConstructor& o);
00185
00186 std::ostream& operator<<(std::ostream& out, const Call& o);
00187
00188
00189
00190 std::ostream& saveXml(std::ostream& out, const Raise& o);
00191
00192 std::ostream& saveXml(std::ostream& out, const EventAction& o);
00193
00194 std::ostream& saveXml(std::ostream& out, const OnEvent& o);
00195
00196 std::ostream& saveXml(std::ostream& out, const States& o);
00197
00198 std::ostream& saveXml(std::ostream& out, const Fsm& o);
00199
00200 std::ostream& saveXml(std::ostream& out, const Call& o);
00201
00202 std::ostream& saveXml(std::ostream& out, const FSMConstructor& o);
00203
00204 void saveXml(std::string prefix, const FSMConstructor& o);
00205
00206
00207
00208
00209 std::ostream& saveDot(std::ostream& out, const Raise& o);
00210
00211 std::ostream& saveDot(std::ostream& out, const EventAction& o);
00212
00213 std::ostream& saveDot(std::ostream& out, const OnEvent& o, const States& state, const Fsm& fsm);
00214
00215 std::ostream& saveDot(std::ostream& out, const States& o, const Fsm& fsm);
00216
00217 std::ostream& saveDot(std::ostream& out, const Fsm& o);
00218
00219 std::ostream& saveDot(std::ostream& out, const Call& o);
00220
00221 std::ostream& saveDot(std::ostream& out, const FSMConstructor& o);
00222
00223 void saveDot(std::string prefix, const FSMConstructor& o);
00224
00225
00226 void map_ids(const Call& o);
00227 void map_ids(const States& o);
00228 void map_ids(const Fsm& o);
00229 void map_ids(const FSMConstructor& o);
00230
00231 std::string searchSimpleState(const States& state);
00232 std::string searchSimpleState(const Container* cnt_fsm, std::string name);
00233
00234 }
00235
00236 void xml_version(std::ostream& out, std::string tab);
00237
00238 #endif