sp_simconditionset.cpp

Go to the documentation of this file.
00001 /** @file sp_simconditionset.cpp  Set of named simulation conditions */
00002 
00003 
00004 /* 
00005    Copyright (C) 2008  Thomas Moor
00006    Exclusive copyright is granted to Klaus Schmidt
00007 */
00008 
00009 #include "sp_simconditionset.h"
00010 #include "sp_lpexecutor.h"
00011 
00012 namespace faudes {
00013 
00014 /*
00015 ************************************************
00016 ************************************************
00017 ************************************************
00018 
00019 implementation of attributes
00020 
00021 ************************************************
00022 ************************************************
00023 ************************************************
00024 */
00025 
00026 
00027 
00028 
00029 // faudes type std
00030 FAUDES_TYPE_IMPLEMENTATION(Void,AttributeSimCondition,AttributeFlags)
00031 
00032 
00033 // construct
00034 AttributeSimCondition::AttributeSimCondition(void) : 
00035   AttributeFlags(),
00036   mEventCondition(false), 
00037   mStateCondition(false)
00038 { 
00039   FD_DX("AttributeSimCondition::AttributeSimCondition()");
00040   mFlags=mDefSCFlags;
00041   Reset();
00042 }
00043 
00044 // construct
00045 AttributeSimCondition::AttributeSimCondition(const AttributeSimCondition& rOther) : 
00046   AttributeFlags(),
00047   mEventCondition(false), 
00048   mStateCondition(false) 
00049 { 
00050   FD_DX("AttributeSimCondition::AttributeSimCondition(attr)");
00051   DoAssign(rOther);
00052 }
00053 
00054 // Assign my members
00055 void AttributeSimCondition::DoAssign(const AttributeSimCondition& rSrcAttr) { 
00056   // call base (incl. virtual clear)
00057   AttributeFlags::DoAssign(rSrcAttr);
00058   // my additional members
00059   mEventCondition=rSrcAttr.mEventCondition;
00060   mStateCondition=rSrcAttr.mStateCondition;
00061   mEventConditionAttribute=rSrcAttr.mEventConditionAttribute;
00062   mStateConditionAttribute=rSrcAttr.mStateConditionAttribute;
00063   // my state
00064   mSatisfied=false;
00065   mActivationTime=0;
00066   mSamplesPeriod.Clear();
00067   mSamplesDuration.Clear();
00068 }
00069 
00070 
00071 // Test for equality
00072 bool AttributeSimCondition::DoEqual(const AttributeSimCondition& rAttr) const { 
00073   // call base (incl. virtual clear)
00074   if(!AttributeFlags::DoEqual(rAttr)) return false;
00075   // test my defining members
00076   if(mEventCondition!=rAttr.mEventCondition) return false;
00077   if(mStateCondition!=rAttr.mStateCondition) return false;
00078   if(!(mEventConditionAttribute==rAttr.mEventConditionAttribute)) return false;
00079   if(!(mStateConditionAttribute==rAttr.mStateConditionAttribute)) return false;
00080   // pass
00081   return true;
00082 }
00083 
00084 
00085 //DoWrite(rTw);
00086 void AttributeSimCondition::DoWrite(TokenWriter& rTw, const std::string& rLabel, const Type* pContext) const {
00087   (void) rLabel;
00088   Token token;
00089 
00090   // report context
00091   FD_DC("AttributeSimCondition::DoWrite(rTr," << rLabel <<", " << pContext << "): cast " <<
00092      dynamic_cast<const ParallelExecutor*>(pContext));
00093 
00094   // write event condition
00095   if(mEventCondition) {
00096     rTw.WriteBegin("EventCondition");
00097     if(Enabled()) rTw.WriteOption("Enabled");
00098     if(!Enabled()) rTw.WriteOption("Disabled");
00099     if(Breakpoint()) rTw.WriteOption("Break");
00100     mEventConditionAttribute.mStart.Write(rTw,"StartEvents");
00101     mEventConditionAttribute.mStop.Write(rTw,"StopEvents");
00102     rTw.WriteEnd("EventCondition");
00103   }
00104 
00105   // write state condition
00106   if(mStateCondition) {
00107     rTw.WriteBegin("StateCondition");
00108     if(Enabled()) rTw.WriteOption("Enabled");
00109     if(!Enabled()) rTw.WriteOption("Disabled");
00110     if(Breakpoint()) rTw.WriteOption("Break");
00111     if(mStateConditionAttribute.mAllFlag) rTw.WriteOption("Conjunction");
00112     if(!mStateConditionAttribute.mAllFlag) rTw.WriteOption("Disjunction");
00113     // get context
00114     const ParallelExecutor* executor= dynamic_cast<const ParallelExecutor*>(pContext);
00115     Idx n=mStateConditionAttribute.mStateSets.size();
00116     // test context
00117     if(executor)
00118       if(executor->Size()!=n) {
00119         std::stringstream errstr;
00120         errstr << "Invalid ParalleExecutor context (dimension mismatch)";
00121         throw Exception("AttributeSimCondition::DoWrite", errstr.str(), 501);
00122       }
00123     // write the sets
00124     for(Idx i=0; i<n; i++) {
00125       const StateSet& set=mStateConditionAttribute.mStateSets.at(i);
00126       if(executor) executor->At(i).Generator().WriteStateSet(rTw,set);
00127       else set.Write(rTw,"StateSet");
00128     }
00129     rTw.WriteEnd("StateCondition");
00130   }
00131 }
00132 
00133 //DoRead(rTr)
00134 void AttributeSimCondition::DoRead(TokenReader& rTr, const std::string& rLabel, const Type* pContext) {
00135   (void) rLabel;
00136   bool err;
00137   Token token;
00138 
00139   // report context
00140   FD_DC("AttributeSimCondition::DoRead(rTr," << rLabel <<", " << pContext << "): cast " <<
00141      dynamic_cast<const ParallelExecutor*>(pContext));
00142 
00143   // clear myself
00144   err=false;
00145   mEventCondition=false;
00146   mStateCondition=false;
00147   Enabled(true);
00148   Breakpoint(false);
00149   mEventConditionAttribute.mStart.Clear();
00150   mEventConditionAttribute.mStop.Clear();
00151   mStateConditionAttribute.mStateSets.clear();
00152   mStateConditionAttribute.mAllFlag=false;
00153 
00154   // switch by toke
00155   rTr.Peek(token);
00156 
00157   // read event condition
00158   if(token.Type()==Token::Begin && token.StringValue()=="EventCondition") {
00159     rTr.ReadBegin("EventCondition");
00160     mEventCondition=true;
00161     while(!rTr.Eos("EventCondition")) {
00162       rTr.Peek(token);
00163       if((token.IsOption()) && (token.OptionValue()=="Enabled")) {
00164   Enabled(true);
00165   rTr.Get(token);
00166         continue;
00167       }
00168       if((token.IsOption()) && (token.OptionValue()=="Disabled")) {
00169   Enabled(false);
00170   rTr.Get(token);
00171         continue;
00172       }
00173       if((token.IsOption()) && (token.OptionValue()=="Break")) {
00174   Breakpoint(true);
00175   rTr.Get(token);
00176         continue;
00177       }
00178       if((token.IsBegin()) && (token.StringValue()=="StartEvents")) {
00179         mEventConditionAttribute.mStart.Read(rTr,"StartEvents");
00180         continue;
00181       }
00182       if((token.IsBegin()) && (token.StringValue()=="StopEvents")) {
00183         mEventConditionAttribute.mStop.Read(rTr,"StopEvents");
00184         continue;
00185       }
00186       // ignore errors
00187       rTr.Get(token);
00188     }
00189     rTr.ReadEnd("EventCondition");
00190   }
00191 
00192   // read state condition
00193   if(token.Type()==Token::Begin && token.StringValue()=="StateCondition") {
00194     rTr.ReadBegin("StateCondition");
00195     mStateCondition=true;
00196     while(!rTr.Eos("StateCondition")) {
00197       rTr.Peek(token);
00198       if((token.IsOption()) && (token.OptionValue()=="Enabled")) {
00199   Enabled(true);
00200   rTr.Get(token);
00201         continue;
00202       }
00203       if((token.IsOption()) && (token.OptionValue()=="Disabled")) {
00204   Enabled(false);
00205   rTr.Get(token);
00206         continue;
00207       }
00208       if((token.IsOption()) && (token.OptionValue()=="Break")) {
00209   Breakpoint(true);
00210   rTr.Get(token);
00211         continue;
00212       }
00213       if((token.IsOption()) && (token.OptionValue()=="Conjunction")) {
00214   mStateConditionAttribute.mAllFlag=true;
00215   rTr.Get(token);
00216         continue;
00217       }
00218       if((token.IsBegin()) && (token.StringValue()=="StateSet")) {
00219         // get context
00220         const ParallelExecutor* executor= dynamic_cast<const ParallelExecutor*>(pContext);
00221         Idx i=mStateConditionAttribute.mStateSets.size();
00222         // test context
00223         if(executor)
00224           if(executor->Size()<=i) {
00225             std::stringstream errstr;
00226             errstr << "Invalid ParalleExecutor context (dimension mismatch)";
00227             throw Exception("AttributeSimCondition::DoRead", errstr.str(), 501);
00228           }
00229         // read the set
00230         StateSet set;
00231         if(executor) executor->At(i).Generator().ReadStateSet(rTr,"StateSet",set);
00232         else set.Read(rTr,"StateSet");
00233         set.Name("StateSet");
00234         mStateConditionAttribute.mStateSets.push_back(set);
00235         continue;
00236       }
00237       // ignore errors
00238       rTr.Get(token);
00239     }
00240     rTr.ReadEnd("StateCondition");
00241   }
00242   // report error
00243   if(err) {
00244     std::stringstream errstr;
00245     errstr << "invalid simulation condition" << rTr.FileLine();
00246     throw Exception("AttributeSimCondition::Read", errstr.str(), 52);
00247   }
00248 }
00249 
00250 // condition state: sample
00251 void AttributeSimCondition::Satisfied(bool on, tpTime::Type now) {
00252   if(on==mSatisfied) return;
00253   mSatisfied=on;
00254   // newly activated
00255   if(mSatisfied) {
00256     if(mActivationTime>0) mSamplesPeriod.Sample(now-mActivationTime); 
00257     mActivationTime=now;
00258   }
00259   // newly deactivated
00260   if(!mSatisfied) {
00261     mSamplesDuration.Sample(now-mActivationTime); 
00262   }
00263 }
00264 
00265 
00266 /*
00267 ************************************************
00268 ************************************************
00269 ************************************************
00270 
00271 implementation of condition set
00272 
00273 ************************************************
00274 ************************************************
00275 ************************************************
00276 */
00277 
00278 // std faudes type (except new)
00279 FAUDES_TYPE_IMPLEMENTATION_COPY(Void,SimConditionSet,TaNameSet<AttributeSimCondition>)
00280 FAUDES_TYPE_IMPLEMENTATION_CAST(Void,SimConditionSet,TaNameSet<AttributeSimCondition>)
00281 FAUDES_TYPE_IMPLEMENTATION_ASSIGN(Void,SimConditionSet,TaNameSet<AttributeSimCondition>)
00282 FAUDES_TYPE_IMPLEMENTATION_EQUAL(Void,SimConditionSet,TaNameSet<AttributeSimCondition>)
00283 
00284 
00285 // Default constructor 
00286 SimConditionSet::SimConditionSet(void) : TaNameSet<AttributeSimCondition>() {
00287   FD_DX("SimConditionSet::SimConditionSet()");
00288 }
00289 
00290 // Copy constructor 
00291 SimConditionSet::SimConditionSet(const SimConditionSet& rOtherSet) : TaNameSet<AttributeSimCondition>(rOtherSet) {
00292   FD_DX("SimConditionSet::SimConditionSet()");
00293 }
00294 
00295 
00296 // std faudes new
00297 SimConditionSet* SimConditionSet::New(void) const {
00298   SimConditionSet* res= new SimConditionSet();
00299   res->mpSymbolTable=mpSymbolTable;
00300   return res;
00301 }
00302 
00303 // get enabled set
00304 SimConditionSet SimConditionSet::EnabledConditions(void) {
00305   SimConditionSet result;
00306   for(Iterator cit=Begin(); cit != End(); ++cit) 
00307     if(Enabled(*cit)) result.Insert(*cit,Attribute(*cit));
00308   return result;
00309 }
00310 
00311 // condition state: reset alls
00312 void SimConditionSet::Reset(void) {
00313   FD_DX("SimConditionSet::Reset()");
00314   for(Iterator cit=Begin(); cit != End(); ++cit) {
00315     AttributeSimCondition* pattr=Attributep(*cit);
00316     pattr->Reset();
00317     pattr->mSamplesPeriod.Name(SymbolicName(*cit)+" - Period");
00318     pattr->mSamplesDuration.Name(SymbolicName(*cit)+" - Duration");
00319   }
00320   FD_DX("SimConditionSet::Reset(): done");
00321 }
00322 
00323 
00324 
00325 } // namespace faudes
00326 

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