26 namespace Syndication {
29 long Model::ModelPrivate::idCounter = 0;
31 Model::Model() : d(new ModelPrivate)
35 Model::Model(
const Model& other)
44 Model& Model::operator=(
const Model& other)
50 bool Model::operator==(
const Model& other)
const
52 return *d == *(other.d);
55 PropertyPtr Model::createProperty(
const QString& uri)
59 if (d->properties.contains(uri))
61 prop = d->properties[uri];
65 prop = PropertyPtr(
new Property(uri) );
66 prop->setModel(*
this);
69 if (d->resources.contains(uri))
71 prop->setId(d->resources[uri]->id());
80 ResourcePtr Model::createResource(
const QString& uri)
84 if (d->resources.contains(uri))
86 res = d->resources[uri];
90 res = ResourcePtr(
new Resource(uri) );
98 SequencePtr Model::createSequence(
const QString& uri)
102 if (d->sequences.contains(uri))
104 seq = d->sequences[uri];
108 seq = SequencePtr(
new Sequence(uri) );
109 seq->setModel(*
this);
112 if (d->resources.contains(uri))
114 seq->setId(d->resources[uri]->id());
123 LiteralPtr Model::createLiteral(
const QString& text)
125 LiteralPtr lit(
new Literal(text));
132 void Model::removeStatement(StatementPtr statement)
134 removeStatement(statement->subject(), statement->predicate(), statement->object());
137 void Model::removeStatement(ResourcePtr subject, PropertyPtr predicate, NodePtr
object)
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);
146 StatementPtr Model::addStatement(ResourcePtr subject, PropertyPtr predicate, NodePtr
object)
149 ResourcePtr subjInternal = subject;
151 if (!d->nodes.contains(subjInternal->id()))
153 subjInternal = ResourcePtr( subject->clone() );
154 subjInternal->setModel(*
this);
155 d->addToHashes(subjInternal);
158 PropertyPtr predInternal = predicate;
160 if (!d->nodes.contains(predInternal->id()))
162 predInternal = PropertyPtr( predicate->clone() );
163 predInternal->setModel(*
this);
164 d->addToHashes(predInternal);
167 NodePtr objInternal = object;
169 if (!d->nodes.contains(objInternal->id()))
171 objInternal = NodePtr( object->clone() );
172 objInternal->setModel(*
this);
173 d->addToHashes(objInternal);
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()));
185 if (!d->statements.contains(key))
187 stmt = StatementPtr(
new Statement(subjInternal, predInternal, objInternal) );
188 d->addToHashes(stmt, key);
192 stmt = d->statements[key];
198 bool Model::isEmpty()
const
200 return d->statements.isEmpty();
203 bool Model::resourceHasProperty(
const Resource* resource, PropertyPtr property)
const
205 return d->resourceHasProperty( resource, property );
208 bool Model::ModelPrivate::resourceHasProperty(
const Resource* resource, PropertyPtr property)
const
211 if (!resources.contains(resource->uri()))
214 QList<StatementPtr> stmts = stmtsBySubject[resource->uri()];
215 QList<StatementPtr>::ConstIterator it = stmts.constBegin();
216 QList<StatementPtr>::ConstIterator end = stmts.constEnd();
218 for ( ; it != end; ++it)
220 if (*((*it)->predicate()) == *property)
227 StatementPtr Model::resourceProperty(
const Resource* resource, PropertyPtr property)
const
229 return d->resourceProperty(resource, property);
232 StatementPtr Model::ModelPrivate::resourceProperty(
const Resource* resource, PropertyPtr property)
const
234 QList<StatementPtr> stmts = stmtsBySubject[resource->uri()];
235 QList<StatementPtr>::ConstIterator it = stmts.constBegin();
236 QList<StatementPtr>::ConstIterator end = stmts.constEnd();
238 for ( ; it != end; ++it)
240 if (*((*it)->predicate()) == *property)
244 return nullStatement;
247 QList<StatementPtr> Model::resourceProperties(
const Resource* resource, PropertyPtr property)
const
249 return d->resourceProperties( resource, property );
252 QList<StatementPtr> Model::ModelPrivate::resourceProperties(
const Resource* resource, PropertyPtr property)
const
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();
259 for ( ; it != end; ++it)
261 if (*((*it)->predicate()) == *property)
269 QList<StatementPtr> Model::statements()
const
271 return d->statements.values();
274 QString Model::debugInfo()
const
278 QList<StatementPtr> stmts = d->statements.values();
279 QList<StatementPtr>::ConstIterator it = stmts.constBegin();
280 QList<StatementPtr>::ConstIterator end = stmts.constEnd();
282 for ( ; it != end; ++it)
284 info += QString::fromLatin1(
"<%1> <%2> ").arg((*it)->subject()->uri()).arg((*it)->predicate()->uri());
286 if ((*it)->object()->isLiteral())
288 info += QString::fromLatin1(
"\"%1\"\n").arg((*it)->asString());
292 info += QString::fromLatin1(
"<%1>\n").arg((*it)->asResource()->uri());
300 QList<ResourcePtr> Model::resourcesWithType(ResourcePtr type)
const
302 QList<ResourcePtr> list;
304 QList<StatementPtr> stmts = d->statements.values();
305 QList<StatementPtr>::ConstIterator it = stmts.constBegin();
306 QList<StatementPtr>::ConstIterator end = stmts.constEnd();
308 for ( ; it != end; ++it)
310 if (*((*it)->predicate()) == *(
RDFVocab::self()->
type()) && *((*it)->object()) == *type )
311 list.append((*it)->subject());
317 NodePtr Model::nodeByID(uint
id)
const
319 return d->nodeByID(
id);
322 NodePtr Model::ModelPrivate::nodeByID(uint _id)
const
324 if (!nodes.contains(_id))
330 return nodes.value(_id);
334 ResourcePtr Model::resourceByID(uint
id)
const
336 return d->resourceByID(
id);
339 ResourcePtr Model::ModelPrivate::resourceByID(uint _id)
const
341 if (!nodes.contains(_id))
347 NodePtr node = nodes.value(_id);
348 if (node->isResource())
349 return boost::static_pointer_cast<Resource>(node);
355 PropertyPtr Model::propertyByID(uint
id)
const
357 return d->propertyByID(
id);
360 PropertyPtr Model::ModelPrivate::propertyByID(uint _id)
const
362 if (!nodes.contains(_id))
368 NodePtr node = nodes.value(_id);
369 if (node->isProperty())
370 return boost::static_pointer_cast<Property>(node);
376 LiteralPtr Model::literalByID(uint
id)
const
378 return d->literalByID(
id);
382 LiteralPtr Model::ModelPrivate::literalByID(uint _id)
const
384 if (!nodes.contains(_id))
390 NodePtr node = nodes.value(_id);
391 if (node->isLiteral())
392 return boost::static_pointer_cast<Literal>(node);
static RDFVocab * self()
returns the singleton instance
PropertyPtr type()
the rdf:type property (A rdf:type B means A is instance of B)