DiaDes  0.1
DIAgnosis of Discrete-Event System
Simulate.cc
Go to the documentation of this file.
1 
38 #include<thread>
39 #include<cstdlib>
40 #include<iostream>
41 #include<fstream>
42 #include<regex>
43 #include<unordered_map>
44 #include<random>
45 #include<chrono>
46 #include<boost/program_options.hpp>
51 #include"../AutomataInterface.hh"
52 #include"../CmdInterface.hh"
53 
54 using namespace std;
55 using namespace Diades::Automata::Experimental;
56 using namespace Diades::Utils;
57 using namespace Diades::CmdInterface;
58 
59 namespace Poptions = boost::program_options;
60 
61 
62 
67 const string program("dd-sim");
68 const string briefcomment(": dd-sim simulates a model. Given a ddaut file, dd-sim computes some runs of the model");
69 const string detailedcomment=R"(
70 dd-sim simulates a model. Given a ddaut file, dd-sim computes some runs of the model
71 
72 Example 1:
73 
74 
75 dd-sim file.ddaut --seed=30 --size=100 --through='.*o\.p=ko.*'
76  --format='@s -> @t [@e]' --format='@o' --timer=10
77  --observable=observable.txt
78  --controllable=controllable.txt -o result.run
79 
80 
81 In Example 1. dd-sim computes a random run (seed of the random generator is 30), the size
82 of the run is 100, and the run must go through a state with a label containing 'o.p=ko'.
83 The run must be found within 10 seconds, if not the simulator stops.
84 The resulting run will be written in the file result1.run following the format:
85 
86 @s -> @t [@e]
87 
88 That is 'sourcestate -> targetstate [event]':
89  - @s stands for source state,
90  - @t stands for target state,
91  - @e stands for event.
92 
93 Other possible tags are:
94  - @o stands for observable event,
95  - @c stands for controllable event.
96 
97 @o and @c are respectively defined in observable.txt and controllable.txt. For a given
98 event @e, the observable parts are the substring of @e that are in observable.txt,
99 the controllable parts are the substring of @e that are in controllable.txt.
100 
101 A second file result2.run contains the sequence of observable events of the same run
102 following the format
103 
104 @o
105 
106 )";
107 
108 /*
109  * File suffixes
110  */
111 FileSuffixes suffixes({"ddaut"});
112 
120 void
121 initialiseOptions(int argc, char * argv[],
122  Poptions::options_description & desc,
123  Poptions::variables_map & vm)
124 {
125  desc.add_options()
126  ("help,h", "produce help message")
127  ("file,f", Poptions::value< string >(), "file that contains the model to simulate (.ddaut format)")
128  ("output,o", Poptions::value< string >(),
129  R"(template name for the generated output files:
130 Suppose the template name is 'output.run' then, depending on the number of requested simulations,
131 the generated files will named as follows: output_1.run, output_2.run, ...)")
132  ("seed",Poptions::value< unsigned int >(),
133  R"(seed used by the random generator: if no seed is given, take as seed the starting time of the process.)")
134  ("size", Poptions::value< unsigned int >(),
135  R"(size is the size (number of transitions) of the run to simulate. If the simulator cannot find
136 such a requested run, it will simplify give up. (default value is 100))")
137  ("through", Poptions::value< string >(),
138  R"(This parameter requests a regular expression as defined at the following address:
139 
140 http://www.cplusplus.com/reference/regex/ECMAScript/
141 
142 This parameter states that the simulator must look for a run that must go at least once through one state
143 whose label matches the given regular expression.)")
144  ("format", Poptions::value< std::vector<string> >(),
145  R"(format of the result. The expression given in the format will be used as a template to write the simulation
146 in the output file. @s stands for source state, @t for target state, @e for transition event, @o observable part of
147 the event, @c controllable part of the event. Many formats are allowed, one format generates one output file.)")
148  ("timer", Poptions::value< unsigned int >(),
149  R"(simulation timer. Does not allow the simulator to look for one run after the time set (in seconds)
150 by the timer is reached. If no timer is set, the program may not end if no run is found.)")
151  ("observable", Poptions::value< string >(),
152  R"(set the file that defines the observable events of the model. An event of the model is considered as observable
153 if at least one of its substring matches an event contained in the given file. If the option observable is not set,
154 no event of the model is considered as observable (@o will keep return the empty string).)")
155  ("controllable", Poptions::value< string >(),
156  R"(set the file that defines the controllable events of the model. An event of the model is considered as controllable
157 if at least one of its substring matches an event contained in the given file. If the option controlable is not set,
158 no event of the model is considered as controlable (@o will keep return the empty string).)")
159  ;
160  Poptions::positional_options_description p;
161  // 1 that The command line can accept 1 file name as positional option
162  p.add("file", 1);
163  Poptions::store(Poptions::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
164  Poptions::notify(vm);
165 }
166 
167 
173 using namespace Diades::CmdInterface;
174 
176 {
177  unsigned int seed;
178  unsigned int size = 100;
179  unsigned int timer = 0;
180  std::string fileName;
181  std::string output;
182  std::string observable;
183  std::string controllable;
184  std::string through;
185  std::vector<std::string> formats;
186 };
187 
188 
189 size_t setOptions(int argc, char** argv, DdSimOptions & options)
190 {
191  try
192  {
193 
194  Poptions::options_description desc("Options");
195  Poptions::variables_map vm;
196  initialiseOptions(argc, argv, desc, vm);
197  if(vm.count("help"))
198  {
200  }
201  if(vm.count("file"))
202  {
203  options.fileName = vm["file"].as<std::string> ();
204  }
205  if(vm.count("observable"))
206  {
207  options.observable = vm["observable"].as<std::string> ();
208  }
209  if(vm.count("controllable"))
210  {
211  options.controllable = vm["controllable"].as<std::string> ();
212  }
213  if(vm.count("output"))
214  {
215  options.output = vm["output"].as<std::string>();
216  }
217  if(vm.count("seed"))
218  {
219  options.seed = vm["seed"].as<unsigned int>();
220  }
221  if(vm.count("size"))
222  {
223  options.size = vm["size"].as<unsigned int>();
224  }
225  if(vm.count("timer"))
226  {
227  options.timer = vm["timer"].as<unsigned int>();
228  }
229  if(vm.count("through"))
230  {
231  options.through = vm["through"].as<std::string>();
232  }
233  if(vm.count("format"))
234  {
235  options.formats = vm["format"].as<std::vector<std::string>>();
236  }
237  }
238  catch(Poptions::required_option & e)
239  {
240  return printCommandLineError(e.what());
241  }
242  catch(Poptions::error & e)
243  {
244  return printCommandLineError(e.what());
245  }
246  return SUCCESS;
247 }
248 
264  State state,
265  const std::regex & regularExpression,
266  CstStMap<char> & throughStates)
267 {
268  if(throughStates[state] == 1) { return true; }
269  if(throughStates[state] == 2) { return false; }
270  bool result = false;
271  if(std::regex_match(mfsm.sMgr.getStateProperty(mfsm.fsm.getStatePropertyId(state)),regularExpression))
272  {
273  throughStates[state] = 1;
274  result = true;
275  }
276  else
277  {
278  throughStates[state] = 2;
279  }
280  return result;
281 }
282 
284 {
285  DdAutFsm fsm;
286  DdAutStateManager sManager;
287  DdAutEventManager eManager;
288  auto result = loadFiniteStateMachine(options.fileName,{fsm,sManager,eManager});
289  StMap<char> throughStates(fsm.behaviour(),fsm.numberOfStates(),0);
290  return SUCCESS;
291 }
292 
293 
294 
295 
296 
297 
304 int
305 main(int argc, char** argv)
306 {
307  auto now = std::chrono::system_clock::now(); // now is a system_clock::time_point
309  options.seed = std::chrono::system_clock::to_time_t(now); // convert to time_t to initialise the seed
310  setOptions(argc, argv, options);
311  auto result = checkDdAutFile(options.fileName);
312  if(result!=SUCCESS) { return result; }
313  if(!options.observable.empty())
314  {
315  result = isOpenableFile(options.observable);
316  if(result!=SUCCESS) { return result; }
317  }
318  if(!options.controllable.empty())
319  {
320  result = isOpenableFile(options.controllable);
321  if(result!=SUCCESS) { return result; }
322  }
323 
324  return prepareSimulation(options);
325  }
326 
327 
std::string controllable
Definition: Simulate.cc:183
const size_t SUCCESS
Definition: CmdInterface.hh:34
Diades::Graph::ConstNodeMap< Type > CstStMap
StatePropertyManager< DdAutStateLabel, DdAutStateId > DdAutStateManager
Definition: DdAutFile.hh:63
size_t printCommandLineError(const string &msg)
Definition: CmdInterface.cc:98
std::string observable
Definition: Simulate.cc:182
size_t isOpenableFile(const std::string &filename)
check that the file can be opened
const string program("dd-sim")
STL namespace.
struct itimerval timer
data structure that gathers an Fsm, a StateManager and a EventManager
Definition: DdAutFile.hh:95
std::string fileName
Definition: Simulate.cc:180
unsigned int timer
Definition: Simulate.cc:179
FileSuffixes suffixes({"ddaut"})
unsigned int size
Definition: Simulate.cc:178
size_t prepareSimulation(const DdSimOptions &options)
Definition: Simulate.cc:283
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...
AutFsm::State State
Definition: Run.cc:72
std::string output
Definition: Simulate.cc:181
void initialiseOptions()
Definition: Simulate.cc:77
FiniteAutomaton< DdAutStateId, DdAutEventId > DdAutFA
Definition: DdAutFile.hh:47
vector< string > options(numberOfOptions)
std::vector< std::string > formats
Definition: Simulate.cc:185
int main(int argc, char **argv)
Definition: Simulate.cc:692
bool isThroughState(const ConstManagedDdAutFsm &mfsm, State state, const std::regex &regularExpression, CstStMap< char > &throughStates)
check whether the state is matching the &#39;through-state&#39; expression given by –through ...
Definition: Simulate.cc:263
std::string through
Definition: Simulate.cc:184
EventManager< DdAutEventLabel, DdAutEventId > DdAutEventManager
Definition: DdAutFile.hh:68
const string briefcomment(": dd-sim simulates a model. Given a ddaut file, dd-sim computes some runs of the model")
Diades::Graph::NodeMap< Type > StMap
StateMachine< DdAutStateId, DdAutEventId > DdAutFsm
Definition: DdAutFile.hh:42
size_t setOptions(int argc, char **argv, DdSimOptions &options)
Definition: Simulate.cc:189
const string detailedcomment
Definition: Simulate.cc:69
const StatePropertyId & getStatePropertyId(State state) const
const StateProperty & getStateProperty(StatePropertyId id) const
unsigned int seed
Definition: Simulate.cc:177
size_t loadFiniteStateMachine(const std::string &input, const Diades::Automata::Experimental::ManagedDdAutFsm &mfsm)
Load the fsm from a file called &#39;input&#39;.
void printUsage(const po::options_description &desc)