libFAUDES

Sections

Index

sp_simconditionset.h

Go to the documentation of this file.
00001 /** @file sp_simconditionset.h  Set of named simulation conditions */
00002 
00003 /*
00004    Copyright (C) 2008  Thomas Moor
00005    Exclusive copyright is granted to Klaus Schmidt
00006 */
00007 
00008 
00009 #include "tokenreader.h"
00010 #include "tokenwriter.h"
00011 #include "nameset.h"
00012 #include "attributes.h"
00013 #include "sp_densityfnct.h"
00014 
00015 
00016 #ifndef FAUDES_SP_SIMCONDITIONSET_H
00017 #define FAUDES_SP_SIMCONDITIONSET_H
00018 
00019 namespace faudes {
00020 
00021 /** 
00022 
00023 @defgroup SimConditionAttributes Simulation Condition Attributes 
00024 
00025 @ingroup SimulatorPlugin 
00026 
00027 Attributes to define simulation conditions used for statistic analysis
00028 of simulation runs.
00029 
00030 @copydoc faudes::SimConditionAttribute
00031 
00032 @{ 
00033 
00034 */
00035 
00036 /**  
00037  * Defining data of event based simulation condition
00038  *
00039  * An event based condition has a set of start events and a set of
00040  * stop events. It becomes satisfied when a start event occurs and it 
00041  * becomes unsatisfied when a stop event occurs. Thus, when start or stop
00042  * events fail to alternate, the first event of the respective type is relevant.
00043  *
00044  */
00045 class SimEventConditionAttribute {
00046  public:
00047   EventSet mStart;
00048   EventSet mStop;
00049   bool operator==(const SimEventConditionAttribute rOther) const { 
00050     return mStart==rOther.mStart && mStop==rOther.mStop;}; 
00051 };
00052 
00053 /**  
00054  * Defining data of state based simulation condition
00055  *
00056  * A state based condition has a set of states per generator an is satisfied
00057  * while the components of the current discrete state vector are within the
00058  * respective set. A flag indicates whether each or some component 
00059  * must comply with the state set to satisfy the condition.
00060  * 
00061  * Note: Since state indices are local to generators, a SimStateConditionAttribute 
00062  * is only meaningful in the context of a particular ParallelExecutor. 
00063  *
00064  */
00065 
00066 // state based condition
00067 class  SimStateConditionAttribute {
00068 public:
00069   bool mAllFlag;
00070   std::vector<StateSet> mStateSets;
00071   bool operator==(const SimStateConditionAttribute rOther) const { 
00072     return mAllFlag==rOther.mAllFlag && mStateSets==rOther.mStateSets;}; 
00073 };
00074 
00075 
00076 /**
00077  * Attribute for a simulation condition
00078  *
00079  * In order to extract statistical data from a simulation by a LoggingExecutor
00080  * or some derived class, so called simulation conditions
00081  * are defined. At any instance of time, a condition is satisfied or
00082  * dissatisfied. Statistical data can then be requested regarding 
00083  * the period and duration of each condition. Currently, two
00084  * types of conditions are available:
00085  *
00086  * - SimStateConditionAttribute addresses conditions of the current state
00087  * - SimEventConditionAttribute addresses the external event sequence.
00088  *
00089  * A Condition may be flagged as a break condition to halt simulation
00090  * when satisfied. A condition may be enabled for tracking or not.
00091  *
00092  * The class SimConditionAttribute summarizes all data to represent
00093  * faudes simulation conditions. It also holds some state of the condition
00094  * wrt execution and provides an interface for sampling. The latter 
00095  * may be seperated to a different class in a future revision. The class
00096  * SimConditionAttribute does, however, not implement any test whether
00097  * or not a condition is satisfied. This is done by the LoggingExecutor.
00098  *
00099  * As a faudes attribute, conditions can be referenced by 
00100  * names the via std faudes container TaNameSet. For token io, a
00101  * ParallelExecutor should be provided as context to access symbolic
00102  * state names. The file format of a set of
00103  * events equipped with condition attributes is illustrated by the below example
00104  * to monitor some performance aspects of one simple machine:
00105  * 
00106  * @code
00107  *
00108  * <Conditions>
00109  *
00110  * % monitor when the machine is idle
00111  * "IdleCond"      
00112  * <StateCondition>
00113  * <StateSet> "idle" </StateSet>
00114  * </StateCondition>
00115  *
00116  * % halt simulation  when the machine is down
00117  * % however, the condition is disabled
00118  * "DownCond"      
00119  * +Break+     
00120  * +Disabled+     
00121  * <StateCondition>
00122  * <StateSet> "down" </StateSet>
00123  * </StateCondition>
00124  *
00125  * % monitor how long the prozessing one work piece takes
00126  * "OperationCond"     
00127  * <EventCondition>
00128  * <StartEvents>  "alpha" </StartEvents>
00129  * <StopEvents>   "beta"  </StopEvents>
00130  * </EventCondition>
00131  *
00132  * </Conditions>
00133  *
00134  * @endcode
00135  */
00136 
00137 class SimConditionAttribute : public AttributeVoid {
00138 
00139 FAUDES_TYPE_DECLARATION(SimConditionAttribute,AttributeVoid)
00140 
00141   public:
00142 
00143   /** Convenience typedef */
00144   typedef std::vector<StateSet>::iterator Iterator;
00145   typedef std::vector<StateSet>::const_iterator CIterator;
00146 
00147   /** 
00148    * Default constructor. Constructs a SimConditionAttribute of 
00149    * with no type ie neither state condition nor event condition.
00150    */
00151   SimConditionAttribute(void);
00152 
00153   /** 
00154    * Copy constructor.
00155    * with no type ie neither state condition nor event condition.
00156    */
00157   SimConditionAttribute(const SimConditionAttribute& rOther);
00158 
00159   /** 
00160    * Test for default value 
00161    * 
00162    * @return
00163    *   True, if this attribute has its default value
00164    */
00165   virtual bool IsDefault(void) const {
00166     return !mEventCondition && !mStateCondition && mEnabled && !mBreakCondition; };
00167 
00168 
00169   /** 
00170    * Test for state condition.
00171    *
00172    * @return
00173    *   True, if this attribute defines a state condition
00174    */
00175   bool IsStateCondition(void) const {return mStateCondition; };
00176 
00177   /** 
00178    * Test for event condition.
00179    *
00180    * @return
00181    *   True, if this attribute defines an event condition
00182    */
00183   bool IsEventCondition(void) const {return mEventCondition; };
00184 
00185   /** 
00186    * Test for break condition.
00187    *
00188    * @return
00189    *   True, if this condition halts simulation.
00190    */
00191   bool IsBreakCondition(void) const {return mBreakCondition; };
00192 
00193   /** 
00194    * Test whether condition is enabled.
00195    *
00196    * @return
00197    *   True, if this condition is monitored during simulation.
00198    */
00199   bool IsEnabled(void) const {return mEnabled; };
00200 
00201   /** 
00202    * Set state condition attribute. Define this attribute to
00203    * represent the specified state condition.
00204    *
00205    * @param rStateConditionAttribute
00206    *     Define state condition
00207    */
00208   void StateCondition(const SimStateConditionAttribute&  rStateConditionAttribute) 
00209     {mStateConditionAttribute=rStateConditionAttribute; mStateCondition=true; mEventCondition=false;
00210     for(Idx i=0; i<mStateConditionAttribute.mStateSets.size(); i++) 
00211       mStateConditionAttribute.mStateSets.at(i).Name("StateSet");
00212   };
00213 
00214   /** 
00215    * Set event condition attribute. Define this attribute to
00216    * represent the specified event condition.
00217    *
00218    * @param rEventConditionAttribute
00219    *     Define event condition
00220    */
00221   void EventCondition(const SimEventConditionAttribute&  rEventConditionAttribute) 
00222     {mEventConditionAttribute=rEventConditionAttribute; mEventCondition=true; mStateCondition=false;};
00223 
00224   /** 
00225    * Set break flag.
00226    * 
00227    * @param on
00228    *   True, to indicate that this condition halts simulation.
00229    */
00230   void BreakCondition(bool on) {mBreakCondition=on; };
00231 
00232   /** 
00233    * Set enabled flag.
00234    * 
00235    * @param on
00236    *   True, to indicate that this condition is to be monitored during simulation.
00237    */
00238   void Enabled(bool on) {mEnabled=on; };
00239 
00240   /**  
00241    * Get event condition. Note that the attribute can only return
00242    * meaningful data if it actually is an event condition.  
00243    *
00244    *  @return
00245    *    Defining data of this attribute's event condition.
00246    *
00247    */
00248   const SimEventConditionAttribute& EventCondition(void) const {return mEventConditionAttribute; };
00249 
00250   /**  
00251    * Get state condition. Note that the attribute can only return
00252    * meaningful data if it actually is an state condition.  
00253    *
00254    *  @return
00255    *    Defining data of this attribute's state condition.
00256    *
00257    */
00258   const SimStateConditionAttribute& StateCondition(void) const {return mStateConditionAttribute; };
00259 
00260   /**
00261    *  Reset conditions execution state. The execution state of a condition
00262    *  consists of all data accumulated during simulation eg statistical data
00263    *  and whether or not the condition is currently satisfied. The execution state
00264    *  resides in the attribute for pragmatic reasons only.
00265    */
00266   void Reset(void) 
00267   { mSatisfied=false; mActivationTime=-1; mSamplesPeriod.Clear(); mSamplesDuration.Clear(); };
00268 
00269   /** 
00270    * Test whether the condition is currently satisfied. This is part
00271    * of the condition execution state. 
00272    *
00273    * @return
00274    *   True, if the conditions is considered satisfied
00275    */
00276   bool Satisfied(void) const { return mSatisfied; };
00277 
00278   /** 
00279    * Set the condition to be satisfied. This is part
00280    * of the condition execution state. Since it is the executor
00281    * that determines whether a condition is satisfied, and since
00282    * the condition state resides in the attribute, the executor
00283    * is meant to notify state changes.
00284    *
00285    * @param on
00286    *   True, if the conditions is considered satisfied
00287    * @param now
00288    *   Time at which the state change occures
00289    */
00290   void Satisfied(bool on, tpTime::Type now);
00291    
00292   /** 
00293    * Sampled period, at which this condition becomes satisfied. 
00294    */
00295   SampledDensityFunction mSamplesPeriod;
00296 
00297   /** 
00298    * Sampled durations, for which this condition remains satisfied. 
00299    */
00300   SampledDensityFunction mSamplesDuration;
00301 
00302 protected:
00303  
00304   /** Indicate precense of a event condition */
00305   bool mEventCondition;
00306 
00307   /** Indicate precense of a state condition */
00308   bool mStateCondition;
00309 
00310   /** Indicate that we halt simulation when this condition is satisfied */
00311   bool mBreakCondition;
00312 
00313   /** Indicate that this condition should be considere at all */
00314   bool mEnabled;
00315 
00316   /** Event based condition data */
00317   SimEventConditionAttribute mEventConditionAttribute;
00318 
00319   /** State based condition data */
00320   SimStateConditionAttribute mStateConditionAttribute;
00321 
00322   /** Condotion state: recorded as satisfied */
00323   bool mSatisfied;
00324 
00325   /** Condition state: when last satisfied became true */
00326   tpTime::Type mActivationTime;
00327    
00328 
00329  protected:
00330 
00331   /**
00332    * Assignment method. 
00333    *
00334    * @param rSrcAttr
00335    *    Source to assign from
00336    */
00337   SimConditionAttribute& DoAssign(const SimConditionAttribute& rSrcAttr);
00338 
00339   /**
00340    * Equality method. 
00341    *
00342    * @param rAttr
00343    *    Source to compare with
00344    */
00345   bool DoEqual(const SimConditionAttribute& rAttr) const;
00346 
00347   /**
00348    * Reads the attribute from TokenReader, see AttributeVoid for public wrappers.
00349    * 
00350    * If the current token indicates a condition section, the method reads the  
00351    * condition data from that section. Else it does nothing. Exceptions may only be thrown
00352    * on invalid data within the condition section. The label argiment is ignored, we 
00353    * use hardcoded labled "EventCondition" and "StateCondition" to figure the type of 
00354    * condition. When a ParallelExecutor is provided as context, it is used to interpret
00355    * symbolc state names of a state condition.
00356    *
00357    * @param rTr
00358    *   TokenReader to read from
00359    * @param rLabel
00360    *   Section to read
00361    * @param pContext
00362    *   Read context to provide contextual information
00363    *
00364    * @exception Exception
00365    *   - IO error (id 1)
00366    */
00367   virtual void DoRead(TokenReader& rTr, const std::string& rLabel="", const Type* pContext=0);
00368  
00369   /**
00370    * Writes the attribute to TokenWriter, see AttributeVoid for public wrappers.
00371    *
00372    * Writes a condition section to include data on state- or event-condition. The label argument
00373    * is ignored, we use hardcoded keywords "EventCondition" and StateCondition". When a 
00374    * ParallelExecutor is provided as context, it state conditions are written with 
00375    * symbolic state names.
00376    *
00377    * @param rTw
00378    *   TokenWriter to write to
00379    * @param rLabel
00380    *   Section to write
00381    * @param pContext
00382    *   Read context to provide contextual information
00383    *
00384    * @exception Exception
00385    *   - IO error (id 2)
00386    */
00387   virtual void DoWrite(TokenWriter& rTw,const std::string& rLabel="", const Type* pContext=0) const;
00388 
00389 
00390 
00391 }; // class SimConditionAttribute
00392 
00393 
00394 /**
00395  * Set of simulation named conditions. 
00396  *
00397  * Note: we currently share a symboltabel with the global
00398  * event set. This will definitely change in a future revision.
00399  */
00400 
00401 class SimConditionSet : public TaNameSet<SimConditionAttribute> {
00402 
00403 FAUDES_TYPE_DECLARATION(SimConditionSet,TaNameSet<SimConditionAttribute>)
00404 
00405  public:
00406 
00407   /** Default constructor */
00408   SimConditionSet(void);
00409 
00410   /** Copy constructor */
00411   SimConditionSet(const SimConditionSet& rOtherSet);
00412 
00413   /**  Virtual destructor */
00414   virtual ~SimConditionSet(void) {};
00415 
00416   /** Test condition for enabled */
00417   bool Enabled(Idx cond) const { return Attribute(cond).IsEnabled(); };
00418 
00419   /** Set condition enabled */
00420   void Enabled(Idx cond, bool on) { Attributep(cond)->Enabled(on); };
00421 
00422   /** Get set of enabled conditions */
00423   SimConditionSet EnabledConditions(void);
00424 
00425   /** Reset all condition states */
00426   void Reset(void);
00427 
00428 protected:
00429 
00430  /**
00431   * Assign from other condition set. 
00432   *
00433   * @param rSourceSet
00434   *   Destination to copy from
00435   * @return
00436   *   ref to this set
00437   */
00438  virtual SimConditionSet& DoAssign(const SimConditionSet& rSourceSet) {
00439    TaNameSet<SimConditionAttribute>::DoAssign(rSourceSet); return *this; };
00440 
00441 
00442  
00443 
00444 };
00445 
00446 
00447 
00448 /** @} doxygen group */
00449 
00450 } // namespace
00451 
00452 #endif

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