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 | #ifndef SYNDICATION_RDF_MODEL_H |
9 | #define SYNDICATION_RDF_MODEL_H |
10 | |
11 | #include "../syndication_export.h" |
12 | |
13 | #include "document.h" |
14 | #include "literal.h" |
15 | #include "node.h" |
16 | #include "property.h" |
17 | #include "resource.h" |
18 | #include "sequence.h" |
19 | #include "statement.h" |
20 | |
21 | #include <QString> |
22 | |
23 | template<class T> |
24 | class QList; |
25 | |
26 | namespace Syndication |
27 | { |
28 | namespace RDF |
29 | { |
30 | /** |
31 | * An RDF model, a set of RDF statements. |
32 | * Model objects are implicitly shared. |
33 | * |
34 | * @author Frank Osterfeld |
35 | */ |
36 | class Model |
37 | { |
38 | friend class ::Syndication::RDF::Document; |
39 | friend class ::Syndication::RDF::Document::Private; |
40 | friend class ::Syndication::RDF::Resource; |
41 | friend class ::Syndication::RDF::Resource::ResourcePrivate; |
42 | friend class ::Syndication::RDF::Statement; |
43 | friend class ::Syndication::RDF::Statement::StatementPrivate; |
44 | |
45 | public: |
46 | /** |
47 | * default constructor, creates an empty model |
48 | * containing no statements |
49 | */ |
50 | Model(); |
51 | |
52 | /** |
53 | * constructs a model from another. |
54 | * Both models will share the same set of statements, |
55 | * so adding/removing statements from one model also |
56 | * modifies the other! |
57 | * |
58 | * @param other another model |
59 | */ |
60 | Model(const Model &other); |
61 | |
62 | /** |
63 | * destructor |
64 | */ |
65 | virtual ~Model(); |
66 | |
67 | /** |
68 | * assigns another model. Both models will share the same |
69 | * set of statements, so adding/removing statements from |
70 | * one model also modifies the other! |
71 | * |
72 | * @param other another model |
73 | */ |
74 | Model &operator=(const Model &other); |
75 | |
76 | /** |
77 | * Returns whether two models objects represent the same model |
78 | * (i.e. share the same underlying statement set). Currently this |
79 | * method does _not_ compare the statement list. |
80 | * Two independently created models containing the same statements |
81 | * are not equal! |
82 | * |
83 | * @param other the model to compare to |
84 | */ |
85 | bool operator==(const Model &other) const; |
86 | |
87 | /** |
88 | * creates a resource and associates it with this model. If the model |
89 | * already contains a resource with the given URI, the existing instance |
90 | * is returned. |
91 | * |
92 | * @param uri the URI of the resource. If a null string, a blank node |
93 | * is created. |
94 | * @return a shared pointer to the requested resource |
95 | */ |
96 | virtual ResourcePtr createResource(const QString &uri = QString()); |
97 | |
98 | /** |
99 | * creates a property and associates it with this model. If the model |
100 | * already contains a property with the given URI, the existing instance |
101 | * is returned. |
102 | * |
103 | * @param uri the URI of the property. This must be non-empty, otherwise |
104 | * null property is returned |
105 | * @return a shared pointer to the requested property |
106 | */ |
107 | virtual PropertyPtr createProperty(const QString &uri); |
108 | |
109 | /** |
110 | * creates a sequence and associates it with this model. If the model |
111 | * already contains a sequence with the given URI, the existing |
112 | * instance is returned. |
113 | * |
114 | * @param uri the URI of the sequence, or a null string for an |
115 | * anonymous instance |
116 | * @return a shared pointer to the requested sequence |
117 | */ |
118 | virtual SequencePtr createSequence(const QString &uri = QString()); |
119 | |
120 | /** |
121 | * creates a literal and associates it with this model. |
122 | * |
123 | * @param text the literal text |
124 | * @return a shared pointer to the requested literal |
125 | */ |
126 | virtual LiteralPtr createLiteral(const QString &text); |
127 | |
128 | /** |
129 | * adds a statement to the model. |
130 | * |
131 | * @param subject |
132 | * @param predicate |
133 | * @param object |
134 | * @return a shared pointer to a statement associated with this |
135 | * model, with the given @c subject, @c predicate and @c object |
136 | */ |
137 | virtual StatementPtr addStatement(ResourcePtr subject, PropertyPtr predicate, NodePtr object); |
138 | |
139 | /** |
140 | * removes a statement from the model. |
141 | * |
142 | * @param subject subject of the statement |
143 | * @param predicate predicate of the statement |
144 | * @param object object of the statement |
145 | */ |
146 | virtual void removeStatement(ResourcePtr subject, PropertyPtr predicate, NodePtr object); |
147 | |
148 | /** |
149 | * removes a statement from the model. |
150 | * |
151 | * @param statement the statement to remove |
152 | */ |
153 | virtual void removeStatement(StatementPtr statement); |
154 | |
155 | /** |
156 | * returns whether this model is empty, i.e. contains no statements. |
157 | */ |
158 | virtual bool isEmpty() const; |
159 | |
160 | /** |
161 | * returns all resources of a given type. |
162 | * subClassOf semantics are ignored. |
163 | * |
164 | * @param type a resource representing an RDFS class |
165 | */ |
166 | virtual QList<ResourcePtr> resourcesWithType(ResourcePtr type) const; |
167 | |
168 | /** |
169 | * returns a list of the statements in this model. |
170 | * |
171 | */ |
172 | virtual QList<StatementPtr> statements() const; |
173 | |
174 | /** |
175 | * searches the model for a node by ID. |
176 | * |
177 | * @param id the ID to search for |
178 | * @return the node with the given ID, or a null node (which is of type |
179 | * Literal) if the model doesn't contain the node with this ID |
180 | */ |
181 | virtual NodePtr nodeByID(uint id) const; |
182 | |
183 | /** |
184 | * searches the model for a resource by ID. |
185 | * |
186 | * @param id the ID to search for |
187 | * @return the resource with the given ID, or a null resource if the |
188 | * model doesn't contain a resource with this ID |
189 | */ |
190 | virtual ResourcePtr resourceByID(uint id) const; |
191 | |
192 | /** |
193 | * searches the model for a property by ID. |
194 | * |
195 | * @param id the ID to search for |
196 | * @return the property with the given ID, or a null property if the |
197 | * model doesn't contain a property with this ID |
198 | */ |
199 | virtual PropertyPtr propertyByID(uint id) const; |
200 | |
201 | /** |
202 | * searches the model for a literal by ID. |
203 | * |
204 | * @param id the ID to search for |
205 | * @return the literal with the given ID, or a null literal if the |
206 | * model doesn't contain a literal with this ID |
207 | */ |
208 | virtual LiteralPtr literalByID(uint id) const; |
209 | //@cond PRIVATE |
210 | /** |
211 | * @internal |
212 | * used by Resource::hasProperty() |
213 | */ |
214 | virtual bool resourceHasProperty(const Resource *resource, PropertyPtr property) const; |
215 | |
216 | /** |
217 | * @internal |
218 | * used by Resource::property() |
219 | */ |
220 | virtual StatementPtr resourceProperty(const Resource *resource, PropertyPtr property) const; |
221 | |
222 | /** |
223 | * @internal |
224 | * used by Resource::properties() |
225 | */ |
226 | virtual QList<StatementPtr> resourceProperties(const Resource *resource, PropertyPtr property) const; |
227 | |
228 | //@endcond |
229 | /** |
230 | * a debug string listing the contained statements for |
231 | * debugging purposes |
232 | * |
233 | * @return debug string |
234 | */ |
235 | virtual QString debugInfo() const; |
236 | |
237 | private: |
238 | class ModelPrivate; |
239 | QSharedPointer<ModelPrivate> d; |
240 | }; |
241 | |
242 | } // namespace RDF |
243 | } // namespace Syndication |
244 | |
245 | #endif // SYNDICATION_RDF_MODEL_H |
246 | |