DiaDes  0.1
DIAgnosis of Discrete-Event System
FaultGenerate.cc
Go to the documentation of this file.
1 
22 #include<iostream>
23 #include<sstream>
24 #include<fstream>
25 #include<string>
26 #include<map>
27 #include<list>
28 #include <cmath>
29 #include<utils/Random.hh>
30 #include<utils/Verbose.hh>
31 #include<utils/Log.hh>
32 #include<utils/CmdInterface.hh>
34 
35 
36 using namespace std;
37 using namespace Diades::Utils;
38 using namespace Diades::Automata;
39 
40 
41 typedef enum { FAULTMAX=0, SEED=1, HELP=2 } Option;
42 typedef enum { DESCOMP=0 } FileExtension;
43 
44 unsigned numberOfOptions = 3;
45 unsigned numberOfFileExtensions = 1;
46 
48 vector<string> options(numberOfOptions);
49 vector<bool> isSet(numberOfOptions,false);
50 
51 
52 string description="Usage: dd-fault-generate\n\t --help |\n\t [--seed randomSeed] [--faultMax nbFaultMax] [file1.des_comp ...] \n\n\t Generate randomly a set of fault events out of the models. If no model file is\n\t provided as argument, read the input standard. The set of chosen faults\n\t is printed on the standard output.";
53 
54 
56 {
57  options[FAULTMAX] = "--faultMax";
58  options[SEED] = "--seed";
59  options[HELP] = "--help";
60  fileExtensions[DESCOMP] = "des_comp";
61 }
62 
63 /**********************************************************************************/
64 
77 int main(int argc, char ** argv)
78 {
80  int index = 1;
81  unsigned faultMax = 0;
82  unsigned seed = Diades::Utils::initialiseSeed();
83  list<string> modelFiles;
84  while(index < argc)
85  {
86  Option currentOption;
87  if(getOption<Option>(argv[index],options,currentOption))
88  {
89  if(isSet[currentOption])
90  {
91  printError("The option '" + options[currentOption] + "' occurs at least twice.");
92  }
93  else
94  {
95  isSet[currentOption] = true;
96  }
97  switch(currentOption)
98  {
99  case HELP:
100  {
101  if((index!=1) || (argc !=2))
102  {
103  printError("Incorrect use of option --help.");
104  }
105  else
106  {
108  }
109  break;
110  }
111  case FAULTMAX:
112  {
113  ++index;
114  getParameter<unsigned>(argc,argv,index,faultMax);
115  break;
116  }
117  case SEED:
118  {
119  ++index;
120  getParameter<unsigned>(argc, argv, index, seed);
121  break;
122  }
123  default:
124  {
126  break;
127  }
128  }
129  }
130  else
131  {
132 
133  vector<string>::const_iterator it =
134  getFileExtension(string(argv[index]),fileExtensions.begin(),fileExtensions.end());
135  if(it == fileExtensions.end())
136  {
137  printError("Unrecognized file extension in file name: " + string(argv[index]));
138  }
139  FileExtension extension = DESCOMP;
140  switch(extension)
141  {
142  case DESCOMP:
143  {
144  modelFiles.push_back(string(argv[index]));
145  break;
146  }
147  }
148  ++index;
149  }
150  }
151  vector<Event> unobservables;
152  if(modelFiles.empty())
153  {
154  try
155  {
156  while(cin)
157  {
158  ObservableComponent component;
159  component.importDesCompModel(cin);
160  for(ObservableComponent::EventIterator it = component.eventBegin();
161  it != component.eventEnd();
162  ++it)
163  {
164  if(component.mask().isUnobservable(*it))
165  {
166  unobservables.push_back(*it);
167  }
168  }
169  }
170  }
171  catch(exception & e)
172  {
173  // nothing to do, just stop.
174  }
175  }
176  else
177  {
178  for(const string & filename : modelFiles)
179  {
180 
181  ObservableComponent component;
182  ifstream file(filename.c_str());
183  component.importDesCompModel(file);
184  file.close();
185  for(ObservableComponent::EventIterator it = component.eventBegin();
186  it != component.eventEnd();
187  ++it)
188  {
189  if(component.mask().isUnobservable(*it))
190  {
191  unobservables.push_back(*it);
192  }
193  }
194  }
195  }
197  long faultNb = 0;
198  if(faultMax==0)
199  {
200  faultNb = Diades::Utils::generateRandomValue(0,unobservables.size());
201  }
202  else
203  {
204  if(faultMax < unobservables.size())
205  {
206  faultNb = Diades::Utils::generateRandomValue(0,faultMax);
207  }
208  else
209  {
210  faultNb = Diades::Utils::generateRandomValue(0,unobservables.size());
211  }
212  }
213  set<unsigned> indexes;
214  Diades::Utils::selectNValues(0,unobservables.size()-1,faultNb,indexes);
215  for(const unsigned & index : indexes)
216  {
217  cout << unobservables[index] << " ";
218  }
219 }
220 
string description
InputIterator getFileExtension(const std::basic_string< CharType, CharTraits > &fileName, InputIterator begin, InputIterator end)
Definition: StringTools.hh:57
unsigned initialiseSeed()
unsigned numberOfOptions
vector< bool > isSet(numberOfOptions, false)
FileExtension
STL namespace.
FileExtension
Definition: abstract.cc:33
void initialiseOptions()
void selectNValues(unsigned int min, unsigned int max, unsigned int n, set< unsigned > &result)
void initialiseRandomGenerator()
An observable Component defined as a automaton.
virtual bool importDesCompModel(const string &filename)
Random generation utilities.
Option
Definition: abstract.cc:32
unsigned numberOfFileExtensions
Option
EventIterator eventBegin() const
Definition: Component.hh:328
const ObservableMask & mask() const
vector< string > options(numberOfOptions)
void printError(string parameter, string message)
int main(int argc, char **argv)
EventIterator eventEnd() const
Definition: Component.hh:338
bool isUnobservable(const Event &e) const
Logging facilities for the Diades projects.
long generateRandomValue(long lower, long upper)
vector< string > fileExtensions(numberOfFileExtensions)
void printUsage(const po::options_description &desc)