DiaDes  0.1
DIAgnosis of Discrete-Event System
HistoryCompare.cc
Go to the documentation of this file.
1 
16 #include<vector>
17 #include<string>
18 #include<fstream>
19 #include<utils/CmdInterface.hh>
20 #include<automata/History.hh>
21 
22 using namespace std;
23 using namespace Diades::Automata;
24 
25 typedef enum { HELP =0 } Option;
26 typedef enum { ARCHV = 0 } FileExtension;
27 unsigned numberOfOptions = 1;
29 
31 vector<string> options(numberOfOptions);
32 vector<bool> isSet(numberOfOptions,false);
33 
34 string description="Usage:\t history_compare --help\n\t history_compare hist1.archv hist2.archv\n\n\t Produce a set of data files with the evolution of performance indicators through the histories.";
35 
36 
38 {
39  options[HELP] = "--help";
40  fileExtensions[ARCHV] = "archv";
41 }
42 
43 void makeComparison(const History & history1,
44  const History & history2, const string & prefix);
45 
46 int main(int argc, char * argv[])
47 {
50  string histFile1;
51  string histFile2;
52  if(argc < 2)
53  {
54  printError("Wrong command line. Try option --help.");
55  }
56  int index = 1;
57  while(index < argc)
58  {
59  Option currentOption;
60  if(getOption<Option>(argv[index],options,currentOption))
61  {
62  if(isSet[currentOption])
63  {
64  printError("The option '" + options[currentOption] + "' occurs at least twice.");
65  }
66  else
67  {
68  isSet[currentOption] = true;
69  }
70  switch(currentOption)
71  {
72  case HELP:
73  {
74  if((index!=1) || (argc !=2))
75  {
76  printError("Incorrect use of option --help.");
77  }
78  else
79  {
81  }
82  break;
83  }
84  default:
85  {
86  printError("Unrecognized option: " + string(argv[index]));
87  break;
88  }
89  }
90  }
91  else
92  {
93  // then it should be the name of a file
94  vector<string>::const_iterator it =
95  getFileExtension(string(argv[index]),fileExtensions.begin(),fileExtensions.end());
96  if(it == fileExtensions.end())
97  {
98  printError("Unrecognized file extension in file name: " + string(argv[index]));
99  }
100  if(histFile1.empty())
101  {
102  histFile1 = string(argv[index]);
103  }
104  else
105  {
106  if(histFile2.empty())
107  {
108  histFile2 = string(argv[index]);
109  }
110  else
111  {
112  printError("Expecting two history files but get at least three.");
113  }
114  }
115  ++index;
116  }
117  }
118  if(histFile1.empty() || histFile2.empty())
119  {
120  printError("Cannot get two history file from the command line.");
121  }
122 
123  { verbose<VbOutput>("Loading the history %1%...") % histFile1; }
124  History history1;
125  {
126  std::ifstream ifs(histFile1);
127  boost::archive::text_iarchive ia(ifs);
128  ia >> history1;
129  }
130 
131  // cout << "HISTORY 1:" << endl;
132  // cout << history1 << endl;
133 
134 
135  { verbose<VbOutput>("Done.\n"); }
136  { verbose<VbOutput>("Loading the history %1%...") % histFile2; }
137  History history2;
138  {
139  std::ifstream ifs(histFile2);
140  boost::archive::text_iarchive ia(ifs);
141  ia >> history2;
142  }
143 
144  Diagnosis diag1 = history1.getDiagnosis(history1.lastTimePoint());
145  Diagnosis diag2 = history2.getDiagnosis(history2.lastTimePoint());
146 
147 
148  if( (history1.lastTimePoint()-history1.initialTimePoint()) > (history2.lastTimePoint()-history2.initialTimePoint()) )
149  {
150  history2.publishDiagnosis(history2.initialTimePoint() + (history1.lastTimePoint()-history1.initialTimePoint()),history2.getDiagnosis(history2.lastTimePoint()));
151  }
152  else
153  {
154  if ((history2.lastTimePoint()-history2.initialTimePoint()) > (history1.lastTimePoint()-history1.initialTimePoint()))
155  {
156  history1.publishDiagnosis(history1.initialTimePoint() + (history2.lastTimePoint()-history2.initialTimePoint()),history1.getDiagnosis(history1.lastTimePoint()));
157  }
158  }
159  // cout << "*********************************" << endl;
160 
161  // cout << "*********************************" << endl;
162  // cout << "HISTORY 2:" << endl;
163  // cout << history2 << endl;
164 
165  { verbose<VbOutput>("Done.\n"); }
166 
167  { verbose<VbOutput>("Comparison... History 1 (%1%) History 2 (%2%)\n") % histFile1 % histFile2 ; }
168  makeComparison(history1,history2,"h1h2");
169 
170  History perfect1;
171  perfect1.publishDiagnosis(perfect1.initialTimePoint(),diag1);
172  perfect1.publishDiagnosis(perfect1.initialTimePoint() + (history1.lastTimePoint()-history1.initialTimePoint()), diag1);
173  { verbose<VbOutput>("Comparison... History 1 Perfect\n"); }
174  makeComparison(history1,perfect1,"h1p");
175  { verbose<VbOutput>("Comparison... History 2 Perfect\n"); }
176 
177  History perfect2;
178  perfect2.publishDiagnosis(perfect2.initialTimePoint(),diag2);
179  perfect2.publishDiagnosis(perfect2.initialTimePoint() + (history2.lastTimePoint()-history2.initialTimePoint()), diag2);
180  makeComparison(history2,perfect2,"h2p");
181 
182 
183 }
184 
185 
186 
187 
188 
189 void makeComparison(const History & history1,
190  const History & history2, const string & prefix)
191 {
192  list< pair<ptime,int> > results;
193  ofstream file;
194  historyCompare(history1,history2,Distance(),results);
195  file.open(prefix + "Distance.data");
196  for(const pair<ptime,int> & point : results)
197  {
198  file << (point.first - history1.initialTimePoint()).total_seconds() << '\t' << point.second << endl;
199  }
200  file.close();
201 
202  results.clear();
203  historyCompare(history1,history2,FcDistance(),results);
204  file.open(prefix + "FcDistance.data");
205  for(const pair<ptime,int> & point : results)
206  {
207  file << (point.first - history1.initialTimePoint()).total_seconds() << '\t' << point.second << endl;
208  }
209  file.close();
210 
211  results.clear();
212  historyCompare(history1,history2,CommonFaults(),results);
213  file.open(prefix + "CommonFaults.data");
214  for(const pair<ptime,int> & point : results)
215  {
216  file << (point.first - history1.initialTimePoint()).total_seconds() << '\t' << point.second << endl;
217  }
218  file.close();
219 
220  results.clear();
221  historyCompare(history1,history2,NonCommonFaults(),results);
222  file.open(prefix + "NonCommonFaults.data");
223  for(const pair<ptime,int> & point : results)
224  {
225  file << (point.first - history1.initialTimePoint()).total_seconds() << '\t' << point.second << endl;
226  }
227  file.close();
228 
229  results.clear();
230  historyCompare(history1,history2,BsDistance(),results);
231  file.open(prefix + "BsDistance.data");
232  for(const pair<ptime,int> & point : results)
233  {
234  file << (point.first - history1.initialTimePoint()).total_seconds() << '\t' << point.second << endl;
235  }
236  file.close();
237 
238  list< pair<ptime,double> > results2;
239  historyCompare(history1,history2,Accuracy(),results2);
240  file.open(prefix + "Accuracy.data");
241  for(const pair<ptime,double> & point : results2)
242  {
243  file << (point.first - history1.initialTimePoint()).total_seconds() << '\t' << point.second << endl;
244  }
245  file.close();
246  results2.clear();
247 
248  historyCompare(history1,history2,BsAccuracy(),results2);
249  file.open(prefix + "BsAccuracy.data");
250  for(const pair<ptime,double> & point : results2)
251  {
252  file << (point.first - history1.initialTimePoint()).total_seconds() << '\t' << point.second << endl;
253  }
254  file.close();
255  results2.clear();
256 
257  historyCompare(history1,history2,FcAccuracy(),results2);
258  file.open(prefix + "FcAccuracy.data");
259  for(const pair<ptime,double> & point : results2)
260  {
261  file << (point.first - history1.initialTimePoint()).total_seconds() << '\t' << point.second << endl;
262  }
263  file.close();
264  results2.clear();
265 
266  historyCompare(history1,history2,Precision(),results2);
267  file.open(prefix + "Precision.data");
268  for(const pair<ptime,double> & point : results2)
269  {
270  file << (point.first - history1.initialTimePoint()).total_seconds() << '\t' << point.second << endl;
271  }
272  file.close();
273  results2.clear();
274 
275  historyCompare(history1,history2,BsPrecision(),results2);
276  file.open(prefix + "BsPrecision.data");
277  for(const pair<ptime,double> & point : results2)
278  {
279  file << (point.first - history1.initialTimePoint()).total_seconds() << '\t' << point.second << endl;
280  }
281  file.close();
282  results2.clear();
283 
284  historyCompare(history1,history2,FcPrecision(),results2);
285  file.open(prefix + "FcPrecision.data");
286  for(const pair<ptime,double> & point : results2)
287  {
288  file << (point.first - history1.initialTimePoint()).total_seconds() << '\t' << point.second << endl;
289  }
290  file.close();
291  results2.clear();
292 
293 }
void publishDiagnosis(ptime t, const Diagnosis &d)
Definition: History.hh:134
InputIterator getFileExtension(const std::basic_string< CharType, CharTraits > &fileName, InputIterator begin, InputIterator end)
Definition: StringTools.hh:57
class History
vector< string > fileExtensions(numberOfFileExtensions)
void setVerboseLevel(Diades::Utils::VerboseLevel level)
Definition: Verbose.hh:135
vector< bool > isSet(numberOfOptions, false)
STL namespace.
unsigned numberOfFileExtensions
int main(int argc, char *argv[])
Option
ptime initialTimePoint() const
Definition: History.hh:146
void makeComparison(const History &history1, const History &history2, const string &prefix)
Option
Definition: abstract.cc:32
vector< string > options(numberOfOptions)
string description
void initialiseOptions()
FileExtension
void printError(string parameter, string message)
unsigned numberOfOptions
void historyCompare(const History &history1, const History &history2, DiagnosisComparisonFunctor compare, list< pair< ptime, typename DiagnosisComparisonFunctor::ValueType > > &result)
Definition: History.hh:303
void printUsage(const po::options_description &desc)