syndication/rdf
model_p.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef SYNDICATION_RDF_MODEL_P_H
00023 #define SYNDICATION_RDF_MODEL_P_H
00024
00025 #include "model.h"
00026 #include "literal.h"
00027 #include "nodevisitor.h"
00028 #include "property.h"
00029 #include "rdfvocab.h"
00030 #include "resource.h"
00031 #include "sequence.h"
00032 #include "statement.h"
00033
00034 #include <QtCore/QHash>
00035 #include <QtCore/QList>
00036 #include <QtCore/QString>
00037
00038 #include <boost/enable_shared_from_this.hpp>
00039
00040 namespace Syndication {
00041 namespace RDF {
00042
00043 class Model::ModelPrivate : public boost::enable_shared_from_this<Model::ModelPrivate>
00044 {
00045 public:
00046 long id;
00047 static long idCounter;
00048 LiteralPtr nullLiteral;
00049 PropertyPtr nullProperty;
00050 ResourcePtr nullResource;
00051 StatementPtr nullStatement;
00052 QHash<QString, StatementPtr> statements;
00053 QHash<QString, QList<StatementPtr> > stmtsBySubject;
00054
00055 QHash<int, NodePtr> nodes;
00056 QHash<QString, ResourcePtr> resources;
00057 QHash<QString, PropertyPtr> properties;
00058 QHash<QString, SequencePtr> sequences;
00059 bool initialized;
00060
00061 class AddToHashesVisitor;
00062
00063
00064 ModelPrivate() : id(idCounter++)
00065 {
00066 addToHashesVisitor = new AddToHashesVisitor(this);
00067 initialized = false;
00068 }
00069
00070 ~ModelPrivate()
00071 {
00072 delete addToHashesVisitor;
00073 }
00074
00075 bool operator==(const ModelPrivate& other) const
00076 {
00077 return id == other.id;
00078 }
00079
00080 class AddToHashesVisitor : public NodeVisitor
00081 {
00082 public:
00083
00084 AddToHashesVisitor(ModelPrivate* parent) : p(parent)
00085 {}
00086
00087 bool visitResource(ResourcePtr res)
00088 {
00089 visitNode(res);
00090 p->resources[res->uri()] = res;
00091 return true;
00092 }
00093
00094 bool visitSequence(SequencePtr seq)
00095 {
00096 visitResource(seq);
00097 p->sequences[seq->uri()] = seq;
00098 return true;
00099 }
00100
00101 bool visitProperty(PropertyPtr prop)
00102 {
00103 visitResource(prop);
00104 p->properties[prop->uri()] = prop;
00105 return true;
00106 }
00107
00108 bool visitNode(NodePtr node)
00109 {
00110 p->nodes[node->id()] = node;
00111 return true;
00112 }
00113
00114 ModelPrivate* p;
00115 };
00116
00117 AddToHashesVisitor* addToHashesVisitor;
00118
00119 bool resourceHasProperty(const Resource* resource,
00120 PropertyPtr property) const;
00121
00122 StatementPtr resourceProperty(const Resource* resource,
00123 PropertyPtr property) const;
00124
00125 QList<StatementPtr> resourceProperties(const Resource* resource,
00126 PropertyPtr property) const;
00127
00128 NodePtr nodeByID(uint id) const;
00129
00130 ResourcePtr resourceByID(uint id) const;
00131
00132 PropertyPtr propertyByID(uint id) const;
00133
00134 LiteralPtr literalByID(uint id) const;
00135
00136 void addToHashes(NodePtr node)
00137 {
00138 addToHashesVisitor->visit(node);
00139 }
00140
00141 void addToHashes(StatementPtr stmt, const QString& key)
00142 {
00143 statements[key] = stmt;
00144 stmtsBySubject[stmt->subject()->uri()].append(stmt);
00145 }
00146
00147 void removeFromHashes(const QString& key)
00148 {
00149 StatementPtr stmt = statements[key];
00150 if (stmt)
00151 stmtsBySubject[stmt->subject()->uri()].removeAll(stmt);
00152 statements.remove(key);
00153
00154 }
00155
00156 void init()
00157 {
00158 if (!initialized)
00159 {
00160 Model m;
00161 m.d = shared_from_this();
00162 nullLiteral = LiteralPtr( new Literal() );
00163 nullLiteral->setModel(m);
00164 nullProperty = PropertyPtr( new Property() );
00165 nullProperty->setModel(m);
00166 nullResource = ResourcePtr( new Resource() );
00167 nullResource->setModel(m);
00168 nullStatement = StatementPtr( new Statement() );
00169 initialized = true;
00170 }
00171 }
00172 };
00173
00174 }
00175 }
00176
00177 #endif // SYNDICATION_RDF_MODEL_P_H