2 ////////////////////////////////////////////////////////////////////////////////////////
4 // This file is part of Boost Statechart Viewer.
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.
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.
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/>.
19 ////////////////////////////////////////////////////////////////////////////////////////
24 #include "stringoper.h"
30 list<string> transitions;
33 string outputFilename;
34 string name_of_machine;
35 string name_of_first_state;
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 )
41 outputFilename = outputFile;
42 name_of_machine = FSM_name;
43 name_of_first_state = firstState;
50 void setEvents(list<string> events)
52 this->events = events;
55 void setTransitions(list<string> transitions)
57 this->transitions = transitions;
60 void setStates(list<string> states)
62 this->states = states;
65 void setNameOfStateMachine(string name_of_FSM)
67 name_of_machine = name_of_FSM;
70 void setNameOfFirstState(string first_state)
72 name_of_first_state = first_state;
75 void setOutputFilename(string outputFilename)
77 this->outputFilename = outputFilename;
80 // outputfile writing methods
81 void write_states(ofstream& filestr) // write states
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
90 cnt = count(state,',');
93 pos1 = state.find(",");
94 ctx = cut_namespaces(state.substr(pos1+1));
95 if(ctx.compare(0,context.length(),context)==0)
97 filestr<<cut_namespaces(state.substr(0,pos1))<<";\n";
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)
109 filestr<<cut_namespaces(state.substr(0,pos1))<<";\n";
114 while(!nstates.empty()) // substates ?
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";
124 //std::cout<<states.size();
125 for(list<string>::iterator i = nstates.begin();i!=nstates.end();i++)
128 cnt = count(state,',');
131 pos1 = state.find(",");
132 ctx = cut_namespaces(state.substr(pos1+1));
133 if(ctx.compare(0,context.length(),context)==0)
135 filestr<<cut_namespaces(state.substr(0,pos1))<<";\n";
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";
154 void write_transitions(ofstream& filestr) // write transitions
158 for(list<string>::iterator i = transitions.begin();i!=transitions.end();i++) // write all transitions
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";
170 void save_to_file() // save state automaton to file
172 if(!name_of_first_state.empty())
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);
182 else cout<<"No state machine was found. So no output file was created.\n";
186 // method for printing statistics about state automaton
187 void print_stats() // print statistics
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";