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

libFAUDES 2.14g --- 2009-12-3 --- c++ source docu by doxygen 1.5.6