DiaDes  0.1
DIAgnosis of Discrete-Event System
AutomataInterface.cc
Go to the documentation of this file.
1 
8 #include<fstream>
11 #include "AutomataInterface.hh"
12 
13 
14 
15 namespace Diades
16 {
17  namespace CmdInterface
18  {
20 
21 
22 
23 
32  size_t loadFiniteStateMachine(const std::string & input, const Diades::Automata::Experimental::ManagedDdAutFsm & mfsm)
33  {
35  ifstream file(input.c_str());
36  if(file.is_open())
37  {
38  auto loaded = descriptor.readStream(file);
39  file.close();
40  if(!loaded)
41  {
42  return printCommandLineError(Msg("Error when loading the 'ddaut' automaton file %1%") % input);
43  }
44  }
45  else
46  {
47  return printCommandLineError(Msg("Error when opening the 'ddaut' automaton file %1%") % input);
48  }
49  auto result = fsmFromDescriptor(descriptor, mfsm.fsm, mfsm.sMgr, mfsm.eMgr);
50  if(!result)
51  {
52  return printCommandLineError(Msg("Error when converting the DdAutFileDescriptor from %1% to a Finite Automaton") % input);
53  }
54  return SUCCESS;
55  }
56 
65  size_t loadFiniteStateAutomaton(const std::string & input,
67  {
69  ifstream file(input.c_str());
70  if(file.is_open())
71  {
72  auto loaded = descriptor.readStream(file);
73  file.close();
74  if(!loaded)
75  {
76  return printCommandLineError(Msg("Error when loading the 'ddaut' automaton file %1%") % input);
77  }
78  }
79  else
80  {
81  return printCommandLineError(Msg("Error when opening the 'ddaut' automaton file %1%") % input);
82  }
83  auto result = faFromDescriptor(descriptor, mfa.fa, mfa.sMgr, mfa.eMgr);
84  if(!result)
85  {
86  return printCommandLineError(Msg("Error when converting the DdAutFileDescriptor from %1% to a Finite Automaton") % input);
87  }
88  return SUCCESS;
89  }
90 
91 
92 
93 
101  size_t
102  writeAut(const Diades::Automata::Experimental::AutFsm & fsm, const std::string & output,
103  const FileSuffixes & suffixes)
104  {
105  std::ofstream out;
106  if(!output.empty())
107  {
108 
109  if(suffixes.match(output, "aut"))
110  {
111  out.open(output);
112  if(!out.is_open())
113  {
114  return printCommandLineError(Msg("Cannot open and write in the 'aut' file %1%") % output);
115  }
116  else
117  {
118  if(!toAutFile(out, fsm, true))
119  {
120  return printCommandLineError(Msg("Error when dumping the deterministic automaton in the 'aut' file %1%") % output);
121  }
122  out.close();
123  }
124  }
125  else
126  {
127 
128  if(suffixes.match(output, "ddaut"))
129  {
130  out.open(output);
131  if(!out.is_open())
132  {
133  return printCommandLineError(Msg("Cannot open and write in the 'ddaut' file %1%") % output);
134  }
135  else
136  {
137  if(!toAutFile(out, fsm, true))
138  {
139  return printCommandLineError(Msg("Error when dumping the deterministic automaton in the 'ddaut' file %1%") % output);
140  }
141  out.close();
142  }
143  }
144  else
145  {
146  return printCommandLineError(Msg("Expecting an output file with a name ending with '.aut' or '.ddaut', but I read %1%") % output);
147  }
148  }
149  }
150  else
151  {
152  if(!toAutFile(std::cout, fsm, true))
153  {
154  return printCommandLineError("Error when dumping the deterministic automaton in 'aut' format ");
155  }
156  }
157  return SUCCESS;
158  }
159 
160 
168  size_t
170  /*
171 
172  const Diades::Automata::Experimental::DdAutFsm & fsm, const Diades::Automata::Experimental::DdAutStateManager & sManager,
173  const Diades::Automata::Experimental::DdAutEventManager & eManager, */
174  const std::string & output,
175  const FileSuffixes & suffixes)
176  {
177  std::ofstream out;
178  if(!output.empty())
179  {
180 
181  if(suffixes.match(output, "aut"))
182  {
183  out.open(output);
184  if(!out.is_open())
185  {
186  return printCommandLineError(Msg("Cannot open and write in the 'aut' file %1%") % output);
187  }
188  else
189  {
190  if(!fsmToAutFile(out, mfsm.fsm, mfsm.eMgr, true))
191  {
192  return printCommandLineError(Msg("Error when dumping the deterministic automaton in the 'aut' file %1%") % output);
193  }
194  out.close();
195  }
196  }
197  else
198  {
199 
200  if(suffixes.match(output, "ddaut"))
201  {
202  out.open(output);
203  if(!out.is_open())
204  {
205  return printCommandLineError(Msg("Cannot open and write in the 'ddaut' file %1%") % output);
206  }
207  else
208  {
209  if(!fsmToDdAutFile(out, mfsm))
210  {
211  return printCommandLineError(Msg("Error when dumping the deterministic automaton in the 'ddaut' file %1%") % output);
212  }
213  out.close();
214  }
215  }
216  else
217  {
218  return printCommandLineError(Msg("Expecting an output file with a name ending with '.aut' or '.ddaut', but I read %1%") % output);
219  }
220  }
221  }
222  else
223  {
224  if(!fsmToDdAutFile(std::cout, mfsm))
225  {
226  return printCommandLineError("Error when dumping the deterministic automaton in 'ddaut' format ");
227  }
228  }
229  return SUCCESS;
230  }
231 
239  size_t
241  const std::string & output,
242  const FileSuffixes & suffixes)
243  {
244  std::ofstream out;
245  if(!output.empty())
246  {
247 
248  if(suffixes.match(output, "aut"))
249  {
250  out.open(output);
251  if(!out.is_open())
252  {
253  return printCommandLineError(Msg("Cannot open and write in the 'aut' file %1%") % output);
254  }
255  else
256  {
257  if(!fsmToAutFile(out, mfa.fa, mfa.eMgr, true))
258  {
259  return printCommandLineError(Msg("Error when dumping the deterministic automaton in the 'aut' file %1%") % output);
260  }
261  out.close();
262  }
263  }
264  else
265  {
266 
267  if(suffixes.match(output, "ddaut"))
268  {
269  out.open(output);
270  if(!out.is_open())
271  {
272  return printCommandLineError(Msg("Cannot open and write in the 'ddaut' file %1%") % output);
273  }
274  else
275  {
276  if(!faToDdAutFile(out, mfa))
277  {
278  return printCommandLineError(Msg("Error when dumping the deterministic automaton in the 'ddaut' file %1%") % output);
279  }
280  out.close();
281  }
282  }
283  else
284  {
285  return printCommandLineError(Msg("Expecting an output file with a name ending with '.aut' or '.ddaut', but I read %1%") % output);
286  }
287  }
288  }
289  else
290  {
291  if(!faToDdAutFile(std::cout, mfa))
292  {
293  return printCommandLineError("Error when dumping the deterministic automaton in 'ddaut' format ");
294  }
295  }
296  return SUCCESS;
297  }
298 
299 
300 
301 
312  size_t
313  checkFileNameSuffix(const std::string & filename, const std::string & suffix)
314  {
315  if(suffix.find('.') != std::string::npos)
316  {
317  return printCommandLineError(Msg("Checking a filename for a wrong suffix that contains at least a '.' character: %1%") % filename);
318  }
319  // note that I do not use FileSuffixes as FileSuffixes does not handle filename with multiple dots
320  auto lastDot = filename.find_last_of(".");
321  if(lastDot == std::string::npos)
322  {
323  return printCommandLineError(Msg("Expecting a file with a name ending with '.%2%', but I read %1%") % filename % suffix);
324  }
325  auto startSuffix = filename.find(suffix,lastDot);
326  if(startSuffix == std::string::npos)
327  {
328  return printCommandLineError(Msg("Expecting a file with a name ending with '.%2%'', but I read %1%") % filename % suffix);
329  }
330  if((filename.length() - startSuffix) != suffix.length())
331  {
332  return printCommandLineError(Msg("Expecting a file with a name ending with '.%2%', but I read %1%") % filename % suffix);
333  }
334  return SUCCESS;
335  }
336 
337 
347  size_t
348  checkDdAutFileName(const std::string & filename)
349  {
350  return checkFileNameSuffix(filename,"ddaut");
351  }
352 
362  size_t isOpenableFile(const std::string & filename)
363  {
364 
365  std::ifstream file(filename.c_str());
366  if(!file.is_open())
367  {
368  return printCommandLineError(Msg("The file %1% cannot be opened.") % filename);
369  }
370  file.close();
371  return SUCCESS;
372  }
373 
374 
385  size_t
386  checkDdAutFile(const std::string & filename)
387  {
388  auto result = checkDdAutFileName(filename);
389  if(result == SUCCESS)
390  {
391  return isOpenableFile(filename);
392  }
393  return result;
394  }
395 
396 
397 
398 
409  size_t
410  checkAutFile(const std::string & filename)
411  {
412  auto result = checkAutFileName(filename);
413  if(result == SUCCESS)
414  {
415  return isOpenableFile(filename);
416  }
417  return result;
418  }
419 
420 
421 
422 
423 
432  size_t
433  checkAutFileName(const std::string & filename)
434  {
435  return checkFileNameSuffix(filename,"aut");
436  }
437 
438 
439 
440  }
441 }
size_t writeAut(const Diades::Automata::Experimental::AutFsm &fsm, const std::string &output, const FileSuffixes &suffixes)
const size_t SUCCESS
Definition: CmdInterface.hh:34
size_t checkDdAutFileName(const std::string &filename)
check that filename is a proper file with the ddaut suffix
size_t writeFiniteAutomaton(const Diades::Automata::Experimental::ConstManagedDdAutFA &mfa, const std::string &output, const FileSuffixes &suffixes)
bool fsmFromDescriptor(const DdAutFileDescriptor &descriptor, DdAutFsm &fsm, DdAutStateManager &sManager, DdAutEventManager &eManager)
Diades::Utils::Msg Msg
size_t printCommandLineError(const string &msg)
Definition: CmdInterface.cc:98
bool faFromDescriptor(const DdAutFileDescriptor &descriptor, DdAutFA &fa, DdAutStateManager &sManager, DdAutEventManager &eManager)
size_t loadFiniteStateAutomaton(const std::string &input, const Diades::Automata::Experimental::ManagedDdAutFA &mfa)
Load the finite automaton from a file called &#39;input&#39;.
size_t isOpenableFile(const std::string &filename)
check that the file can be opened
bool fsmToAutFile(ostream &stream, const DdAutFsm &fsm, const DdAutEventManager &eManager, bool enforce=false)
data structure that gathers an FA, a StateManager and a EventManager
Definition: DdAutFile.hh:115
data structure that gathers an Fsm, a StateManager and a EventManager
Definition: DdAutFile.hh:95
data structure that gathers an FA, a StateManager and a EventManager
Definition: DdAutFile.hh:135
bool faToDdAutFile(ostream &stream, const ConstManagedDdAutFA &mFa)
size_t checkFileNameSuffix(const std::string &filename, const std::string &suffix)
check that filename has the expected suffix
bool match(const std::string &fileName, const std::string &suffix) const
Definition: CmdInterface.cc:54
size_t checkDdAutFile(const std::string &filename)
check that filename is a proper file with the ddaut suffix check that the file &#39;filename&#39; can be ope...
size_t checkAutFile(const std::string &filename)
check that filename is a proper file with the aut suffix
size_t checkAutFileName(const std::string &filename)
check that filename is a proper filename with the aut suffix
Namespace of the Diades project.
bool fsmToDdAutFile(ostream &stream, const ConstManagedDdAutFsm &mFsm)
data structure that gathers an Fsm, a StateManager and a EventManager
Definition: DdAutFile.hh:76
bool toAutFile(ostream &stream, const AutFsm &fsm, bool enforce=false)
size_t writeFiniteStateMachine(const Diades::Automata::Experimental::ConstManagedDdAutFsm &mfsm, const std::string &output, const FileSuffixes &suffixes)
FileSuffixes suffixes({"aut","ddaut"})
boost::format Msg
Definition: Verbose.hh:42
size_t loadFiniteStateMachine(const std::string &input, const Diades::Automata::Experimental::ManagedDdAutFsm &mfsm)
Load the fsm from a file called &#39;input&#39;.
virtual bool readStream(std::istream &stream)