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