sp_simconditionset.cpp
Go to the documentation of this file.
1 /** @file sp_simconditionset.cpp Set of named simulation conditions */
2 
3 
4 /*
5  Copyright (C) 2008 Thomas Moor
6  Exclusive copyright is granted to Klaus Schmidt
7 */
8 
9 #include "sp_simconditionset.h"
10 #include "sp_lpexecutor.h"
11 
12 namespace faudes {
13 
14 /*
15 ************************************************
16 ************************************************
17 ************************************************
18 
19 implementation of attributes
20 
21 ************************************************
22 ************************************************
23 ************************************************
24 */
25 
26 
27 
28 
29 // faudes type std
30 FAUDES_TYPE_IMPLEMENTATION(Void,AttributeSimCondition,AttributeFlags)
31 
32 
33 // construct
36  mEventCondition(false),
37  mStateCondition(false)
38 {
39  FD_DX("AttributeSimCondition::AttributeSimCondition()");
40  mFlags=mDefSCFlags;
41  Reset();
42 }
43 
44 // construct
47  mEventCondition(false),
48  mStateCondition(false)
49 {
50  FD_DX("AttributeSimCondition::AttributeSimCondition(attr)");
51  DoAssign(rOther);
52 }
53 
54 // Assign my members
56  // call base (incl. virtual clear)
57  AttributeFlags::DoAssign(rSrcAttr);
58  // my additional members
63  // my state
64  mSatisfied=false;
68 }
69 
70 
71 // Test for equality
73  // call base (incl. virtual clear)
74  if(!AttributeFlags::DoEqual(rAttr)) return false;
75  // test my defining members
76  if(mEventCondition!=rAttr.mEventCondition) return false;
77  if(mStateCondition!=rAttr.mStateCondition) return false;
78  if(!(mEventConditionAttribute==rAttr.mEventConditionAttribute)) return false;
79  if(!(mStateConditionAttribute==rAttr.mStateConditionAttribute)) return false;
80  // pass
81  return true;
82 }
83 
84 
85 //DoWrite(rTw);
86 void AttributeSimCondition::DoWrite(TokenWriter& rTw, const std::string& rLabel, const Type* pContext) const {
87  (void) rLabel;
88  Token token;
89 
90  // report context
91  FD_DC("AttributeSimCondition::DoWrite(rTr," << rLabel <<", " << pContext << "): cast " <<
92  dynamic_cast<const ParallelExecutor*>(pContext));
93 
94  // write event condition
95  if(mEventCondition) {
96  rTw.WriteBegin("EventCondition");
97  if(Enabled()) rTw.WriteOption("Enabled");
98  if(!Enabled()) rTw.WriteOption("Disabled");
99  if(Breakpoint()) rTw.WriteOption("Break");
100  mEventConditionAttribute.mStart.Write(rTw,"StartEvents");
101  mEventConditionAttribute.mStop.Write(rTw,"StopEvents");
102  rTw.WriteEnd("EventCondition");
103  }
104 
105  // write state condition
106  if(mStateCondition) {
107  rTw.WriteBegin("StateCondition");
108  if(Enabled()) rTw.WriteOption("Enabled");
109  if(!Enabled()) rTw.WriteOption("Disabled");
110  if(Breakpoint()) rTw.WriteOption("Break");
111  if(mStateConditionAttribute.mAllFlag) rTw.WriteOption("Conjunction");
112  if(!mStateConditionAttribute.mAllFlag) rTw.WriteOption("Disjunction");
113  // get context
114  const ParallelExecutor* executor= dynamic_cast<const ParallelExecutor*>(pContext);
116  // test context
117  if(executor)
118  if(executor->Size()!=n) {
119  std::stringstream errstr;
120  errstr << "Invalid ParalleExecutor context (dimension mismatch)";
121  throw Exception("AttributeSimCondition::DoWrite", errstr.str(), 501);
122  }
123  // write the sets
124  for(Idx i=0; i<n; i++) {
126  if(executor) executor->At(i).Generator().WriteStateSet(rTw,set);
127  else set.Write(rTw,"StateSet");
128  }
129  rTw.WriteEnd("StateCondition");
130  }
131 }
132 
133 //DoRead(rTr)
134 void AttributeSimCondition::DoRead(TokenReader& rTr, const std::string& rLabel, const Type* pContext) {
135  (void) rLabel;
136  bool err;
137  Token token;
138 
139  // report context
140  FD_DC("AttributeSimCondition::DoRead(rTr," << rLabel <<", " << pContext << "): cast " <<
141  dynamic_cast<const ParallelExecutor*>(pContext));
142 
143  // clear myself
144  err=false;
145  mEventCondition=false;
146  mStateCondition=false;
147  Enabled(true);
148  Breakpoint(false);
153 
154  // switch by toke
155  rTr.Peek(token);
156 
157  // read event condition
158  if(token.Type()==Token::Begin && token.StringValue()=="EventCondition") {
159  rTr.ReadBegin("EventCondition");
160  mEventCondition=true;
161  while(!rTr.Eos("EventCondition")) {
162  rTr.Peek(token);
163  if((token.IsOption()) && (token.OptionValue()=="Enabled")) {
164  Enabled(true);
165  rTr.Get(token);
166  continue;
167  }
168  if((token.IsOption()) && (token.OptionValue()=="Disabled")) {
169  Enabled(false);
170  rTr.Get(token);
171  continue;
172  }
173  if((token.IsOption()) && (token.OptionValue()=="Break")) {
174  Breakpoint(true);
175  rTr.Get(token);
176  continue;
177  }
178  if((token.IsBegin()) && (token.StringValue()=="StartEvents")) {
179  mEventConditionAttribute.mStart.Read(rTr,"StartEvents");
180  continue;
181  }
182  if((token.IsBegin()) && (token.StringValue()=="StopEvents")) {
183  mEventConditionAttribute.mStop.Read(rTr,"StopEvents");
184  continue;
185  }
186  // ignore errors
187  rTr.Get(token);
188  }
189  rTr.ReadEnd("EventCondition");
190  }
191 
192  // read state condition
193  if(token.Type()==Token::Begin && token.StringValue()=="StateCondition") {
194  rTr.ReadBegin("StateCondition");
195  mStateCondition=true;
196  while(!rTr.Eos("StateCondition")) {
197  rTr.Peek(token);
198  if((token.IsOption()) && (token.OptionValue()=="Enabled")) {
199  Enabled(true);
200  rTr.Get(token);
201  continue;
202  }
203  if((token.IsOption()) && (token.OptionValue()=="Disabled")) {
204  Enabled(false);
205  rTr.Get(token);
206  continue;
207  }
208  if((token.IsOption()) && (token.OptionValue()=="Break")) {
209  Breakpoint(true);
210  rTr.Get(token);
211  continue;
212  }
213  if((token.IsOption()) && (token.OptionValue()=="Conjunction")) {
215  rTr.Get(token);
216  continue;
217  }
218  if((token.IsBegin()) && (token.StringValue()=="StateSet")) {
219  // get context
220  const ParallelExecutor* executor= dynamic_cast<const ParallelExecutor*>(pContext);
222  // test context
223  if(executor)
224  if(executor->Size()<=i) {
225  std::stringstream errstr;
226  errstr << "Invalid ParalleExecutor context (dimension mismatch)";
227  throw Exception("AttributeSimCondition::DoRead", errstr.str(), 501);
228  }
229  // read the set
230  StateSet set;
231  if(executor) executor->At(i).Generator().ReadStateSet(rTr,"StateSet",set);
232  else set.Read(rTr,"StateSet");
233  set.Name("StateSet");
234  mStateConditionAttribute.mStateSets.push_back(set);
235  continue;
236  }
237  // ignore errors
238  rTr.Get(token);
239  }
240  rTr.ReadEnd("StateCondition");
241  }
242  // report error
243  if(err) {
244  std::stringstream errstr;
245  errstr << "invalid simulation condition" << rTr.FileLine();
246  throw Exception("AttributeSimCondition::Read", errstr.str(), 52);
247  }
248 }
249 
250 // condition state: sample
252  if(on==mSatisfied) return;
253  mSatisfied=on;
254  // newly activated
255  if(mSatisfied) {
257  mActivationTime=now;
258  }
259  // newly deactivated
260  if(!mSatisfied) {
262  }
263 }
264 
265 
266 /*
267 ************************************************
268 ************************************************
269 ************************************************
270 
271 implementation of condition set
272 
273 ************************************************
274 ************************************************
275 ************************************************
276 */
277 
278 // std faudes type (except new)
283 
284 
285 // Default constructor
287  FD_DX("SimConditionSet::SimConditionSet()");
288 }
289 
290 // Copy constructor
292  FD_DX("SimConditionSet::SimConditionSet()");
293 }
294 
295 
296 // std faudes new
298  SimConditionSet* res= new SimConditionSet();
300  return res;
301 }
302 
303 // get enabled set
305  SimConditionSet result;
306  for(Iterator cit=Begin(); cit != End(); ++cit)
307  if(Enabled(*cit)) result.Insert(*cit,Attribute(*cit));
308  return result;
309 }
310 
311 // condition state: reset alls
313  FD_DX("SimConditionSet::Reset()");
314  for(Iterator cit=Begin(); cit != End(); ++cit) {
315  AttributeSimCondition* pattr=Attributep(*cit);
316  pattr->Reset();
317  pattr->mSamplesPeriod.Name(SymbolicName(*cit)+" - Period");
318  pattr->mSamplesDuration.Name(SymbolicName(*cit)+" - Duration");
319  }
320  FD_DX("SimConditionSet::Reset(): done");
321 }
322 
323 
324 
325 } // namespace faudes
326 
#define FD_DC(message)
Debug: optional report on container operations.
#define FAUDES_TYPE_IMPLEMENTATION(ftype, ctype, cbase)
faudes type implementation macros, overall
Definition: cfl_types.h:946
#define FAUDES_TYPE_IMPLEMENTATION_EQUAL(ftype, ctype, cbase)
Definition: cfl_types.h:904
#define FAUDES_TYPE_IMPLEMENTATION_COPY(ftype, ctype, cbase)
Definition: cfl_types.h:891
#define FAUDES_TYPE_IMPLEMENTATION_CAST(ftype, ctype, cbase)
Definition: cfl_types.h:893
#define FAUDES_TYPE_IMPLEMENTATION_ASSIGN(ftype, ctype, cbase)
Definition: cfl_types.h:896
Boolean flags Attribute.
bool DoEqual(const AttributeFlags &rOther) const
Test equality of configuration data.
void DoAssign(const AttributeFlags &rSrcAttr)
Assignment method.
Attribute for a simulation condition.
bool Enabled(void) const
Test whether condition is enabled.
bool DoEqual(const AttributeSimCondition &rAttr) const
Equality method.
void DoAssign(const AttributeSimCondition &rSrcAttr)
Assignment method.
bool mSatisfied
Condotion state: recorded as satisfied.
SimEventCondition mEventConditionAttribute
Event based condition data.
SampledDensityFunction mSamplesDuration
Sampled durations, for which this condition remains satisfied.
virtual void DoRead(TokenReader &rTr, const std::string &rLabel="", const Type *pContext=0)
Reads the attribute from TokenReader, see AttributeVoid for public wrappers.
bool Breakpoint(void) const
Test for break condition.
bool Satisfied(void) const
Test whether the condition is currently satisfied.
void Reset(void)
Reset conditions execution state.
SimStateCondition mStateConditionAttribute
State based condition data.
virtual void DoWrite(TokenWriter &rTw, const std::string &rLabel="", const Type *pContext=0) const
Writes the attribute to TokenWriter, see AttributeVoid for public wrappers.
bool mStateCondition
Indicate precense of a state condition.
SampledDensityFunction mSamplesPeriod
Sampled period, at which this condition becomes satisfied.
AttributeSimCondition(void)
Default constructor.
Time::Type mActivationTime
Condition state: when last satisfied became true.
bool mEventCondition
Indicate precense of a event condition.
const std::string & Name(void) const
Faudes exception class.
void Generator(const TimedGenerator &rGen)
Initialise from TimedGenerator.
Definition: sp_executor.cpp:46
Set of indices.
Definition: cfl_indexset.h:78
void SymbolicName(Idx index, const std::string &rName)
Set new name for existing index.
SymbolTable * mpSymbolTable
Pointer to local SymbolTable.
Definition: cfl_nameset.h:428
Synchronized parallel execution of TimedGenerators.
Definition: sp_pexecutor.h:64
Idx Size(void) const
Number of TimedGenerators.
const Executor & At(int i) const
Definition: sp_pexecutor.h:183
void Sample(Time::Type time)
Set of simulation named conditions.
bool Enabled(Idx cond) const
Test condition for enabled.
SimConditionSet(void)
Default constructor.
SimConditionSet EnabledConditions(void)
Get set of enabled conditions.
void Reset(void)
Reset all condition states.
std::vector< StateSet > mStateSets
virtual bool Insert(const Idx &rIndex)
Add an element by index.
Definition: cfl_nameset.h:988
AttributeSimCondition * Attributep(const Idx &rElem)
Definition: cfl_nameset.h:843
const AttributeSimCondition & Attribute(const Idx &rElem) const
Definition: cfl_nameset.h:844
Int Type
Datatype for point on time axis.
A TokenReader reads sequential tokens from a file or string.
std::string FileLine(void) const
Return "filename:line".
bool Eos(const std::string &rLabel)
Peek a token and check whether it ends the specified section.
void ReadEnd(const std::string &rLabel)
Close the current section by matching the previous ReadBegin().
void ReadBegin(const std::string &rLabel)
Open a section by specified label.
bool Get(Token &token)
Get next token.
bool Peek(Token &token)
Peek next token.
A TokenWriter writes sequential tokens to a file, a string or stdout.
void WriteEnd(const std::string &rLabel)
Write end label.
void WriteBegin(const std::string &rLabel)
Write begin label.
void WriteOption(const std::string &rOpt)
Write option (may not contain any "+")
Tokens model atomic data for stream IO.
Definition: cfl_token.h:53
const std::string & StringValue(void) const
Get string value of a name token.
Definition: cfl_token.cpp:177
@ Begin
<label> (begin of section)
Definition: cfl_token.h:83
bool IsBegin(void) const
Test token Type.
Definition: cfl_token.cpp:258
const std::string & OptionValue(void) const
Get option value of a name token.
Definition: cfl_token.cpp:183
bool IsOption(void) const
Test token Type.
Definition: cfl_token.cpp:238
TokenType Type(void) const
Get token Type.
Definition: cfl_token.cpp:198
Base class of all libFAUDES objects that participate in the run-time interface.
Definition: cfl_types.h:239
void Read(const std::string &rFileName, const std::string &rLabel="", const Type *pContext=0)
Read configuration data from file with label specified.
Definition: cfl_types.cpp:261
virtual Type * New(void) const
Construct on heap.
Definition: cfl_types.cpp:54
void Write(const Type *pContext=0) const
Write configuration data to console.
Definition: cfl_types.cpp:139
virtual void Clear(void)
Clear all set.
Definition: cfl_baseset.h:1902
Iterator End(void) const
Iterator to the end of set.
Definition: cfl_baseset.h:1896
Iterator Begin(void) const
Iterator to the begin of set.
Definition: cfl_baseset.h:1891
const std::string & Name(void) const
Return name of TBaseSet.
Definition: cfl_baseset.h:1755
libFAUDES resides within the namespace faudes.
uint32_t Idx
Type definition for index type (allways 32bit)
#define FD_DX(message)
Definition: sp_executor.h:27
Executor with logging facilities
Set of named simulation conditions.

libFAUDES 2.32b --- 2024.03.01 --- c++ api documentaion by doxygen