DiaDes  0.1
DIAgnosis of Discrete-Event System
ClassicalDiagnoser.hh
Go to the documentation of this file.
1 #ifndef __DIADES__AUTOMATA__CLASSICALDIAGNOSER__HH
2 #define __DIADES__AUTOMATA__CLASSICALDIAGNOSER__HH
3 
4 
5 #include <fstream>
6 #include <list>
7 #include <set>
8 #include <map>
9 #include <unordered_map>
10 #include <unordered_set>
11 #include <boost/dynamic_bitset.hpp>
12 #include <diades/graph/Graph.hh>
13 #include <diades/graph/NodeMap.hh>
15 #include <diades/graph/EdgeMap.hh>
18 
19 using namespace Diades::Graph;
20 
21 namespace boost
22 {
23  namespace serialization
24  {
25  typedef boost::dynamic_bitset<> FaultSet;
26 
27  template<class Archive>
28  void load(Archive & ar, FaultSet & bset, const unsigned int version)
29  {
30  std::string s;
31  ar & s;
32  bset.resize(s.length(),false);
33  for(unsigned i = 0; i < s.length();++i)
34  {
35  bset.set(bset.size() - 1 - i, (s[i]=='1'));
36  }
37  }
38 
39 
40  template<class Archive>
41  void save(Archive & ar, const FaultSet & bset, const unsigned int version)
42  {
43  std::string s;
44  to_string(bset,s);
45  ar & s;
46  }
47 
48 
49  }
50 }
51 
52 BOOST_SERIALIZATION_SPLIT_FREE(boost::dynamic_bitset<>)
53 
54 
55 
56 
57 namespace Diades {
58  namespace Automata {
59 
71 
72 
73  public:
74  typedef enum {Diagnosed, Observable} EventType;
75  typedef enum {Safe,Sure,Ambiguous} FaultStatus;
76 
77  public:
78  static string typeName() { return "Automata::ClassicalDiagnoser"; }
80 
81 
82  private:
83 
84  // Mappings
85  unordered_set<Event> _diagnosedEvents;
94  typedef int ModelState;
95  typedef boost::dynamic_bitset<> FaultSet;
96  vector<FaultSet::size_type> faultToIndex;
97  vector<Event> indexToFault;
98  typedef pair< ModelState, FaultSet> DiagnosisHypothesis;
99  typedef multimap<ModelState, FaultSet > DiagnoserInfoState;
100 
102 
105  typedef unordered_map< Event, unordered_multimap< State, FaultSet > > ReachableInfo;
106 
107 
109  vector<string> labelStates;
110 
111  protected:
112 
113 
114 
123  virtual void computeDiagnoser(const ObservableComponent & model,
124  const unordered_set<Event> & faults, bool silentClosure);
125 
126 
127 
128  private:
129 
130 
140  void computeSilentClosure(const ObservableComponent & model, State source, const unordered_set<Event> & faults,
141  unordered_multimap< State, FaultSet > & result);
142 
143 
144 
151  void initModelStatesLabels(const ObservableComponent & model, vector<State> & modelStateOf,
152  ConstNodeMap<int> & valueOf);
153 
154 
155 
156 
157 
167  void computeReachableStates(const ObservableComponent & model,
168  ConstNodeMap< ReachableInfo > & reachables,
169  bool silentClosure,
170  ConstNodeMap< unordered_multimap< State, FaultSet > > & silent);
171 
172 
173 
186  void computeNonDeterministicDiagnoser(const ObservableComponent & model,
187  const unordered_set<Event> & faults,
188  Graph::Graph & nonDeterministicDiagnoser,
189  NodeMap< pair<int, FaultSet > > & stateInfoNonDet,
190  EdgeMap< Event > & transInfoNonDet,
191  unordered_set<State> & initialStateNonDetState,
192  bool silentClosure);
193  public:
196 
197 
207  ClassicalDiagnoser(const ObservableComponent & model, const unordered_set<Event> & faults, bool silentClosure=false);
208 
209 
210 
217  FaultStatus getFaultStatus(State diagnoserState, const Event & fault) const;
218 
219 
220  // /**
221  // * Load constructor
222  // * @param fileName name of the file to load
223  // **/
224  // ClassicalDiagnoser(const string & fileName):ObservableComponent()
225  // {
226  // ifstream file(fileName.c_str());
227  // file >> *this;
228  // file.close();
229  // }
230 
231 
236  clear();
237  }
239 
242 
243 
244 
245 
249  unordered_set<Event>::const_iterator beginOfDiagnosedEvents() const {
250  return _diagnosedEvents.begin();
251  }
252 
256  unordered_set<Event>::const_iterator endOfDiagnosedEvents() const {
257  return _diagnosedEvents.end();
258  }
259 
263  unsigned numberOfDiagnosedEvents() const
264  {
265  return _diagnosedEvents.size();
266  }
267 
268 
269 
271 
278  State initialState() const { if(_initial.empty()) { return State(); } return *(_initial.begin()); }
279 
280 
281 
286  bool initialize() { currentDiagnoserState = initialState(); return currentDiagnoserState.valid(); }
287 
291  void getDiagnosis(Diagnosis & diagnosis) const;
292 
296  void diagnose(const Event & obs);
297 
303  bool isConsistent(const Event & obs) const;
304 
308  //int memoryUsage() const;
309 
312 
319  bool diagnoser2dot(const string & fileName, string & log) const;
320 
349  bool diagnoser2hd(const string & fileName, string & log) const;
350 
351 
352 
353 
354 
355  // /** output stream operator (saving) */
356  // friend ostream& operator<<(ostream& os, const ClassicalDiagnoser & d);
357 
358  // /** input stream operator (loading) */
359  // friend istream& operator>>(istream& is, ClassicalDiagnoser & d);
361 
362  public:
363 
364  template<class Archive>
365  void serialize(Archive & ar, const unsigned int version)
366  {
367  // serialize base class information
368  ar & boost::serialization::base_object<ObservableComponent>(*this);
369  ar & _diagnosedEvents;
370  ar & faultToIndex;
371  ar & indexToFault;
372  ar & stateInfo;
373  ar & currentDiagnoserState;
374  ar & labelStates;
375  }
376 
377 
378  };
379 
380 
381  };
382 };
383 
384 
385 #endif
boost::dynamic_bitset FaultSet
vector< FaultSet::size_type > faultToIndex
unordered_set< Event >::const_iterator beginOfDiagnosedEvents() const
pair< ModelState, FaultSet > DiagnosisHypothesis
Diades::Utils::Exception< ClassicalDiagnoser > Exception
An observable Component defined as a automaton.
unordered_map< Event, unordered_multimap< State, FaultSet > > ReachableInfo
AutFsm::State State
Definition: Run.cc:72
Node source(const Edge &edge) const
Definition: GraphIntOld.hh:744
void serialize(Archive &ar, const unsigned int version)
Namespace of the Diades project.
unordered_set< Event >::const_iterator endOfDiagnosedEvents() const
NodeMap< DiagnoserInfoState > stateInfo
multimap< ModelState, FaultSet > DiagnoserInfoState
Log log(Logger logger, const string &msg)
Definition: Log.hh:124
Diades::Graph::Node State
Definition: BeliefState.hh:36
void load(Archive &ar, FaultSet &bset, const unsigned int version)
Namespace of the Graph library (libGraph)
Definition: BFS.hh:9
void save(Archive &ar, const FaultSet &bset, const unsigned int version)