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(etstr)) {
00236       FD_DC("TaNameSet(" << this << ")::DoRead(..): inserting element in xml format \"" 
00237       << token.StringValue() << "\"");
00238       rTr.ReadBegin(etstr,token);
00239       name=token.AttributeStringValue("name");
00240       // read faudes attribute
00241       attrp->Read(rTr,"",pContext);
00242       // skip unknown faudes attributes
00243       rTr.ReadEnd(etstr);
00244       // insert element with attribute
00245       Idx index=Insert(name); 
00246       Attribute(index,*attrp);
00247       continue;
00248     }
00249     // cannot process token
00250     delete attrp;
00251     std::stringstream errstr;
00252     errstr << "Invalid token of type " << token.Type() << " at " << rTr.FileLine();
00253     throw Exception("NameSet::DoRead", errstr.str(), 50);
00254   }
00255   rTr.ReadEnd(label);
00256   delete attrp;
00257   FD_DC("NameSet(" << this << ")::DoRead(tr," << label << ", " << pContext << "): done");
00258 }
00259 
00260 
00261 // Insert(index)
00262 bool NameSet::Insert(const Idx& index) { 
00263 #ifdef FAUDES_CHECKED
00264   if(!mpSymbolTable->Exists(index)) {
00265     std::stringstream errstr;
00266     errstr << "index " << index << " not known to symboltable" << std::endl;
00267     throw Exception("NameSet::Insert", errstr.str(), 65);
00268   }
00269 #endif
00270   return TBaseSet<Idx>::Insert(index);
00271 }
00272 
00273 // Insert(rName)
00274 Idx NameSet::Insert(const std::string& rName) {
00275   FD_DC("NameSet(" << this << ")::Insert(" << rName <<")");
00276   Idx index = mpSymbolTable->InsEntry(rName);
00277   TBaseSet<Idx>::Insert(index);
00278   return index;
00279 }
00280 
00281 // InsertSet(set)
00282 void NameSet::InsertSet(const NameSet& rOtherSet) {
00283   FD_DC("NameSet(" << this << ")::InsertSet(" << rOtherSet.ToString() << ")");
00284 #ifdef FAUDES_CHECKED
00285   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00286     std::stringstream errstr;
00287     errstr << "symboltable mismatch aka not implemented" << std::endl;
00288     throw Exception("NameSet::InsertSet", errstr.str(), 67);
00289   }
00290 #endif
00291   TBaseSet<Idx>::InsertSet(rOtherSet);
00292 }
00293     
00294 // Erase(index)
00295 bool NameSet::Erase(const Idx& index) {
00296   FD_DC("NameSet(" << this << ")::Erase(" << index <<")");
00297   return TBaseSet<Idx>::Erase(index);
00298 }
00299 
00300 // Erase(rName)
00301 bool NameSet::Erase(const std::string& rName) {
00302   FD_DC("NameSet(" << this << ")::Erase(" << rName <<")");
00303   Idx index = mpSymbolTable->Index(rName);
00304 #ifdef FAUDES_CHECKED
00305   if (index == 0) {
00306     std::stringstream errstr;
00307     errstr << "name \"" << rName << "\" not found in NameSet" << std::endl;
00308     throw Exception("NameSet::Erase", errstr.str(), 66);
00309   }
00310 #endif
00311   return TBaseSet<Idx>::Erase(index);
00312 }
00313 
00314 // Erase(pos)
00315 NameSet::Iterator NameSet::Erase(const Iterator& pos) {
00316   return TBaseSet<Idx>::Erase(pos);
00317 }
00318 
00319 // EraseSet(set)
00320 void NameSet::EraseSet(const NameSet& rOtherSet) {
00321 #ifdef FAUDES_CHECKED
00322   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00323     std::stringstream errstr;
00324     errstr << "symboltable mismatch aka not implemented" << std::endl;
00325     throw Exception("NameSet::EraseSet", errstr.str(), 67);
00326   }
00327 #endif
00328   TBaseSet<Idx>::EraseSet(rOtherSet);
00329 }
00330 
00331 // RestrictSet(set)
00332 void NameSet::RestrictSet(const NameSet& rOtherSet) {
00333 #ifdef FAUDES_CHECKED
00334   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00335     std::stringstream errstr;
00336     errstr << "symboltable mismatch aka not implemented" << std::endl;
00337     throw Exception("NameSet::RestrictSet", errstr.str(), 67);
00338   }
00339 #endif
00340   TBaseSet<Idx>::RestrictSet(rOtherSet);
00341 }
00342 
00343 // SymbolicName(index)
00344 std::string NameSet::SymbolicName(Idx index) const {
00345   return mpSymbolTable->Symbol(index);
00346 }
00347 
00348 // SymbolicName(index, name)
00349 void NameSet::SymbolicName(Idx index, const std::string& rName) {
00350   FD_DC("NameSet(" << this << ")::SymbolicName(" << index << ", " << rName <<")");
00351 #ifdef FAUDES_CHECKED
00352   if (! Exists(index)) {
00353     std::stringstream errstr;
00354     errstr << "index " << index << " not in this set" << std::endl;
00355     throw Exception("NameSet::SymbolicName", errstr.str(), 60);
00356   }
00357 #endif
00358   mpSymbolTable->SetEntry(index, rName);
00359 }
00360 
00361 // SymbolicName(name, name)
00362 void NameSet::SymbolicName(const std::string& rName, 
00363           const std::string& rNewName) {
00364   FD_DC("NameSet(" << this << ")::SymbolicName(" << rName << ", " 
00365   << rNewName <<")");
00366 #ifdef FAUDES_CHECKED
00367   if (! Exists(rName)) {
00368     std::stringstream errstr;
00369     errstr << "name \"" << rName << "\" not found in NameSet" << std::endl;
00370     throw Exception("NameSet::Symbolic", errstr.str(), 66);
00371   }
00372 #endif
00373   mpSymbolTable->SetEntry(Index(rName), rNewName);
00374 }
00375 
00376 // Index(rName)
00377 Idx NameSet::Index(const std::string& rName) const {
00378   return mpSymbolTable->Index(rName);
00379 }
00380 
00381 // Exists(index)
00382 bool NameSet::Exists(const Idx& index) const {
00383   return TBaseSet<Idx>::Exists(index);
00384 }
00385 
00386 // Exists(rName)
00387 bool NameSet::Exists(const std::string& rName) const {
00388   return TBaseSet<Idx>::Exists(mpSymbolTable->Index(rName)) ;
00389 }
00390 
00391 // Find(index) const
00392 NameSet::Iterator NameSet::Find(const Idx& index) const {
00393   return TBaseSet<Idx>::Find(index);
00394 }
00395 
00396 // Find(rName) const
00397 NameSet::Iterator NameSet::Find(const std::string& rName) const {
00398   return TBaseSet<Idx>::Find(mpSymbolTable->Index(rName));
00399 }
00400 
00401 
00402 // operator +
00403 NameSet NameSet::operator + (const NameSet& rOtherSet) const {
00404   FD_DC("NameSet(" << this << ")::operator + (" << &rOtherSet << ")");
00405 #ifdef FAUDES_CHECKED
00406   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00407     std::stringstream errstr;
00408     errstr << "symboltable mismazch aka not implemented" << std::endl;
00409     throw Exception("NameSet::Operator+", errstr.str(), 67);
00410   }
00411 #endif
00412   NameSet res= NewN();
00413   FnctUnion(rOtherSet,res);
00414   return res;
00415 }
00416 
00417 // operator -
00418 NameSet NameSet::operator - (const NameSet& rOtherSet) const {
00419   FD_DC("NameSet(" << this << ")::operator - (" << &rOtherSet << ")");
00420 #ifdef FAUDES_CHECKED
00421   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00422     std::stringstream errstr;
00423     errstr << "symboltable mismazch aka not implemented" << std::endl;
00424     throw Exception("NameSet::Operator-", errstr.str(), 67);
00425   }
00426 #endif
00427   NameSet res= NewN();
00428   FnctDifference(rOtherSet,res);
00429   return res;
00430 }
00431 
00432 // operator *
00433 NameSet NameSet::operator * (const NameSet& rOtherSet) const {
00434   FD_DC("NameSet(" << this << ")::operator * (" << &rOtherSet << ")");
00435 #ifdef FAUDES_CHECKED
00436   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00437     std::stringstream errstr;
00438     errstr << "symboltable mismazch aka not implemented" << std::endl;
00439     throw Exception("NameSet::Operator*", errstr.str(), 67);
00440   }
00441 #endif
00442   NameSet res= NewN();
00443   FnctIntersection(rOtherSet,res);
00444   return res;
00445 }
00446 
00447 // operator <=
00448 bool NameSet::operator <= (const NameSet& rOtherSet) const {
00449 #ifdef FAUDES_CHECKED
00450   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00451     std::stringstream errstr;
00452     errstr << "symboltable mismazch aka not implemented" << std::endl;
00453     throw Exception("NameSet::Operator<=", errstr.str(), 67);
00454   }
00455 #endif
00456   return TBaseSet<Idx>::operator <= (rOtherSet);
00457 }
00458 
00459 // operator >=
00460 bool NameSet::operator >= (const NameSet& rOtherSet) const {
00461 #ifdef FAUDES_CHECKED
00462   if(rOtherSet.mpSymbolTable!=mpSymbolTable) {
00463     std::stringstream errstr;
00464     errstr << "symboltable mismazch aka not implemented" << std::endl;
00465     throw Exception("NameSet::Operator>=", errstr.str(), 67);
00466   }
00467 #endif
00468   return TBaseSet<Idx>::operator >= (rOtherSet);
00469 }
00470 
00471 // Str(index)
00472 std::string NameSet::Str(const Idx& index) const {
00473   return mpSymbolTable->Symbol(index)+"#"+faudes::ToStringInteger(index);
00474 }
00475 
00476 
00477 /*
00478 *********************************************************************************
00479 *********************************************************************************
00480 *********************************************************************************
00481 
00482  Misc
00483 
00484 *********************************************************************************
00485 *********************************************************************************
00486 *********************************************************************************
00487 */
00488 
00489 
00490 // RTI convenience function
00491 void SetIntersection(const EventSetVector& rSetVec, EventSet& rRes) {
00492   // prepare result
00493   rRes.Clear();
00494   // ignore empty
00495   if(rSetVec.Size()==0) return;
00496   // copy first
00497   rRes.Assign(rSetVec.At(0));
00498   // perform intersecttion 
00499   for(EventSetVector::Position i=1; i<rSetVec.Size(); i++) 
00500     SetIntersection(rSetVec.At(i),rRes,rRes);
00501 }
00502 
00503 
00504 // RTI convenience function
00505 void SetUnion(const EventSetVector& rSetVec, EventSet& rRes) {
00506   // prepare result
00507   rRes.Clear();
00508   // ignore empty
00509   if(rSetVec.Size()==0) return;
00510   // copy first
00511   rRes.Assign(rSetVec.At(0));
00512   // perform union
00513   for(EventSetVector::Position i=1; i<rSetVec.Size(); i++) 
00514     SetUnion(rSetVec.At(i),rRes,rRes);
00515 }
00516 
00517 
00518 } // namespace faudes
00519 
00520 

libFAUDES 2.23h --- 2014.04.03 --- c++ api documentaion by doxygen