diag_generator.h
Go to the documentation of this file.
1 /** @file diag_generator.h
2  Structure of diagnosers and methods to handle them.
3 */
4 
5 
6 #ifndef DIAG_GENERATOR_H
7 #define DIAG_GENERATOR_H
8 
9 #include "corefaudes.h"
10 #include "diag_attrdiagstate.h"
11 #include "diag_attrfailuretypes.h"
12 #include "diag_attrfailureevents.h"
13 
14 #include "diag_debug.h"
15 
16 namespace faudes {
17 
18 /**
19  Provides the structure and methods to build and handle diagnosers.
20  The diagnoser states carry state estimates for the generator under observation, which are
21  implemented using state attributes.
22 
23  @ingroup DiagnosisPlugIn
24 */
25 template <class GlobalAttr, class StateAttr, class EventAttr, class TransAttr>
26  class TdiagGenerator : public TcGenerator <GlobalAttr, StateAttr, EventAttr, TransAttr> {
27 
28  private:
29  /** Pointer to static LabelSymbolTable of DiagLabelSet. */
31 
32  public:
33 
34  /** @name Constructor, Destructor */ // and Copy-Methods */
35  /** @{ doxygen group */
36 
37  /**
38  * Creates an emtpy diagnoser
39  */
40  TdiagGenerator(void);
41 
42  /**
43  * Construct diagnoser from std generator.
44  *
45  * @param rOtherGen
46  */
47  TdiagGenerator(const Generator& rOtherGen);
48 
49  /**
50  * Copy constructor.
51  *
52  * @param rOtherGen
53  */
54  TdiagGenerator(const TdiagGenerator& rOtherGen);
55 
56  /**
57  * Construct from file
58  *
59  * @param rFileName
60  * Filename
61  *
62  * @exception Exception
63  * If opening/reading fails an Exception object is thrown (id 1, 50, 51)
64  */
65  TdiagGenerator(const std::string& rFileName);
66 
67  /**
68  * Construct on heap
69  *
70  * @return
71  * new Generator
72  */
73  virtual TdiagGenerator* New(void) const;
74 
75  /**
76  * Construct copy on heap
77  *
78  * @return
79  * new Generator
80  */
81  virtual TdiagGenerator* Copy(void) const;
82 
83  /** Default destructor. */
85  FD_DG("TdiagGenerator(" << this << ")::~TdiagGenerator()");
86  }
87 
88  /**
89  * Assignment operator (uses copy )
90  * Note: you must reimplement this operator in derived
91  * classes in order to handle internal pointers correctly
92  *
93  * @param rOtherGen
94  * Other generator
95  */
96  virtual TdiagGenerator& operator= (const TdiagGenerator& rOtherGen) {this->Assign(rOtherGen); return *this;};
97 
98  /**
99  * Assignment operator (uses copy )
100  *
101  * @param rOtherGen
102  * Other generator
103  */
104  virtual TdiagGenerator& operator= (const Generator& rOtherGen) {this->Assign(rOtherGen); return *this;};
105 
106  /** @} doxygen group */
107 
108  /**
109  Adds a failure type with associated failure events to the global attribute.
110  If failure type does already exists the failure events are overridden.
111  @param failureType
112  Name of failure type.
113  @param rfailureEvents
114  Associated failure events.
115  @return
116  Index of failure type in msLabelSymbolTable of DiagLabelSet
117  */
118  Idx InsFailureTypeMapping(const std::string& failureType, const EventSet& rfailureEvents);
119 
120  /**
121  Insert entire failure type map in the diagnoser.
122  @param rFailureMap
123  Map of failure type names to failure events.
124  */
125  void InsFailureTypeMap(const std::map<std::string,EventSet>& rFailureMap);
126 
127  /**
128  Returns the failure type of a particular failure events.
129  @param failureEvent
130  A failure event.
131  @return
132  Index of failure type in msLabelSymbolTable.
133  */
134  Idx GetFailureType(Idx failureEvent) const;
135 
136  /**
137  Returns the all failure events of the failure partition.
138  @return
139  EventSet containing all failure events.
140  */
141  EventSet GetAllFailureEvents(void) const;
142 
143  /**
144  Inserts a generator state estimate to a diagnoser state.
145  @param dStateIndex
146  Index of diagnoser state.
147  @param gStateIndex
148  Index of generator state estimate.
149  @param labelIndex
150  Index of associated label.
151  */
152  void InsStateLabelMapping(Idx dStateIndex, Idx gStateIndex, Idx labelIndex);
153 
154  /**
155  Inserts a DiagLabelSet containing a complete set of generator state estimates to a diagnoser state.
156  @param dStateIndex
157  Index of diagnoser state.
158  @param gState
159  Index of generator state estimate.
160  @param labels
161  Associated DiagLabelSet containing the generator state estimates.
162  */
163  void InsStateLabelMap(Idx dStateIndex, Idx gState, const DiagLabelSet& labels);
164 
165  /**
166  Set a diagnoser state attribute.
167  @param dStateIndex
168  Index of diagnoser state.
169  @param newAttr
170  The new attribute.
171  */
172  void SetStateAttr(Idx dStateIndex, const AttributeDiagnoserState& newAttr);
173 
174  /**
175  Prints all generator state estimates of a diagnoser state to a string.
176  @param dStateIndex
177  Index of diagnoser state.
178  @return
179  String containing state estimates.
180  */
181  std::string SAStr(Idx dStateIndex) const;
182 
183  /**
184  Writes generator to dot input format.
185  The dot file format is specified by the graphiz package; see http://www.graphviz.org.
186  The package includes the dot command line tool to generate a graphical representation
187  of the generators graph.
188  See also Generator::GraphWrite(). This functions sets the re-indexing to minimal indices.
189  @param rFileName
190  File to write
191  @exception Exception
192  - IO errors (id 2)
193  */
194  void DotWrite(const std::string& rFileName) const;
195 
196  protected:
197 
198 
199 }; // class TdiagGenerator
200 
201 
202 
203 // convenience typedef for standard diagnoser
205 
206 /** Compatibility: pre 2.20b used diagGenerator as C++ class name*/
207 #ifdef FAUDES_COMPATIBILITY
209 #endif
210 
211 
212 /*
213  ***********************************************************************
214  ***********************************************************************
215  ***********************************************************************
216  ***********************************************************************
217 
218  TdiagGenerator implementation
219 
220  ***********************************************************************
221  ***********************************************************************
222  ***********************************************************************
223  ***********************************************************************
224  */
225 
226 // convenient scope macors
227 #define THIS TdiagGenerator<GlobalAttr, StateAttr, EventAttr, TransAttr>
228 #define BASE TcGenerator<GlobalAttr, StateAttr, EventAttr, TransAttr>
229 #define TEMP template<class GlobalAttr, class StateAttr, class EventAttr, class TransAttr>
230 
231 
232 // TdiagGenerator() - default constructor
233 TEMP THIS::TdiagGenerator(void) : BASE() {
234  FD_DG("TdiagGenerator(" << this << ")::TdiagGenerator()");
236  BASE::mReindexOnWrite=false;
237 }
238 
239 // TdiagGenerator(rOtherGen)
240 TEMP THIS::TdiagGenerator(const TdiagGenerator& rOtherGen) : BASE(rOtherGen) {
241  FD_DG("TdiagGenerator(" << this << ")::TdiagGenerator(rOtherGen)");
243  BASE::mReindexOnWrite=false;
244 }
245 
246 // TdiagGenerator(rOtherGen)
247 TEMP THIS::TdiagGenerator(const Generator& rOtherGen) : BASE(rOtherGen) {
248  FD_DG("TdiagGenerator(" << this << ")::TdiagGenerator(rOtherGen)");
250  BASE::mReindexOnWrite=false;
251 }
252 
253 // TdiagGenerator(rFileName)
254 TEMP THIS::TdiagGenerator(const std::string& rFileName) : BASE() {
255  FD_DG("TDiagGenerator(" << this << ")::TdiagGenerator(rFilename) : done");
257  BASE::mReindexOnWrite=false;
258  BASE::Read(rFileName);
259 }
260 
261 // New()
262 TEMP THIS* THIS::New(void) const {
263  // allocate
264  THIS* res = new THIS();
265  // fix base data
266  res->EventSymbolTablep(BASE::mpEventSymbolTable);
267  res->mStateNamesEnabled=BASE::mStateNamesEnabled;
268  res->mReindexOnWrite=BASE::mReindexOnWrite;
269  // fix my data
270  res->mpLabelSymbolTable=mpLabelSymbolTable;
271  return res;
272 }
273 
274 // Copy()
275 TEMP THIS* THIS::Copy(void) const {
276  // allocate
277  THIS* res = new THIS(*this);
278  // done
279  return res;
280 }
281 
282 
283 // InsFailureTypeMapping()
284 TEMP Idx THIS::InsFailureTypeMapping(const std::string& failureType, const EventSet& rfailureEvents) {
285  return BASE::pGlobalAttribute->AddFailureTypeMapping(failureType, rfailureEvents);
286 }
287 
288 // InsFailureTypeMap()
289 TEMP void THIS::InsFailureTypeMap(const std::map<std::string,EventSet>& rFailureMap) {
290  BASE::pGlobalAttribute->AddFailureTypeMap(rFailureMap);
291 }
292 
293 // GetFailureType()
294 // not used
295 TEMP Idx THIS::GetFailureType(Idx failureEvent) const {
296  return BASE::pGlobalAttribute->FailureType(failureEvent);
297 }
298 
299 // GetAllFailureEvents()
300 TEMP EventSet THIS::GetAllFailureEvents(void) const{
301  return BASE::pGlobalAttribute->AllFailureEvents();
302 }
303 
304 // InsStateLabelMapping()
305 TEMP void THIS::InsStateLabelMapping(Idx dStateIndex, Idx gStateIndex, Idx labelIndex) {
306  BASE::StateAttributep(dStateIndex)->AddStateLabelMapping(gStateIndex, labelIndex);
307 }
308 
309 // InsStateLabelMap()
310 // not used
311 TEMP void THIS::InsStateLabelMap(Idx dStateIndex, Idx gState, const DiagLabelSet& labels) {
312  BASE::StateAttributep(dStateIndex)->AddStateLabelMap(gState, labels);
313 }
314 
315 // SetStateAttr()
316 // not used
317 TEMP void THIS::SetStateAttr(Idx dStateIndex, const AttributeDiagnoserState& newAttr) {
318  BASE::StateAttribute(dStateIndex,newAttr);
319 }
320 
321 // PrettyPrintStateAttr()
322 TEMP std::string THIS::SAStr(Idx dStateIndex) const {
323  return BASE::StateAttribute(dStateIndex).Str();
324 }
325 
326 // DotWrite()
327 TEMP void THIS::DotWrite(const std::string& rFileName) const {
328  FD_DG("TdiagGenerator(" << this << ")::DotWrite(" << rFileName << ")");
329  BASE::SetMinStateIndexMap();
330  StateSet::Iterator lit;
331  typename BASE::ATransSet::Iterator tit;
332  try {
333  std::ofstream stream;
334  stream.exceptions(std::ios::badbit|std::ios::failbit);
335  stream.open(rFileName.c_str());
336  stream << "digraph \"" << BASE::Name() << "\" {" << std::endl;
337  stream << " rankdir=LR" << std::endl;
338  stream << " node [shape=box];" << std::endl;
339  stream << std::endl;
340  stream << " // initial states" << std::endl;
341  int i = 1;
342  for (lit = BASE::InitStatesBegin(); lit != BASE::InitStatesEnd(); ++lit) {
343  //std::string xname = BASE::StateName(*lit);
344  std::string xname = SAStr(*lit);
345  if(xname=="") xname=ToStringInteger(BASE::MinStateIndex(*lit));
346  stream << " dot_dummyinit_" << i << " [shape=none, label=\"\" ];" << std::endl;
347  stream << " dot_dummyinit_" << i << " -> \"" << xname << "\";" << std::endl;
348  i++;
349  }
350  stream << std::endl;
351  stream << " // marked states" << std::endl;
352  for (lit = BASE::MarkedStatesBegin(); lit != BASE::MarkedStatesEnd(); ++lit) {
353  //std::string xname= BASE::StateName(*lit);
354  std::string xname = SAStr(*lit);
355  if(xname=="") xname=ToStringInteger(BASE::MinStateIndex(*lit));
356  stream << " \"" << xname << "\" [shape=doubleoctagon];" << std::endl;
357  }
358  stream << std::endl;
359  stream << " // rest of stateset" << std::endl;
360  for (lit = BASE::StatesBegin(); lit != BASE::StatesEnd(); ++lit) {
361  if (! (BASE::ExistsInitState(*lit) || BASE::ExistsMarkedState(*lit)) ) {
362  //std::string xname = BASE::StateName(*lit);
363  std::string xname = SAStr(*lit);
364  if(xname=="") xname=ToStringInteger(BASE::MinStateIndex(*lit));
365  stream << " \"" << xname << "\";" << std::endl;
366  //cout << "Label of state " << xname << ": " << PrettyPrintStateAttr(*lit) << endl;
367  }
368  }
369  stream << std::endl;
370  stream << " // transition relation" << std::endl;
371  for (tit = BASE::TransRelBegin(); tit != BASE::TransRelEnd(); ++tit) {
372  //std::string x1name= BASE::StateName(tit->X1);
373  std::string x1name = SAStr(tit->X1);
374  if(x1name=="") x1name=ToStringInteger(BASE::MinStateIndex(tit->X1));
375  //std::string x2name= BASE::StateName(tit->X2);
376  std::string x2name = SAStr(tit->X2);
377  if(x2name=="") x2name=ToStringInteger(BASE::MinStateIndex(tit->X2));
378  stream << " \"" << x1name << "\" -> \"" << x2name
379  << "\" [label=\"" << BASE::EventName(tit->Ev) << "\"];" << std::endl;
380  }
381  stream << "};" << std::endl;
382  stream.close();
383  }
384  catch (std::ios::failure&) {
385  throw Exception("TdiagGenerator::DotWrite",
386  "Exception opening/writing dotfile \""+rFileName+"\"", 2);
387  }
388  BASE::ClearMinStateIndexMap();
389 }
390 
391 
392 
393 #undef THIS
394 #undef BASE
395 #undef TEMP
396 
397 } // namespace faudes
398 
399 #endif

libFAUDES 2.26g --- 2015.08.17 --- c++ api documentaion by doxygen