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  d1.GraphWrite("tmp_demo_diagnoser_1.svg");
82 
83  // Dead diagnoser d2 from file
84  d2.Read("tmp_diag_diagnoser_1.gen");
85 
86  // Report to console
87  std::cout << "################################\n";
88  std::cout << "# tutorial, diagnoser d2\n";
89  d2.Write();
90  std::cout << "################################\n";
91 
92 
93  // Report to console
94  AttributeDiagnoserState currDStateAttr;
95  TaIndexSet<DiagLabelSet> currDStateMap;
96  TaIndexSet<DiagLabelSet>::Iterator currDStateMapIt;
97  currDStateAttr = d2.StateAttribute(s3);
98  currDStateMap = currDStateAttr.DiagnoserStateMap();
99 
100  std::cout << "################################\n";
101  std::cout << "# tutorial, parsing state estimates for state s3 of d2\n";
102  for(currDStateMapIt = currDStateMap.Begin(); currDStateMapIt != currDStateMap.End(); ++ currDStateMapIt){
103  cout << *currDStateMapIt << " " << currDStateMap.Attribute(*currDStateMapIt).ToString() << endl;
104  }
105  std::cout << "################################\n";
106 
107  // Test protocol
108  FAUDES_TEST_DUMP("diagnoser d2", d2);
109 
110  // ******************** Failure typemaps
111 
112  // AttributeFailureEvents stores a set of failure and indicator events
113  AttributeFailureEvents attrFE;
114 
115  // Add indicator events to attrFE
116  attrFE.mIndicatorEvents.Insert("Indicator1");
117  attrFE.mIndicatorEvents.Insert("Indicator2");
118  // Add failure events to attrFE
119  attrFE.mFailureEvents.Insert("Failure1");
120  attrFE.mFailureEvents.Insert("Failure2");
121  attrFE.mFailureEvents.Insert("Failure3");
122 
123  // Declare a AttributeFailureTypeMap (to store the failure and indicator partition)
124  AttributeFailureTypeMap failureTypes;
125 
126  // Insert attrFE in the partition and associate it with the failure type name "FailureType1"
127  failureTypes.mFailureTypeMap.Insert("FailureType1",attrFE);
128 
129  // Write the failure and indicator partition to console and file
130  std::cout << "################################\n";
131  std::cout << "# tutorial, failure types\n";
132  failureTypes.Write();
133  std::cout << "################################\n";
134  failureTypes.Write("tmp_diag_failure_typemap_2.txt");
135 
136  // ******************** Event-diagnosability with respect to a failure partition
137 
138  // Declare needed variables
139  System gen;
140  Diagnoser diag;
141  AttributeFailureTypeMap failurePartition;
142  string reportString;
143 
144  // Report to console
145  std::cout << "################################\n";
146  std::cout << "# diagnosability, failure types, system 4 \n";
147  std::cout << "# a) read data \n";
148 
149  // Read input generator and failure/indicator partition from file
150  gen.Read("data/diag_system_4.gen");
151  failureTypes.Read("data/diag_failure_typemap_4.txt");
152 
153  // Write input generator to png file
154  gen.GraphWrite("tmp_demo_system_4.png");
155 
156  // Report to console
157  std::cout << "# b) run diagnosability test (expect result FALSE and warning)\n";
158 
159  // Test generator gen for diagnosability with respect to failure partition failureTypes
160  bool isdiagft=IsEventDiagnosable(gen,failureTypes,reportString);
161  if(isdiagft){
162  cout << "System is diagnosable." << endl;
163  } else {
164  cout << "System is not diagnosable." << endl;
165  cout << reportString << endl;
166  }
167 
168  // Report to console
169  std::cout << "# c) run i-diagnosability test (expect result TRUE)\n";
170 
171  // Test protocol
172  FAUDES_TEST_DUMP("diag failuretype",isdiagft);
173 
174  // Test generator gen for I-diagnosability with respect to failure partition failureTypes
175  bool isdiagie=IsIndicatorEventDiagnosable(gen,failureTypes,reportString);
176  if(isdiagie) {
177  cout << "System is I-diagnosable." << endl;
178  } else {
179  cout << "System is not I-diagnosable." << endl;
180  cout << reportString << endl;
181  }
182 
183  // Report to console
184  std::cout << "# done \n";
185  std::cout << "################################\n";
186 
187  // Test protocol
188  FAUDES_TEST_DUMP("diag indicator",isdiagie);
189 
190 
191  // ******************** Event-diagnoser synthesis w.r.t. failure types
192 
193  // Report to console
194  std::cout << "################################\n";
195  std::cout << "# tutorial, event-diagnoser synthesis, wrt failure types\n";
196 
197  // Read the generator from file
198  gen.Read("data/diag_system_3.gen");
199 
200  // Read the failure partition from file
201  failurePartition.Read("data/diag_failure_typemap_3.txt");
202 
203  // Write generator to file
204  gen.Write("tmp_diag_system_3.gen");
205 
206  // compute the diagnoser
207  EventDiagnoser(gen,failurePartition,diag);
208 
209  // Write the diagnoser to file
210  diag.Write("tmp_diag_diagnoser_3.gen");
211 
212  // Report result
213  std::cout << "Diagnoser statistics\n";
214  diag.SWrite();
215  std::cout << "################################\n";
216 
217  // Test protocol
218  FAUDES_TEST_DUMP("synthesis failure types", diag);
219 
220 
221  return 0;
222 }
223 

libFAUDES 2.24g --- 2014.09.15 --- c++ api documentaion by doxygen