27 #include <QtCore/QList>
31 #include <Soprano/QueryResultIterator>
32 #include <Soprano/Model>
33 #include <Soprano/Vocabulary/NRL>
34 #include <Soprano/Vocabulary/RDFS>
35 #include <Soprano/Vocabulary/RDF>
36 #include <Soprano/Vocabulary/OWL>
39 #define D static_cast<Nepomuk2::Types::ClassPrivate*>( d.data() )
41 Nepomuk2::Types::ClassPrivate::ClassPrivate(
const QUrl& uri )
42 : EntityPrivate( uri ),
43 propertiesAvailable( uri.isValid() ? -1 : 0 )
48 bool Nepomuk2::Types::ClassPrivate::load()
54 if ( EntityPrivate::load() ) {
56 if ( parents.isEmpty() ) {
57 if ( uri != Soprano::Vocabulary::RDFS::Resource() ) {
58 parents += Soprano::Vocabulary::RDFS::Resource();
69 bool Nepomuk2::Types::ClassPrivate::loadAncestors()
75 if ( uri == Soprano::Vocabulary::RDFS::Resource() ) {
81 Soprano::QueryResultIterator it
82 = ResourceManager::instance()->mainModel()->executeQuery( QString(
"select distinct ?s where { "
83 "{ ?s a <%1> . } UNION { ?s a <%2> . } "
84 "OPTIONAL { graph ?g { ?s <%3> ?ss . } . "
85 "{ ?g a <%4> . } UNION { ?g a <%5> . } . } . "
86 "FILTER(!BOUND(?ss)) . }")
87 .arg( Soprano::Vocabulary::RDFS::Class().toString() )
88 .arg( Soprano::Vocabulary::OWL::Class().toString() )
89 .arg( Soprano::Vocabulary::RDFS::subClassOf().toString() )
90 .arg( Soprano::Vocabulary::NRL::Ontology().toString() )
91 .arg( Soprano::Vocabulary::NRL::KnowledgeBase().toString() ),
92 Soprano::Query::QueryLanguageSparql );
94 QUrl resUri = it.binding(
"s" ).uri();
95 if ( resUri != Soprano::Vocabulary::RDFS::Resource() ) {
96 children.append( resUri );
101 return EntityPrivate::loadAncestors();
108 if( value.isResource() && value.uri() == uri ) {
112 if( property == Soprano::Vocabulary::RDFS::subClassOf() ) {
113 parents.append( Class( value.uri() ) );
121 bool Nepomuk2::Types::ClassPrivate::addAncestorProperty(
const QUrl& ancestorResource,
const QUrl& property )
124 if ( property == Soprano::Vocabulary::RDFS::subClassOf() &&
125 ancestorResource != uri ) {
126 children.append( Class( ancestorResource ) );
134 void Nepomuk2::Types::ClassPrivate::initProperties()
136 QMutexLocker lock( &mutex );
138 if ( propertiesAvailable < 0 ) {
139 propertiesAvailable = loadProperties() ? 1 : 0;
144 bool Nepomuk2::Types::ClassPrivate::loadProperties()
147 Soprano::QueryResultIterator it
148 = ResourceManager::instance()->mainModel()->executeQuery( QString(
"select distinct ?p where { "
149 "{ ?p <%1> <%2> . } "
152 "OPTIONAL { ?p <%1> ?undefdom . } . "
154 "FILTER(!bound(?undefdom)) . } "
157 "OPTIONAL { ?p <%1> ?undefdom1 . } . "
159 "OPTIONAL { ?p1 <%1> ?undefdom2 . } . "
161 "FILTER(!bound(?undefdom1) && !bound(?undefdom2)) . } "
163 .arg( Soprano::Vocabulary::RDFS::domain().toString() )
164 .arg( QString::fromAscii( uri.toEncoded() ) )
165 .arg( Soprano::Vocabulary::RDFS::subPropertyOf().toString() ),
166 Soprano::Query::QueryLanguageSparql );
169 if( it.lastError() ) {
170 it = ResourceManager::instance()->mainModel()->executeQuery( QString(
"select ?p where { "
172 .arg( Soprano::Vocabulary::RDFS::domain().toString() )
173 .arg( QString::fromAscii( uri.toEncoded() ) ),
174 Soprano::Query::QueryLanguageSparql );
177 while ( it.next() ) {
178 domainOf.append(
Property( it.binding(
"p" ).uri() ) );
183 it = ResourceManager::instance()->mainModel()->executeQuery( QString(
"select ?p where { "
185 .arg( Soprano::Vocabulary::RDFS::range().toString() )
186 .arg( QString::fromAscii( uri.toEncoded() ) ),
187 Soprano::Query::QueryLanguageSparql );
188 while ( it.next() ) {
189 rangeOf.append(
Property( it.binding(
"p" ).uri() ) );
192 return !it.lastError();
196 void Nepomuk2::Types::ClassPrivate::reset(
bool recursive )
200 QMutexLocker lock( &mutex );
202 if ( propertiesAvailable != -1 ) {
214 propertiesAvailable = -1;
217 if ( available != -1 ) {
219 foreach( Class c, parents ) {
227 if ( ancestorsAvailable != -1 ) {
229 foreach( Class c, children ) {
234 ancestorsAvailable = -1;
237 EntityPrivate::reset( recursive );
241 QSet<Nepomuk2::Types::Class> Nepomuk2::Types::ClassPrivate::findParentClasses( ClassPrivate* requestingClass )
243 QSet<Class> allParents;
245 for ( QList<Class>::iterator it = parents.begin(); it != parents.end(); ++it ) {
246 ClassPrivate* p =
static_cast<Nepomuk2::Types::ClassPrivate*
>( it->d.data() );
247 if ( p != requestingClass ) {
249 allParents += p->findParentClasses( requestingClass );
258 QSet<Nepomuk2::Types::Class> Nepomuk2::Types::ClassPrivate::findSubClasses( ClassPrivate* requestingClass )
260 QSet<Class> allChildren;
262 for ( QList<Class>::iterator it = children.begin(); it != children.end(); ++it ) {
263 ClassPrivate* p =
static_cast<Nepomuk2::Types::ClassPrivate*
>( it->d.data() );
264 if ( p != requestingClass ) {
266 allChildren += p->findSubClasses( requestingClass );
315 return QList<Nepomuk2::Types::Property>();
322 return const_cast<Class*
>(
this)->rangeOf();
333 return QList<Nepomuk2::Types::Property>();
340 return const_cast<Class*
>(
this)->domainOf();
348 for ( QList<Property>::const_iterator it =
D->domainOf.constBegin();
349 it !=
D->domainOf.constEnd(); ++it ) {
351 if ( p.
name() == name ) {
363 return const_cast<Class*
>(
this)->findPropertyByName(name);
371 for ( QList<Property>::iterator it =
D->domainOf.begin();
372 it !=
D->domainOf.end(); ++it ) {
374 if ( p.
label( language ) == label ) {
386 return const_cast<Class*
>(
this)->findPropertyByLabel( label, language );
397 return QList<Nepomuk2::Types::Class>();
404 return const_cast<Class*
>(
this)->parentClasses();
415 return QList<Nepomuk2::Types::Class>();
422 return const_cast<Class*
>(
this)->subClasses();
430 return D->findParentClasses(
D ).toList();
433 return QList<Nepomuk2::Types::Class>();
440 return const_cast<Class*
>(
this)->allParentClasses();
448 return D->findSubClasses(
D ).toList();
451 return QList<Nepomuk2::Types::Class>();
458 return const_cast<Class*
>(
this)->allSubClasses();
467 if (
D->children.contains( other ) ) {
471 for ( QList<Nepomuk2::Types::Class>::iterator it =
D->children.begin();
472 it !=
D->children.end(); ++it ) {
473 if ( ( *it ).isParentOf( other ) ) {
486 return const_cast<Class*
>(
this)->isParentOf( other );
495 if (
D->parents.contains( other ) ) {
499 for ( QList<Nepomuk2::Types::Class>::iterator it =
D->parents.begin();
500 it !=
D->parents.end(); ++it ) {
501 if ( ( *it ).isSubClassOf( other ) ) {
514 return const_cast<Class*
>(
this)->isSubClassOf( other );
bool isParentOf(const Class &other)
Check if a class inherits this class.
QExplicitlySharedDataPointer< ClassPrivate > getClass(const QUrl &uri)
KJob * addProperty(const QList< QUrl > &resources, const QUrl &property, const QVariantList &values, const KComponentData &component=KGlobal::mainComponent())
Add one or more property values to one or more resources.
Property findPropertyByLabel(const QString &label, const QString &language=QString())
Search for a property in the class by its label.
QList< Property > rangeOf()
A Property has a certain range which is a Class or a Literal.
QList< Class > allSubClasses()
Recursively determines all sub classes of this class, not only the direct ones.
Class & operator=(const Class &)
Copy operator.
QList< Class > allParentClasses()
Recursively determines all parent classes of this class, not only the direct ones.
Abstract base class for Class and Property;.
QList< Property > domainOf()
A Property has a certain domain which is a Class.
A Class is a resource of type rdf:Class.
A property is a resource of type rdf:Property which relates a domain with a range.
bool isSubClassOf(const Class &other)
Check if this class is derived from another class.
QList< Class > parentClasses()
Each class can have multiple parent classes.
QString name() const
The name of the resource.
Represents the property of a resource.
QString label(const QString &language=KGlobal::locale() ->language())
Retrieve the label of the entity (rdfs:label)
QExplicitlySharedDataPointer< EntityPrivate > d
static EntityManager * self()
Property findPropertyByName(const QString &name)
Search for a property in the class by its name.
Class()
Default constructor.
QList< Class > subClasses()