About
User Reference
C++ API
luafaudes
Developer
Links
libFAUDES online
libFAUDES

Sections

Index

diag_generator.h

Go to the documentation of this file.
00001 /** @file diag_generator.h
00002     Structure of diagnosers and methods to handle them.
00003 */
00004 
00005 
00006 #ifndef DIAG_GENERATOR_H
00007 #define DIAG_GENERATOR_H
00008 
00009 #include "corefaudes.h"
00010 #include "diag_attrdiagstate.h"
00011 #include "diag_attrfailuretypes.h"
00012 #include "diag_attrfailureevents.h"
00013 
00014 #include "diag_debug.h"
00015 
00016 namespace faudes {
00017 
00018 /**
00019    Provides the structure and methods to build and handle diagnosers. 
00020    The diagnoser states carry state estimates for the generator under observation, which are 
00021    implemented using state attributes.
00022 
00023    @ingroup DiagnosisPlugIn
00024 */
00025 template <class GlobalAttr, class StateAttr, class EventAttr, class TransAttr>
00026   class TdiagGenerator : public TcGenerator <GlobalAttr, StateAttr, EventAttr, TransAttr> {
00027 
00028  private:
00029     /** Pointer to static LabelSymbolTable of DiagLabelSet. */
00030     SymbolTable* mpLabelSymbolTable;
00031 
00032  public:  
00033  
00034     /** @name Constructor, Destructor */ // and Copy-Methods */
00035     /** @{ doxygen group */
00036 
00037     /**
00038      * Creates an emtpy diagnoser 
00039      */
00040     TdiagGenerator(void);
00041 
00042     /** 
00043      * Construct diagnoser from std generator.
00044      *
00045      * @param rOtherGen
00046      */
00047     TdiagGenerator(const Generator& rOtherGen);
00048    
00049     /** 
00050      * Copy constructor.
00051      *
00052      * @param rOtherGen
00053      */
00054     TdiagGenerator(const TdiagGenerator& rOtherGen);
00055    
00056     /**
00057      * Construct from file
00058      *
00059      * @param rFileName
00060      *   Filename
00061      *
00062      * @exception Exception
00063      *   If opening/reading fails an Exception object is thrown (id 1, 50, 51)
00064      */
00065     TdiagGenerator(const std::string& rFileName);
00066 
00067     /**
00068      * Construct on heap
00069      *
00070      * @return 
00071      *   new Generator 
00072      */
00073     virtual TdiagGenerator* New(void) const;
00074 
00075     /**
00076      * Construct copy on heap
00077      *
00078      * @return 
00079      *   new Generator 
00080      */
00081     virtual TdiagGenerator* Copy(void) const;
00082 
00083     /** Default destructor. */
00084     ~TdiagGenerator(void) {
00085       FD_DG("TdiagGenerator(" << this << ")::~TdiagGenerator()");
00086     }
00087 
00088     /**
00089      * Assignment operator (uses copy )
00090      * Note: you must reimplement this operator in derived 
00091      * classes in order to handle internal pointers correctly
00092      *
00093      * @param rOtherGen
00094      *   Other generator
00095      */
00096     virtual TdiagGenerator& operator= (const TdiagGenerator& rOtherGen) {this->Assign(rOtherGen); return *this;};
00097 
00098     /**
00099      * Assignment operator (uses copy )
00100      *
00101      * @param rOtherGen
00102      *   Other generator
00103      */
00104      virtual TdiagGenerator& operator= (const Generator& rOtherGen) {this->Assign(rOtherGen); return *this;};
00105     
00106     /** @} doxygen group */
00107   
00108     /**
00109        Adds a failure type with associated failure events to the global attribute.
00110        If failure type does already exists the failure events are overridden.
00111        @param failureType
00112        Name of failure type.
00113        @param rfailureEvents
00114        Associated failure events.
00115        @return
00116        Index of failure type in msLabelSymbolTable of DiagLabelSet  
00117     */
00118     Idx InsFailureTypeMapping(const std::string& failureType, const EventSet& rfailureEvents);
00119 
00120     /**
00121        Insert entire failure type map in the diagnoser.
00122        @param rFailureMap
00123        Map of failure type names to failure events.
00124     */  
00125     void InsFailureTypeMap(const std::map<std::string,EventSet>& rFailureMap);
00126   
00127     /**
00128        Returns the failure type of a particular failure events.
00129        @param failureEvent
00130        A failure event.
00131        @return  
00132        Index of failure type in msLabelSymbolTable.
00133     */
00134     Idx GetFailureType(Idx failureEvent) const;
00135   
00136     /**
00137        Returns the all failure events of the failure partition.
00138        @return  
00139        EventSet containing all failure events.
00140     */
00141     EventSet GetAllFailureEvents(void) const;
00142   
00143     /**
00144        Inserts a generator state estimate to a diagnoser state.
00145        @param dStateIndex
00146        Index of diagnoser state.
00147        @param gStateIndex
00148        Index of generator state estimate.
00149        @param labelIndex
00150        Index of associated label.
00151     */
00152     void InsStateLabelMapping(Idx dStateIndex, Idx gStateIndex, Idx labelIndex);
00153   
00154     /**
00155        Inserts a DiagLabelSet containing a complete set of generator state estimates to a diagnoser state.
00156        @param dStateIndex
00157        Index of diagnoser state.
00158        @param gState
00159        Index of generator state estimate.
00160        @param labels
00161        Associated DiagLabelSet containing the generator state estimates.
00162     */
00163     void InsStateLabelMap(Idx dStateIndex, Idx gState, const DiagLabelSet& labels);
00164   
00165     /**
00166        Set a diagnoser state attribute.
00167        @param dStateIndex
00168        Index of diagnoser state.
00169        @param newAttr
00170        The new attribute.
00171     */  
00172     void SetStateAttr(Idx dStateIndex, const AttributeDiagnoserState& newAttr);
00173   
00174     /**
00175        Prints all generator state estimates of a diagnoser state to a string.
00176        @param dStateIndex
00177        Index of diagnoser state.
00178        @return
00179        String containing state estimates.
00180     */  
00181     std::string SAStr(Idx dStateIndex) const;
00182 
00183     /**
00184        Writes generator to dot input format.
00185        The dot file format is specified by the graphiz package; see http://www.graphviz.org.
00186        The package includes the dot command line tool to generate a graphical representation 
00187        of the generators graph.
00188        See also Generator::GraphWrite(). This functions sets the re-indexing to minimal indices.
00189        @param rFileName
00190        File to write
00191        @exception Exception
00192        - IO errors (id 2)
00193     */
00194     void DotWrite(const std::string& rFileName) const;
00195     
00196  protected:
00197  
00198     /**
00199        Writes attribute to TokenWriter, see AttributeVoid for public wrappers.
00200        Label and Context argument are ignored.  
00201        @param rTw
00202        TokenWriter to write to.
00203        @param rLabel
00204        Section to write.
00205        @param pContext
00206        Write context to provide contextual information.
00207     */ 
00208     void DoWrite(TokenWriter& rTw, const std::string& rLabel="", const Type* pContext=0) const;
00209 
00210 }; // class TdiagGenerator
00211 
00212 
00213 
00214 // convenience typedef for standard diagnoser
00215 typedef TdiagGenerator<AttributeFailureTypeMap, AttributeDiagnoserState, AttributeCFlags, AttributeVoid> Diagnoser;
00216 
00217 /** Compatibility: pre 2.20b used diagGenerator as C++ class name*/
00218 #ifdef FAUDES_COMPATIBILITY
00219 typedef TdiagGenerator<AttributeFailureTypeMap, AttributeDiagnoserState, AttributeCFlags, AttributeVoid> diagGenerator;
00220 #endif
00221 
00222 
00223 /*
00224  ***********************************************************************
00225  ***********************************************************************
00226  ***********************************************************************
00227  ***********************************************************************
00228 
00229  TdiagGenerator implementation
00230 
00231  ***********************************************************************
00232  ***********************************************************************
00233  ***********************************************************************
00234  ***********************************************************************
00235  */
00236 
00237 // convenient scope macors
00238 #define THIS TdiagGenerator<GlobalAttr, StateAttr, EventAttr, TransAttr>
00239 #define BASE TcGenerator<GlobalAttr, StateAttr, EventAttr, TransAttr>
00240 #define TEMP template<class GlobalAttr, class StateAttr, class EventAttr, class TransAttr>
00241 
00242 
00243 // TdiagGenerator() - default constructor
00244 TEMP THIS::TdiagGenerator(void) : BASE() {
00245   FD_DG("TdiagGenerator(" << this << ")::TdiagGenerator()");
00246   mpLabelSymbolTable = DiagLabelSet::StaticLabelSymbolTablep();
00247   BASE::mReindexOnWrite=false;
00248 }
00249 
00250 // TdiagGenerator(rOtherGen)
00251 TEMP THIS::TdiagGenerator(const TdiagGenerator& rOtherGen) : BASE(rOtherGen) {
00252   FD_DG("TdiagGenerator(" << this << ")::TdiagGenerator(rOtherGen)");
00253   mpLabelSymbolTable = DiagLabelSet::StaticLabelSymbolTablep();
00254   BASE::mReindexOnWrite=false;
00255 }
00256 
00257 // TdiagGenerator(rOtherGen)
00258 TEMP THIS::TdiagGenerator(const Generator& rOtherGen) : BASE(rOtherGen) {
00259   FD_DG("TdiagGenerator(" << this << ")::TdiagGenerator(rOtherGen)");
00260   mpLabelSymbolTable = DiagLabelSet::StaticLabelSymbolTablep();
00261   BASE::mReindexOnWrite=false;
00262 }
00263 
00264 // TdiagGenerator(rFileName)
00265 TEMP THIS::TdiagGenerator(const std::string& rFileName) : BASE() {
00266   FD_DG("TDiagGenerator(" << this << ")::TdiagGenerator(rFilename) : done");
00267   mpLabelSymbolTable = DiagLabelSet::StaticLabelSymbolTablep();
00268   BASE::mReindexOnWrite=false;
00269   BASE::Read(rFileName);
00270 }
00271 
00272 // New()
00273 TEMP THIS* THIS::New(void) const {
00274   // allocate
00275   THIS* res = new THIS();
00276   // fix base data
00277   res->EventSymbolTablep(BASE::mpEventSymbolTable);
00278   res->mStateNamesEnabled=BASE::mStateNamesEnabled;
00279   res->mReindexOnWrite=BASE::mReindexOnWrite;  
00280   // fix my data
00281   res->mpLabelSymbolTable=mpLabelSymbolTable;
00282   return res;
00283 }
00284 
00285 // Copy()
00286 TEMP THIS* THIS::Copy(void) const {
00287   // allocate
00288   THIS* res = new THIS(*this);
00289   // done
00290   return res;
00291 }
00292 
00293 // DoWrite()
00294 TEMP void THIS::DoWrite(TokenWriter& rTw, const std::string& rLabel, const Type* pContext) const {
00295   (void) pContext;
00296   // figure section
00297   std::string label=rLabel;
00298   if(label=="") label="Generator"; 
00299   FD_DG("TdiagGenerator(" << this << ")::DoWrite(): section " << label);
00300   // reindex for file output
00301   if(rTw.FileMode()) BASE::SetMinStateIndexMap();
00302   // write diagnoser
00303   rTw.WriteBegin(label);
00304   rTw << BASE::mMyName;
00305   rTw << "\n";
00306   rTw << "\n";
00307   BASE::SWrite(rTw);
00308   rTw << "\n";
00309   BASE::pAlphabet->Write(rTw);
00310   rTw << "\n";
00311   BASE::WriteStateSet(rTw, *BASE::pStates);
00312   rTw << "\n";
00313   BASE::WriteTransRel(rTw);
00314   rTw << "\n";
00315   BASE::WriteStateSet(rTw, BASE::mInitStates);
00316   rTw << "\n";
00317   BASE::WriteStateSet(rTw, BASE::mMarkedStates);
00318   rTw << "\n";
00319   // write FailureTypeMap
00320   BASE::pGlobalAttribute->Write(rTw);
00321   rTw << "\n";
00322   rTw.WriteEnd(label);
00323   // end of reindex
00324   BASE::ClearMinStateIndexMap();
00325 }
00326 
00327 // InsFailureTypeMapping()
00328 TEMP Idx THIS::InsFailureTypeMapping(const std::string& failureType, const EventSet& rfailureEvents) {
00329   return BASE::pGlobalAttribute->AddFailureTypeMapping(failureType, rfailureEvents);
00330 }
00331 
00332 // InsFailureTypeMap()
00333 TEMP void THIS::InsFailureTypeMap(const std::map<std::string,EventSet>& rFailureMap) {
00334   BASE::pGlobalAttribute->AddFailureTypeMap(rFailureMap);
00335 }
00336 
00337 // GetFailureType()
00338 // not used
00339 TEMP Idx THIS::GetFailureType(Idx failureEvent) const {
00340   return BASE::pGlobalAttribute->FailureType(failureEvent);
00341 }
00342 
00343 // GetAllFailureEvents()
00344 TEMP EventSet THIS::GetAllFailureEvents(void) const{
00345   return BASE::pGlobalAttribute->AllFailureEvents();
00346 }
00347 
00348 // InsStateLabelMapping()
00349 TEMP void THIS::InsStateLabelMapping(Idx dStateIndex, Idx gStateIndex, Idx labelIndex) {
00350   BASE::StateAttributep(dStateIndex)->AddStateLabelMapping(gStateIndex, labelIndex);
00351 }
00352 
00353 // InsStateLabelMap()
00354 // not used
00355 TEMP void THIS::InsStateLabelMap(Idx dStateIndex, Idx gState, const DiagLabelSet& labels) {
00356   BASE::StateAttributep(dStateIndex)->AddStateLabelMap(gState, labels);
00357 }
00358 
00359 // SetStateAttr()
00360 // not used
00361 TEMP void THIS::SetStateAttr(Idx dStateIndex, const AttributeDiagnoserState& newAttr) {
00362   BASE::StateAttribute(dStateIndex,newAttr);
00363 }
00364 
00365 // PrettyPrintStateAttr()
00366 TEMP std::string THIS::SAStr(Idx dStateIndex) const {
00367   return BASE::StateAttribute(dStateIndex).Str();
00368 }
00369 
00370 // DotWrite()
00371 TEMP void THIS::DotWrite(const std::string& rFileName) const {
00372   FD_DG("TdiagGenerator(" << this << ")::DotWrite(" << rFileName << ")");
00373   BASE::SetMinStateIndexMap();
00374   StateSet::Iterator lit;
00375   typename BASE::ATransSet::Iterator tit;
00376   try {
00377     std::ofstream stream;
00378     stream.exceptions(std::ios::badbit|std::ios::failbit);
00379     stream.open(rFileName.c_str());
00380     stream << "digraph \"" << BASE::Name() << "\" {" << std::endl;
00381     stream << "  rankdir=LR" << std::endl;
00382     stream << "  node [shape=box];" << std::endl;
00383     stream << std::endl;
00384     stream << "  // initial states" << std::endl;
00385     int i = 1;
00386     for (lit = BASE::InitStatesBegin(); lit != BASE::InitStatesEnd(); ++lit) {
00387       //std::string xname = BASE::StateName(*lit);
00388       std::string xname = SAStr(*lit);
00389       if(xname=="") xname=ToStringInteger(BASE::MinStateIndex(*lit));
00390       stream << "  dot_dummyinit_" << i << " [shape=none, label=\"\" ];" << std::endl;
00391       stream << "  dot_dummyinit_" << i << " -> \"" << xname << "\";" << std::endl; 
00392       i++;
00393     }
00394     stream << std::endl;
00395     stream << "  // marked states" << std::endl;
00396     for (lit = BASE::MarkedStatesBegin(); lit != BASE::MarkedStatesEnd(); ++lit) {
00397       //std::string xname= BASE::StateName(*lit);
00398       std::string xname = SAStr(*lit);
00399       if(xname=="") xname=ToStringInteger(BASE::MinStateIndex(*lit));
00400       stream << "  \"" << xname << "\" [shape=doubleoctagon];" << std::endl;
00401     }
00402     stream << std::endl;
00403     stream << "  // rest of stateset" << std::endl;
00404     for (lit = BASE::StatesBegin(); lit != BASE::StatesEnd(); ++lit) {
00405       if (! (BASE::ExistsInitState(*lit) || BASE::ExistsMarkedState(*lit)) ) {
00406   //std::string xname = BASE::StateName(*lit);
00407   std::string xname = SAStr(*lit);
00408   if(xname=="") xname=ToStringInteger(BASE::MinStateIndex(*lit));
00409   stream << "  \"" << xname << "\";" << std::endl;
00410   //cout << "Label of state " << xname << ": " << PrettyPrintStateAttr(*lit) << endl;
00411       }
00412     }
00413     stream << std::endl;
00414     stream << "  // transition relation" << std::endl;
00415     for (tit = BASE::TransRelBegin(); tit != BASE::TransRelEnd(); ++tit) {
00416       //std::string x1name= BASE::StateName(tit->X1);
00417       std::string x1name = SAStr(tit->X1);
00418       if(x1name=="") x1name=ToStringInteger(BASE::MinStateIndex(tit->X1));
00419       //std::string x2name= BASE::StateName(tit->X2);
00420       std::string x2name = SAStr(tit->X2);
00421       if(x2name=="") x2name=ToStringInteger(BASE::MinStateIndex(tit->X2));
00422       stream << "  \"" << x1name  << "\" -> \"" << x2name
00423        << "\" [label=\"" << BASE::EventName(tit->Ev) << "\"];" << std::endl;
00424     }
00425     stream << "};" << std::endl;
00426     stream.close();
00427   }
00428   catch (std::ios::failure&) {
00429     throw Exception("TdiagGenerator::DotWrite", 
00430         "Exception opening/writing dotfile \""+rFileName+"\"", 2);
00431   } 
00432   BASE::ClearMinStateIndexMap();
00433 }
00434 
00435 
00436 
00437 #undef THIS
00438 #undef BASE
00439 #undef TEMP
00440 
00441 }  // namespace faudes
00442 
00443 #endif

libFAUDES 2.20d --- 2011.04.26 --- c++ source docu by doxygen