DiaDes  0.1
DIAgnosis of Discrete-Event System
ToDot.cc
Go to the documentation of this file.
1 
11 #include <cstdlib>
12 #include<iostream>
13 #include<fstream>
14 #include<regex>
15 #include<boost/program_options.hpp>
16 
19 #include"../AutomataInterface.hh"
23 using namespace std;
24 using namespace Diades::Automata::Experimental;
25 using namespace Diades::Utils;
26 using namespace Diades::CmdInterface;
27 
28 namespace Poptions = boost::program_options;
29 
30 
31 
36 const string program("dd-dot");
37 const string briefcomment(": this program exports any input as a dot file (output file or standard output).");
38 /*
39  * File suffixes
40  */
41 FileSuffixes suffixes({"aut", "ddaut", "ddsync", "dot"});
42 
50 void
51 initialiseOptions(int argc, char * argv[],
52  Poptions::options_description & desc,
53  Poptions::variables_map & vm)
54 {
55  desc.add_options()
56  ("help,h", "produce help message")
57  ("file,f", Poptions::value< string >(), "file (.aut/.ddaut/.ddsync format)")
58  ("output,o", Poptions::value< string >(), "outfile name (.dot format)")
59  ;
60  Poptions::positional_options_description p;
61  // 1 that The command line can accept 1 file name as positional option
62  p.add("file", 1);
63  Poptions::store(Poptions::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
64  Poptions::notify(vm);
65 }
66 
67 
73 
74 size_t
75 exportAutToDot(const std::string & fileName, const std::string & output)
76 {
77  AutFsm fsm;
78  ifstream file(fileName.c_str());
79  if(file.is_open())
80  {
81  auto loaded = fromAutFile(file, fsm);
82  file.close();
83  if(!loaded)
84  {
85  return printCommandLineError(Msg("Error when loading the automaton file %1%") % fileName);
86  }
87  }
88  else
89  {
90  return printCommandLineError(Msg("Error when opening the automaton file %1%") % fileName);
91  }
92  if(output.empty())
93  {
94  toDot(std::cout, fsm);
95  }
96  else
97  {
98  ofstream outfile(output.c_str());
99  if(outfile.is_open())
100  {
101  toDot(outfile, fsm);
102  outfile.close();
103  }
104  else
105  {
106  return printCommandLineError(Msg("Error when creating the dot file %1%") % output);
107  }
108  }
109  return SUCCESS;
110 }
111 
112 size_t
113 exportDdAutToDot(const std::string & fileName, const std::string & output)
114 {
115  DdAutFileDescriptor descriptor;
116  DdAutEventManager eManager;
117  DdAutStateManager sManager;
118  DdAutStateManager sManagerDet;
119  ifstream file(fileName.c_str());
120  if(file.is_open())
121  {
122  auto loaded = descriptor.readStream(file);
123  file.close();
124  if(!loaded)
125  {
126  return printCommandLineError(Msg("Error when loading the 'ddaut' automaton file %1%") % fileName);
127  }
128  }
129  else
130  {
131  return printCommandLineError(Msg("Error when opening the 'ddaut' automaton file %1%") % fileName);
132  }
133 
134  DdAutFA fa;
135  bool result = faFromDescriptor(descriptor, fa, sManager, eManager);
138  if(!result)
139  {
140  return printCommandLineError(Msg("Error when converting the DdAutFileDescriptor from %1% to a Finite Automaton") % fileName);
141  }
142  if(output.empty())
143  {
144  toDot(std::cout, fa,statePrinter,eventPrinter);
145  }
146  else
147  {
148  ofstream outfile(output.c_str());
149  if(outfile.is_open())
150  {
151  toDot(outfile, fa,statePrinter,eventPrinter);
152  outfile.close();
153  }
154  else
155  {
156  return printCommandLineError(Msg("Error when creating the dot file %1%") % output);
157  }
158  }
160 }
161 
162 size_t
163 exportDdSyncToDot(const std::string & fileName, const std::string & output)
164 {
165 
166  ifstream rulefile(fileName.c_str());
167  if(!rulefile.is_open())
168  {
169  return printCommandLineError(Msg("Error when opening the synchronisation rule file %1%") % fileName);
170  }
171  DdSyncDescriptor descriptor;
172  std::string errorMsg;
173  if(!descriptor.readStream(rulefile, errorMsg))
174  {
175  return printCommandLineError(Msg("Error when loading the synchronisation rule file %1%: %2%")
176  % fileName % errorMsg);
177  }
178  if(output.empty())
179  {
180  toDot(std::cout, descriptor);
181  }
182  else
183  {
184  ofstream outfile(output.c_str());
185  if(outfile.is_open())
186  {
187  toDot(outfile, descriptor);
188  outfile.close();
189  }
190  else
191  {
192  return printCommandLineError(Msg("Error when creating the dot file %1%") % output);
193  }
194  }
195 
197 }
198 
199 size_t
200 exportToDot(const string & fileName, const string & output)
201 {
202 
203  if(!output.empty())
204  {
205  if(!suffixes.match(output, "dot"))
206  {
207  return printCommandLineError(Msg("Expecting an output file with a name ending with .dot, but I read %1%") % output);
208  }
209  }
210  if(suffixes.match(fileName, "aut"))
211  {
212  return exportAutToDot(fileName, output);
213  }
214  else
215  {
216  if(suffixes.match(fileName, "ddaut"))
217  {
218  return exportDdAutToDot(fileName, output);
219  }
220  else
221  {
222  if(suffixes.match(fileName, "ddsync"))
223  {
224  return exportDdSyncToDot(fileName, output);
225  }
226  else
227  {
228  return printCommandLineError(Msg("Expecting a file with a name ending with .aut/.ddaut/.ddsync, but I read %1%") % fileName);
229  }
230  }
231  }
233 }
234 
241 int
242 main(int argc, char** argv)
243 {
244  std::string fileName, output;
245  try
246  {
247  Poptions::options_description desc("Options");
248  Poptions::variables_map vm;
249  initialiseOptions(argc, argv, desc, vm);
250  if(vm.count("help"))
251  {
252  return printUsage(program + briefcomment, desc);
253  }
254  if(vm.count("file"))
255  {
256  fileName = vm["file"].as<std::string> ();
257  }
258  if(vm.count("output"))
259  {
260  output = vm["output"].as<std::string>();
261  }
262  }
263  catch(Poptions::required_option & e)
264  {
265  return printCommandLineError(e.what());
266  }
267  catch(Poptions::error & e)
268  {
269  return printCommandLineError(e.what());
270  }
271  return exportToDot(fileName, output);
272 }
273 
274 
const size_t SUCCESS
Definition: CmdInterface.hh:34
StateMachine< AutStateId, AutEventId > AutFsm
Definition: AutFile.hh:45
StatePropertyManager< DdAutStateLabel, DdAutStateId > DdAutStateManager
Definition: DdAutFile.hh:63
virtual bool readStream(std::istream &stream, std::string &error)
const string briefcomment(": this program exports any input as a dot file (output file or standard output).")
const size_t ERROR_UNHANDLED_EXCEPTION
Definition: CmdInterface.hh:35
size_t printCommandLineError(const string &msg)
Definition: CmdInterface.cc:98
bool faFromDescriptor(const DdAutFileDescriptor &descriptor, DdAutFA &fa, DdAutStateManager &sManager, DdAutEventManager &eManager)
size_t exportDdSyncToDot(const std::string &fileName, const std::string &output)
Definition: ToDot.cc:163
STL namespace.
vector< string > options(numberOfOptions)
bool match(const std::string &fileName, const std::string &suffix) const
Definition: CmdInterface.cc:54
size_t exportDdAutToDot(const std::string &fileName, const std::string &output)
Definition: ToDot.cc:113
ostream & toDot(ostream &os, const StateMachine &fsm, const PrintStateProperty &statePrinter, const PrintEventInfo &eventPrinter)
Definition: Io.hh:267
void initialiseOptions(int argc, char *argv[], Poptions::options_description &desc, Poptions::variables_map &vm)
Definition: ToDot.cc:51
FiniteAutomaton< DdAutStateId, DdAutEventId > DdAutFA
Definition: DdAutFile.hh:47
size_t exportToDot(const string &fileName, const string &output)
Definition: ToDot.cc:200
FileSuffixes suffixes({"aut", "ddaut", "ddsync", "dot"})
EventManager< DdAutEventLabel, DdAutEventId > DdAutEventManager
Definition: DdAutFile.hh:68
bool fromAutFile(std::istream &stream, AutFsm &fsm)
const string program("dd-dot")
size_t exportAutToDot(const std::string &fileName, const std::string &output)
Definition: ToDot.cc:75
boost::format Msg
Definition: Verbose.hh:42
int main(int argc, char **argv)
Definition: ToDot.cc:242
virtual bool readStream(std::istream &stream)
void printUsage(const po::options_description &desc)