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

libFAUDES 2.32f --- 2024.12.22 --- c++ api documentaion by doxygen