libFAUDES

Sections

Index

cfl_nameset.cpp

Go to the documentation of this file.
00001 /** @file cfl_nameset.cpp @brief Classes NameSet */
00002 
00003 /* FAU Discrete Event Systems Library (libfaudes)
00004 
00005    Copyright (C) 2006  Bernd Opitz
00006    Exclusive copyright is granted to Klaus Schmidt
00007 
00008    This library is free software; you can redistribute it and/or
00009    modify it under the terms of the GNU Lesser General Public
00010    License as published by the Free Software Foundation; either
00011    version 2.1 of the License, or (at your option) any later version.
00012 
00013    This library is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016    Lesser General Public License for more details.
00017 
00018    You should have received a copy of the GNU Lesser General Public
00019    License along with this library; if not, write to the Free Software
00020    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00021 
00022 
00023 #include "cfl_nameset.h"
00024 
00025 namespace faudes {
00026 
00027 /*
00028 *********************************************************************************
00029 *********************************************************************************
00030 *********************************************************************************
00031 
00032  NameSet 
00033 
00034 *********************************************************************************
00035 *********************************************************************************
00036 *********************************************************************************
00037 */
00038 
00039 // std faudes type (cannot do New() with macro)
00040 FAUDES_TYPE_IMPLEMENTATION_COPY(EventSet,NameSet,TBaseSet<Idx>)
00041 FAUDES_TYPE_IMPLEMENTATION_CAST(EventSet,NameSet,TBaseSet<Idx>)
00042 FAUDES_TYPE_IMPLEMENTATION_ASSIGN(EventSet,NameSet,TBaseSet<Idx>)
00043 FAUDES_TYPE_IMPLEMENTATION_EQUAL(EventSet,NameSet,TBaseSet<Idx>)
00044 
00045 
00046 // empty constructor
00047 NameSet::NameSet(void) : TBaseSet<Idx>(){
00048   mpSymbolTable = SymbolTable::GlobalEventSymbolTablep();
00049   Name("NameSet");
00050   FD_DC("NameSet("<<this<<")::NameSet() with symtab "<< mpSymbolTable); 
00051 }
00052 
00053 // constructor from nameset
00054 NameSet::NameSet(const NameSet& rOtherSet) : TBaseSet<Idx>() {
00055   FD_DC("NameSet(" << this << ")::NameSet(rOtherSet " << &rOtherSet << ")");
00056   mpSymbolTable = rOtherSet.mpSymbolTable;
00057   Assign(rOtherSet);
00058   FD_DC("NameSet(" << this << ")::NameSet(rOtherSet " << &rOtherSet << "): done");
00059 }
00060 
00061 // read file constructor
00062 NameSet::NameSet(const std::string& rFilename, const std::string& rLabel) : TBaseSet<Idx>() {
00063   FD_DC("NameSet(" << this << ")::NameSet(" << rFilename << ")");
00064   mpSymbolTable = SymbolTable::GlobalEventSymbolTablep();
00065   Read(rFilename, rLabel);
00066 }
00067 
00068 // destructor
00069 NameSet::~NameSet(void) {
00070   FD_DC("NameSet("<<this<<")::~NameSet()");
00071 }
00072 
00073 // New()
00074 NameSet* NameSet::New(void) const {
00075   NameSet* res= new NameSet();
00076   res->mpSymbolTable=mpSymbolTable;
00077   return res;
00078 }
00079 
00080 // NewN()
00081 NameSet NameSet::NewN(void) const {
00082   NameSet res;
00083   res.mpSymbolTable=mpSymbolTable;
00084   return res;
00085 }
00086 
00087 // copy (attributes to default)
00088 void NameSet::DoAssign(const NameSet& rSourceSet) {
00089   FD_DC("NameSet(" << this << ")::DoAssign(..)");
00090   // fix my symboltable
00091   mpSymbolTable=rSourceSet.mpSymbolTable;
00092   // call base 
00093   TBaseSet<Idx>::DoAssign(rSourceSet);
00094 } 
00095 
00096 // Compare
00097 bool NameSet::DoEqual(const NameSet& rOtherSet) const {
00098   FD_DC("NameSet::DoEqual()");
00099 #ifdef FAUDES_CHECKED
00100   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00101     std::stringstream errstr;
00102     errstr << "symboltable mismazch aka not implemented" << std::endl;
00103     throw Exception("NameSet::DoEqual()", errstr.str(), 67);
00104   }
00105 #endif
00106   return TBaseSet<Idx>::DoEqual(rOtherSet);
00107 }
00108 
00109 // SymbolTablep()
00110 SymbolTable* NameSet::SymbolTablep(void) const {
00111   return mpSymbolTable;
00112 }
00113 
00114 // SymbolTablep(pSymTab)
00115 void NameSet::SymbolTablep(SymbolTable* pSymTab) {
00116   FD_DC("NameSet(" << this << ")::SymbolTablep(" << pSymTab << ")");
00117   if(!Empty()) {
00118     Clear();
00119   } 
00120   mpSymbolTable=pSymTab;
00121 }
00122 
00123 // DoWrite(tw, rLabel)
00124 void NameSet::DoWrite(TokenWriter& tw, const std::string& rLabel, const Type* pContext) const {
00125   std::string label=rLabel;
00126   if(label=="") label=Name();  
00127   FD_DC("NameSet(" << this << ")::DoWrite(..): section " << label << " #" << Size());
00128   tw.WriteBegin(label);
00129   Token token;
00130   Iterator it;
00131   for (it = Begin(); it != End(); ++it) {
00132 #ifdef FAUDES_DEBUG_CODE
00133     if (SymbolicName(*it) == "") {
00134       FD_ERR("NameSet::Write(): "
00135      << "index " << *it << " not in SymbolTable. aborting...");
00136       abort();
00137     }
00138 #endif
00139     token.SetString(SymbolicName(*it));
00140     tw << token;
00141     Attribute(*it).Write(tw,"",pContext);
00142   }
00143   tw.WriteEnd(label);
00144 }
00145 
00146 // DoDWrite()
00147 void NameSet::DoDWrite(TokenWriter& tw, const std::string& rLabel, const Type* pContext) const {
00148   std::string label=rLabel;
00149   if(label=="") label=Name(); 
00150   if(label=="") label=TypeName(); 
00151   tw.WriteBegin(label);
00152   Token token;
00153   Iterator it;
00154   for (it = Begin(); it != End(); ++it) {
00155     tw << Str(*it);
00156     Attribute(*it).Write(tw,"",pContext);
00157   }
00158   tw.WriteEnd(label);
00159 }
00160 
00161 
00162 // DoXWrite(tw, rLabel)
00163 void NameSet::DoXWrite(TokenWriter& tw, const std::string& rLabel, const Type* pContext) const {
00164   // Set up defaults
00165   std::string label=rLabel;
00166   std::string ftype=TypeName();
00167   std::string etstr=XElementTag();
00168   // Write begin
00169   if(label=="") label=ftype;
00170   if(label=="") label="NameSet";
00171   Token btag;
00172   btag.SetBegin(label);
00173   if(Name()!=label) btag.InsAttributeString("name",Name());
00174   if(ftype!=label && ftype!="") btag.InsAttributeString("ftype",ftype);
00175   FD_DC("NameSet(" << this << ")::DoXWrite(..): section " << label << " #" << Size());
00176   tw << btag;
00177   // loop elements
00178   for (Iterator it = Begin(); it != End(); ++it) {
00179     Token etoken;
00180     const AttributeVoid& attr=Attribute(*it);
00181     // case a: no attribute value
00182     if(attr.IsDefault()) {
00183       etoken.SetEmpty(etstr);
00184       etoken.InsAttributeString("name",SymbolicName(*it));
00185       tw << etoken;
00186     } 
00187     // case b: incl attribute value
00188     if(!attr.IsDefault()) {
00189       etoken.SetBegin(etstr);
00190       etoken.InsAttributeString("name",SymbolicName(*it));
00191       tw << etoken;
00192       Attribute(*it).XWrite(tw,"",pContext);
00193       etoken.SetEnd(etstr);
00194       tw << etoken;
00195     }
00196   }
00197   tw.WriteEnd(label);
00198 }
00199 
00200 // DoRead(rTr, rLabel, pContext)
00201 void NameSet::DoRead(TokenReader& rTr, const std::string& rLabel, const Type* pContext) {
00202   // set up defaults
00203   std::string label=rLabel;
00204   std::string ftype=TypeName();
00205   std::string etstr=XElementTag();
00206   // figure section
00207   Token token;
00208   if(label=="") {
00209     rTr.Peek(token);
00210     if(token.Type()==Token::Begin) label=token.StringValue();
00211   }
00212   if(label=="") label=ftype; 
00213   Name(label);
00214   // read begin
00215   rTr.ReadBegin(label,token); 
00216   if(token.ExistsAttributeString("name"))
00217     Name(token.AttributeStringValue("name"));
00218   FD_DC("NameSet(" << this << ")::DoRead(..): section " << label << " with symtab " << mpSymbolTable);
00219   // prepare attribute
00220   AttributeVoid* attrp = Attribute().New();
00221   FD_DC("NameSet(" << this << ")::DoRead(..): attribute type " << typeid(*attrp).name());
00222   // loop tokens
00223   std::string name;
00224   while(!rTr.Eos(label)) {
00225     rTr.Peek(token);
00226     // read by name (faudes format)
00227     if(token.IsString()) {
00228       FD_DC("TaNameSet(" << this << ")::DoRead(..): inserting element in faudes format \"" 
00229       << token.StringValue() << "\"");
00230       rTr.Get(token);
00231       name=token.StringValue();
00232       // read faudes attribute
00233       attrp->Read(rTr,"",pContext);
00234       // skip unknown faudes attributes
00235       AttributeVoid::Skip(rTr);
00236       // insert element with attribute
00237       Idx index=Insert(name); 
00238       Attribute(index,*attrp);
00239       continue;
00240     }
00241     // read element section (XML format)
00242     if(token.IsBegin()) 
00243     if(token.StringValue()==etstr) {
00244       FD_DC("TaNameSet(" << this << ")::DoRead(..): inserting element in xml format \"" 
00245       << token.StringValue() << "\"");
00246       rTr.ReadBegin(etstr,token);
00247       name=token.AttributeStringValue("name");
00248       // read faudes attribute
00249       attrp->Read(rTr,"",pContext);
00250       // skip unknown faudes attributes
00251       rTr.ReadEnd(etstr);
00252       // insert element with attribute
00253       Idx index=Insert(name); 
00254       Attribute(index,*attrp);
00255       continue;
00256     }
00257     // cannot process token
00258     delete attrp;
00259     std::stringstream errstr;
00260     errstr << "Invalid token of type " << token.Type() << " at " << rTr.FileLine();
00261     throw Exception("NameSet::DoRead", errstr.str(), 50);
00262   }
00263   rTr.ReadEnd(label);
00264   delete attrp;
00265   FD_DC("NameSet(" << this << ")::DoRead(tr," << label << ", " << pContext << "): done");
00266 }
00267 
00268 
00269 // Insert(index)
00270 bool NameSet::Insert(const Idx& index) { 
00271 #ifdef FAUDES_CHECKED
00272   if(!mpSymbolTable->Exists(index)) {
00273     std::stringstream errstr;
00274     errstr << "index " << index << " not known to symboltable" << std::endl;
00275     throw Exception("NameSet::Insert", errstr.str(), 65);
00276   }
00277 #endif
00278   return TBaseSet<Idx>::Insert(index);
00279 }
00280 
00281 // Insert(rName)
00282 Idx NameSet::Insert(const std::string& rName) {
00283   FD_DC("NameSet(" << this << ")::Insert(" << rName <<")");
00284   Idx index = mpSymbolTable->InsEntry(rName);
00285   TBaseSet<Idx>::Insert(index);
00286   return index;
00287 }
00288 
00289 // InsertSet(set)
00290 void NameSet::InsertSet(const NameSet& rOtherSet) {
00291   FD_DC("NameSet(" << this << ")::InsertSet(" << rOtherSet.ToString() << ")");
00292 #ifdef FAUDES_CHECKED
00293   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00294     std::stringstream errstr;
00295     errstr << "symboltable mismatch aka not implemented" << std::endl;
00296     throw Exception("NameSet::InsertSet", errstr.str(), 67);
00297   }
00298 #endif
00299   TBaseSet<Idx>::InsertSet(rOtherSet);
00300 }
00301     
00302 // Erase(index)
00303 bool NameSet::Erase(const Idx& index) {
00304   FD_DC("NameSet(" << this << ")::Erase(" << index <<")");
00305   return TBaseSet<Idx>::Erase(index);
00306 }
00307 
00308 // Erase(rName)
00309 bool NameSet::Erase(const std::string& rName) {
00310   FD_DC("NameSet(" << this << ")::Erase(" << rName <<")");
00311   Idx index = mpSymbolTable->Index(rName);
00312 #ifdef FAUDES_CHECKED
00313   if (index == 0) {
00314     std::stringstream errstr;
00315     errstr << "name \"" << rName << "\" not found in NameSet" << std::endl;
00316     throw Exception("NameSet::Erase", errstr.str(), 66);
00317   }
00318 #endif
00319   return TBaseSet<Idx>::Erase(index);
00320 }
00321 
00322 // Erase(pos)
00323 NameSet::Iterator NameSet::Erase(const Iterator& pos) {
00324   return TBaseSet<Idx>::Erase(pos);
00325 }
00326 
00327 // EraseSet(set)
00328 void NameSet::EraseSet(const NameSet& rOtherSet) {
00329 #ifdef FAUDES_CHECKED
00330   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00331     std::stringstream errstr;
00332     errstr << "symboltable mismatch aka not implemented" << std::endl;
00333     throw Exception("NameSet::EraseSet", errstr.str(), 67);
00334   }
00335 #endif
00336   TBaseSet<Idx>::EraseSet(rOtherSet);
00337 }
00338 
00339 // RestrictSet(set)
00340 void NameSet::RestrictSet(const NameSet& rOtherSet) {
00341 #ifdef FAUDES_CHECKED
00342   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00343     std::stringstream errstr;
00344     errstr << "symboltable mismatch aka not implemented" << std::endl;
00345     throw Exception("NameSet::RestrictSet", errstr.str(), 67);
00346   }
00347 #endif
00348   TBaseSet<Idx>::RestrictSet(rOtherSet);
00349 }
00350 
00351 // SymbolicName(index)
00352 std::string NameSet::SymbolicName(Idx index) const {
00353   return mpSymbolTable->Symbol(index);
00354 }
00355 
00356 // SymbolicName(index, name)
00357 void NameSet::SymbolicName(Idx index, const std::string& rName) {
00358   FD_DC("NameSet(" << this << ")::SymbolicName(" << index << ", " << rName <<")");
00359 #ifdef FAUDES_CHECKED
00360   if (! Exists(index)) {
00361     std::stringstream errstr;
00362     errstr << "index " << index << " not in this set" << std::endl;
00363     throw Exception("NameSet::SymbolicName", errstr.str(), 60);
00364   }
00365 #endif
00366   mpSymbolTable->SetEntry(index, rName);
00367 }
00368 
00369 // SymbolicName(name, name)
00370 void NameSet::SymbolicName(const std::string& rName, 
00371           const std::string& rNewName) {
00372   FD_DC("NameSet(" << this << ")::SymbolicName(" << rName << ", " 
00373   << rNewName <<")");
00374 #ifdef FAUDES_CHECKED
00375   if (! Exists(rName)) {
00376     std::stringstream errstr;
00377     errstr << "name \"" << rName << "\" not found in NameSet" << std::endl;
00378     throw Exception("NameSet::Symbolic", errstr.str(), 66);
00379   }
00380 #endif
00381   mpSymbolTable->SetEntry(Index(rName), rNewName);
00382 }
00383 
00384 // Index(rName)
00385 Idx NameSet::Index(const std::string& rName) const {
00386   return mpSymbolTable->Index(rName);
00387 }
00388 
00389 // Exists(index)
00390 bool NameSet::Exists(const Idx& index) const {
00391   return TBaseSet<Idx>::Exists(index);
00392 }
00393 
00394 // Exists(rName)
00395 bool NameSet::Exists(const std::string& rName) const {
00396   return TBaseSet<Idx>::Exists(mpSymbolTable->Index(rName)) ;
00397 }
00398 
00399 // Find(index) const
00400 NameSet::Iterator NameSet::Find(const Idx& index) const {
00401   return TBaseSet<Idx>::Find(index);
00402 }
00403 
00404 // Find(rName) const
00405 NameSet::Iterator NameSet::Find(const std::string& rName) const {
00406   return TBaseSet<Idx>::Find(mpSymbolTable->Index(rName));
00407 }
00408 
00409 
00410 // operator +
00411 NameSet NameSet::operator + (const NameSet& rOtherSet) const {
00412   FD_DC("NameSet(" << this << ")::operator + (" << &rOtherSet << ")");
00413 #ifdef FAUDES_CHECKED
00414   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00415     std::stringstream errstr;
00416     errstr << "symboltable mismazch aka not implemented" << std::endl;
00417     throw Exception("NameSet::Operator+", errstr.str(), 67);
00418   }
00419 #endif
00420   NameSet res= NewN();
00421   FnctUnion(rOtherSet,res);
00422   return res;
00423 }
00424 
00425 // operator -
00426 NameSet NameSet::operator - (const NameSet& rOtherSet) const {
00427   FD_DC("NameSet(" << this << ")::operator - (" << &rOtherSet << ")");
00428 #ifdef FAUDES_CHECKED
00429   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00430     std::stringstream errstr;
00431     errstr << "symboltable mismazch aka not implemented" << std::endl;
00432     throw Exception("NameSet::Operator-", errstr.str(), 67);
00433   }
00434 #endif
00435   NameSet res= NewN();
00436   FnctDifference(rOtherSet,res);
00437   return res;
00438 }
00439 
00440 // operator *
00441 NameSet NameSet::operator * (const NameSet& rOtherSet) const {
00442   FD_DC("NameSet(" << this << ")::operator * (" << &rOtherSet << ")");
00443 #ifdef FAUDES_CHECKED
00444   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00445     std::stringstream errstr;
00446     errstr << "symboltable mismazch aka not implemented" << std::endl;
00447     throw Exception("NameSet::Operator*", errstr.str(), 67);
00448   }
00449 #endif
00450   NameSet res= NewN();
00451   FnctIntersection(rOtherSet,res);
00452   return res;
00453 }
00454 
00455 // operator <=
00456 bool NameSet::operator <= (const NameSet& rOtherSet) const {
00457 #ifdef FAUDES_CHECKED
00458   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00459     std::stringstream errstr;
00460     errstr << "symboltable mismazch aka not implemented" << std::endl;
00461     throw Exception("NameSet::Operator<=", errstr.str(), 67);
00462   }
00463 #endif
00464   return TBaseSet<Idx>::operator <= (rOtherSet);
00465 }
00466 
00467 // operator >=
00468 bool NameSet::operator >= (const NameSet& rOtherSet) const {
00469 #ifdef FAUDES_CHECKED
00470   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00471     std::stringstream errstr;
00472     errstr << "symboltable mismazch aka not implemented" << std::endl;
00473     throw Exception("NameSet::Operator>=", errstr.str(), 67);
00474   }
00475 #endif
00476   return TBaseSet<Idx>::operator >= (rOtherSet);
00477 }
00478 
00479 // Str(index)
00480 std::string NameSet::Str(const Idx& index) const {
00481   return mpSymbolTable->Symbol(index)+"["+faudes::ToStringInteger(index)+"]";
00482 }
00483 
00484 
00485 /*
00486 *********************************************************************************
00487 *********************************************************************************
00488 *********************************************************************************
00489 
00490  Misc
00491 
00492 *********************************************************************************
00493 *********************************************************************************
00494 *********************************************************************************
00495 */
00496 
00497 
00498 // RTI convenience function
00499 void SetIntersection(const EventSetVector& rSetVec, EventSet& rRes) {
00500   // prepare result
00501   rRes.Clear();
00502   // ignore empty
00503   if(rSetVec.Size()==0) return;
00504   // copy first
00505   rRes.Assign(rSetVec.At(0));
00506   // perform intersecttion 
00507   for(EventSetVector::Position i=1; i<rSetVec.Size(); i++) 
00508     SetIntersection(rSetVec.At(i),rRes,rRes);
00509 }
00510 
00511 
00512 // RTI convenience function
00513 void SetUnion(const EventSetVector& rSetVec, EventSet& rRes) {
00514   // prepare result
00515   rRes.Clear();
00516   // ignore empty
00517   if(rSetVec.Size()==0) return;
00518   // copy first
00519   rRes.Assign(rSetVec.At(0));
00520   // perform union
00521   for(EventSetVector::Position i=1; i<rSetVec.Size(); i++) 
00522     SetUnion(rSetVec.At(i),rRes,rRes);
00523 }
00524 
00525 
00526 } // namespace faudes
00527 
00528 

libFAUDES 2.18b --- 2010-12-17 --- c++ source docu by doxygen 1.6.3