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 FAUDES_API 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  *
91  * Note: you must reimplement this operator in derived
92  * classes in order to handle internal pointers correctly
93  *
94  * @param rOtherGen
95  * Other generator
96  */
97  virtual TdiagGenerator& operator= (const Generator& rOtherGen) {this->Assign(rOtherGen); return *this;};
98 
99  /** @} doxygen group */
100 
101  /**
102  Adds a failure type with associated failure events to the global attribute.
103  If failure type does already exists the failure events are overridden.
104  @param failureType
105  Name of failure type.
106  @param rfailureEvents
107  Associated failure events.
108  @return
109  Index of failure type in msLabelSymbolTable of DiagLabelSet
110  */
111  Idx InsFailureTypeMapping(const std::string& failureType, const EventSet& rfailureEvents);
112 
113  /**
114  Insert entire failure type map in the diagnoser.
115  @param rFailureMap
116  Map of failure type names to failure events.
117  */
118  void InsFailureTypeMap(const std::map<std::string,EventSet>& rFailureMap);
119 
120  /**
121  Returns the failure type of a particular failure events.
122  @param failureEvent
123  A failure event.
124  @return
125  Index of failure type in msLabelSymbolTable.
126  */
127  Idx GetFailureType(Idx failureEvent) const;
128 
129  /**
130  Returns the all failure events of the failure partition.
131  @return
132  EventSet containing all failure events.
133  */
134  EventSet GetAllFailureEvents(void) const;
135 
136  /**
137  Inserts a generator state estimate to a diagnoser state.
138  @param dStateIndex
139  Index of diagnoser state.
140  @param gStateIndex
141  Index of generator state estimate.
142  @param labelIndex
143  Index of associated label.
144  */
145  void InsStateLabelMapping(Idx dStateIndex, Idx gStateIndex, Idx labelIndex);
146 
147  /**
148  Inserts a DiagLabelSet containing a complete set of generator state estimates to a diagnoser state.
149  @param dStateIndex
150  Index of diagnoser state.
151  @param gState
152  Index of generator state estimate.
153  @param labels
154  Associated DiagLabelSet containing the generator state estimates.
155  */
156  void InsStateLabelMap(Idx dStateIndex, Idx gState, const DiagLabelSet& labels);
157 
158  /**
159  Set a diagnoser state attribute.
160  @param dStateIndex
161  Index of diagnoser state.
162  @param newAttr
163  The new attribute.
164  */
165  void SetStateAttr(Idx dStateIndex, const AttributeDiagnoserState& newAttr);
166 
167  /**
168  Prints all generator state estimates of a diagnoser state to a string.
169  @param dStateIndex
170  Index of diagnoser state.
171  @return
172  String containing state estimates.
173  */
174  std::string SAStr(Idx dStateIndex) const;
175 
176  /**
177  Writes generator to dot input format.
178  The dot file format is specified by the graphiz package; see http://www.graphviz.org.
179  The package includes the dot command line tool to generate a graphical representation
180  of the generators graph.
181  See also Generator::GraphWrite(). This functions sets the re-indexing to minimal indices.
182  @param rFileName
183  File to write
184  @exception Exception
185  - IO errors (id 2)
186  */
187  void DotWrite(const std::string& rFileName) const;
188 
189  protected:
190 
191 
192 }; // class TdiagGenerator
193 
194 
195 
196 // convenience typedef for standard diagnoser
198 
199 /** Compatibility: pre 2.20b used diagGenerator as C++ class name*/
200 #ifdef FAUDES_COMPATIBILITY
202 #endif
203 
204 
205 /*
206  ***********************************************************************
207  ***********************************************************************
208  ***********************************************************************
209  ***********************************************************************
210 
211  TdiagGenerator implementation
212 
213  ***********************************************************************
214  ***********************************************************************
215  ***********************************************************************
216  ***********************************************************************
217  */
218 
219 // convenient scope macors
220 #define THIS TdiagGenerator<GlobalAttr, StateAttr, EventAttr, TransAttr>
221 #define BASE TcGenerator<GlobalAttr, StateAttr, EventAttr, TransAttr>
222 #define TEMP template<class GlobalAttr, class StateAttr, class EventAttr, class TransAttr>
223 
224 
225 // TdiagGenerator() - default constructor
226 TEMP THIS::TdiagGenerator(void) : BASE() {
227  FD_DG("TdiagGenerator(" << this << ")::TdiagGenerator()");
229  BASE::mReindexOnWrite=false;
230 }
231 
232 // TdiagGenerator(rOtherGen)
233 TEMP THIS::TdiagGenerator(const TdiagGenerator& rOtherGen) : BASE(rOtherGen) {
234  FD_DG("TdiagGenerator(" << this << ")::TdiagGenerator(rOtherGen)");
236  BASE::mReindexOnWrite=false;
237 }
238 
239 // TdiagGenerator(rOtherGen)
240 TEMP THIS::TdiagGenerator(const Generator& rOtherGen) : BASE(rOtherGen) {
241  FD_DG("TdiagGenerator(" << this << ")::TdiagGenerator(rOtherGen)");
243  BASE::mReindexOnWrite=false;
244 }
245 
246 // TdiagGenerator(rFileName)
247 TEMP THIS::TdiagGenerator(const std::string& rFileName) : BASE() {
248  FD_DG("TDiagGenerator(" << this << ")::TdiagGenerator(rFilename) : done");
250  BASE::mReindexOnWrite=false;
251  BASE::Read(rFileName);
252 }
253 
254 // New()
255 TEMP THIS* THIS::New(void) const {
256  // allocate
257  THIS* res = new THIS();
258  // fix base data
259  res->EventSymbolTablep(BASE::mpEventSymbolTable);
260  res->mStateNamesEnabled=BASE::mStateNamesEnabled;
261  res->mReindexOnWrite=BASE::mReindexOnWrite;
262  // fix my data
263  res->mpLabelSymbolTable=mpLabelSymbolTable;
264  return res;
265 }
266 
267 // Copy()
268 TEMP THIS* THIS::Copy(void) const {
269  // allocate
270  THIS* res = new THIS(*this);
271  // done
272  return res;
273 }
274 
275 
276 // InsFailureTypeMapping()
277 TEMP Idx THIS::InsFailureTypeMapping(const std::string& failureType, const EventSet& rfailureEvents) {
278  return BASE::pGlobalAttribute->AddFailureTypeMapping(failureType, rfailureEvents);
279 }
280 
281 // InsFailureTypeMap()
282 TEMP void THIS::InsFailureTypeMap(const std::map<std::string,EventSet>& rFailureMap) {
283  BASE::pGlobalAttribute->AddFailureTypeMap(rFailureMap);
284 }
285 
286 // GetFailureType()
287 // not used
288 TEMP Idx THIS::GetFailureType(Idx failureEvent) const {
289  return BASE::pGlobalAttribute->FailureType(failureEvent);
290 }
291 
292 // GetAllFailureEvents()
293 TEMP EventSet THIS::GetAllFailureEvents(void) const{
294  return BASE::pGlobalAttribute->AllFailureEvents();
295 }
296 
297 // InsStateLabelMapping()
298 TEMP void THIS::InsStateLabelMapping(Idx dStateIndex, Idx gStateIndex, Idx labelIndex) {
299  BASE::StateAttributep(dStateIndex)->AddStateLabelMapping(gStateIndex, labelIndex);
300 }
301 
302 // InsStateLabelMap()
303 // not used
304 TEMP void THIS::InsStateLabelMap(Idx dStateIndex, Idx gState, const DiagLabelSet& labels) {
305  BASE::StateAttributep(dStateIndex)->AddStateLabelMap(gState, labels);
306 }
307 
308 // SetStateAttr()
309 // not used
310 TEMP void THIS::SetStateAttr(Idx dStateIndex, const AttributeDiagnoserState& newAttr) {
311  BASE::StateAttribute(dStateIndex,newAttr);
312 }
313 
314 // PrettyPrintStateAttr()
315 TEMP std::string THIS::SAStr(Idx dStateIndex) const {
316  return BASE::StateAttribute(dStateIndex).Str();
317 }
318 
319 // DotWrite()
320 TEMP void THIS::DotWrite(const std::string& rFileName) const {
321  FD_DG("TdiagGenerator(" << this << ")::DotWrite(" << rFileName << ")");
322  BASE::SetMinStateIndexMap();
323  StateSet::Iterator lit;
324  typename BASE::ATransSet::Iterator tit;
325  try {
326  std::ofstream stream;
327  stream.exceptions(std::ios::badbit|std::ios::failbit);
328  stream.open(rFileName.c_str());
329  stream << "digraph \"" << BASE::Name() << "\" {" << std::endl;
330  stream << " rankdir=LR" << std::endl;
331  stream << " node [shape=box];" << std::endl;
332  stream << std::endl;
333  stream << " // initial states" << std::endl;
334  int i = 1;
335  for (lit = BASE::InitStatesBegin(); lit != BASE::InitStatesEnd(); ++lit) {
336  //std::string xname = BASE::StateName(*lit);
337  std::string xname = SAStr(*lit);
338  if(xname=="") xname=ToStringInteger(BASE::MinStateIndex(*lit));
339  stream << " dot_dummyinit_" << i << " [shape=none, label=\"\" ];" << std::endl;
340  stream << " dot_dummyinit_" << i << " -> \"" << xname << "\";" << std::endl;
341  i++;
342  }
343  stream << std::endl;
344  stream << " // marked states" << std::endl;
345  for (lit = BASE::MarkedStatesBegin(); lit != BASE::MarkedStatesEnd(); ++lit) {
346  //std::string xname= BASE::StateName(*lit);
347  std::string xname = SAStr(*lit);
348  if(xname=="") xname=ToStringInteger(BASE::MinStateIndex(*lit));
349  stream << " \"" << xname << "\" [shape=doubleoctagon];" << std::endl;
350  }
351  stream << std::endl;
352  stream << " // rest of stateset" << std::endl;
353  for (lit = BASE::StatesBegin(); lit != BASE::StatesEnd(); ++lit) {
354  if (! (BASE::ExistsInitState(*lit) || BASE::ExistsMarkedState(*lit)) ) {
355  //std::string xname = BASE::StateName(*lit);
356  std::string xname = SAStr(*lit);
357  if(xname=="") xname=ToStringInteger(BASE::MinStateIndex(*lit));
358  stream << " \"" << xname << "\";" << std::endl;
359  //cout << "Label of state " << xname << ": " << PrettyPrintStateAttr(*lit) << endl;
360  }
361  }
362  stream << std::endl;
363  stream << " // transition relation" << std::endl;
364  for (tit = BASE::TransRelBegin(); tit != BASE::TransRelEnd(); ++tit) {
365  //std::string x1name= BASE::StateName(tit->X1);
366  std::string x1name = SAStr(tit->X1);
367  if(x1name=="") x1name=ToStringInteger(BASE::MinStateIndex(tit->X1));
368  //std::string x2name= BASE::StateName(tit->X2);
369  std::string x2name = SAStr(tit->X2);
370  if(x2name=="") x2name=ToStringInteger(BASE::MinStateIndex(tit->X2));
371  stream << " \"" << x1name << "\" -> \"" << x2name
372  << "\" [label=\"" << BASE::EventName(tit->Ev) << "\"];" << std::endl;
373  }
374  stream << "}" << std::endl;
375  stream.close();
376  }
377  catch (std::ios::failure&) {
378  throw Exception("TdiagGenerator::DotWrite",
379  "Exception opening/writing dotfile \""+rFileName+"\"", 2);
380  }
381  BASE::ClearMinStateIndexMap();
382 }
383 
384 
385 
386 #undef THIS
387 #undef BASE
388 #undef TEMP
389 
390 } // namespace faudes
391 
392 #endif
#define FD_DG(message)
Debug: optional report on generator operations.
#define FAUDES_API
Interface export/import symbols: windows.
Definition: cfl_platform.h:81
Implements state estimates for the current status of the generator.
Implements the label representation for state estimates.
static SymbolTable * StaticLabelSymbolTablep(void)
Get pointer to static LabelSymbolTable.
Faudes exception class.
Set of indices with symbolic names.
Definition: cfl_nameset.h:69
A SymbolTable associates sybolic names with indices.
Generator with controllability attributes.
Provides the structure and methods to build and handle diagnosers.
~TdiagGenerator(void)
Default destructor.
SymbolTable * mpLabelSymbolTable
Pointer to static LabelSymbolTable of DiagLabelSet.
Base class of all FAUDES generators.
Includes all libFAUDES headers, no plugins.
State estimates for the current status of the generator (as state attributes).
Failure and indicator events for a common failure type.
Contains the failure and indicator partition for a system (used as global attribute).
Includes debugging to diagnosis plug-in.
#define TEMP
#define BASE
#define THIS
libFAUDES resides within the namespace faudes.
uint32_t Idx
Type definition for index type (allways 32bit)
TdiagGenerator< AttributeFailureTypeMap, AttributeDiagnoserState, AttributeCFlags, AttributeVoid > Diagnoser
TdiagGenerator< AttributeFailureTypeMap, AttributeDiagnoserState, AttributeCFlags, AttributeVoid > diagGenerator
Compatibility: pre 2.20b used diagGenerator as C++ class name.
std::string ToStringInteger(Int number)
integer to string
Definition: cfl_helper.cpp:43

libFAUDES 2.32b --- 2024.03.01 --- c++ api documentaion by doxygen