• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepimlibs API Reference
  • KDE Home
  • Contact Us
 

syndication/rdf

  • sources
  • kde-4.14
  • kdepimlibs
  • syndication
  • rdf
model.cpp
1 /*
2  * This file is part of the syndication library
3  *
4  * Copyright (C) 2006 Frank Osterfeld <osterfeld@kde.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "model.h"
24 #include "model_p.h"
25 
26 namespace Syndication {
27 namespace RDF {
28 
29 long Model::ModelPrivate::idCounter = 0;
30 
31 Model::Model() : d(new ModelPrivate)
32 {
33 }
34 
35 Model::Model(const Model& other)
36 {
37  *this = other;
38 }
39 
40 Model::~Model()
41 {
42 }
43 
44 Model& Model::operator=(const Model& other)
45 {
46  d = other.d;
47  return *this;
48 }
49 
50 bool Model::operator==(const Model& other) const
51 {
52  return *d == *(other.d);
53 }
54 
55 PropertyPtr Model::createProperty(const QString& uri)
56 {
57  PropertyPtr prop;
58 
59  if (d->properties.contains(uri))
60  {
61  prop = d->properties[uri];
62  }
63  else
64  {
65  prop = PropertyPtr( new Property(uri) );
66  prop->setModel(*this);
67  // if there is a resource object with the same uri, replace
68  // the resource object by the new property object and reuse the id
69  if (d->resources.contains(uri))
70  {
71  prop->setId(d->resources[uri]->id());
72  }
73  d->addToHashes(prop);
74  }
75 
76  return prop;
77 
78 }
79 
80 ResourcePtr Model::createResource(const QString& uri)
81 {
82  ResourcePtr res;
83 
84  if (d->resources.contains(uri))
85  {
86  res = d->resources[uri];
87  }
88  else
89  {
90  res = ResourcePtr( new Resource(uri) );
91  res->setModel(*this);
92  d->addToHashes(res);
93  }
94 
95  return res;
96 }
97 
98 SequencePtr Model::createSequence(const QString& uri)
99 {
100  SequencePtr seq;
101 
102  if (d->sequences.contains(uri))
103  {
104  seq = d->sequences[uri];
105  }
106  else
107  {
108  seq = SequencePtr( new Sequence(uri) );
109  seq->setModel(*this);
110  // if there is a resource object with the same uri, replace
111  // the resource object by the new sequence object and reuse the id
112  if (d->resources.contains(uri))
113  {
114  seq->setId(d->resources[uri]->id());
115  }
116 
117  d->addToHashes(seq);
118  }
119 
120  return seq;
121 }
122 
123 LiteralPtr Model::createLiteral(const QString& text)
124 {
125  LiteralPtr lit(new Literal(text));
126 
127  d->addToHashes(lit);
128  return lit;
129 }
130 
131 
132 void Model::removeStatement(StatementPtr statement)
133 {
134  removeStatement(statement->subject(), statement->predicate(), statement->object());
135 }
136 
137 void Model::removeStatement(ResourcePtr subject, PropertyPtr predicate, NodePtr object)
138 {
139  QString key = QString::fromLatin1("%1-%2-%3")
140  .arg(QString::number(subject->id()))
141  .arg(QString::number(predicate->id()))
142  .arg(QString::number(object->id()));
143  d->removeFromHashes(key);
144 }
145 
146 StatementPtr Model::addStatement(ResourcePtr subject, PropertyPtr predicate, NodePtr object)
147 {
148  d->init();
149  ResourcePtr subjInternal = subject;
150 
151  if (!d->nodes.contains(subjInternal->id()))
152  {
153  subjInternal = ResourcePtr( subject->clone() );
154  subjInternal->setModel(*this);
155  d->addToHashes(subjInternal);
156  }
157 
158  PropertyPtr predInternal = predicate;
159 
160  if (!d->nodes.contains(predInternal->id()))
161  {
162  predInternal = PropertyPtr( predicate->clone() );
163  predInternal->setModel(*this);
164  d->addToHashes(predInternal);
165  }
166 
167  NodePtr objInternal = object;
168 
169  if (!d->nodes.contains(objInternal->id()))
170  {
171  objInternal = NodePtr( object->clone() );
172  objInternal->setModel(*this);
173  d->addToHashes(objInternal);
174  }
175 
176  // TODO: avoid duplicated stmts with literal objects!
177 
178  QString key = QString::fromLatin1("%1-%2-%3")
179  .arg(QString::number(subjInternal->id()))
180  .arg(QString::number(predInternal->id()))
181  .arg(QString::number(objInternal->id()));
182 
183  StatementPtr stmt;
184 
185  if (!d->statements.contains(key))
186  {
187  stmt = StatementPtr( new Statement(subjInternal, predInternal, objInternal) );
188  d->addToHashes(stmt, key);
189  }
190  else
191  {
192  stmt = d->statements[key];
193  }
194 
195  return stmt;
196 }
197 
198 bool Model::isEmpty() const
199 {
200  return d->statements.isEmpty();
201 }
202 
203 bool Model::resourceHasProperty(const Resource* resource, PropertyPtr property) const
204 {
205  return d->resourceHasProperty( resource, property );
206 }
207 
208 bool Model::ModelPrivate::resourceHasProperty(const Resource* resource, PropertyPtr property) const
209 {
210  // resource unknown
211  if (!resources.contains(resource->uri()))
212  return false;
213 
214  QList<StatementPtr> stmts = stmtsBySubject[resource->uri()];
215  QList<StatementPtr>::ConstIterator it = stmts.constBegin();
216  QList<StatementPtr>::ConstIterator end = stmts.constEnd();
217 
218  for ( ; it != end; ++it)
219  {
220  if (*((*it)->predicate()) == *property)
221  return true;
222  }
223 
224  return false;
225 }
226 
227 StatementPtr Model::resourceProperty(const Resource* resource, PropertyPtr property) const
228 {
229  return d->resourceProperty(resource, property);
230 }
231 
232 StatementPtr Model::ModelPrivate::resourceProperty(const Resource* resource, PropertyPtr property) const
233 {
234  QList<StatementPtr> stmts = stmtsBySubject[resource->uri()];
235  QList<StatementPtr>::ConstIterator it = stmts.constBegin();
236  QList<StatementPtr>::ConstIterator end = stmts.constEnd();
237 
238  for ( ; it != end; ++it)
239  {
240  if (*((*it)->predicate()) == *property)
241  return *it;
242  }
243 
244  return nullStatement;
245 }
246 
247 QList<StatementPtr> Model::resourceProperties(const Resource* resource, PropertyPtr property) const
248 {
249  return d->resourceProperties( resource, property );
250 }
251 
252 QList<StatementPtr> Model::ModelPrivate::resourceProperties(const Resource* resource, PropertyPtr property) const
253 {
254  QList<StatementPtr> res;
255  QList<StatementPtr> stmts = stmtsBySubject[resource->uri()];
256  QList<StatementPtr>::ConstIterator it = stmts.constBegin();
257  QList<StatementPtr>::ConstIterator end = stmts.constEnd();
258 
259  for ( ; it != end; ++it)
260  {
261  if (*((*it)->predicate()) == *property)
262  res.append(*it);
263  }
264 
265  return res;
266 }
267 
268 
269 QList<StatementPtr> Model::statements() const
270 {
271  return d->statements.values();
272 }
273 
274 QString Model::debugInfo() const
275 {
276  QString info;
277 
278  QList<StatementPtr> stmts = d->statements.values();
279  QList<StatementPtr>::ConstIterator it = stmts.constBegin();
280  QList<StatementPtr>::ConstIterator end = stmts.constEnd();
281 
282  for ( ; it != end; ++it)
283  {
284  info += QString::fromLatin1("<%1> <%2> ").arg((*it)->subject()->uri()).arg((*it)->predicate()->uri());
285 
286  if ((*it)->object()->isLiteral())
287  {
288  info += QString::fromLatin1("\"%1\"\n").arg((*it)->asString());
289  }
290  else
291  {
292  info += QString::fromLatin1("<%1>\n").arg((*it)->asResource()->uri());
293  }
294 
295  }
296 
297  return info;
298 }
299 
300 QList<ResourcePtr> Model::resourcesWithType(ResourcePtr type) const
301 {
302  QList<ResourcePtr> list;
303 
304  QList<StatementPtr> stmts = d->statements.values();
305  QList<StatementPtr>::ConstIterator it = stmts.constBegin();
306  QList<StatementPtr>::ConstIterator end = stmts.constEnd();
307 
308  for ( ; it != end; ++it)
309  {
310  if (*((*it)->predicate()) == *(RDFVocab::self()->type()) && *((*it)->object()) == *type )
311  list.append((*it)->subject());
312  }
313 
314  return list;
315 }
316 
317 NodePtr Model::nodeByID(uint id) const
318 {
319  return d->nodeByID(id);
320 }
321 
322 NodePtr Model::ModelPrivate::nodeByID(uint _id) const
323 {
324  if (!nodes.contains(_id))
325  {
326  return nullLiteral;
327  }
328  else
329  {
330  return nodes.value(_id);
331  }
332 }
333 
334 ResourcePtr Model::resourceByID(uint id) const
335 {
336  return d->resourceByID(id);
337 }
338 
339 ResourcePtr Model::ModelPrivate::resourceByID(uint _id) const
340 {
341  if (!nodes.contains(_id))
342  {
343  return nullResource;
344  }
345  else
346  {
347  NodePtr node = nodes.value(_id);
348  if (node->isResource())
349  return boost::static_pointer_cast<Resource>(node);
350  else
351  return nullResource;
352  }
353 }
354 
355 PropertyPtr Model::propertyByID(uint id) const
356 {
357  return d->propertyByID(id);
358 }
359 
360 PropertyPtr Model::ModelPrivate::propertyByID(uint _id) const
361 {
362  if (!nodes.contains(_id))
363  {
364  return nullProperty;
365  }
366  else
367  {
368  NodePtr node = nodes.value(_id);
369  if (node->isProperty())
370  return boost::static_pointer_cast<Property>(node);
371  else
372  return nullProperty;
373  }
374 }
375 
376 LiteralPtr Model::literalByID(uint id) const
377 {
378  return d->literalByID(id);
379 }
380 
381 
382 LiteralPtr Model::ModelPrivate::literalByID(uint _id) const
383 {
384  if (!nodes.contains(_id))
385  {
386  return nullLiteral;
387  }
388  else
389  {
390  NodePtr node = nodes.value(_id);
391  if (node->isLiteral())
392  return boost::static_pointer_cast<Literal>(node);
393  else
394  return nullLiteral;
395  }
396 }
397 
398 } // namespace RDF
399 } // namespace Syndication
Syndication::RDF::Model::Model
Model()
default constructor, creates an empty model containing no statements
Definition: model.cpp:31
Syndication::RDF::Resource
Resources are the entities in the RDF graph.
Definition: resource.h:51
Syndication::RDF::Statement
An RDF statement, consisting of a triple (subject, predicate, object).
Definition: statement.h:43
Syndication::RDF::Model
An RDF model, a set of RDF statements.
Definition: model.h:49
Syndication::RDF::Model::createProperty
virtual PropertyPtr createProperty(const QString &uri)
creates a property and associates it with this model.
Definition: model.cpp:55
Syndication::RDF::Property
a property is node type that represents properties of things, like "name" is a property of a person...
Definition: property.h:45
Syndication::RDF::Model::operator=
Model & operator=(const Model &other)
assigns another model.
Definition: model.cpp:44
Syndication::RDF::Model::nodeByID
virtual NodePtr nodeByID(uint id) const
searches the model for a node by ID.
Definition: model.cpp:317
Syndication::RDF::Model::createSequence
virtual SequencePtr createSequence(const QString &uri=QString())
creates a sequence and associates it with this model.
Definition: model.cpp:98
Syndication::RDF::Model::operator==
bool operator==(const Model &other) const
Returns whether two models objects represent the same model (i.e.
Definition: model.cpp:50
Syndication::RDF::Model::debugInfo
virtual QString debugInfo() const
/** a debug string listing the contained statements for debugging purposes
Definition: model.cpp:274
Syndication::RDF::Sequence
Sequence container, a sequence contains an ordered list of RDF nodes.
Definition: sequence.h:45
Syndication::RDF::Model::~Model
virtual ~Model()
destructor
Definition: model.cpp:40
Syndication::RDF::Model::addStatement
virtual StatementPtr addStatement(ResourcePtr subject, PropertyPtr predicate, NodePtr object)
adds a statement to the model.
Definition: model.cpp:146
QString::number
QString number(int n, int base)
QList::append
void append(const T &value)
Syndication::RDF::Model::resourceByID
virtual ResourcePtr resourceByID(uint id) const
searches the model for a resource by ID.
Definition: model.cpp:334
QString
QList
Definition: document.h:30
Syndication::RDF::Model::isEmpty
virtual bool isEmpty() const
returns whether this model is empty, i.e.
Definition: model.cpp:198
Syndication::RDF::Model::resourcesWithType
virtual QList< ResourcePtr > resourcesWithType(ResourcePtr type) const
returns all resources of a given type.
Definition: model.cpp:300
Syndication::RDF::Model::literalByID
virtual LiteralPtr literalByID(uint id) const
searches the model for a literal by ID.
Definition: model.cpp:376
Syndication::RDF::Model::removeStatement
virtual void removeStatement(ResourcePtr subject, PropertyPtr predicate, NodePtr object)
removes a statement from the model.
Definition: model.cpp:137
Syndication::RDF::Model::propertyByID
virtual PropertyPtr propertyByID(uint id) const
searches the model for a property by ID.
Definition: model.cpp:355
Syndication::RDF::Literal
a node type representing simple string values.
Definition: literal.h:44
Syndication::RDF::Model::statements
virtual QList< StatementPtr > statements() const
returns a list of the statements in this model.
Definition: model.cpp:269
QString::fromLatin1
QString fromLatin1(const char *str, int size)
Syndication::RDF::Model::createLiteral
virtual LiteralPtr createLiteral(const QString &text)
creates a literal and associates it with this model.
Definition: model.cpp:123
Syndication::RDF::Model::createResource
virtual ResourcePtr createResource(const QString &uri=QString())
creates a resource and associates it with this model.
Definition: model.cpp:80
Syndication::RDF::RDFVocab::self
static RDFVocab * self()
returns the singleton instance
Definition: rdfvocab.cpp:51
QList::constEnd
const_iterator constEnd() const
QList::constBegin
const_iterator constBegin() const
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
Syndication::RDF::RDFVocab::type
PropertyPtr type()
the rdf:type property (A rdf:type B means A is instance of B)
Definition: rdfvocab.cpp:82
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:37:38 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

syndication/rdf

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

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal