]> rtime.felk.cvut.cz Git - boost-statechart-viewer.git/blob - src/iooper.h
acd72c79eb01fd182aba7331d2ff39b68bd00418
[boost-statechart-viewer.git] / src / iooper.h
1
2 ////////////////////////////////////////////////////////////////////////////////////////  
3 //    
4 //    This file is part of Boost Statechart Viewer.
5 //
6 //    Boost Statechart Viewer is free software: you can redistribute it and/or modify
7 //    it under the terms of the GNU General Public License as published by
8 //    the Free Software Foundation, either version 3 of the License, or
9 //    (at your option) any later version.
10 //
11 //    Boost Statechart Viewer is distributed in the hope that it will be useful,
12 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //    GNU General Public License for more details.
15 //
16 //    You should have received a copy of the GNU General Public License
17 //    along with Boost Statechart Viewer.  If not, see <http://www.gnu.org/licenses/>.
18 //
19 ////////////////////////////////////////////////////////////////////////////////////////
20
21 #include <fstream>
22 #include <list>
23
24 #include "stringoper.h"
25
26 using namespace std;
27
28 class IO_operations
29 {
30         list<string> transitions;
31         list<string> states;
32         list<string> events;
33         string outputFilename;
34         string name_of_machine;
35         string name_of_first_state;
36         public:
37         IO_operations() {}
38         
39         IO_operations( const string outputFile, const string FSM_name, const string firstState, const list<string> trans, const list<string> state, const list<string> ev )
40         {
41                 outputFilename = outputFile;
42                 name_of_machine = FSM_name;
43                 name_of_first_state = firstState;
44                 transitions = trans;
45                 states = state;
46                 events = ev;
47         }
48
49         // set methods
50         void setEvents(list<string> events)
51         {
52                 this->events = events;
53         }
54         
55         void setTransitions(list<string> transitions)
56         {
57                 this->transitions = transitions;
58         }
59
60         void setStates(list<string> states)
61         {
62                 this->states = states;
63         }
64
65         void setNameOfStateMachine(string name_of_FSM)
66         {
67                 name_of_machine = name_of_FSM;
68         }
69
70         void setNameOfFirstState(string first_state)
71         {
72                 name_of_first_state = first_state;
73         }
74
75         void setOutputFilename(string outputFilename)
76         {
77                 this->outputFilename = outputFilename;
78         }
79         
80         // outputfile writing methods
81         void write_states(ofstream& filestr) // write states
82         {
83                 int pos1, pos2, cnt, subs;
84                 string context, state, ctx;
85                 list<string> nstates = states;
86                 context = name_of_machine;
87                 for(list<string>::iterator i = nstates.begin();i!=nstates.end();i++) // write all states in the context of the automaton
88                 {
89                         state = *i;
90                         cnt = count(state,',');
91                         if(cnt==1)
92                         {
93                                 pos1 = state.find(",");
94                                 ctx = cut_namespaces(state.substr(pos1+1));
95                                 if(ctx.compare(0,context.length(),context)==0)
96                                 {
97                                         filestr<<cut_namespaces(state.substr(0,pos1))<<";\n";
98                                         nstates.erase(i);
99                                         i--;
100                                 }
101                         }
102                         if(cnt==2)
103                         {
104                                 pos1 = state.find(",");
105                                 pos2 = state.rfind(",");
106                                 ctx = cut_namespaces(state.substr(pos1+1,pos2-pos1-1));
107                                 if(ctx.compare(0,context.length(),context)==0)
108                                 {                               
109                                         filestr<<cut_namespaces(state.substr(0,pos1))<<";\n";
110                                 }
111                         }
112                 }
113                 subs = 0;
114                 while(!nstates.empty()) // substates ?
115                 {
116                         state = nstates.front();
117                         filestr<<"subgraph cluster"<<subs<<" {\n";                      
118                         pos1 = state.find(",");
119                         pos2 = state.rfind(",");
120                         context = cut_namespaces(state.substr(0,pos1));
121                         filestr<<"label=\""<<context<<"\";\n";
122                         filestr<<cut_namespaces(state.substr(pos2+1))<<" [peripheries=2] ;\n";  
123                         nstates.pop_front();    
124                         //std::cout<<states.size();     
125                         for(list<string>::iterator i = nstates.begin();i!=nstates.end();i++)
126                         {
127                                 state = *i;
128                                 cnt = count(state,',');
129                                 if(cnt==1)
130                                 {
131                                         pos1 = state.find(",");
132                                         ctx = cut_namespaces(state.substr(pos1+1));
133                                         if(ctx.compare(0,context.length(),context)==0)
134                                         {
135                                                 filestr<<cut_namespaces(state.substr(0,pos1))<<";\n";
136                                                 nstates.erase(i);
137                                                 i--;
138                                         }
139                                 }
140                                 if(cnt==2)
141                                 {
142                                         pos1 = state.find(",");
143                                         pos2 = state.rfind(",");
144                                         ctx = cut_namespaces(state.substr(pos1+1,pos2-pos1-1));
145                                         if(ctx.compare(0,context.length(),context)==0) filestr<<cut_namespaces(state.substr(0,pos1))<<";\n";
146                                 }
147                         }
148                         filestr<<"}\n";
149                         subs+=1;        
150                 }
151                 return;
152         }
153
154         void write_transitions(ofstream& filestr) // write transitions
155         {
156                 int pos1, pos2;
157                 string state;
158                 for(list<string>::iterator i = transitions.begin();i!=transitions.end();i++) // write all transitions
159                 {
160                         state = *i;
161                         pos1 = state.find(",");
162                         filestr<<cut_namespaces(state.substr(0,pos1))<<"->";
163                         pos2 = state.rfind(",");
164                         filestr<<cut_namespaces(state.substr(pos2+1));
165                         filestr<<"[label=\""<<cut_namespaces(state.substr(pos1+1,pos2-pos1-1))<<"\"];\n";
166                 }               
167                 return;
168         }
169
170         void save_to_file() // save state automaton to file
171         {
172                 if(!name_of_first_state.empty())        
173                 {       
174                         ofstream filestr(outputFilename.c_str());
175                         filestr<<"digraph "<< name_of_machine<< " {\n";
176                         filestr<<name_of_first_state<<" [peripheries=2] ;\n";
177                         write_states(filestr);
178                         write_transitions(filestr);
179                         filestr<<"}";           
180                         filestr.close();
181                 }
182                 else cout<<"No state machine was found. So no output file was created.\n";
183                 return;
184         }
185                 
186         // method for printing statistics about state automaton
187         void print_stats() // print statistics
188         {
189                 cout<<"\n"<<"Statistics: \n";
190                 cout<<"Number of states: "<<states.size()<<"\n";
191                 cout<<"Number of events: "<<events.size()<<"\n";
192                 cout<<"Number of transitions: "<<transitions.size()<<"\n";
193                 return;
194         }
195
196 };