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 }; // class TdiagGenerator
00200 
00201 
00202 
00203 // convenience typedef for standard diagnoser
00204 typedef TdiagGenerator<AttributeFailureTypeMap, AttributeDiagnoserState, AttributeCFlags, AttributeVoid> Diagnoser;
00205 
00206 /** Compatibility: pre 2.20b used diagGenerator as C++ class name*/
00207 #ifdef FAUDES_COMPATIBILITY
00208 typedef TdiagGenerator<AttributeFailureTypeMap, AttributeDiagnoserState, AttributeCFlags, AttributeVoid> diagGenerator;
00209 #endif
00210 
00211 
00212 /*
00213  ***********************************************************************
00214  ***********************************************************************
00215  ***********************************************************************
00216  ***********************************************************************
00217 
00218  TdiagGenerator implementation
00219 
00220  ***********************************************************************
00221  ***********************************************************************
00222  ***********************************************************************
00223  ***********************************************************************
00224  */
00225 
00226 // convenient scope macors
00227 #define THIS TdiagGenerator<GlobalAttr, StateAttr, EventAttr, TransAttr>
00228 #define BASE TcGenerator<GlobalAttr, StateAttr, EventAttr, TransAttr>
00229 #define TEMP template<class GlobalAttr, class StateAttr, class EventAttr, class TransAttr>
00230 
00231 
00232 // TdiagGenerator() - default constructor
00233 TEMP THIS::TdiagGenerator(void) : BASE() {
00234   FD_DG("TdiagGenerator(" << this << ")::TdiagGenerator()");
00235   mpLabelSymbolTable = DiagLabelSet::StaticLabelSymbolTablep();
00236   BASE::mReindexOnWrite=false;
00237 }
00238 
00239 // TdiagGenerator(rOtherGen)
00240 TEMP THIS::TdiagGenerator(const TdiagGenerator& rOtherGen) : BASE(rOtherGen) {
00241   FD_DG("TdiagGenerator(" << this << ")::TdiagGenerator(rOtherGen)");
00242   mpLabelSymbolTable = DiagLabelSet::StaticLabelSymbolTablep();
00243   BASE::mReindexOnWrite=false;
00244 }
00245 
00246 // TdiagGenerator(rOtherGen)
00247 TEMP THIS::TdiagGenerator(const Generator& rOtherGen) : BASE(rOtherGen) {
00248   FD_DG("TdiagGenerator(" << this << ")::TdiagGenerator(rOtherGen)");
00249   mpLabelSymbolTable = DiagLabelSet::StaticLabelSymbolTablep();
00250   BASE::mReindexOnWrite=false;
00251 }
00252 
00253 // TdiagGenerator(rFileName)
00254 TEMP THIS::TdiagGenerator(const std::string& rFileName) : BASE() {
00255   FD_DG("TDiagGenerator(" << this << ")::TdiagGenerator(rFilename) : done");
00256   mpLabelSymbolTable = DiagLabelSet::StaticLabelSymbolTablep();
00257   BASE::mReindexOnWrite=false;
00258   BASE::Read(rFileName);
00259 }
00260 
00261 // New()
00262 TEMP THIS* THIS::New(void) const {
00263   // allocate
00264   THIS* res = new THIS();
00265   // fix base data
00266   res->EventSymbolTablep(BASE::mpEventSymbolTable);
00267   res->mStateNamesEnabled=BASE::mStateNamesEnabled;
00268   res->mReindexOnWrite=BASE::mReindexOnWrite;  
00269   // fix my data
00270   res->mpLabelSymbolTable=mpLabelSymbolTable;
00271   return res;
00272 }
00273 
00274 // Copy()
00275 TEMP THIS* THIS::Copy(void) const {
00276   // allocate
00277   THIS* res = new THIS(*this);
00278   // done
00279   return res;
00280 }
00281 
00282 
00283 // InsFailureTypeMapping()
00284 TEMP Idx THIS::InsFailureTypeMapping(const std::string& failureType, const EventSet& rfailureEvents) {
00285   return BASE::pGlobalAttribute->AddFailureTypeMapping(failureType, rfailureEvents);
00286 }
00287 
00288 // InsFailureTypeMap()
00289 TEMP void THIS::InsFailureTypeMap(const std::map<std::string,EventSet>& rFailureMap) {
00290   BASE::pGlobalAttribute->AddFailureTypeMap(rFailureMap);
00291 }
00292 
00293 // GetFailureType()
00294 // not used
00295 TEMP Idx THIS::GetFailureType(Idx failureEvent) const {
00296   return BASE::pGlobalAttribute->FailureType(failureEvent);
00297 }
00298 
00299 // GetAllFailureEvents()
00300 TEMP EventSet THIS::GetAllFailureEvents(void) const{
00301   return BASE::pGlobalAttribute->AllFailureEvents();
00302 }
00303 
00304 // InsStateLabelMapping()
00305 TEMP void THIS::InsStateLabelMapping(Idx dStateIndex, Idx gStateIndex, Idx labelIndex) {
00306   BASE::StateAttributep(dStateIndex)->AddStateLabelMapping(gStateIndex, labelIndex);
00307 }
00308 
00309 // InsStateLabelMap()
00310 // not used
00311 TEMP void THIS::InsStateLabelMap(Idx dStateIndex, Idx gState, const DiagLabelSet& labels) {
00312   BASE::StateAttributep(dStateIndex)->AddStateLabelMap(gState, labels);
00313 }
00314 
00315 // SetStateAttr()
00316 // not used
00317 TEMP void THIS::SetStateAttr(Idx dStateIndex, const AttributeDiagnoserState& newAttr) {
00318   BASE::StateAttribute(dStateIndex,newAttr);
00319 }
00320 
00321 // PrettyPrintStateAttr()
00322 TEMP std::string THIS::SAStr(Idx dStateIndex) const {
00323   return BASE::StateAttribute(dStateIndex).Str();
00324 }
00325 
00326 // DotWrite()
00327 TEMP void THIS::DotWrite(const std::string& rFileName) const {
00328   FD_DG("TdiagGenerator(" << this << ")::DotWrite(" << rFileName << ")");
00329   BASE::SetMinStateIndexMap();
00330   StateSet::Iterator lit;
00331   typename BASE::ATransSet::Iterator tit;
00332   try {
00333     std::ofstream stream;
00334     stream.exceptions(std::ios::badbit|std::ios::failbit);
00335     stream.open(rFileName.c_str());
00336     stream << "digraph \"" << BASE::Name() << "\" {" << std::endl;
00337     stream << "  rankdir=LR" << std::endl;
00338     stream << "  node [shape=box];" << std::endl;
00339     stream << std::endl;
00340     stream << "  // initial states" << std::endl;
00341     int i = 1;
00342     for (lit = BASE::InitStatesBegin(); lit != BASE::InitStatesEnd(); ++lit) {
00343       //std::string xname = BASE::StateName(*lit);
00344       std::string xname = SAStr(*lit);
00345       if(xname=="") xname=ToStringInteger(BASE::MinStateIndex(*lit));
00346       stream << "  dot_dummyinit_" << i << " [shape=none, label=\"\" ];" << std::endl;
00347       stream << "  dot_dummyinit_" << i << " -> \"" << xname << "\";" << std::endl; 
00348       i++;
00349     }
00350     stream << std::endl;
00351     stream << "  // marked states" << std::endl;
00352     for (lit = BASE::MarkedStatesBegin(); lit != BASE::MarkedStatesEnd(); ++lit) {
00353       //std::string xname= BASE::StateName(*lit);
00354       std::string xname = SAStr(*lit);
00355       if(xname=="") xname=ToStringInteger(BASE::MinStateIndex(*lit));
00356       stream << "  \"" << xname << "\" [shape=doubleoctagon];" << std::endl;
00357     }
00358     stream << std::endl;
00359     stream << "  // rest of stateset" << std::endl;
00360     for (lit = BASE::StatesBegin(); lit != BASE::StatesEnd(); ++lit) {
00361       if (! (BASE::ExistsInitState(*lit) || BASE::ExistsMarkedState(*lit)) ) {
00362   //std::string xname = BASE::StateName(*lit);
00363   std::string xname = SAStr(*lit);
00364   if(xname=="") xname=ToStringInteger(BASE::MinStateIndex(*lit));
00365   stream << "  \"" << xname << "\";" << std::endl;
00366   //cout << "Label of state " << xname << ": " << PrettyPrintStateAttr(*lit) << endl;
00367       }
00368     }
00369     stream << std::endl;
00370     stream << "  // transition relation" << std::endl;
00371     for (tit = BASE::TransRelBegin(); tit != BASE::TransRelEnd(); ++tit) {
00372       //std::string x1name= BASE::StateName(tit->X1);
00373       std::string x1name = SAStr(tit->X1);
00374       if(x1name=="") x1name=ToStringInteger(BASE::MinStateIndex(tit->X1));
00375       //std::string x2name= BASE::StateName(tit->X2);
00376       std::string x2name = SAStr(tit->X2);
00377       if(x2name=="") x2name=ToStringInteger(BASE::MinStateIndex(tit->X2));
00378       stream << "  \"" << x1name  << "\" -> \"" << x2name
00379        << "\" [label=\"" << BASE::EventName(tit->Ev) << "\"];" << std::endl;
00380     }
00381     stream << "};" << std::endl;
00382     stream.close();
00383   }
00384   catch (std::ios::failure&) {
00385     throw Exception("TdiagGenerator::DotWrite", 
00386         "Exception opening/writing dotfile \""+rFileName+"\"", 2);
00387   } 
00388   BASE::ClearMinStateIndexMap();
00389 }
00390 
00391 
00392 
00393 #undef THIS
00394 #undef BASE
00395 #undef TEMP
00396 
00397 }  // namespace faudes
00398 
00399 #endif

libFAUDES 2.23h --- 2014.04.03 --- c++ api documentaion by doxygen