sp_simeventset.cpp

Go to the documentation of this file.
00001 
00004 /* 
00005    Copyright (C) 2008  Thomas Moor
00006    Exclusive copyright is granted to Klaus Schmidt
00007 */
00008 
00009 #include "sp_simeventset.h"
00010  
00011 namespace faudes {
00012 
00013 
00014 //DoWrite(rTw);
00015 void SimEventAttribute::DoWrite(TokenWriter& rTw, const std::string& rLabel, const Type* pContext) const {
00016   (void) rLabel;
00017   // do base first
00018   AttributeCFlags::DoWrite(rTw,"",pContext);
00019   FD_DC("SimEventAttribute(" << this << ")::DoWrite(tr)");
00020   Token token;
00021   // write priority
00022   if(mPriority) {
00023     rTw.WriteBegin("Priority");
00024     Token token;
00025     token.SetInteger(mPriorityAttribute.mPriority);
00026     rTw.Write(token);
00027     rTw.WriteEnd("Priority");
00028   }
00029   // write stochastic
00030   if(mStochastic) {
00031     rTw.WriteBegin("Stochastic");
00032     if(mStochasticAttribute.mType==SimStochasticEventAttribute::Extern) rTw.WriteOption("Extern");
00033     if(mStochasticAttribute.mType==SimStochasticEventAttribute::Trigger) rTw.WriteOption("Trigger");
00034     if(mStochasticAttribute.mType==SimStochasticEventAttribute::Delay) rTw.WriteOption("Delay");
00035     if(mStochasticAttribute.mPdf==SimStochasticEventAttribute::Exponential) rTw.WriteOption("Exponential");
00036     if(mStochasticAttribute.mPdf==SimStochasticEventAttribute::Gauss) rTw.WriteOption("Gauss");
00037     if(mStochasticAttribute.mPdf==SimStochasticEventAttribute::Uniform) rTw.WriteOption("Uniform");
00038     if(mStochasticAttribute.mParameter.size()>0) {
00039       rTw.WriteBegin("Parameter");
00040       std::vector<double>::const_iterator pit=mStochasticAttribute.mParameter.begin();
00041       for(; pit!=mStochasticAttribute.mParameter.end(); pit++) {
00042   rTw.WriteFloat(*pit);
00043       }
00044       rTw.WriteEnd("Parameter");
00045     }
00046     rTw.WriteEnd("Stochastic");
00047   }
00048 }
00049 
00050 //DoRead(rTr)
00051 void SimEventAttribute::DoRead(TokenReader& rTr, const std::string& rLabel, const Type* pContext) {
00052   (void) rLabel;
00053   // call base first
00054   AttributeCFlags::DoRead(rTr,"",pContext);
00055 
00056   // report
00057   FD_DC("SimEventAttribute(" << this << ")::DoRead(tr)");
00058 
00059   // clear myself
00060   mStochastic=false;
00061   mPriority=false;
00062 
00063   // read as long as we can handle
00064   Token token;
00065   bool err=false;
00066   while(rTr.Peek(token)) {
00067 
00068     // try priority
00069     if(token.Type()==Token::Begin && token.StringValue()=="Priority") {
00070       rTr.ReadBegin("Priority");
00071       mPriority=true;
00072       rTr.Get(token);
00073       if((token.Type()!=Token::Float) && (token.Type()!=Token::Integer)) { // accept negative
00074         err=true; 
00075       } else {
00076         mPriorityAttribute.mPriority=(int) token.FloatValue();
00077       }
00078       rTr.ReadEnd("Priority");
00079       continue;
00080     }
00081 
00082     // try stochastic
00083     if(token.Type()==Token::Begin && token.StringValue()=="Stochastic") {
00084       rTr.ReadBegin("Stochastic");
00085       // stochastic default
00086       mStochastic=true;
00087       mStochasticAttribute.mType = SimStochasticEventAttribute::Extern;
00088       mStochasticAttribute.mPdf = SimStochasticEventAttribute::Exponential;
00089       mStochasticAttribute.mParameter.clear();
00090       mStochasticAttribute.mParameter.push_back(1);
00091       // mandatory option: intern/extern
00092       rTr.Get(token);
00093       if (token.Type() != Token::Option) {
00094   err=true; 
00095       } else {
00096         if(token.StringValue() == "Extern") 
00097     {mStochasticAttribute.mType = SimStochasticEventAttribute::Extern; };
00098         if(token.StringValue() == "Trigger") 
00099     {mStochasticAttribute.mType = SimStochasticEventAttribute::Trigger; };
00100         if(token.StringValue() == "Delay") 
00101     {mStochasticAttribute.mType = SimStochasticEventAttribute::Delay; };
00102       } 
00103       // mandatory option: exponential/gauss/ etc
00104       rTr.Get(token);
00105       if (token.Type() != Token::Option) {
00106   err=true; 
00107       } else {
00108         if(token.StringValue() == "Exponential") 
00109       {mStochasticAttribute.mPdf = SimStochasticEventAttribute::Exponential;};
00110         if(token.StringValue() == "Gauss") 
00111     {mStochasticAttribute.mPdf = SimStochasticEventAttribute::Gauss;};
00112         if(token.StringValue() == "Uniform") 
00113       {mStochasticAttribute.mPdf = SimStochasticEventAttribute::Uniform;};
00114       }  
00115       // optional data: parameter vector
00116       rTr.Peek(token);
00117       if(token.StringValue()=="Parameter" && token.Type()==Token::Begin) {
00118         mStochasticAttribute.mParameter.clear();
00119         rTr.ReadBegin("Parameter");
00120         while (!rTr.Eos("Parameter")) {
00121           double par = rTr.ReadFloat();
00122           mStochasticAttribute.mParameter.push_back(par);      
00123         }
00124         rTr.ReadEnd("Parameter");
00125       }
00126       // check parameter vector consistency
00127       if(mStochasticAttribute.mPdf == SimStochasticEventAttribute::Exponential 
00128          && mStochasticAttribute.mParameter.size() != 1) 
00129         err=true;
00130       if(mStochasticAttribute.mPdf == SimStochasticEventAttribute::Gauss 
00131          && mStochasticAttribute.mParameter.size() != 2) 
00132         err=true;
00133       if(mStochasticAttribute.mPdf == SimStochasticEventAttribute::Uniform 
00134          && mStochasticAttribute.mParameter.size() != 2) 
00135         err=true;
00136       rTr.ReadEnd("Stochastic");
00137     }
00138 
00139     // report error
00140     if(err) {
00141       std::stringstream errstr;
00142       errstr << "invalid simulation event property" << rTr.FileLine();
00143       throw Exception("SimEventAttribute::Read", errstr.str(), 52);
00144     }
00145 
00146     // cannot digest? -> done
00147     break;
00148     
00149   }
00150 }
00151 
00152 
00153 
00154 
00155 } // namespace faudes
00156 

Generated on Mon Nov 10 08:13:15 2008 for libFAUDES 2.11v by  doxygen 1.4.4