cfl_nameset.cpp
Go to the documentation of this file.
1 /** @file cfl_nameset.cpp @brief Classes NameSet */
2 
3 /* FAU Discrete Event Systems Library (libfaudes)
4 
5  Copyright (C) 2006 Bernd Opitz
6  Exclusive copyright is granted to Klaus Schmidt
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Lesser General Public
10  License as published by the Free Software Foundation; either
11  version 2.1 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with this library; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
21 
22 
23 #include "cfl_nameset.h"
24 
25 namespace faudes {
26 
27 /*
28 *********************************************************************************
29 *********************************************************************************
30 *********************************************************************************
31 
32  NameSet
33 
34 *********************************************************************************
35 *********************************************************************************
36 *********************************************************************************
37 */
38 
39 
40 // std faudes type (cannot do New() with macro)
41 FAUDES_TYPE_IMPLEMENTATION_COPY(EventSet,NameSet,TBaseSet<Idx>)
43 FAUDES_TYPE_IMPLEMENTATION_ASSIGN(EventSet,NameSet,TBaseSet<Idx>)
44 FAUDES_TYPE_IMPLEMENTATION_EQUAL(EventSet,NameSet,TBaseSet<Idx>)
45 
46 
47 // empty constructor
48 NameSet::NameSet(void) : TBaseSet<Idx>(){
49  mpSymbolTable = SymbolTable::GlobalEventSymbolTablep();
50  Name("NameSet");
51  FD_DC("NameSet("<<this<<")::NameSet() with symtab "<< mpSymbolTable);
52 }
53 
54 // constructor from nameset
55 NameSet::NameSet(const NameSet& rOtherSet) : TBaseSet<Idx>() {
56  FD_DC("NameSet(" << this << ")::NameSet(rOtherSet " << &rOtherSet << ")");
57  mpSymbolTable = rOtherSet.mpSymbolTable;
58  Assign(rOtherSet);
59  FD_DC("NameSet(" << this << ")::NameSet(rOtherSet " << &rOtherSet << "): done");
60 }
61 
62 // read file constructor
63 NameSet::NameSet(const std::string& rFilename, const std::string& rLabel) : TBaseSet<Idx>() {
64  FD_DC("NameSet(" << this << ")::NameSet(" << rFilename << ")");
66  Read(rFilename, rLabel);
67 }
68 
69 // destructor
71  FD_DC("NameSet("<<this<<")::~NameSet()");
72 }
73 
74 // New()
75 NameSet* NameSet::New(void) const {
76  NameSet* res= new NameSet();
78  return res;
79 }
80 
81 // NewN()
82 NameSet NameSet::NewN(void) const {
83  NameSet res;
85  return res;
86 }
87 
88 // copy (attributes to default)
89 void NameSet::DoAssign(const NameSet& rSourceSet) {
90  FD_DC("NameSet(" << this << ")::DoAssign(..)");
91  // fix my symboltable
92  mpSymbolTable=rSourceSet.mpSymbolTable;
93  // call base
94  TBaseSet<Idx>::DoAssign(rSourceSet);
95 }
96 
97 // Compare
98 bool NameSet::DoEqual(const NameSet& rOtherSet) const {
99  FD_DC("NameSet::DoEqual()");
100 #ifdef FAUDES_CHECKED
101  if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
102  std::stringstream errstr;
103  errstr << "symboltable mismazch aka not implemented" << std::endl;
104  throw Exception("NameSet::DoEqual()", errstr.str(), 67);
105  }
106 #endif
107  return TBaseSet<Idx>::DoEqual(rOtherSet);
108 }
109 
110 // SymbolTablep()
112  return mpSymbolTable;
113 }
114 
115 // SymbolTablep(pSymTab)
117  FD_DC("NameSet(" << this << ")::SymbolTablep(" << pSymTab << ")");
118  if(!Empty()) {
119  Clear();
120  }
121  mpSymbolTable=pSymTab;
122 }
123 
124 // DoWrite(tw, rLabel)
125 void NameSet::DoWrite(TokenWriter& tw, const std::string& rLabel, const Type* pContext) const {
126  std::string label=rLabel;
127  if(label=="") label=Name();
128  FD_DC("NameSet(" << this << ")::DoWrite(..): section " << label << " #" << Size());
129  tw.WriteBegin(label);
130  Token token;
131  Iterator it;
132  for (it = Begin(); it != End(); ++it) {
133 #ifdef FAUDES_DEBUG_CODE
134  if (SymbolicName(*it) == "") {
135  FD_ERR("NameSet::Write(): "
136  << "index " << *it << " not in SymbolTable. aborting...");
137  abort();
138  }
139 #endif
140  token.SetString(SymbolicName(*it));
141  tw << token;
142  Attribute(*it).Write(tw,"",pContext);
143  }
144  tw.WriteEnd(label);
145 }
146 
147 // DoDWrite()
148 void NameSet::DoDWrite(TokenWriter& tw, const std::string& rLabel, const Type* pContext) const {
149  std::string label=rLabel;
150  if(label=="") label=Name();
151  if(label=="") label=TypeName();
152  tw.WriteBegin(label);
153  Token token;
154  Iterator it;
155  for (it = Begin(); it != End(); ++it) {
156  tw << Str(*it);
157  Attribute(*it).Write(tw,"",pContext);
158  }
159  tw.WriteEnd(label);
160 }
161 
162 
163 // DoXWrite(tw, rLabel)
164 void NameSet::DoXWrite(TokenWriter& tw, const std::string& rLabel, const Type* pContext) const {
165  // Set up outer tag
166  Token btag=XBeginTag(rLabel,"NameSet");
167  tw.Write(btag);
168  FD_DC("NameSet(" << this << ")::DoXWrite(..): section " << btag.StringValue() << " #" << Size());
169  // loop elements
170  std::string etstr=XElementTag();
171  for (Iterator it = Begin(); it != End(); ++it) {
172  Token etoken;
173  const AttributeVoid& attr=Attribute(*it);
174  // case a: no attribute value
175  if(attr.IsDefault()) {
176  etoken.SetEmpty(etstr);
177  etoken.InsAttributeString("name",SymbolicName(*it));
178  tw << etoken;
179  }
180  // case b: incl attribute value
181  if(!attr.IsDefault()) {
182  etoken.SetBegin(etstr);
183  etoken.InsAttributeString("name",SymbolicName(*it));
184  tw << etoken;
185  Attribute(*it).XWrite(tw,"",pContext);
186  etoken.SetEnd(etstr);
187  tw << etoken;
188  }
189  }
190  tw.WriteEnd(btag.StringValue());
191 }
192 
193 // DoRead(rTr, rLabel, pContext)
194 void NameSet::DoRead(TokenReader& rTr, const std::string& rLabel, const Type* pContext) {
195  // set up defaults
196  std::string label=rLabel;
197  std::string ftype=TypeName();
198  std::string etstr=XElementTag();
199  // figure section
200  Token token;
201  if(label=="") {
202  rTr.Peek(token);
203  if(token.Type()==Token::Begin) label=token.StringValue();
204  }
205  if(label=="") label=ftype;
206  Name(label);
207  // read begin
208  rTr.ReadBegin(label,token);
209  if(token.ExistsAttributeString("name"))
210  Name(token.AttributeStringValue("name"));
211  FD_DC("NameSet(" << this << ")::DoRead(..): section " << label << " with symtab " << mpSymbolTable);
212  // prepare attribute
213  AttributeVoid* attrp = Attribute().New();
214  FD_DC("NameSet(" << this << ")::DoRead(..): attribute type " << typeid(*attrp).name());
215  // loop tokens
216  std::string name;
217  while(!rTr.Eos(label)) {
218  rTr.Peek(token);
219  // read by name (faudes format)
220  if(token.IsString()) {
221  FD_DC("TaNameSet(" << this << ")::DoRead(..): inserting element in faudes format \""
222  << token.StringValue() << "\"");
223  rTr.Get(token);
224  name=token.StringValue();
225  // read faudes attribute
226  attrp->Read(rTr,"",pContext);
227  // skip unknown faudes attributes
228  AttributeVoid::Skip(rTr);
229  // insert element with attribute
230  Idx index=Insert(name);
231  Attribute(index,*attrp);
232  continue;
233  }
234  // read element section (XML format)
235  if(token.IsBegin(etstr)) {
236  FD_DC("TaNameSet(" << this << ")::DoRead(..): inserting element in xml format \""
237  << token.StringValue() << "\"");
238  rTr.ReadBegin(etstr,token);
239  name=token.AttributeStringValue("name");
240  // read faudes attribute
241  attrp->Read(rTr,"",pContext);
242  // skip unknown faudes attributes
243  rTr.ReadEnd(etstr);
244  // insert element with attribute
245  Idx index=Insert(name);
246  Attribute(index,*attrp);
247  continue;
248  }
249  // cannot process token
250  delete attrp;
251  std::stringstream errstr;
252  errstr << "Invalid token of type " << token.Type() << " at " << rTr.FileLine();
253  throw Exception("NameSet::DoRead", errstr.str(), 50);
254  }
255  rTr.ReadEnd(label);
256  delete attrp;
257  FD_DC("NameSet(" << this << ")::DoRead(tr," << label << ", " << pContext << "): done");
258 }
259 
260 
261 // Insert(index)
262 bool NameSet::Insert(const Idx& index) {
263 #ifdef FAUDES_CHECKED
264  if(!mpSymbolTable->Exists(index)) {
265  std::stringstream errstr;
266  errstr << "index " << index << " not known to symboltable" << std::endl;
267  throw Exception("NameSet::Insert", errstr.str(), 65);
268  }
269 #endif
270  return TBaseSet<Idx>::Insert(index);
271 }
272 
273 // Insert(rName)
274 Idx NameSet::Insert(const std::string& rName) {
275  FD_DC("NameSet(" << this << ")::Insert(" << rName <<")");
276  Idx index = mpSymbolTable->InsEntry(rName);
277  TBaseSet<Idx>::Insert(index);
278  return index;
279 }
280 
281 // InsertSet(set)
282 void NameSet::InsertSet(const NameSet& rOtherSet) {
283  FD_DC("NameSet(" << this << ")::InsertSet(" << rOtherSet.ToString() << ")");
284 #ifdef FAUDES_CHECKED
285  if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
286  std::stringstream errstr;
287  errstr << "symboltable mismatch aka not implemented" << std::endl;
288  throw Exception("NameSet::InsertSet", errstr.str(), 67);
289  }
290 #endif
291  TBaseSet<Idx>::InsertSet(rOtherSet);
292 }
293 
294 // Erase(index)
295 bool NameSet::Erase(const Idx& index) {
296  FD_DC("NameSet(" << this << ")::Erase(" << index <<")");
297  return TBaseSet<Idx>::Erase(index);
298 }
299 
300 // Erase(rName)
301 bool NameSet::Erase(const std::string& rName) {
302  FD_DC("NameSet(" << this << ")::Erase(" << rName <<")");
303  Idx index = mpSymbolTable->Index(rName);
304 #ifdef FAUDES_CHECKED
305  if (index == 0) {
306  std::stringstream errstr;
307  errstr << "name \"" << rName << "\" not found in NameSet" << std::endl;
308  throw Exception("NameSet::Erase", errstr.str(), 66);
309  }
310 #endif
311  return TBaseSet<Idx>::Erase(index);
312 }
313 
314 // Erase(pos)
315 NameSet::Iterator NameSet::Erase(const Iterator& pos) {
316  return TBaseSet<Idx>::Erase(pos);
317 }
318 
319 // EraseSet(set)
320 void NameSet::EraseSet(const NameSet& rOtherSet) {
321 #ifdef FAUDES_CHECKED
322  if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
323  std::stringstream errstr;
324  errstr << "symboltable mismatch aka not implemented" << std::endl;
325  throw Exception("NameSet::EraseSet", errstr.str(), 67);
326  }
327 #endif
328  TBaseSet<Idx>::EraseSet(rOtherSet);
329 }
330 
331 // RestrictSet(set)
332 void NameSet::RestrictSet(const NameSet& rOtherSet) {
333 #ifdef FAUDES_CHECKED
334  if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
335  std::stringstream errstr;
336  errstr << "symboltable mismatch aka not implemented" << std::endl;
337  throw Exception("NameSet::RestrictSet", errstr.str(), 67);
338  }
339 #endif
340  TBaseSet<Idx>::RestrictSet(rOtherSet);
341 }
342 
343 // SymbolicName(index)
344 std::string NameSet::SymbolicName(Idx index) const {
345  return mpSymbolTable->Symbol(index);
346 }
347 
348 // SymbolicName(index, name)
349 void NameSet::SymbolicName(Idx index, const std::string& rName) {
350  FD_DC("NameSet(" << this << ")::SymbolicName(" << index << ", " << rName <<")");
351 #ifdef FAUDES_CHECKED
352  if (! Exists(index)) {
353  std::stringstream errstr;
354  errstr << "index " << index << " not in this set" << std::endl;
355  throw Exception("NameSet::SymbolicName", errstr.str(), 60);
356  }
357 #endif
358  mpSymbolTable->SetEntry(index, rName);
359 }
360 
361 // SymbolicName(name, name)
362 void NameSet::SymbolicName(const std::string& rName,
363  const std::string& rNewName) {
364  FD_DC("NameSet(" << this << ")::SymbolicName(" << rName << ", "
365  << rNewName <<")");
366 #ifdef FAUDES_CHECKED
367  if (! Exists(rName)) {
368  std::stringstream errstr;
369  errstr << "name \"" << rName << "\" not found in NameSet" << std::endl;
370  throw Exception("NameSet::Symbolic", errstr.str(), 66);
371  }
372 #endif
373  mpSymbolTable->SetEntry(Index(rName), rNewName);
374 }
375 
376 // Index(rName)
377 Idx NameSet::Index(const std::string& rName) const {
378  return mpSymbolTable->Index(rName);
379 }
380 
381 // Exists(index)
382 bool NameSet::Exists(const Idx& index) const {
383  return TBaseSet<Idx>::Exists(index);
384 }
385 
386 // Exists(rName)
387 bool NameSet::Exists(const std::string& rName) const {
388  return TBaseSet<Idx>::Exists(mpSymbolTable->Index(rName)) ;
389 }
390 
391 // Find(index) const
392 NameSet::Iterator NameSet::Find(const Idx& index) const {
393  return TBaseSet<Idx>::Find(index);
394 }
395 
396 // Find(rName) const
397 NameSet::Iterator NameSet::Find(const std::string& rName) const {
398  return TBaseSet<Idx>::Find(mpSymbolTable->Index(rName));
399 }
400 
401 
402 // operator +
403 NameSet NameSet::operator + (const NameSet& rOtherSet) const {
404  FD_DC("NameSet(" << this << ")::operator + (" << &rOtherSet << ")");
405 #ifdef FAUDES_CHECKED
406  if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
407  std::stringstream errstr;
408  errstr << "symboltable mismazch aka not implemented" << std::endl;
409  throw Exception("NameSet::Operator+", errstr.str(), 67);
410  }
411 #endif
412  NameSet res= NewN();
413  FnctUnion(rOtherSet,res);
414  return res;
415 }
416 
417 // operator -
418 NameSet NameSet::operator - (const NameSet& rOtherSet) const {
419  FD_DC("NameSet(" << this << ")::operator - (" << &rOtherSet << ")");
420 #ifdef FAUDES_CHECKED
421  if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
422  std::stringstream errstr;
423  errstr << "symboltable mismazch aka not implemented" << std::endl;
424  throw Exception("NameSet::Operator-", errstr.str(), 67);
425  }
426 #endif
427  NameSet res= NewN();
428  FnctDifference(rOtherSet,res);
429  return res;
430 }
431 
432 // operator *
433 NameSet NameSet::operator * (const NameSet& rOtherSet) const {
434  FD_DC("NameSet(" << this << ")::operator * (" << &rOtherSet << ")");
435 #ifdef FAUDES_CHECKED
436  if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
437  std::stringstream errstr;
438  errstr << "symboltable mismazch aka not implemented" << std::endl;
439  throw Exception("NameSet::Operator*", errstr.str(), 67);
440  }
441 #endif
442  NameSet res= NewN();
443  FnctIntersection(rOtherSet,res);
444  return res;
445 }
446 
447 // operator <=
448 bool NameSet::operator <= (const NameSet& rOtherSet) const {
449 #ifdef FAUDES_CHECKED
450  if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
451  std::stringstream errstr;
452  errstr << "symboltable mismazch aka not implemented" << std::endl;
453  throw Exception("NameSet::Operator<=", errstr.str(), 67);
454  }
455 #endif
456  return TBaseSet<Idx>::operator <= (rOtherSet);
457 }
458 
459 // operator >=
460 bool NameSet::operator >= (const NameSet& rOtherSet) const {
461 #ifdef FAUDES_CHECKED
462  if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
463  std::stringstream errstr;
464  errstr << "symboltable mismazch aka not implemented" << std::endl;
465  throw Exception("NameSet::Operator>=", errstr.str(), 67);
466  }
467 #endif
468  return TBaseSet<Idx>::operator >= (rOtherSet);
469 }
470 
471 // Str(index)
472 std::string NameSet::Str(const Idx& index) const {
473  return mpSymbolTable->Symbol(index)+"#"+faudes::ToStringInteger(index);
474 }
475 
476 
477 /*
478 *********************************************************************************
479 *********************************************************************************
480 *********************************************************************************
481 
482  Misc
483 
484 *********************************************************************************
485 *********************************************************************************
486 *********************************************************************************
487 */
488 
489 
490 // RTI convenience function
491 void SetIntersection(const EventSetVector& rSetVec, EventSet& rRes) {
492  // prepare result
493  rRes.Clear();
494  // ignore empty
495  if(rSetVec.Size()==0) return;
496  // copy first
497  rRes.Assign(rSetVec.At(0));
498  // perform intersecttion
499  for(EventSetVector::Position i=1; i<rSetVec.Size(); i++)
500  SetIntersection(rSetVec.At(i),rRes,rRes);
501 }
502 
503 
504 // RTI convenience function
505 void SetUnion(const EventSetVector& rSetVec, EventSet& rRes) {
506  // prepare result
507  rRes.Clear();
508  // ignore empty
509  if(rSetVec.Size()==0) return;
510  // copy first
511  rRes.Assign(rSetVec.At(0));
512  // perform union
513  for(EventSetVector::Position i=1; i<rSetVec.Size(); i++)
514  SetUnion(rSetVec.At(i),rRes,rRes);
515 }
516 
517 
518 } // namespace faudes
519 
520 

libFAUDES 2.26g --- 2015.08.17 --- c++ api documentaion by doxygen