DiaDes  0.1
DIAgnosis of Discrete-Event System
TsToDdAut.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 
20 #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-ts2ddaut");
37 const string briefcomment(": this program converts a '.ts' altarica format file to a '.ddaut' file (output file or standard output).");
38 /*
39  * File suffixes
40  */
41 FileSuffixes suffixes({"ts", "ddaut"});
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 (.ts format)")
58  ("output,o", Poptions::value< string >(), "outfile name (.ddaut format)")
59  ("dict,d", Poptions::value< string >(), "conversion into more human-friendly manner associated with a dictionary")
60  ("mealy,m", "full conversion as a Mealy machine (target state content in the event)")
61  ("update,u", "conversion as a Mealy machine (the event contains state updates)")
62  ("components,c", "write ddaut components (globally consistent behaviours) extracted from the ts file if the ts file describes a synchronised system")
63  ;
64  Poptions::positional_options_description p;
65  // 1 that The command line can accept 1 file name as positional option
66  p.add("file", 1);
67  Poptions::store(Poptions::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
68  Poptions::notify(vm);
69 }
70 
71 
76 
77 
79 {
80  for(size_t i = 1; i < fsms.size(); ++i)
81  {
82  TsFsmPtr fsm;
83  TsEvtMgrPtr eManager;
84  TsStMgrPtr sManager;
85  std::tie(fsm,sManager,eManager) = fsms.at(i);
86  if(fsm->name().empty())
87  {
88  fsm->setName("_global_system");
89  }
90  std::string fileName = fsm->name() + ".ddaut";
91  ofstream out(fileName.c_str());
92  if(out.is_open())
93  {
94  auto saved = fsmToDdAutFile(out,{*fsm,*sManager,*eManager});
95  out.close();
96  if(!saved)
97  {
98  return printCommandLineError(Msg("Error when saving the component 'ddaut' automaton file %1%") % fileName);
99  }
100  }
101  else
102  {
103  return printCommandLineError(Msg("Error when opening the component 'ddaut' automaton file %1%") % fileName);
104  }
105  }
106  return SUCCESS;
107 }
108 
109 
110 size_t
111 exportTsToDdAut2(const std::string & fileName, const std::string & output, const std::string & dict,
112  TsConversionMode mode, bool writeComponents)
113 {
114  ManagedTsFsms fsms;
115  ifstream file(fileName.c_str());
116  if(file.is_open())
117  {
118  auto loaded = fromTsFile(file,fsms,mode);
119  file.close();
120  if(!loaded)
121  {
122  return printCommandLineError(Msg("Error when loading the 'ts' automaton file %1%") % fileName);
123  }
124  }
125  else
126  {
127  return printCommandLineError(Msg("Error when opening the 'ts' automaton file %1%") % fileName);
128  }
129  TsFsmPtr fsm;
130  TsEvtMgrPtr eManager;
131  TsStMgrPtr sManager;
132  std::tie(fsm,sManager,eManager) = fsms.at(0);
133 
134 
135  DdAutStateManager sPrettyManager;
136  DdAutEventManager ePrettyManager;
137  if(!dict.empty())
138  {
139  for(const auto & stateProperty : sManager->storage())
140  {
141  std::stringstream stream;
142  stream << sManager->statePropertyId(stateProperty);
143  DdAutStateManager::Info idString;
144  stream >> idString;
145  sPrettyManager.setStateProperty(idString, sManager->statePropertyId(stateProperty));
146  }
147 
148  for(const auto & event : eManager->storage())
149  {
150  ePrettyManager.setEvent(tsPrettyEvent(event), eManager->eventId(event));
151  }
152  ofstream out(dict.c_str());
153  if(out.is_open())
154  {
155  out << "state\n";
156  for(const auto & stateProperty : sManager->storage())
157  {
158  out << sPrettyManager.getStateProperty( sManager->statePropertyId(stateProperty))
159  << " "
160  << stateProperty
161  << "\n";
162  }
163  out << "event\n";
164  for(const auto & eventProperty : eManager->storage())
165  {
166  out << ePrettyManager.getEvent(eManager->eventId(eventProperty))
167  << " "
168  << eventProperty
169  << "\n";
170  }
171  out.close();
172  }
173  if(output.empty())
174  {
175  auto saved = fsmToDdAutFile(std::cout,{*fsm,sPrettyManager,ePrettyManager});
176  if(!saved)
177  {
178  return printCommandLineError(Msg("Error when converting the 'ddaut' automaton file"));
179  }
180  }
181  else
182  {
183  ofstream out2(output.c_str());
184  if(out2.is_open())
185  {
186  auto saved = fsmToDdAutFile(out2,{*fsm,sPrettyManager,ePrettyManager});
187  out2.close();
188  if(!saved)
189  {
190  return printCommandLineError(Msg("Error when saving the 'ddaut' automaton file %1%") % fileName);
191  }
192  }
193  else
194  {
195  return printCommandLineError(Msg("Error when opening the 'ddaut' automaton file %1%") % fileName);
196  }
197  }
198  }
199  else
200  {
201  if(output.empty())
202  {
203  auto saved = fsmToDdAutFile(std::cout,{*fsm,*sManager,*eManager});
204  if(!saved)
205  {
206  return printCommandLineError(Msg("Error when converting the 'ddaut' automaton file"));
207  }
208  }
209  else
210  {
211  ofstream out(output.c_str());
212  if(out.is_open())
213  {
214  auto saved = fsmToDdAutFile(out,{*fsm,*sManager,*eManager});
215  out.close();
216  if(!saved)
217  {
218  return printCommandLineError(Msg("Error when saving the 'ddaut' automaton file %1%") % fileName);
219  }
220  }
221  else
222  {
223  return printCommandLineError(Msg("Error when opening the 'ddaut' automaton file %1%") % fileName);
224  }
225  }
226  }
227  if(writeComponents)
228  {
229  writeDdAutComponents(fsms);
230  }
232 }
233 
234 
235 size_t
236 exportTsToDdAut(const string & fileName, const string & output, const string & dict, TsConversionMode mode,
237  bool writeComponents)
238 {
239 
240  if(!output.empty())
241  {
242  if(!suffixes.match(output, "ddaut"))
243  {
244  return printCommandLineError(Msg("Expecting an output file with a name ending with .ddaut, but I read %1%") % output);
245  }
246  }
247 
248  if(suffixes.match(fileName, "ts"))
249  {
250  return exportTsToDdAut2(fileName, output, dict, mode,writeComponents);
251  }
252  else
253  {
254  return printCommandLineError(Msg("Expecting a file with a name ending with .ts, but I read %1%") % fileName);
255  }
257 }
258 
265 int
266 main(int argc, char** argv)
267 {
268  std::string fileName, output, dict;
269  bool writeComponents =false;
271  try
272  {
273  Poptions::options_description desc("Options");
274  Poptions::variables_map vm;
275  initialiseOptions(argc, argv, desc, vm);
276  if(vm.count("help"))
277  {
278  return printUsage(program + briefcomment, desc);
279  }
280  if(vm.count("file"))
281  {
282  fileName = vm["file"].as<std::string> ();
283  }
284  if(vm.count("output"))
285  {
286  output = vm["output"].as<std::string>();
287  }
288  if(vm.count("dict"))
289  {
290  dict = vm["dict"].as<std::string>();
291  }
292  if(vm.count("components"))
293  {
294  writeComponents = true;
295  }
296 
297  if(vm.count("mealy") && !(vm.count("update")))
298  {
300  }
301  if(vm.count("update") && !(vm.count("mealy")))
302  {
304  }
305  if(vm.count("update") && vm.count("full"))
306  {
307  return printCommandLineError("Cannot convert in full mode and in update mode at the same time.");
308  }
309  }
310  catch(Poptions::required_option & e)
311  {
312  return printCommandLineError(e.what());
313  }
314  catch(Poptions::error & e)
315  {
316  return printCommandLineError(e.what());
317  }
318  return exportTsToDdAut(fileName, output, dict, mode, writeComponents);
319 }
320 
321 
size_t writeDdAutComponents(ManagedTsFsms &fsms)
Definition: TsToDdAut.cc:78
const size_t SUCCESS
Definition: CmdInterface.hh:34
StatePropertyManager< DdAutStateLabel, DdAutStateId > DdAutStateManager
Definition: DdAutFile.hh:63
const size_t ERROR_UNHANDLED_EXCEPTION
Definition: CmdInterface.hh:35
bool setStateProperty(const StateProperty &stateProperty, StatePropertyId id)
size_t printCommandLineError(const string &msg)
Definition: CmdInterface.cc:98
int main(int argc, char **argv)
Definition: TsToDdAut.cc:266
size_t exportTsToDdAut(const string &fileName, const string &output, const string &dict, TsConversionMode mode, bool writeComponents)
Definition: TsToDdAut.cc:236
STL namespace.
size_t exportTsToDdAut2(const std::string &fileName, const std::string &output, const std::string &dict, TsConversionMode mode, bool writeComponents)
Definition: TsToDdAut.cc:111
vector< string > options(numberOfOptions)
bool setEvent(const Event &event, EventId id)
Definition: Event.hh:156
const string program("dd-ts2ddaut")
bool match(const std::string &fileName, const std::string &suffix) const
Definition: CmdInterface.cc:54
bool fromTsFile(std::istream &stream, ManagedTsFsms &fsms, TsConversionMode mode)
Load a Ts file as a DdAutFsm.
const Event & getEvent(EventId id) const
Definition: Event.hh:183
bool fsmToDdAutFile(ostream &stream, const ConstManagedDdAutFsm &mFsm)
const string briefcomment(": this program converts a '.ts' altarica format file to a '.ddaut' file (output file or standard output).")
std::shared_ptr< TsStMgr > TsStMgrPtr
Definition: TsFile.hh:27
EventManager< DdAutEventLabel, DdAutEventId > DdAutEventManager
Definition: DdAutFile.hh:68
FileSuffixes suffixes({"ts", "ddaut"})
std::vector< ManagedTsFsm > ManagedTsFsms
Definition: TsFile.hh:30
std::shared_ptr< TsEvtMgr > TsEvtMgrPtr
Definition: TsFile.hh:28
StateMachine< DdAutStateId, DdAutEventId > DdAutFsm
Definition: DdAutFile.hh:42
boost::format Msg
Definition: Verbose.hh:42
const StateProperty & getStateProperty(StatePropertyId id) const
std::shared_ptr< TsFsm > TsFsmPtr
Definition: TsFile.hh:24
void initialiseOptions(int argc, char *argv[], Poptions::options_description &desc, Poptions::variables_map &vm)
Definition: TsToDdAut.cc:51
std::string tsPrettyEvent(const std::string &tsEvent)
void printUsage(const po::options_description &desc)