• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

Nepomuk-Core

  • sources
  • kde-4.12
  • kdelibs
  • nepomuk-core
  • libnepomukcore
  • types
class.cpp
Go to the documentation of this file.
1 /* This file is part of the Nepomuk-KDE libraries
2  Copyright (c) 2007 Sebastian Trueg <trueg@kde.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "class.h"
21 #include "class_p.h"
22 #include "ontology.h"
23 #include "resourcemanager.h"
24 #include "property.h"
25 #include "entitymanager.h"
26 
27 #include <QtCore/QList>
28 
29 #include <kdebug.h>
30 
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>
37 
38 #undef D
39 #define D static_cast<Nepomuk2::Types::ClassPrivate*>( d.data() )
40 
41 Nepomuk2::Types::ClassPrivate::ClassPrivate( const QUrl& uri )
42  : EntityPrivate( uri ),
43  propertiesAvailable( uri.isValid() ? -1 : 0 )
44 {
45 }
46 
47 
48 bool Nepomuk2::Types::ClassPrivate::load()
49 {
50  //
51  // Nearly all here can be done in a very clean way. There is only
52  // one special case: rdfs:Resource, the base class of them all
53  //
54  if ( EntityPrivate::load() ) {
55  // undefined super class means that we are derived from rdfs:Resource directly
56  if ( parents.isEmpty() ) {
57  if ( uri != Soprano::Vocabulary::RDFS::Resource() ) {
58  parents += Soprano::Vocabulary::RDFS::Resource();
59  }
60  }
61  return true;
62  }
63  else {
64  return false;
65  }
66 }
67 
68 
69 bool Nepomuk2::Types::ClassPrivate::loadAncestors()
70 {
71  //
72  // Nearly all here can be done in a very clean way. There is only
73  // one special case: rdfs:Resource, the base class of them all
74  //
75  if ( uri == Soprano::Vocabulary::RDFS::Resource() ) {
76  //
77  // All classes that do not explicetely state a superclass are
78  // derived from rdfs:Resource. This query selects those classes
79  // (might fail on redland though)
80  //
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 );
93  while ( it.next() ) {
94  QUrl resUri = it.binding( "s" ).uri();
95  if ( resUri != Soprano::Vocabulary::RDFS::Resource() ) {
96  children.append( resUri );
97  }
98  }
99  }
100 
101  return EntityPrivate::loadAncestors();
102 }
103 
104 
105 bool Nepomuk2::Types::ClassPrivate::addProperty( const QUrl& property, const Soprano::Node& value )
106 {
107  // we avoid subclassing loops (as created for crappy inferencing) by checking for our own uri
108  if( value.isResource() && value.uri() == uri ) {
109  return false;
110  }
111 
112  if( property == Soprano::Vocabulary::RDFS::subClassOf() ) {
113  parents.append( Class( value.uri() ) );
114  return true;
115  }
116 
117  return false;
118 }
119 
120 
121 bool Nepomuk2::Types::ClassPrivate::addAncestorProperty( const QUrl& ancestorResource, const QUrl& property )
122 {
123  // we avoid subclassing loops (as created for crappy inferencing) by checking for our own uri
124  if ( property == Soprano::Vocabulary::RDFS::subClassOf() &&
125  ancestorResource != uri ) {
126  children.append( Class( ancestorResource ) );
127  return true;
128  }
129 
130  return false;
131 }
132 
133 
134 void Nepomuk2::Types::ClassPrivate::initProperties()
135 {
136  QMutexLocker lock( &mutex );
137 
138  if ( propertiesAvailable < 0 ) {
139  propertiesAvailable = loadProperties() ? 1 : 0;
140  }
141 }
142 
143 
144 bool Nepomuk2::Types::ClassPrivate::loadProperties()
145 {
146  // load domains with a hack to get at least a subset of properties that inherit their domain from parents
147  Soprano::QueryResultIterator it
148  = ResourceManager::instance()->mainModel()->executeQuery( QString("select distinct ?p where { "
149  "{ ?p <%1> <%2> . } "
150  "UNION "
151  "{ ?p <%3> ?p1 . "
152  "OPTIONAL { ?p <%1> ?undefdom . } . "
153  "?p1 <%1> <%2> . "
154  "FILTER(!bound(?undefdom)) . } "
155  "UNION "
156  "{ ?p <%3> ?p1 . "
157  "OPTIONAL { ?p <%1> ?undefdom1 . } . "
158  "?p1 <%3> ?p2 . "
159  "OPTIONAL { ?p1 <%1> ?undefdom2 . } . "
160  "?p2 <%1> <%2> . "
161  "FILTER(!bound(?undefdom1) && !bound(?undefdom2)) . } "
162  "}")
163  .arg( Soprano::Vocabulary::RDFS::domain().toString() )
164  .arg( QString::fromAscii( uri.toEncoded() ) )
165  .arg( Soprano::Vocabulary::RDFS::subPropertyOf().toString() ),
166  Soprano::Query::QueryLanguageSparql );
167 
168  // redland cannot handle UNION queries! So fallback to the "old" query
169  if( it.lastError() ) {
170  it = ResourceManager::instance()->mainModel()->executeQuery( QString("select ?p where { "
171  "?p <%1> <%2> . }")
172  .arg( Soprano::Vocabulary::RDFS::domain().toString() )
173  .arg( QString::fromAscii( uri.toEncoded() ) ),
174  Soprano::Query::QueryLanguageSparql );
175  }
176 
177  while ( it.next() ) {
178  domainOf.append( Property( it.binding( "p" ).uri() ) );
179  }
180 
181 
182  // load ranges
183  it = ResourceManager::instance()->mainModel()->executeQuery( QString("select ?p where { "
184  "?p <%1> <%2> . }")
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() ) );
190  }
191 
192  return !it.lastError();
193 }
194 
195 
196 void Nepomuk2::Types::ClassPrivate::reset( bool recursive )
197 {
198  kDebug();
199 
200  QMutexLocker lock( &mutex );
201 
202  if ( propertiesAvailable != -1 ) {
203  if ( recursive ) {
204  foreach( Property p, domainOf ) {
205  p.reset( true );
206  }
207  foreach( Property p, rangeOf ) {
208  p.reset( true );
209  }
210  }
211 
212  domainOf.clear();
213  rangeOf.clear();
214  propertiesAvailable = -1;
215  }
216 
217  if ( available != -1 ) {
218  if ( recursive ) {
219  foreach( Class c, parents ) {
220  c.reset( true );
221  }
222  }
223  parents.clear();
224  available = -1;
225  }
226 
227  if ( ancestorsAvailable != -1 ) {
228  if ( recursive ) {
229  foreach( Class c, children ) {
230  c.reset( true );
231  }
232  }
233  children.clear();
234  ancestorsAvailable = -1;
235  }
236 
237  EntityPrivate::reset( recursive );
238 }
239 
240 
241 QSet<Nepomuk2::Types::Class> Nepomuk2::Types::ClassPrivate::findParentClasses( ClassPrivate* requestingClass )
242 {
243  QSet<Class> allParents;
244 
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 ) {
248  p->init();
249  allParents += p->findParentClasses( requestingClass );
250  allParents += *it;
251  }
252  }
253 
254  return allParents;
255 }
256 
257 
258 QSet<Nepomuk2::Types::Class> Nepomuk2::Types::ClassPrivate::findSubClasses( ClassPrivate* requestingClass )
259 {
260  QSet<Class> allChildren;
261 
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 ) {
265  p->initAncestors();
266  allChildren += p->findSubClasses( requestingClass );
267  allChildren += *it;
268  }
269  }
270 
271  return allChildren;
272 }
273 
274 
275 
276 Nepomuk2::Types::Class::Class()
277  : Entity()
278 {
279  d = 0;
280 }
281 
282 
283 Nepomuk2::Types::Class::Class( const QUrl& uri )
284  : Entity()
285 {
286  d = EntityManager::self()->getClass( uri );
287 }
288 
289 
290 Nepomuk2::Types::Class::Class( const Class& other )
291  : Entity( other )
292 {
293 }
294 
295 
296 Nepomuk2::Types::Class::~Class()
297 {
298 }
299 
300 
301 Nepomuk2::Types::Class& Nepomuk2::Types::Class::operator=( const Class& other )
302 {
303  d = other.d;
304  return *this;
305 }
306 
307 
308 QList<Nepomuk2::Types::Property> Nepomuk2::Types::Class::rangeOf()
309 {
310  if ( d ) {
311  D->initProperties();
312  return D->rangeOf;
313  }
314  else {
315  return QList<Nepomuk2::Types::Property>();
316  }
317 }
318 
319 
320 QList<Nepomuk2::Types::Property> Nepomuk2::Types::Class::rangeOf() const
321 {
322  return const_cast<Class*>(this)->rangeOf();
323 }
324 
325 
326 QList<Nepomuk2::Types::Property> Nepomuk2::Types::Class::domainOf()
327 {
328  if ( d ) {
329  D->initProperties();
330  return D->domainOf;
331  }
332  else {
333  return QList<Nepomuk2::Types::Property>();
334  }
335 }
336 
337 
338 QList<Nepomuk2::Types::Property> Nepomuk2::Types::Class::domainOf() const
339 {
340  return const_cast<Class*>(this)->domainOf();
341 }
342 
343 
344 Nepomuk2::Types::Property Nepomuk2::Types::Class::findPropertyByName( const QString& name )
345 {
346  if ( d ) {
347  D->initProperties();
348  for ( QList<Property>::const_iterator it = D->domainOf.constBegin();
349  it != D->domainOf.constEnd(); ++it ) {
350  const Property& p = *it;
351  if ( p.name() == name ) {
352  return p;
353  }
354  }
355  }
356 
357  return Property();
358 }
359 
360 
361 Nepomuk2::Types::Property Nepomuk2::Types::Class::findPropertyByName( const QString& name ) const
362 {
363  return const_cast<Class*>(this)->findPropertyByName(name);
364 }
365 
366 
367 Nepomuk2::Types::Property Nepomuk2::Types::Class::findPropertyByLabel( const QString& label, const QString& language )
368 {
369  if ( d ) {
370  D->initProperties();
371  for ( QList<Property>::iterator it = D->domainOf.begin();
372  it != D->domainOf.end(); ++it ) {
373  Property& p = *it;
374  if ( p.label( language ) == label ) {
375  return p;
376  }
377  }
378  }
379 
380  return Property();
381 }
382 
383 
384 Nepomuk2::Types::Property Nepomuk2::Types::Class::findPropertyByLabel( const QString& label, const QString& language ) const
385 {
386  return const_cast<Class*>(this)->findPropertyByLabel( label, language );
387 }
388 
389 
390 QList<Nepomuk2::Types::Class> Nepomuk2::Types::Class::parentClasses()
391 {
392  if ( d ) {
393  D->init();
394  return D->parents;
395  }
396  else {
397  return QList<Nepomuk2::Types::Class>();
398  }
399 }
400 
401 
402 QList<Nepomuk2::Types::Class> Nepomuk2::Types::Class::parentClasses() const
403 {
404  return const_cast<Class*>(this)->parentClasses();
405 }
406 
407 
408 QList<Nepomuk2::Types::Class> Nepomuk2::Types::Class::subClasses()
409 {
410  if ( d ) {
411  D->initAncestors();
412  return D->children;
413  }
414  else {
415  return QList<Nepomuk2::Types::Class>();
416  }
417 }
418 
419 
420 QList<Nepomuk2::Types::Class> Nepomuk2::Types::Class::subClasses() const
421 {
422  return const_cast<Class*>(this)->subClasses();
423 }
424 
425 
426 QList<Nepomuk2::Types::Class> Nepomuk2::Types::Class::allParentClasses()
427 {
428  if ( d ) {
429  D->init();
430  return D->findParentClasses( D ).toList();
431  }
432  else {
433  return QList<Nepomuk2::Types::Class>();
434  }
435 }
436 
437 
438 QList<Nepomuk2::Types::Class> Nepomuk2::Types::Class::allParentClasses() const
439 {
440  return const_cast<Class*>(this)->allParentClasses();
441 }
442 
443 
444 QList<Nepomuk2::Types::Class> Nepomuk2::Types::Class::allSubClasses()
445 {
446  if ( d ) {
447  D->initAncestors();
448  return D->findSubClasses( D ).toList();
449  }
450  else {
451  return QList<Nepomuk2::Types::Class>();
452  }
453 }
454 
455 
456 QList<Nepomuk2::Types::Class> Nepomuk2::Types::Class::allSubClasses() const
457 {
458  return const_cast<Class*>(this)->allSubClasses();
459 }
460 
461 
462 bool Nepomuk2::Types::Class::isParentOf( const Class& other )
463 {
464  if ( d ) {
465  D->initAncestors();
466 
467  if ( D->children.contains( other ) ) {
468  return true;
469  }
470  else {
471  for ( QList<Nepomuk2::Types::Class>::iterator it = D->children.begin();
472  it != D->children.end(); ++it ) {
473  if ( ( *it ).isParentOf( other ) ) {
474  return true;
475  }
476  }
477  }
478  }
479 
480  return false;
481 }
482 
483 
484 bool Nepomuk2::Types::Class::isParentOf( const Class& other ) const
485 {
486  return const_cast<Class*>(this)->isParentOf( other );
487 }
488 
489 
490 bool Nepomuk2::Types::Class::isSubClassOf( const Class& other )
491 {
492  if ( d ) {
493  D->init();
494 
495  if ( D->parents.contains( other ) ) {
496  return true;
497  }
498  else {
499  for ( QList<Nepomuk2::Types::Class>::iterator it = D->parents.begin();
500  it != D->parents.end(); ++it ) {
501  if ( ( *it ).isSubClassOf( other ) ) {
502  return true;
503  }
504  }
505  }
506  }
507 
508  return false;
509 }
510 
511 
512 bool Nepomuk2::Types::Class::isSubClassOf( const Class& other ) const
513 {
514  return const_cast<Class*>(this)->isSubClassOf( other );
515 }
Nepomuk2::Types::Class::isParentOf
bool isParentOf(const Class &other)
Check if a class inherits this class.
Definition: class.cpp:462
class.h
Nepomuk2::Types::EntityManager::getClass
QExplicitlySharedDataPointer< ClassPrivate > getClass(const QUrl &uri)
Definition: entitymanager.cpp:36
ontology.h
Nepomuk2::Types::Class::~Class
~Class()
Destructor.
Definition: class.cpp:296
Nepomuk2::addProperty
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.
Definition: datamanagement.cpp:36
Nepomuk2::Types::Class::findPropertyByLabel
Property findPropertyByLabel(const QString &label, const QString &language=QString())
Search for a property in the class by its label.
Definition: class.cpp:367
Nepomuk2::Types::Class::rangeOf
QList< Property > rangeOf()
A Property has a certain range which is a Class or a Literal.
Definition: class.cpp:308
Nepomuk2::Types::Class::allSubClasses
QList< Class > allSubClasses()
Recursively determines all sub classes of this class, not only the direct ones.
Definition: class.cpp:444
Nepomuk2::Types::Class::operator=
Class & operator=(const Class &)
Copy operator.
Definition: class.cpp:301
Nepomuk2::Types::Class::allParentClasses
QList< Class > allParentClasses()
Recursively determines all parent classes of this class, not only the direct ones.
Definition: class.cpp:426
Nepomuk2::Types::Entity
Abstract base class for Class and Property;.
Definition: entity.h:54
Nepomuk2::Types::Class::domainOf
QList< Property > domainOf()
A Property has a certain domain which is a Class.
Definition: class.cpp:326
Nepomuk2::Types::Class
A Class is a resource of type rdf:Class.
Definition: class.h:49
Nepomuk2::Types::Property
A property is a resource of type rdf:Property which relates a domain with a range.
Definition: libnepomukcore/types/property.h:52
Nepomuk2::Types::Class::isSubClassOf
bool isSubClassOf(const Class &other)
Check if this class is derived from another class.
Definition: class.cpp:490
resourcemanager.h
entitymanager.h
Nepomuk2::Types::Class::parentClasses
QList< Class > parentClasses()
Each class can have multiple parent classes.
Definition: class.cpp:390
Nepomuk2::Types::Entity::name
QString name() const
The name of the resource.
Definition: entity.cpp:181
Property
Represents the property of a resource.
Definition: rcgen/property.h:29
Nepomuk2::Types::Entity::label
QString label(const QString &language=KGlobal::locale() ->language())
Retrieve the label of the entity (rdfs:label)
Definition: entity.cpp:187
Nepomuk2::Types::Entity::d
QExplicitlySharedDataPointer< EntityPrivate > d
Definition: entity.h:250
property.h
Nepomuk2::Types::EntityManager::self
static EntityManager * self()
Definition: entitymanager.cpp:84
Nepomuk2::Types::Class::findPropertyByName
Property findPropertyByName(const QString &name)
Search for a property in the class by its name.
Definition: class.cpp:344
Nepomuk2::Types::Class::Class
Class()
Default constructor.
Definition: class.cpp:276
Nepomuk2::Types::Class::subClasses
QList< Class > subClasses()
Definition: class.cpp:408
D
#define D
Definition: class.cpp:39
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:08 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Nepomuk-Core

Skip menu "Nepomuk-Core"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal