28 #include <boost/accumulators/accumulators.hpp> 29 #include <boost/accumulators/statistics/stats.hpp> 30 #include <boost/accumulators/statistics/min.hpp> 31 #include <boost/accumulators/statistics/max.hpp> 32 #include <boost/accumulators/statistics/mean.hpp> 33 #include <boost/accumulators/statistics/variance.hpp> 34 #include <boost/date_time/posix_time/posix_time.hpp> 35 #include <utils/CmdInterface.hh> 43 #include<io/xml/automata/Simulation.hh> 51 using boost::accumulators::extract::max;
52 using boost::accumulators::extract::min;
64 string description=
"Usage:\t diagnose --help\n\t diagnose [--statistics] [--faults evt1 evt2] file1.des_comp file2.des_comp [file3.des_comp...] sync.rules scenario.xml\n\t diagnose [--statistics] [--faults evt1 evt2] file1.des_comp scenario.xml\n\t diagnose [--statistics] [--faults evt1 evt2] file.des_comp file.abstraction scenario.xml\n\t diagnose [--statistics] [--faults evt1 evt2] file.diagnoser scenario.xml\n\n\n\t Diagnose the sequence of observations given in the scenarios. Print on the\n\t standard output the final diagnosis. Write the diagnostic history in\n\t a file 'history.archv' and the perfect diagnosis in 'perfect_history.archv'.\n\t The events listed after the --faults option are considered as faults.\n\n\t If the input models are a set of models, 'diagnose' uses a component-based\n\t algorithm. If the input model is a global one, 'diagnose' uses a global \n\t model algorithm. If the input model is a diagnoser, 'diagnose' uses a diagnoser\n\t algorithm.\n\t Options: --statistics write some statistics in a file 'output.statistics'.";
87 list<string> & modelFiles,set<Event> & faults);
89 list< EventOccurrence > & simulation,
90 list< EventOccurrence > & observations);
95 int main(
int argc,
char * argv[])
100 list<string> modelFiles;
103 list< EventOccurrence > simulation;
104 list< EventOccurrence > observations;
109 vector<const ObservableComponent *> components;
110 { verbose<VbOutput>(
"Loading the models..."); }
115 ParametrizedSynchronisation::ComponentVector vectorComp;
116 unsigned nbModel = modelFiles.size() - 2;
122 components.push_back(component);
123 vectorComp.push_back(component);
124 modelFiles.pop_front();
129 modelFiles.pop_front();
137 modelFiles.pop_front();
143 std::ifstream ifs(modelFiles.front().c_str());
144 boost::archive::text_iarchive ia(ifs);
147 diagnoser.sanityCheck(log);
148 modelFiles.pop_front();
154 cout <<
"ALGORITHM NOT IMPLEMENTED YET. SORRY MATE." << endl;
158 { verbose<VbOutput>(
"Done.\n"); }
159 { verbose<VbOutput>(
"Loading the scenario..."); }
160 readScenario(modelFiles.front(),simulation,observations);
161 { verbose<VbOutput>(
"Done.\n"); }
162 { verbose<VbOutput>(
"Starting the diagnosis...\n"); }
171 list<string> & modelFiles,
181 string abstractionFile;
184 while( index < argc )
187 if(getOption<Option>(argv[index],
options,currentOption))
189 if(
isSet[currentOption])
191 printError(
"The option '" +
options[currentOption] +
"' occurs at least twice.");
195 isSet[currentOption] =
true;
197 switch(currentOption)
201 if((index!=1) || (argc !=2))
203 printError(
"Incorrect use of option --help.");
219 set<string> faultstring;
222 if(faultstring.empty())
224 printError(
"No parameter after the option " + options[currentOption]);
228 for(
const string & s : faultstring)
230 faults.insert(EventFactory::factory()->getEvent(s));
237 printError(
"Unrecognized option: " +
string(argv[index]));
245 vector<string>::const_iterator it =
250 printError(
"Unrecognized file extension in file name: " +
string(argv[index]));
265 printError(
"The Diagnose command does not accept a diagnoser and a model file at the same time.");
269 printError(
"The Diagnose command does not accept a diagnoser and an abstraction file at the same time.");
273 printError(
"The Diagnose command does not accept a diagnoser and a model file at the same time.");
276 modelFiles.push_back(
string(argv[index]));
283 printError(
"The Diagnose command does not accept a diagnoser and a model file at the same time.");
285 if((type==
Abstract) && (nbModel==1))
287 printError(
"The Diagnose command does not accept several model files and an abstraction file at the same time.");
290 modelFiles.push_back(
string(argv[index]));
297 printError(
"The Diagnose command does not accept a diagnoser and an abstraction file at the same time.");
299 if(nbModel>1 || (type==
Comp) )
301 printError(
"The Diagnose command does not accept several model files and an abstraction file at the same time.");
303 abstractionFile = string(argv[index]);
311 printError(
"The Diagnose command does not accept a diagnoser and an synchronisation file at the same time.");
315 printError(
"The Diagnose command does not accept a diagnoser and a synchronisation file at the same time.");
318 if(!ruleFile.empty())
320 printError(
"The Diagnose command does not accept two synchronisation files at the same time.");
322 ruleFile = string(argv[index]);
330 printError(
"The Diagnose command does not accept two scenarios at the same time.");
332 xmlFile = string(argv[index]);
339 if(type==
Unknown && nbModel !=1)
341 printError(
"A synchronisation file is required.");
343 if(nbModel==1 && type==
Unknown)
349 printError(
"Sorry mate, certainly a bug but impossible to correctly interpret your command.");
353 modelFiles.push_back(ruleFile);
357 modelFiles.push_back(abstractionFile);
363 modelFiles.push_back(xmlFile);
373 list< EventOccurrence > & simulation,
374 list< EventOccurrence > & observations)
376 back_insert_iterator< list< EventOccurrence > > simIt(simulation);
377 back_insert_iterator< list< EventOccurrence > > obsIt(observations);
380 Diades::Io::Xml::loadXmlSimulation(filename, simIt,obsIt);
384 printError(
"The function readScenario has caught an exception when loading the file " + filename +
": \n" + e.what());
395 list<Diagnosis> diagnoses;
420 time_duration algorithmClock = seconds(0);
424 accumulator_set< long, stats< tag::min, tag::max, tag::mean, tag::variance(lazy) > > stats;
427 file.open(
"output.statistics");
443 int nbObs = observations.size();
449 verbose<VbOutput>(
"\nObservation: %1% at time %2% (%3%/%4%).") % occurrence.second
450 % occurrence.first % currentObs % nbObs;
452 boost::posix_time::ptime initialTime(boost::posix_time::microsec_clock::local_time());
453 faultDiagnosis.
diagnose(occurrence.second);
456 boost::posix_time::ptime endTime(boost::posix_time::microsec_clock::local_time());
457 time_duration algorithmicTime = endTime - initialTime;
460 stats(algorithmicTime.total_microseconds());
475 if(algorithmClock < occurrence.first)
478 algorithmClock = occurrence.first + algorithmicTime;
483 algorithmClock = algorithmClock + algorithmicTime;
500 cout << endl <<
"Final Diagnosis:" << endl;
501 cout << diagnoses.back() << endl;
502 cout <<
"History saved in " << flush;
503 std::ofstream ofs1(
"history.archv");
504 boost::archive::text_oarchive oa1(ofs1);
506 cout <<
"history.archv" << endl;
507 cout <<
"Perfect history saved in " << flush;
508 std::ofstream ofs2(
"perfect_history.archv");
509 boost::archive::text_oarchive oa2(ofs2);
510 oa2 << perfectHistory;
511 cout <<
"perfect_history.archv" << endl;
515 cout <<
"Statistics saved in " << endl;
516 file <<
"/* Result are in microseconds */" << endl;
517 file <<
"Mean time:" << boost::accumulators::extract::mean(stats) << endl;
518 file <<
"Max time:" << boost::accumulators::extract::max(stats) << endl;
519 file <<
"Min time:" << boost::accumulators::extract::min(stats) << endl;
520 file <<
"Variance: " << boost::accumulators::extract::variance(stats) << endl;
522 cout <<
"output.statistics" << endl;
void publishDiagnosis(ptime t, const Diagnosis &d)
vector< string > options(numberOfOptions)
InputIterator getFileExtension(const std::basic_string< CharType, CharTraits > &fileName, InputIterator begin, InputIterator end)
void setVerboseLevel(Diades::Utils::VerboseLevel level)
vector< string > fileExtensions(numberOfFileExtensions)
vector< bool > isSet(numberOfOptions, false)
bool loadSynchronisationRules(const ParametrizedSynchronisation::ComponentVector &models, const string &filename, ParametrizedSynchronisation &sync)
pair< time_duration, Event > EventOccurrence
ptime initialTimePoint() const
void diagnose(const Event &obs)
An observable Component defined as a automaton.
virtual bool importDesCompModel(const string &filename)
void getDiagnosis(Diagnosis &diagnosis)
void publishObservation(ptime t, const Event &e)
unsigned numberOfFileExtensions
void printError(string parameter, string message)
Log log(Logger logger, const string &msg)
void runDiagnosis(FaultDiagnosis &faultDiagnosis, const list< EventOccurrence > &observations, bool statistics)
void readParameters(int argc, char *argv[], Algotype &type, list< string > &modelFiles, set< Event > &faults)
int main(int argc, char *argv[])
void readScenario(const string &filename, list< EventOccurrence > &simulation, list< EventOccurrence > &observations)
void printUsage(const po::options_description &desc)