• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

syndication/rdf

model.cpp

00001 /*
00002  * This file is part of the syndication library
00003  *
00004  * Copyright (C) 2006 Frank Osterfeld <osterfeld@kde.org>
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Library General Public License
00017  * along with this library; see the file COPYING.LIB.  If not, write to
00018  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "model.h"
00024 #include "model_p.h"
00025 
00026 namespace Syndication {
00027 namespace RDF {
00028 
00029 long Model::ModelPrivate::idCounter = 0;
00030 
00031 Model::Model() : d(new ModelPrivate)
00032 {
00033 }
00034 
00035 Model::Model(const Model& other)
00036 {
00037     *this = other;
00038 }
00039 
00040 Model::~Model()
00041 {
00042 }
00043 
00044 Model& Model::operator=(const Model& other)
00045 {
00046     d = other.d;
00047     return *this;
00048 }
00049 
00050 bool Model::operator==(const Model& other) const
00051 {
00052     return *d == *(other.d);
00053 }
00054 
00055 PropertyPtr Model::createProperty(const QString& uri)
00056 {
00057     PropertyPtr prop;
00058 
00059     if (d->properties.contains(uri))
00060     {
00061         prop = d->properties[uri];
00062     }
00063     else
00064     {
00065         prop = PropertyPtr( new Property(uri) );
00066         prop->setModel(*this);
00067         // if there is a resource object with the same uri, replace
00068         // the resource object by the new property object and reuse the id
00069         if (d->resources.contains(uri))
00070         {
00071             prop->setId(d->resources[uri]->id());
00072         }
00073         d->addToHashes(prop);
00074     }
00075 
00076     return prop;
00077 
00078 }
00079 
00080 ResourcePtr Model::createResource(const QString& uri)
00081 {
00082     ResourcePtr res;
00083 
00084     if (d->resources.contains(uri))
00085     {
00086         res = d->resources[uri];
00087     }
00088     else
00089     {
00090         res = ResourcePtr( new Resource(uri) );
00091         res->setModel(*this);
00092         d->addToHashes(res);
00093     }
00094 
00095     return res;
00096 }
00097 
00098 SequencePtr Model::createSequence(const QString& uri)
00099 {
00100     SequencePtr seq;
00101 
00102     if (d->sequences.contains(uri))
00103     {
00104         seq = d->sequences[uri];
00105     }
00106     else
00107     {
00108         seq = SequencePtr( new Sequence(uri) );
00109         seq->setModel(*this);
00110         // if there is a resource object with the same uri, replace
00111         // the resource object by the new sequence object and reuse the id
00112         if (d->resources.contains(uri))
00113         {
00114             seq->setId(d->resources[uri]->id());
00115         }
00116 
00117         d->addToHashes(seq);
00118     }
00119 
00120     return seq;
00121 }
00122 
00123 LiteralPtr Model::createLiteral(const QString& text)
00124 {
00125     LiteralPtr lit(new Literal(text));
00126 
00127     d->addToHashes(lit);
00128     return lit;
00129 }
00130 
00131 
00132 void Model::removeStatement(StatementPtr statement)
00133 {
00134     removeStatement(statement->subject(), statement->predicate(), statement->object());
00135 }
00136 
00137 void Model::removeStatement(ResourcePtr subject, PropertyPtr predicate, NodePtr object)
00138 {
00139     QString key = QString("%1-%2-%3")
00140             .arg(QString::number(subject->id()))
00141             .arg(QString::number(predicate->id()))
00142             .arg(QString::number(object->id()));
00143     d->removeFromHashes(key);
00144 }
00145 
00146 StatementPtr Model::addStatement(ResourcePtr subject, PropertyPtr predicate, NodePtr object)
00147 {
00148     d->init();
00149     ResourcePtr subjInternal = subject;
00150 
00151     if (!d->nodes.contains(subjInternal->id()))
00152     {
00153         subjInternal = ResourcePtr( subject->clone() );
00154         subjInternal->setModel(*this);
00155         d->addToHashes(subjInternal);
00156     }
00157 
00158     PropertyPtr predInternal = predicate;
00159 
00160     if (!d->nodes.contains(predInternal->id()))
00161     {
00162         predInternal = PropertyPtr( predicate->clone() );
00163         predInternal->setModel(*this);
00164         d->addToHashes(predInternal);
00165     }
00166 
00167     NodePtr objInternal = object;
00168 
00169     if (!d->nodes.contains(objInternal->id()))
00170     {
00171         objInternal = NodePtr( object->clone() );
00172         objInternal->setModel(*this);
00173         d->addToHashes(objInternal);
00174     }
00175 
00176     // TODO: avoid duplicated stmts with literal objects!
00177 
00178     QString key = QString("%1-%2-%3")
00179             .arg(QString::number(subjInternal->id()))
00180             .arg(QString::number(predInternal->id()))
00181             .arg(QString::number(objInternal->id()));
00182 
00183     StatementPtr stmt;
00184 
00185     if (!d->statements.contains(key))
00186     {
00187         stmt = StatementPtr( new Statement(subjInternal, predInternal, objInternal) );
00188         d->addToHashes(stmt, key);
00189     }
00190     else
00191     {
00192         stmt = d->statements[key];
00193     }
00194 
00195     return stmt;
00196 }
00197 
00198 bool Model::isEmpty() const
00199 {
00200     return d->statements.isEmpty();
00201 }
00202 
00203 bool Model::resourceHasProperty(const Resource* resource, PropertyPtr property) const
00204 {
00205     return d->resourceHasProperty( resource, property );
00206 }
00207 
00208 bool Model::ModelPrivate::resourceHasProperty(const Resource* resource, PropertyPtr property) const
00209 {
00210     // resource unknown
00211     if (!resources.contains(resource->uri()))
00212         return false;
00213 
00214     QList<StatementPtr> stmts = stmtsBySubject[resource->uri()];
00215     QList<StatementPtr>::ConstIterator it = stmts.constBegin();
00216     QList<StatementPtr>::ConstIterator end = stmts.constEnd();
00217 
00218     for ( ; it != end; ++it)
00219     {
00220         if (*((*it)->predicate()) == *property)
00221             return true;
00222     }
00223 
00224     return false;
00225 }
00226 
00227 StatementPtr Model::resourceProperty(const Resource* resource, PropertyPtr property) const
00228 {
00229     return d->resourceProperty(resource, property);
00230 }
00231 
00232 StatementPtr Model::ModelPrivate::resourceProperty(const Resource* resource, PropertyPtr property) const
00233 {
00234     QList<StatementPtr> stmts = stmtsBySubject[resource->uri()];
00235     QList<StatementPtr>::ConstIterator it = stmts.constBegin();
00236     QList<StatementPtr>::ConstIterator end = stmts.constEnd();
00237 
00238     for ( ; it != end; ++it)
00239     {
00240         if (*((*it)->predicate()) == *property)
00241             return *it;
00242     }
00243 
00244     return nullStatement;
00245 }
00246 
00247 QList<StatementPtr> Model::resourceProperties(const Resource* resource, PropertyPtr property) const
00248 {
00249     return d->resourceProperties( resource, property );
00250 }
00251 
00252 QList<StatementPtr> Model::ModelPrivate::resourceProperties(const Resource* resource, PropertyPtr property) const
00253 {
00254     QList<StatementPtr> res;
00255     QList<StatementPtr> stmts = stmtsBySubject[resource->uri()];
00256     QList<StatementPtr>::ConstIterator it = stmts.constBegin();
00257     QList<StatementPtr>::ConstIterator end = stmts.constEnd();
00258 
00259     for ( ; it != end; ++it)
00260     {
00261         if (*((*it)->predicate()) == *property)
00262             res.append(*it);
00263     }
00264 
00265     return res;
00266 }
00267 
00268 
00269 QList<StatementPtr> Model::statements() const
00270 {
00271     return d->statements.values();
00272 }
00273 
00274 QString Model::debugInfo() const
00275 {
00276     QString info;
00277 
00278     QList<StatementPtr> stmts = d->statements.values();
00279     QList<StatementPtr>::ConstIterator it = stmts.constBegin();
00280     QList<StatementPtr>::ConstIterator end = stmts.constEnd();
00281 
00282     for ( ; it != end; ++it)
00283     {
00284         info += QString("<%1> <%2> ").arg((*it)->subject()->uri()).arg((*it)->predicate()->uri());
00285 
00286         if ((*it)->object()->isLiteral())
00287         {
00288             info += QString("\"%1\"\n").arg((*it)->asString());
00289         }
00290         else
00291         {
00292             info += QString("<%1>\n").arg((*it)->asResource()->uri());
00293         }
00294 
00295     }
00296 
00297     return info;
00298 }
00299 
00300 QList<ResourcePtr> Model::resourcesWithType(ResourcePtr type) const
00301 {
00302     QList<ResourcePtr> list;
00303 
00304     QList<StatementPtr> stmts = d->statements.values();
00305     QList<StatementPtr>::ConstIterator it = stmts.constBegin();
00306     QList<StatementPtr>::ConstIterator end = stmts.constEnd();
00307 
00308     for ( ; it != end; ++it)
00309     {
00310         if (*((*it)->predicate()) == *(RDFVocab::self()->type()) && *((*it)->object()) == *type )
00311             list.append((*it)->subject());
00312     }
00313 
00314     return list;
00315 }
00316 
00317 NodePtr Model::nodeByID(uint id) const
00318 {
00319     return d->nodeByID(id);
00320 }
00321 
00322 NodePtr Model::ModelPrivate::nodeByID(uint id) const
00323 {
00324     if (!nodes.contains(id))
00325     {
00326         return nullLiteral;
00327     }
00328     else
00329     {
00330         return nodes.value(id);
00331     }
00332 }
00333 
00334 ResourcePtr Model::resourceByID(uint id) const
00335 {
00336     return d->resourceByID(id);
00337 }
00338 
00339 ResourcePtr Model::ModelPrivate::resourceByID(uint id) const
00340 {
00341     if (!nodes.contains(id))
00342     {
00343         return nullResource;
00344     }
00345     else
00346     {
00347         NodePtr node = nodes.value(id);
00348         if (node->isResource())
00349             return boost::static_pointer_cast<Resource>(node);
00350         else
00351             return nullResource;
00352     }
00353 }
00354 
00355 PropertyPtr Model::propertyByID(uint id) const
00356 {
00357     return d->propertyByID(id);
00358 }
00359 
00360 PropertyPtr Model::ModelPrivate::propertyByID(uint id) const
00361 {
00362     if (!nodes.contains(id))
00363     {
00364         return nullProperty;
00365     }
00366     else
00367     {
00368         NodePtr node = nodes.value(id);
00369         if (node->isProperty())
00370             return  boost::static_pointer_cast<Property>(node);
00371         else
00372             return nullProperty;
00373     }
00374 }
00375 
00376 LiteralPtr Model::literalByID(uint id) const
00377 {
00378     return d->literalByID(id);
00379 }
00380 
00381 
00382 LiteralPtr Model::ModelPrivate::literalByID(uint id) const
00383 {
00384     if (!nodes.contains(id))
00385     {
00386         return nullLiteral;
00387     }
00388     else
00389     {
00390         NodePtr node = nodes.value(id);
00391         if (node->isLiteral())
00392             return boost::static_pointer_cast<Literal>(node);
00393         else
00394             return nullLiteral;
00395     }
00396 }
00397 
00398 } // namespace RDF
00399 } // namespace Syndication

syndication/rdf

Skip menu "syndication/rdf"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  • kabc
  • kblog
  • kcal
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal