DiaDes  0.1
DIAgnosis of Discrete-Event System
Io.hh
Go to the documentation of this file.
1 /*
2  * @os Io.hh
3  * @author Yannick Pencole, ypencole@laas.fr, LAAS-CNRS, Univ. Toulouse, FRANCE
4  *
5  * Created on 02 november 2017, 11:11
6  */
7 
8 #ifndef __DIADES__AUTOMATA__EXPERIMENTAL__IO__HH__
9 #define __DIADES__AUTOMATA__EXPERIMENTAL__IO__HH__
10 #include<fstream>
13 
15 
16 #include "EventSet.hh"
18 
19 namespace Diades
20 {
21  namespace Automata
22  {
23  namespace Experimental
24  {
30  inline ostream &
31  toDotHeader(ostream & os)
32  {
33  os << "digraph G {\n";
34  os << "\tratio=fill;\n";
35  os << "\tpage=\"8.5,11.6\";\n";
36  os << "\tsize=\"7.5,10.5\";\n";
37  return os;
38  }
39 
40 
41 
47  template<typename StateMachine>
48  ostream &
49  toDotHeader(const StateMachine & fsm, ostream & os)
50  {
51  toDotHeader(os);
52  os << "\tlabel=\"Component name = " << fsm.name() << "\\nState Number= ";
53  os << fsm.numberOfStates() << "\\n";
54  os << "Transition Number= " << fsm.numberOfTransitions() << "\";\n";
55  return os;
56  }
57 
63  template<typename StateMachine>
64  ostream &
65  toDotInitialStates(const StateMachine & fsm, ostream & os)
66  {
67  int indexInitial = 0;
68  // writing the initial states
69  for(auto it = fsm.initialStateBegin();
70  it != fsm.initialStateEnd();
71  ++it)
72  {
73  os << "\t init" << indexInitial << "\t [label=\"\",color=white];\n";
74  os << "\t init" << indexInitial << " -> " << *it << ";\n";
75  ++indexInitial;
76  }
77  return os;
78  }
79 
80  template<typename StateMachine,
81  typename PrintStateProperty>
82  ostream &
83  toDotStates(const StateMachine& fsm, const PrintStateProperty & statePrinter, ostream & os)
84  {
85  for(auto stateIt = fsm.stateBegin();
86  stateIt != fsm.stateEnd();
87  stateIt++)
88  {
89  os << "\t" << *stateIt << "\t [shape=rectangle,label=\"";
90  statePrinter.print(os, fsm.getStatePropertyId(*stateIt));
91  os << "\"];\n";
92  }
93  return os;
94  }
95  template<typename S, typename I, typename NS, typename NI, typename PrintStateProperty>
96  ostream & toDotStates(const FiniteAutomaton<S,I,NS,NI> & fsm, const PrintStateProperty & statePrinter, ostream & os)
97  {
98  for(auto stateIt = fsm.stateBegin();
99  stateIt != fsm.stateEnd();
100  stateIt++)
101  {
102  os << "\t" << *stateIt << "\t [shape=";
103  if(fsm.isAcceptingState(*stateIt))
104  {
105  os << "doubleoctagon";
106  }
107  else
108  {
109  os << "rectangle";
110  }
111  os << ",label=\"";
112  statePrinter.print(os, fsm.getStatePropertyId(*stateIt));
113  os << "\"];\n";
114  }
115  return os;
116  }
117 
118  template<typename StateMachine>
119  ostream &
120  toDotStates(const StateMachine& fsm, ostream & os)
121  {
122  // writing the states
123  for(auto stateIt = fsm.stateBegin();
124  stateIt != fsm.stateEnd();
125  stateIt++)
126  {
127  os << "\t" << *stateIt << "\t [shape=rectangle,label=\"";
128  os << *stateIt << ":" << fsm.getStatePropertyId(*stateIt);
129  os << "\"];\n";
130  }
131  return os;
132  }
133 
134  template<typename S, typename I, typename NS, typename NI>
135  ostream &
136  toDotStates(const FiniteAutomaton<S,I,NS,NI> & fa, ostream & os)
137  {
138  // writing the states
139  for(auto stateIt = fa.stateBegin();
140  stateIt != fa.stateEnd();
141  stateIt++)
142  {
143  os << "\t" << *stateIt << "\t [shape=";
144  if(fa.isAcceptingState(*stateIt))
145  {
146  os << "doubleoctagon";
147  }
148  else
149  {
150  os << "rectangle";
151  }
152  os << ",label=\"";
153  os << *stateIt << ":" << fa.getStatePropertyId(*stateIt);
154  os << "\"];\n";
155  }
156  return os;
157  }
158 
159  inline ostream &
160  toDotEnding(ostream & os)
161  {
162  // writing the end of os
163  os << "}\n";
164  return os;
165  }
166 
167  template<typename StateMachine,
168  typename PrintEventInfo>
169  ostream &
170  toDotTransitions(const StateMachine& fsm, const PrintEventInfo & eventPrinter, ostream & os)
171  {
172  // writing the transitions
173  for(auto transIt = fsm.transitionBegin();
174  transIt != fsm.transitionEnd();
175  ++transIt)
176  {
177  os << "\t" << transIt->source() << " -> " << transIt->target()
178  << " [label=\"";
179  eventPrinter.print(os, fsm.getEvent(*transIt));
180  os << "\"];\n";
181  }
182  return os;
183  }
184 
185  template<typename StateProperty, typename EventInfo, typename PrintEventInfo>
186  ostream &
188  const PrintEventInfo & eventPrinter, ostream & os)
189  {
190 
191  // writing the transitions
192  for(auto transIt = fsm.transitionBegin();
193  transIt != fsm.transitionEnd();
194  ++transIt)
195  {
196  os << "\t" << transIt->source() << " -> " << transIt->target()
197  << " [label=\"";
198  eventPrinter.print(os, fsm.getEvent(*transIt));
199  if(fsm.isFaulty(fsm.getEvent(*transIt)))
200  {
201  os << "\",color=red];\n";
202  }
203  else
204  {
205  os << "\"];\n";
206  }
207  }
208  return os;
209  }
210 
211  template<typename StateMachine>
212  ostream &
213  toDotTransitions(const StateMachine& fsm, ostream & os)
214  {
215 
216  // writing the transitions
217  for(auto transIt = fsm.transitionBegin();
218  transIt != fsm.transitionEnd();
219  ++transIt)
220  {
221  os << "\t" << transIt->source() << " -> " << transIt->target()
222  << " [label=\"";
223  os << fsm.getEvent(*transIt);
224  os << "\"];\n";
225  }
226  return os;
227  }
228 
229  template<typename StateProperty, typename EventInfo>
230  ostream &
232  {
233 
234  // writing the transitions
235  for(auto transIt = fsm.transitionBegin();
236  transIt != fsm.transitionEnd();
237  ++transIt)
238  {
239  os << "\t" << transIt->source() << " -> " << transIt->target()
240  << " [label=\"";
241  os << fsm.getEvent(*transIt);
242  if(fsm.isFaulty(fsm.getEvent(*transIt)))
243  {
244  os << "\",color=red];\n";
245  }
246  else
247  {
248  os << "\"];\n";
249  }
250  }
251  return os;
252  }
253 
264  template<typename StateMachine,
265  typename PrintStateProperty, typename PrintEventInfo>
266  ostream &
267  toDot(ostream & os,
268  const StateMachine & fsm,
269  const PrintStateProperty & statePrinter,
270  const PrintEventInfo & eventPrinter)
271  {
272 
273  toDotHeader(fsm, os);
274  toDotInitialStates(fsm, os);
275  toDotStates(fsm, statePrinter, os);
276  toDotTransitions(fsm, eventPrinter, os);
277  return toDotEnding(os);
278  }
279 
288  template<typename StateMachine>
289  ostream &
290  toDot(ostream & os,
291  const StateMachine & fsm)
292  {
293  toDotHeader(fsm, os);
294  toDotInitialStates(fsm, os);
295  toDotStates(fsm, os);
296  toDotTransitions(fsm, os);
297  return toDotEnding(os);
298  }
299 
308  template<typename StateMachine>
309  bool
310  toDot(const std::string & filename,
311  const StateMachine & fsm)
312  {
313  std::ofstream file(filename);
314  if(!file.is_open())
315  {
316  return false;
317  }
318  toDot(file,fsm);
319  file.close();
320  return true;
321  }
322 
323 
324 
325 
326 
327 
328 
335  template<>
336  inline ostream &
337  toDot(ostream & os,
338  const DdSyncDescriptor & descriptor)
339  {
340  return descriptor.toDot(os);
341  }
342 
343 
344  }
345  }
346 }
347 
348 
349 #endif
ostream & toDotHeader(ostream &os)
Definition: Io.hh:31
ostream & toDotTransitions(const StateMachine &fsm, const PrintEventInfo &eventPrinter, ostream &os)
Definition: Io.hh:170
ostream & toDotEnding(ostream &os)
Definition: Io.hh:160
ostream & toDotInitialStates(const StateMachine &fsm, ostream &os)
Definition: Io.hh:65
InitialStateIterator initialStateEnd() const
ostream & toDotStates(const StateMachine &fsm, const PrintStateProperty &statePrinter, ostream &os)
Definition: Io.hh:83
ostream & toDot(ostream &os, const StateMachine &fsm, const PrintStateProperty &statePrinter, const PrintEventInfo &eventPrinter)
Definition: Io.hh:267
const EventPropertyId & getEvent(Transition t) const
Namespace of the Diades project.
InitialStateIterator initialStateBegin() const
TransitionIterator transitionBegin() const
const StatePropertyId & getStatePropertyId(State state) const