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