DiaDes  0.1
DIAgnosis of Discrete-Event System
History.hh
Go to the documentation of this file.
1 #ifndef __DIADES__AUTOMATA__HISTORY__HH__
2 #define __DIADES__AUTOMATA__HISTORY__HH__
3 
13 #include<set>
14 #include<map>
15 
16 
17 #include <boost/archive/text_oarchive.hpp>
18 #include <boost/archive/text_iarchive.hpp>
19 #include <boost/date_time/posix_time/posix_time.hpp>
20 #include <boost/date_time/posix_time/time_serialize.hpp>
21 #include "ObservableComponent.hh"
22 #include "Diagnosis.hh"
23 
24 
25 using namespace boost::posix_time;
26 using namespace Diades::Utils;
27 
28 namespace Diades
29 {
30  namespace Automata
31  {
56  class History
57  {
58  public:
64  typedef const Diagnosis* DiagPtr;
70  typedef pair<ptime,Event> Observation;
75  typedef map<ptime,Event>::const_iterator ObservationIterator;
76 
81  typedef set<ptime>::const_iterator TimeIterator;
82  private:
84  vector<ptime> _timePoints;
85  vector<Diagnosis> _diags;
86  map<ptime,Event> _obs;
87  set<ptime> _hist;
89 
90  public:
97  History():_initialTimePoint(microsec_clock::local_time()),_timePoints(),_diags(),_obs(),_hist(),_nullDiagnosis(){
98  _hist.insert(_initialTimePoint);
99  }
100 
102  typedef vector < unordered_map<StateId,unordered_set<StateId> > > Dict;
103 
110  History(const History & hist,
111  const Dict & dict);
112 
113 
114 
121  void publishObservation(ptime t, const Event & e)
122  {
123  _obs[t] = e;
124  _hist.insert(t);
125  }
126 
127 
134  void publishDiagnosis(ptime t, const Diagnosis & d)
135  {
136  _timePoints.push_back(t);
137  _diags.push_back(d);
138  _hist.insert(t);
139  }
146  ptime initialTimePoint() const
147  {
148  return _initialTimePoint;
149  }
156  ptime lastTimePoint() const
157  {
158  return *(_hist.rbegin());
159  }
160 
169  TimeIterator begin(ptime t) const
170  {
171  TimeIterator it = _hist.begin();
172  while((it!=_hist.end()) && (t > *it))
173  {
174  ++it;
175  }
176  return it;
177  }
186  TimeIterator end(ptime t) const
187  {
188  TimeIterator it = _hist.begin();
189  while((it!=_hist.end()) && (t >= *it))
190  {
191  ++it;
192  }
193  return it;
194  }
203  const Diagnosis & getDiagnosis(ptime t) const;
204 
213  ObservationIterator obsBegin(ptime t) const
214  {
215  ObservationIterator it = _obs.begin();
216  while((it != _obs.end()) && (it->first < t))
217  {
218  ++it;
219  }
220  return it;
221  }
222 
231  ObservationIterator obsEnd(ptime t) const
232  {
233  ObservationIterator it = _obs.begin();
234  while((it != _obs.end()) && (it->first <= t))
235  {
236  ++it;
237  }
238  return it;
239  }
240 
249  friend ostream & operator<<(ostream & os, const History & history);
250 
251 
252  private:
253  friend class boost::serialization::access;
254 
263  template<class Archive>
264  void serialize(Archive & ar, const unsigned int version)
265  {
266  ar & _initialTimePoint;
267  ar & _timePoints;
268  ar & _diags;
269  ar & _obs;
270  ar & _hist;
271  ar & _nullDiagnosis;
272  }
273  };
274 
275 
276 
302  template<typename DiagnosisComparisonFunctor>
303  void historyCompare(const History & history1, const History & history2, DiagnosisComparisonFunctor compare,
304  list< pair<ptime,typename DiagnosisComparisonFunctor::ValueType> > & result)
305  {
306  // offset between histories, I synchronise on the history 1
307  //ptime initialTime = history1.initialTimePoint();
308  time_duration offset = history2.initialTimePoint() - history1.initialTimePoint();
309  History::TimeIterator it1 = history1.begin(history1.initialTimePoint());
310  History::TimeIterator it2 = history2.begin(history2.initialTimePoint());
311  bool commonTemporalWindow = true;
312  //cout << "initial it1 = " << *it1 << endl;
313  //cout << "initial it2 = " << *it2 << endl;
314  //cout << "initial it1 + offset = " << (*it1+offset) << endl;
315  //cout << "initial it2 - offset = " << (*it2-offset) << endl;
316  while(commonTemporalWindow && (it1 != history1.end(history1.lastTimePoint()))
317  &&
318  (it2 != history2.end(history2.lastTimePoint())))
319  {
320  //cout << "it1 = " << *it1 << endl;
321  //cout << "it2 = " << *it2 << endl;
322  //cout << "it1 + offset = " << (*it1+offset) << endl;
323  //cout << "it2 - offset = " << (*it2-offset) << endl;
324  //cout << "history1.lastTimePoint() = " << history1.lastTimePoint() << endl;
325  //cout << "history2.lastTimePoint() = " << history2.lastTimePoint() << endl;
326 
327 
328 
329 
330  if(commonTemporalWindow)
331  {
332  if(*it1 < (*it2 - offset))
333  {
334  //cout << "*it is earlier than *it2 with regards to the synchronisation" << endl;
335  // *it is earlier than *it2 with regards to the synchronisation
336  commonTemporalWindow = ((*it1 + offset) <= history2.lastTimePoint());
337  if(commonTemporalWindow)
338  {
339  typename DiagnosisComparisonFunctor::ValueType res = compare(history1.getDiagnosis(*it1), history2.getDiagnosis(*it1 + offset));
340  result.push_back(make_pair(*it1,res));
341  ++it1;
342  }
343  }
344  else
345  {
346  // *it2 is either *it1 or an earlier time point with regards to the synchronisation
347  //cout << "*it2 is either *it1 or an earlier time point with regards to the synchronisation" << endl;
348  if(*it1 == (*it2 - offset))
349  {
350  commonTemporalWindow = (*it1 + offset) <= history2.lastTimePoint() && (*it2 -offset <= history1.lastTimePoint());
351  }
352  else
353  {
354  commonTemporalWindow = (*it2 -offset <= history1.lastTimePoint());
355  }
356  if(commonTemporalWindow)
357  {
358  typename DiagnosisComparisonFunctor::ValueType res = compare(history1.getDiagnosis(*it2 - offset), history2.getDiagnosis(*it2));
359  result.push_back(make_pair(*it2-offset,res));
360  }
361  if(*it1 == (*it2 - offset))
362  {
363  //cout << "*it1 == (*it2 - offset)" << endl;
364  ++it1;
365  ++it2;
366  }
367  else
368  {
369  //cout << "*it1 != (*it2 - offset)" << endl;
370  ++it2;
371  }
372  }
373  }
374  //cout << "commonTemporalWindow ? " << commonTemporalWindow << endl;
375  }
376  }
377 
404  template<typename DiagnosisComparisonFunctor>
405  void historyCompareWithTimeStep(const History & history1, const History & history2,
406  DiagnosisComparisonFunctor compare,
407  time_duration timeStep,
408  list< pair<ptime,int> > & result)
409  {
410  // offset between histories, I synchronise on the history 1
411  ptime initialTime = history1.initialTimePoint();
412  time_duration offset = history2.initialTimePoint() - history1.initialTimePoint();
413  bool commonTemporalWindow = true;
414  while(commonTemporalWindow && (initialTime <= history1.lastTimePoint()))
415  {
416  commonTemporalWindow = (initialTime + offset) < history2.lastTimePoint();
417  if(commonTemporalWindow)
418  {
419  int res = compare(history1.getDiagnosis(initialTime), history2.getDiagnosis(initialTime + offset));
420  result.push_back(make_pair(initialTime,res));
421  }
422  initialTime += timeStep;
423  }
424  }
425 
426  };
427 };
428 
429 
430 #endif
ptime lastTimePoint() const
Definition: History.hh:156
void publishDiagnosis(ptime t, const Diagnosis &d)
Definition: History.hh:134
set< ptime >::const_iterator TimeIterator
Definition: History.hh:81
ObservationIterator obsEnd(ptime t) const
Definition: History.hh:231
vector< unordered_map< StateId, unordered_set< StateId > > > Dict
Definition: History.hh:102
ptime initialTimePoint() const
Definition: History.hh:146
ObservationIterator obsBegin(ptime t) const
Definition: History.hh:213
TimeIterator begin(ptime t) const
Definition: History.hh:169
map< ptime, Event >::const_iterator ObservationIterator
Definition: History.hh:75
TimeIterator end(ptime t) const
Definition: History.hh:186
Namespace of the Diades project.
void publishObservation(ptime t, const Event &e)
Definition: History.hh:121
std::ostream & operator<<(std::ostream &os, const Identifier &identifier)
Definition: Identifier.hh:501
const Diagnosis & getDiagnosis(ptime t) const
Diagnosis _nullDiagnosis
Definition: History.hh:88
set< ptime > _hist
Definition: History.hh:87
map< ptime, Event > _obs
Definition: History.hh:86
void serialize(Archive &ar, const unsigned int version)
Definition: History.hh:264
Candidate::StateId StateId
Definition: History.hh:101
void historyCompareWithTimeStep(const History &history1, const History &history2, DiagnosisComparisonFunctor compare, time_duration timeStep, list< pair< ptime, int > > &result)
Definition: History.hh:405
pair< ptime, Event > Observation
Definition: History.hh:70
Diades::Graph::Node::NodeId StateId
Definition: Candidate.hh:44
vector< ptime > _timePoints
Definition: History.hh:84
vector< Diagnosis > _diags
Definition: History.hh:85
const Diagnosis * DiagPtr
Definition: History.hh:64
void historyCompare(const History &history1, const History &history2, DiagnosisComparisonFunctor compare, list< pair< ptime, typename DiagnosisComparisonFunctor::ValueType > > &result)
Definition: History.hh:303