diag_1_eventdiagnosis.cpp
Go to the documentation of this file.
1 /** @file diag_1_eventdiagnosis.cpp
2 Demonstrate diagnoser structure and methods for std event diagnosability.
3 @ingroup Tutorials
4 @include diag_1_eventdiagnosis.cpp
5 */
6 
7 #include "diag_include.h"
8 
9 
10 using namespace std;
11 using namespace faudes;
12 
13 
14 int main(void) {
15 
16  // ******************** Basic usage of diagnoser objects
17 
18  // The diagnoser is derived from the basic generator and, hence,
19  // provides std access to states, events and transitions
20 
21  // Declare diagnoser
22  Diagnoser d1, d2;
23 
24  // Assign name to diagnoser d1
25  d1.Name("Diagnoser");
26 
27  // Insert some states in diagnoser d1
28  Idx s1 = d1.InsInitState("s1");
29  Idx s2 = d1.InsState("s2");
30  Idx s3 = d1.InsState("s3");
31 
32  // Insert some events in diagnoser d1
33  Idx eventA = d1.InsEvent("a");
34  Idx eventB = d1.InsEvent("b");
35 
36  // Set transitions in diagnoser d1
37  d1.SetTransition(s1, eventA, s2);
38  d1.SetTransition(s2, eventB, s3);
39  d1.SetTransition(s3, eventA, s3);
40 
41 
42  // ******************** Failure type map of diagnoser objects
43 
44  // Declare some two failure event sets
45  EventSet failures1, failures2;
46 
47  failures1.Insert("WPblocked");
48  failures1.Insert("WPfelldown");
49  failures2.Insert("sfRunsContinuously");
50  failures2.Insert("cb1RunsContinuously");
51 
52  // Introduce failure types to diagnoser
53  Idx F1 = d1.InsFailureTypeMapping("F1", failures1);
54  Idx F2 = d1.InsFailureTypeMapping("F2", failures2);
55 
56  // delete all failure types
57  // d1.ClearFailureTypeMap();
58 
59 
60  // ******************** State estimate attributes of diagnoser objects
61 
62  // Attatch generator state estimates to the diagnoser states
63 
64  // Diagnoser state s1 carries the generator state estimate 1N
65  d1.InsStateLabelMapping(s1,1,DiagLabelSet::IndexOfLabelN());
66  // Diagnoser state s2 carries the generator state estimate 3F1, 5N
67  d1.InsStateLabelMapping(s2,3,F1);
68  d1.InsStateLabelMapping(s2,5,DiagLabelSet::IndexOfLabelN());
69  // Diagnoser state s3 carries the generator state estimate 7F1F2, 9A
70  d1.InsStateLabelMapping(s3,7,F1);
71  d1.InsStateLabelMapping(s3,7,F2);
72  d1.InsStateLabelMapping(s3,9,DiagLabelSet::IndexOfLabelA());
73 
74  // delete all state estimates
75  // d1.ClearStateAttributes();
76 
77  // ******************** Diagnoser file io
78 
79  // write diagnoser d1 to file
80  d1.Write("tmp_diag_diagnoser_1.gen");
81  try {
82  d1.GraphWrite("tmp_demo_diagnoser_1.svg");
83  } catch(faudes::Exception& exception) {
84  std::cout << "warning: cannot execute graphviz' dot. " << std::endl;
85  }
86 
87  // Dead diagnoser d2 from file
88  d2.Read("tmp_diag_diagnoser_1.gen");
89 
90  // Report to console
91  std::cout << "################################\n";
92  std::cout << "# tutorial, diagnoser d2\n";
93  d2.Write();
94  std::cout << "################################\n";
95 
96 
97  // Report to console
98  AttributeDiagnoserState currDStateAttr;
99  TaIndexSet<DiagLabelSet> currDStateMap;
100  TaIndexSet<DiagLabelSet>::Iterator currDStateMapIt;
101  currDStateAttr = d2.StateAttribute(s3);
102  currDStateMap = currDStateAttr.DiagnoserStateMap();
103 
104  std::cout << "################################\n";
105  std::cout << "# tutorial, parsing state estimates for state s3 of d2\n";
106  for(currDStateMapIt = currDStateMap.Begin(); currDStateMapIt != currDStateMap.End(); ++ currDStateMapIt){
107  cout << *currDStateMapIt << " " << currDStateMap.Attribute(*currDStateMapIt).ToString() << endl;
108  }
109  std::cout << "################################\n";
110 
111  // Test protocol
112  FAUDES_TEST_DUMP("diagnoser d2", d2);
113 
114  // ******************** Failure typemaps
115 
116  // AttributeFailureEvents stores a set of failure and indicator events
117  AttributeFailureEvents attrFE;
118 
119  // Add indicator events to attrFE
120  attrFE.mIndicatorEvents.Insert("Indicator1");
121  attrFE.mIndicatorEvents.Insert("Indicator2");
122  // Add failure events to attrFE
123  attrFE.mFailureEvents.Insert("Failure1");
124  attrFE.mFailureEvents.Insert("Failure2");
125  attrFE.mFailureEvents.Insert("Failure3");
126 
127  // Declare a AttributeFailureTypeMap (to store the failure and indicator partition)
128  AttributeFailureTypeMap failureTypes;
129 
130  // Insert attrFE in the partition and associate it with the failure type name "FailureType1"
131  failureTypes.mFailureTypeMap.Insert("FailureType1",attrFE);
132 
133  // Write the failure and indicator partition to console and file
134  std::cout << "################################\n";
135  std::cout << "# tutorial, failure types\n";
136  failureTypes.Write();
137  std::cout << "################################\n";
138  failureTypes.Write("tmp_diag_failure_typemap_2.txt");
139 
140  // ******************** Event-diagnosability with respect to a failure partition
141 
142  // Declare needed variables
143  System gen;
144  Diagnoser diag;
145  AttributeFailureTypeMap failurePartition;
146  string reportString;
147 
148  // Report to console
149  std::cout << "################################\n";
150  std::cout << "# diagnosability, failure types, system 4 \n";
151  std::cout << "# a) read data \n";
152 
153  // Read input generator and failure/indicator partition from file
154  gen.Read("data/diag_system_4.gen");
155  failureTypes.Read("data/diag_failure_typemap_4.txt");
156 
157  // Write input generator to png file
158  try {
159  gen.GraphWrite("tmp_demo_system_4.png");
160  } catch(faudes::Exception& exception) {
161  std::cout << "warning: cannot execute graphviz' dot. " << std::endl;
162  }
163 
164  // Report to console
165  std::cout << "# b) run diagnosability test (expect result FALSE and warning)\n";
166 
167  // Test generator gen for diagnosability with respect to failure partition failureTypes
168  bool isdiagft=IsEventDiagnosable(gen,failureTypes,reportString);
169  if(isdiagft){
170  cout << "System is diagnosable." << endl;
171  } else {
172  cout << "System is not diagnosable." << endl;
173  cout << reportString << endl;
174  }
175 
176  // Report to console
177  std::cout << "# c) run i-diagnosability test (expect result TRUE)\n";
178 
179  // Test protocol
180  FAUDES_TEST_DUMP("diag failuretype",isdiagft);
181 
182  // Test generator gen for I-diagnosability with respect to failure partition failureTypes
183  bool isdiagie=IsIndicatorEventDiagnosable(gen,failureTypes,reportString);
184  if(isdiagie) {
185  cout << "System is I-diagnosable." << endl;
186  } else {
187  cout << "System is not I-diagnosable." << endl;
188  cout << reportString << endl;
189  }
190 
191  // Report to console
192  std::cout << "# done \n";
193  std::cout << "################################\n";
194 
195  // Test protocol
196  FAUDES_TEST_DUMP("diag indicator",isdiagie);
197 
198 
199  // ******************** Event-diagnoser synthesis w.r.t. failure types
200 
201  // Report to console
202  std::cout << "################################\n";
203  std::cout << "# tutorial, event-diagnoser synthesis, wrt failure types\n";
204 
205  // Read the generator from file
206  gen.Read("data/diag_system_3.gen");
207 
208  // Read the failure partition from file
209  failurePartition.Read("data/diag_failure_typemap_3.txt");
210 
211  // Write generator to file
212  gen.Write("tmp_diag_system_3.gen");
213 
214  // compute the diagnoser
215  EventDiagnoser(gen,failurePartition,diag);
216 
217  // Write the diagnoser to file
218  diag.Write("tmp_diag_diagnoser_3.gen");
219 
220  // Report result
221  std::cout << "Diagnoser statistics\n";
222  diag.SWrite();
223  std::cout << "################################\n";
224 
225  // Test protocol
226  FAUDES_TEST_DUMP("synthesis failure types", diag);
227 
228 
229  return 0;
230 }
231 
#define FAUDES_TEST_DUMP(mes, dat)
Test protocol record macro ("mangle" filename for platform independance)
Definition: cfl_helper.h:483
Implements state estimates for the current status of the generator.
const TaIndexSet< DiagLabelSet > & DiagnoserStateMap(void) const
Get mDiagnoserStateMap.
Stores the failure and indicator events for a particular failure type.
EventSet mIndicatorEvents
Set of indicator events.
EventSet mFailureEvents
Set of failure events.
Partitions the failure and indicator events.
TaNameSet< AttributeFailureEvents > mFailureTypeMap
Failure and indicator event partition.
Faudes exception class.
Set of indices with symbolic names.
Definition: cfl_nameset.h:69
bool Insert(const Idx &rIndex)
Add an element by index.
Idx InsState(void)
Add new anonymous state to generator.
bool InsEvent(Idx index)
Add an existing event to alphabet by index.
bool SetTransition(Idx x1, Idx ev, Idx x2)
Add a transition to generator by indices.
void StateAttribute(Idx index, const StateAttr &rAttr)
Set attribute for existing state.
Set of indices with attributes.
Definition: cfl_indexset.h:316
const Attr & Attribute(const Idx &rElem) const
Definition: cfl_indexset.h:528
Generator with controllability attributes.
Provides the structure and methods to build and handle diagnosers.
Idx InsFailureTypeMapping(const std::string &failureType, const EventSet &rfailureEvents)
Adds a failure type with associated failure events to the global attribute.
void InsStateLabelMapping(Idx dStateIndex, Idx gStateIndex, Idx labelIndex)
Inserts a generator state estimate to a diagnoser state.
void Read(const std::string &rFileName, const std::string &rLabel="", const Type *pContext=0)
Read configuration data from file with label specified.
Definition: cfl_types.cpp:261
void Write(const Type *pContext=0) const
Write configuration data to console.
Definition: cfl_types.cpp:139
void SWrite(TokenWriter &rTw) const
Write statistics comment to TokenWriter.
Definition: cfl_types.cpp:256
void Name(const std::string &rName)
Set the generator's name.
Idx InsInitState(void)
Create new anonymous state and set as initial state.
void GraphWrite(const std::string &rFileName, const std::string &rOutFormat="", const std::string &rDotExec="dot") const
Produce graphical representation of this generator.
int main(void)
Includes all header files of the diagnosis plug-in.
Iterator End(void) const
Iterator to the end of set.
Definition: cfl_baseset.h:1896
Iterator Begin(void) const
Iterator to the begin of set.
Definition: cfl_baseset.h:1891
libFAUDES resides within the namespace faudes.
uint32_t Idx
Type definition for index type (allways 32bit)
bool IsIndicatorEventDiagnosable(const System &rGen, const AttributeFailureTypeMap &rFailureTypeMap, string &rReportString)
bool IsEventDiagnosable(const System &rGen, const AttributeFailureTypeMap &rFailureTypeMap, string &rReportString)
void EventDiagnoser(const System &rOrigGen, const map< string, EventSet > &rFailureTypeMap, Diagnoser &rDiagGen)

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