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 vGenerator& 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 vGenerator::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> diagGenerator;
00216 
00217 
00218 /*
00219  ***********************************************************************
00220  ***********************************************************************
00221  ***********************************************************************
00222  ***********************************************************************
00223 
00224  TdiagGenerator implementation
00225 
00226  ***********************************************************************
00227  ***********************************************************************
00228  ***********************************************************************
00229  ***********************************************************************
00230  */
00231 
00232 // convenient scope macors
00233 #define THIS TdiagGenerator<GlobalAttr, StateAttr, EventAttr, TransAttr>
00234 #define BASE TcGenerator<GlobalAttr, StateAttr, EventAttr, TransAttr>
00235 #define TEMP template<class GlobalAttr, class StateAttr, class EventAttr, class TransAttr>
00236 
00237 
00238 // TdiagGenerator() - default constructor
00239 TEMP THIS::TdiagGenerator(void) : BASE() {
00240   FD_DG("TdiagGenerator(" << this << ")::TdiagGenerator()");
00241   mpLabelSymbolTable = DiagLabelSet::StaticLabelSymbolTablep();
00242 }
00243 
00244 // TdiagGenerator(rOtherGen)
00245 TEMP THIS::TdiagGenerator(const TdiagGenerator& rOtherGen) : BASE(rOtherGen) {
00246   FD_DG("TdiagGenerator(" << this << ")::TdiagGenerator(rOtherGen)");
00247   mpLabelSymbolTable = DiagLabelSet::StaticLabelSymbolTablep();
00248 }
00249 
00250 // TdiagGenerator(rOtherGen)
00251 TEMP THIS::TdiagGenerator(const Generator& rOtherGen) : BASE(rOtherGen) {
00252   FD_DG("TdiagGenerator(" << this << ")::TdiagGenerator(rOtherGen)");
00253   mpLabelSymbolTable = DiagLabelSet::StaticLabelSymbolTablep();
00254 }
00255 
00256 // TdiagGenerator(rFileName)
00257 TEMP THIS::TdiagGenerator(const std::string& rFileName) : BASE() {
00258   FD_DG("TDiagGenerator(" << this << ")::TdiagGenerator(rFilename) : done");
00259   mpLabelSymbolTable = DiagLabelSet::StaticLabelSymbolTablep();
00260   BASE::Read(rFileName);
00261 }
00262 
00263 // New()
00264 TEMP THIS* THIS::New(void) const {
00265   // allocate
00266   THIS* res = new THIS();
00267   // fix base data
00268   res->EventSymbolTablep(BASE::mpEventSymbolTable);
00269   res->mStateNamesEnabled=BASE::mStateNamesEnabled;
00270   // fix my data
00271   res->mpLabelSymbolTable=mpLabelSymbolTable;
00272   return res;
00273 }
00274 
00275 // Copy()
00276 TEMP THIS* THIS::Copy(void) const {
00277   // allocate
00278   THIS* res = new THIS(*this);
00279   // done
00280   return res;
00281 }
00282 
00283 // DoWrite()
00284 TEMP void THIS::DoWrite(TokenWriter& rTw, const std::string& rLabel, const Type* pContext) const {
00285   (void) pContext;
00286   // figure section
00287   std::string label=rLabel;
00288   if(label=="") label="Generator"; 
00289   FD_DG("TdiagGenerator(" << this << ")::DoWrite(): section " << label);
00290   // reindex for file output
00291   if(rTw.FileMode()) BASE::SetMinStateIndexMap();
00292   // write diagnoser
00293   rTw.WriteBegin(label);
00294   rTw << BASE::mMyName;
00295   rTw << "\n";
00296   rTw << "\n";
00297   BASE::SWrite(rTw);
00298   rTw << "\n";
00299   BASE::pAlphabet->Write(rTw);
00300   rTw << "\n";
00301   BASE::WriteStateSet(rTw, *BASE::pStates);
00302   rTw << "\n";
00303   BASE::WriteTransRel(rTw);
00304   rTw << "\n";
00305   BASE::WriteStateSet(rTw, BASE::mInitStates);
00306   rTw << "\n";
00307   BASE::WriteStateSet(rTw, BASE::mMarkedStates);
00308   rTw << "\n";
00309   // write FailureTypeMap
00310   BASE::pGlobalAttribute->Write(rTw);
00311   rTw << "\n";
00312   rTw.WriteEnd(label);
00313   // end of reindex
00314   BASE::ClearMinStateIndexMap();
00315 }
00316 
00317 // InsFailureTypeMapping()
00318 TEMP Idx THIS::InsFailureTypeMapping(const std::string& failureType, const EventSet& rfailureEvents) {
00319   return BASE::pGlobalAttribute->AddFailureTypeMapping(failureType, rfailureEvents);
00320 }
00321 
00322 // InsFailureTypeMap()
00323 TEMP void THIS::InsFailureTypeMap(const std::map<std::string,EventSet>& rFailureMap) {
00324   BASE::pGlobalAttribute->AddFailureTypeMap(rFailureMap);
00325 }
00326 
00327 // GetFailureType()
00328 // not used
00329 TEMP Idx THIS::GetFailureType(Idx failureEvent) const {
00330   return BASE::pGlobalAttribute->FailureType(failureEvent);
00331 }
00332 
00333 // GetAllFailureEvents()
00334 TEMP EventSet THIS::GetAllFailureEvents(void) const{
00335   return BASE::pGlobalAttribute->AllFailureEvents();
00336 }
00337 
00338 // InsStateLabelMapping()
00339 TEMP void THIS::InsStateLabelMapping(Idx dStateIndex, Idx gStateIndex, Idx labelIndex) {
00340   BASE::StateAttributep(dStateIndex)->AddStateLabelMapping(gStateIndex, labelIndex);
00341 }
00342 
00343 // InsStateLabelMap()
00344 // not used
00345 TEMP void THIS::InsStateLabelMap(Idx dStateIndex, Idx gState, const DiagLabelSet& labels) {
00346   BASE::StateAttributep(dStateIndex)->AddStateLabelMap(gState, labels);
00347 }
00348 
00349 // SetStateAttr()
00350 // not used
00351 TEMP void THIS::SetStateAttr(Idx dStateIndex, const AttributeDiagnoserState& newAttr) {
00352   BASE::StateAttribute(dStateIndex,newAttr);
00353 }
00354 
00355 // PrettyPrintStateAttr()
00356 TEMP std::string THIS::SAStr(Idx dStateIndex) const {
00357   return BASE::StateAttribute(dStateIndex).Str();
00358 }
00359 
00360 // DotWrite()
00361 TEMP void THIS::DotWrite(const std::string& rFileName) const {
00362   FD_DG("TdiagGenerator(" << this << ")::DotWrite(" << rFileName << ")");
00363   BASE::SetMinStateIndexMap();
00364   StateSet::Iterator lit;
00365   typename BASE::ATransSet::Iterator tit;
00366   try {
00367     std::ofstream stream;
00368     stream.exceptions(std::ios::badbit|std::ios::failbit);
00369     stream.open(rFileName.c_str());
00370     stream << "digraph \"" << BASE::Name() << "\" {" << std::endl;
00371     stream << "  rankdir=LR" << std::endl;
00372     stream << "  node [shape=box];" << std::endl;
00373     stream << std::endl;
00374     stream << "  // initial states" << std::endl;
00375     int i = 1;
00376     for (lit = BASE::InitStatesBegin(); lit != BASE::InitStatesEnd(); ++lit) {
00377       //std::string xname = BASE::StateName(*lit);
00378       std::string xname = SAStr(*lit);
00379       if(xname=="") xname=ToStringInteger(BASE::MinStateIndex(*lit));
00380       stream << "  dot_dummyinit_" << i << " [shape=none, label=\"\" ];" << std::endl;
00381       stream << "  dot_dummyinit_" << i << " -> \"" << xname << "\";" << std::endl; 
00382       i++;
00383     }
00384     stream << std::endl;
00385     stream << "  // marked states" << std::endl;
00386     for (lit = BASE::MarkedStatesBegin(); lit != BASE::MarkedStatesEnd(); ++lit) {
00387       //std::string xname= BASE::StateName(*lit);
00388       std::string xname = SAStr(*lit);
00389       if(xname=="") xname=ToStringInteger(BASE::MinStateIndex(*lit));
00390       stream << "  \"" << xname << "\" [shape=doubleoctagon];" << std::endl;
00391     }
00392     stream << std::endl;
00393     stream << "  // rest of stateset" << std::endl;
00394     for (lit = BASE::StatesBegin(); lit != BASE::StatesEnd(); ++lit) {
00395       if (! (BASE::ExistsInitState(*lit) || BASE::ExistsMarkedState(*lit)) ) {
00396   //std::string xname = BASE::StateName(*lit);
00397   std::string xname = SAStr(*lit);
00398   if(xname=="") xname=ToStringInteger(BASE::MinStateIndex(*lit));
00399   stream << "  \"" << xname << "\";" << std::endl;
00400   //cout << "Label of state " << xname << ": " << PrettyPrintStateAttr(*lit) << endl;
00401       }
00402     }
00403     stream << std::endl;
00404     stream << "  // transition relation" << std::endl;
00405     for (tit = BASE::TransRelBegin(); tit != BASE::TransRelEnd(); ++tit) {
00406       //std::string x1name= BASE::StateName(tit->X1);
00407       std::string x1name = SAStr(tit->X1);
00408       if(x1name=="") x1name=ToStringInteger(BASE::MinStateIndex(tit->X1));
00409       //std::string x2name= BASE::StateName(tit->X2);
00410       std::string x2name = SAStr(tit->X2);
00411       if(x2name=="") x2name=ToStringInteger(BASE::MinStateIndex(tit->X2));
00412       stream << "  \"" << x1name  << "\" -> \"" << x2name
00413        << "\" [label=\"" << BASE::EventName(tit->Ev) << "\"];" << std::endl;
00414     }
00415     stream << "};" << std::endl;
00416     stream.close();
00417   }
00418   catch (std::ios::failure&) {
00419     throw Exception("TdiagGenerator::DotWrite", 
00420         "Exception opening/writing dotfile \""+rFileName+"\"", 2);
00421   } 
00422   BASE::ClearMinStateIndexMap();
00423 }
00424 
00425 
00426 
00427 #undef THIS
00428 #undef BASE
00429 #undef TEMP
00430 
00431 }  // namespace faudes
00432 
00433 #endif

libFAUDES 2.18b --- 2010-12-17 --- c++ source docu by doxygen 1.6.3