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

libFAUDES 2.20d --- 2011.04.26 --- c++ source docu by doxygen