Syndication

model.cpp
1/*
2 This file is part of the syndication library
3 SPDX-FileCopyrightText: 2006 Frank Osterfeld <osterfeld@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "model.h"
9#include "model_p.h"
10
11namespace Syndication
12{
13namespace RDF
14{
15long Model::ModelPrivate::idCounter = 0;
16
18 : d(new ModelPrivate)
19{
20}
21
22Model::Model(const Model &other)
23{
24 *this = other;
25}
26
28{
29}
30
32{
33 d = other.d;
34 return *this;
35}
36
37bool Model::operator==(const Model &other) const
38{
39 return *d == *(other.d);
40}
41
43{
44 PropertyPtr prop;
45
46 if (d->properties.contains(uri)) {
47 prop = d->properties[uri];
48 } else {
49 prop = PropertyPtr(new Property(uri));
50 prop->setModel(*this);
51 // if there is a resource object with the same uri, replace
52 // the resource object by the new property object and reuse the id
53 if (d->resources.contains(uri)) {
54 prop->setId(d->resources[uri]->id());
55 }
56 d->addToHashes(prop);
57 }
58
59 return prop;
60}
61
63{
64 ResourcePtr res;
65
66 if (d->resources.contains(uri)) {
67 res = d->resources[uri];
68 } else {
69 res = ResourcePtr(new Resource(uri));
70 res->setModel(*this);
71 d->addToHashes(res);
72 }
73
74 return res;
75}
76
77SequencePtr Model::createSequence(const QString &uri)
78{
79 SequencePtr seq;
80
81 if (d->sequences.contains(uri)) {
82 seq = d->sequences[uri];
83 } else {
84 seq = SequencePtr(new Sequence(uri));
85 seq->setModel(*this);
86 // if there is a resource object with the same uri, replace
87 // the resource object by the new sequence object and reuse the id
88 if (d->resources.contains(uri)) {
89 seq->setId(d->resources[uri]->id());
90 }
91
92 d->addToHashes(seq);
93 }
94
95 return seq;
96}
97
98LiteralPtr Model::createLiteral(const QString &text)
99{
100 LiteralPtr lit(new Literal(text));
101
102 d->addToHashes(lit);
103 return lit;
104}
105
107{
108 removeStatement(statement->subject(), statement->predicate(), statement->object());
109}
110
111void Model::removeStatement(ResourcePtr subject, PropertyPtr predicate, NodePtr object)
112{
113 /* clang-format off */
114 QString key = QStringLiteral("%1-%2-%3").arg(QString::number(subject->id()),
115 QString::number(predicate->id()),
116 QString::number(object->id()));
117 /* clang-format on */
118
119 d->removeFromHashes(key);
120}
121
122StatementPtr Model::addStatement(ResourcePtr subject, PropertyPtr predicate, NodePtr object)
123{
124 d->init(d);
125 ResourcePtr subjInternal = subject;
126
127 if (!d->nodes.contains(subjInternal->id())) {
128 subjInternal = ResourcePtr(subject->clone());
129 subjInternal->setModel(*this);
130 d->addToHashes(subjInternal);
131 }
132
133 PropertyPtr predInternal = predicate;
134
135 if (!d->nodes.contains(predInternal->id())) {
136 predInternal = PropertyPtr(predicate->clone());
137 predInternal->setModel(*this);
138 d->addToHashes(predInternal);
139 }
140
141 NodePtr objInternal = object;
142
143 if (!d->nodes.contains(objInternal->id())) {
144 objInternal = NodePtr(object->clone());
145 objInternal->setModel(*this);
146 d->addToHashes(objInternal);
147 }
148
149 // TODO: avoid duplicated stmts with literal objects!
150
151 /* clang-format off */
152 QString key = QStringLiteral("%1-%2-%3").arg(QString::number(subjInternal->id()),
153 QString::number(predInternal->id()),
154 QString::number(objInternal->id()));
155 /* clang-format on */
156
157 StatementPtr stmt;
158
159 if (!d->statements.contains(key)) {
160 stmt = StatementPtr(new Statement(subjInternal, predInternal, objInternal));
161 d->addToHashes(stmt, key);
162 } else {
163 stmt = d->statements[key];
164 }
165
166 return stmt;
167}
168
169bool Model::isEmpty() const
170{
171 return d->statements.isEmpty();
172}
173
174bool Model::resourceHasProperty(const Resource *resource, PropertyPtr property) const
175{
176 return d->resourceHasProperty(resource, property);
177}
178
179bool Model::ModelPrivate::resourceHasProperty(const Resource *resource, PropertyPtr property) const
180{
181 // resource unknown
182 if (!resources.contains(resource->uri())) {
183 return false;
184 }
185
186 const QList<StatementPtr> &stmts = stmtsBySubject[resource->uri()];
187
188 return std::any_of(stmts.cbegin(), stmts.cend(), [&property](const StatementPtr &p) {
189 return *(p->predicate()) == *property;
190 });
191}
192
193StatementPtr Model::resourceProperty(const Resource *resource, PropertyPtr property) const
194{
195 return d->resourceProperty(resource, property);
196}
197
198StatementPtr Model::ModelPrivate::resourceProperty(const Resource *resource, PropertyPtr property) const
199{
200 const QList<StatementPtr> &stmts = stmtsBySubject[resource->uri()];
201 auto it = std::find_if(stmts.cbegin(), stmts.cend(), [&property](const StatementPtr &p) {
202 return *(p->predicate()) == *property;
203 });
204 return it != stmts.cend() ? *it : nullStatement;
205}
206
207QList<StatementPtr> Model::resourceProperties(const Resource *resource, PropertyPtr property) const
208{
209 return d->resourceProperties(resource, property);
210}
211
212QList<StatementPtr> Model::ModelPrivate::resourceProperties(const Resource *resource, PropertyPtr property) const
213{
215
216 const QList<StatementPtr> &stmts = stmtsBySubject[resource->uri()];
217 for (const auto &p : stmts) {
218 if (*(p->predicate()) == *property) {
219 res.append(p);
220 }
221 }
222
223 return res;
224}
225
227{
228 return d->statements.values();
229}
230
232{
233 QString info;
234 for (const StatementPtr &stmtPtr : std::as_const(d->statements)) {
235 info += QStringLiteral("<%1> <%2> ").arg(stmtPtr->subject()->uri(), stmtPtr->predicate()->uri());
236
237 if (stmtPtr->object()->isLiteral()) {
238 info += QStringLiteral("\"%1\"\n").arg(stmtPtr->asString());
239 } else {
240 info += QStringLiteral("<%1>\n").arg(stmtPtr->asResource()->uri());
241 }
242 }
243
244 return info;
245}
246
248{
250
251 for (const StatementPtr &stmtPtr : std::as_const(d->statements)) {
252 if (*(stmtPtr->predicate()) == *(RDFVocab::self()->type()) //
253 && stmtPtr->object()->isResource() && *(dynamic_cast<Resource *>(stmtPtr->object().data())) == *type) {
254 list.append(stmtPtr->subject());
255 }
256 }
257
258 return list;
259}
260
261NodePtr Model::nodeByID(uint id) const
262{
263 return d->nodeByID(id);
264}
265
266NodePtr Model::ModelPrivate::nodeByID(uint _id) const
267{
268 if (!nodes.contains(_id)) {
269 return nullLiteral;
270 } else {
271 return nodes.value(_id);
272 }
273}
274
276{
277 return d->resourceByID(id);
278}
279
280ResourcePtr Model::ModelPrivate::resourceByID(uint _id) const
281{
282 if (!nodes.contains(_id)) {
283 return nullResource;
284 } else {
285 NodePtr node = nodes.value(_id);
286 if (node->isResource()) {
287 return node.staticCast<Resource>();
288 } else {
289 return nullResource;
290 }
291 }
292}
293
295{
296 return d->propertyByID(id);
297}
298
299PropertyPtr Model::ModelPrivate::propertyByID(uint _id) const
300{
301 if (!nodes.contains(_id)) {
302 return nullProperty;
303 } else {
304 NodePtr node = nodes.value(_id);
305 if (node->isProperty()) {
306 return node.staticCast<Property>();
307 } else {
308 return nullProperty;
309 }
310 }
311}
312
313LiteralPtr Model::literalByID(uint id) const
314{
315 return d->literalByID(id);
316}
317
318LiteralPtr Model::ModelPrivate::literalByID(uint _id) const
319{
320 if (!nodes.contains(_id)) {
321 return nullLiteral;
322 } else {
323 NodePtr node = nodes.value(_id);
324 if (node->isLiteral()) {
325 return node.staticCast<Literal>();
326 } else {
327 return nullLiteral;
328 }
329 }
330}
331
332} // namespace RDF
333} // namespace Syndication
a node type representing simple string values.
Definition literal.h:29
An RDF model, a set of RDF statements.
Definition model.h:37
virtual ResourcePtr resourceByID(uint id) const
searches the model for a resource by ID.
Definition model.cpp:275
virtual StatementPtr addStatement(ResourcePtr subject, PropertyPtr predicate, NodePtr object)
adds a statement to the model.
Definition model.cpp:122
virtual void removeStatement(ResourcePtr subject, PropertyPtr predicate, NodePtr object)
removes a statement from the model.
Definition model.cpp:111
virtual ResourcePtr createResource(const QString &uri=QString())
creates a resource and associates it with this model.
Definition model.cpp:62
virtual QList< StatementPtr > statements() const
returns a list of the statements in this model.
Definition model.cpp:226
virtual bool isEmpty() const
returns whether this model is empty, i.e.
Definition model.cpp:169
virtual SequencePtr createSequence(const QString &uri=QString())
creates a sequence and associates it with this model.
Definition model.cpp:77
virtual PropertyPtr propertyByID(uint id) const
searches the model for a property by ID.
Definition model.cpp:294
Model()
default constructor, creates an empty model containing no statements
Definition model.cpp:17
bool operator==(const Model &other) const
Returns whether two models objects represent the same model (i.e.
Definition model.cpp:37
virtual PropertyPtr createProperty(const QString &uri)
creates a property and associates it with this model.
Definition model.cpp:42
virtual ~Model()
destructor
Definition model.cpp:27
virtual LiteralPtr literalByID(uint id) const
searches the model for a literal by ID.
Definition model.cpp:313
Model & operator=(const Model &other)
assigns another model.
Definition model.cpp:31
virtual NodePtr nodeByID(uint id) const
searches the model for a node by ID.
Definition model.cpp:261
virtual QString debugInfo() const
/** a debug string listing the contained statements for debugging purposes
Definition model.cpp:231
virtual LiteralPtr createLiteral(const QString &text)
creates a literal and associates it with this model.
Definition model.cpp:98
virtual QList< ResourcePtr > resourcesWithType(ResourcePtr type) const
returns all resources of a given type.
Definition model.cpp:247
a property is node type that represents properties of things, like "name" is a property of a person,...
Definition property.h:32
PropertyPtr type()
the rdf:type property (A rdf:type B means A is instance of B)
Definition rdfvocab.cpp:65
static RDFVocab * self()
returns the singleton instance
Definition rdfvocab.cpp:36
Resources are the entities in the RDF graph.
Definition resource.h:37
Sequence container, a sequence contains an ordered list of RDF nodes.
Definition sequence.h:32
An RDF statement, consisting of a triple (subject, predicate, object).
Definition statement.h:29
void append(QList< T > &&value)
const_iterator cbegin() const const
const_iterator cend() const const
QString arg(Args &&... args) const const
QString number(double n, char format, int precision)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:09:18 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.