• 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
  • rcgen
ontologyparser.cpp
Go to the documentation of this file.
1 /*
2  *
3  * $Id: sourceheader 511311 2006-02-19 14:51:05Z trueg $
4  *
5  * This file is part of the Nepomuk KDE project.
6  * Copyright (C) 2006-2007 Sebastian Trueg <trueg@kde.org>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  * See the file "COPYING.LIB" for the exact licensing terms.
13  */
14 
15 #include "ontologyparser.h"
16 
17 #include "property.h"
18 #include "resourceclass.h"
19 
20 #include <soprano/soprano.h>
21 
22 #include <QtCore/QMap>
23 #include <QtCore/QStringList>
24 #include <QtCore/QFile>
25 #include <QtCore/QHash>
26 #include <QtCore/QDebug>
27 #include <QtCore/QDir>
28 
29 
30 extern bool quiet;
31 
32 
33 class OntologyParser::Private
34 {
35 public:
36  Private() {
37  // default parent class
38  resources.insert( Soprano::Vocabulary::RDFS::Resource(),
39  ResourceClass( Soprano::Vocabulary::RDFS::Resource() ) );
40 
41  // build xsd -> Qt type map
42  xmlSchemaTypes.insert( Soprano::Vocabulary::XMLSchema::integer(), "qint64" );
43  xmlSchemaTypes.insert( Soprano::Vocabulary::XMLSchema::negativeInteger(), "qint64" );
44  xmlSchemaTypes.insert( Soprano::Vocabulary::XMLSchema::nonNegativeInteger(), "quint64" );
45  xmlSchemaTypes.insert( Soprano::Vocabulary::XMLSchema::xsdLong(), "qint64" );
46  xmlSchemaTypes.insert( Soprano::Vocabulary::XMLSchema::unsignedLong(), "quint64" );
47  xmlSchemaTypes.insert( Soprano::Vocabulary::XMLSchema::xsdInt(), "qint32" );
48  xmlSchemaTypes.insert( Soprano::Vocabulary::XMLSchema::unsignedInt(), "quint32" );
49  xmlSchemaTypes.insert( Soprano::Vocabulary::XMLSchema::xsdShort(), "qint16" );
50  xmlSchemaTypes.insert( Soprano::Vocabulary::XMLSchema::unsignedShort(), "quint16" );
51  xmlSchemaTypes.insert( Soprano::Vocabulary::XMLSchema::xsdFloat(), "double" );
52  xmlSchemaTypes.insert( Soprano::Vocabulary::XMLSchema::xsdDouble(), "double" );
53  xmlSchemaTypes.insert( Soprano::Vocabulary::XMLSchema::boolean(), "bool" );
54  xmlSchemaTypes.insert( Soprano::Vocabulary::XMLSchema::date(), "QDate" );
55  xmlSchemaTypes.insert( Soprano::Vocabulary::XMLSchema::time(), "QTime" );
56  xmlSchemaTypes.insert( Soprano::Vocabulary::XMLSchema::dateTime(), "QDateTime" );
57  xmlSchemaTypes.insert( Soprano::Vocabulary::XMLSchema::string(), "QString" );
58  }
59 
60  // the classes to be generated as set on the command line
61  // if empty, all classes are generated
62  QStringList classesToGenerate;
63 
64  // the optional visibility string. If not-empty generated classes will be exported via
65  // <visibility>_EXPORT by including the header <visibility>_export.h.
66  QString visibility;
67 
68  QMap<QUrl, ResourceClass> resources;
69  QMap<QUrl, Property> properties;
70  QMap<QUrl, QString> comments;
71  const Soprano::Parser* rdfParser;
72 
73  QHash<QUrl, QString> xmlSchemaTypes;
74 
75  ResourceClass* getClass( const QUrl& uri ) {
76  ResourceClass& r = resources[uri];
77  r.setUri( uri );
78  if ( !r.parentClass(false) ) {
79  r.setParentResource( &resources[Soprano::Vocabulary::RDFS::Resource()] );
80  }
81  return &r;
82  }
83 
84  Property* getProperty( const QUrl& uri ) {
85  Property& p = properties[uri];
86  p.setUri( uri );
87  return &p;
88  }
89 };
90 
91 
92 OntologyParser::OntologyParser()
93 {
94  d = new Private;
95  d->rdfParser = 0;
96 }
97 
98 
99 OntologyParser::~OntologyParser()
100 {
101  delete d;
102 }
103 
104 
105 bool OntologyParser::parse( const QString& filename, const QString& serializationMimeType )
106 {
107  Soprano::RdfSerialization serialization = Soprano::SerializationUnknown;
108  if( !serializationMimeType.isEmpty() ) {
109  serialization = Soprano::mimeTypeToSerialization( serializationMimeType );
110  }
111  else if ( filename.endsWith( "trig" ) ) {
112  serialization = Soprano::SerializationTrig;
113  }
114  else {
115  serialization = Soprano::SerializationRdfXml;
116  }
117 
118  if ( !( d->rdfParser = Soprano::PluginManager::instance()->discoverParserForSerialization( serialization, serializationMimeType ) ) ) {
119  return false;
120  }
121 
122  if ( !quiet )
123  qDebug() << "(OntologyParser) Parsing " << filename << endl;
124 
125  Soprano::StatementIterator it = d->rdfParser->parseFile( filename,
126  QUrl("http://org.kde.nepomuk/dummybaseuri"),
127  serialization,
128  serializationMimeType );
129  bool success = true;
130 
131  while( it.next() ) {
132  const Soprano::Statement& s = *it;
133 
134  if( s.predicate().uri() == Soprano::Vocabulary::RDFS::subClassOf() ) {
135  ResourceClass* rc = d->getClass( s.subject().uri().toString() );
136  rc->setParentResource( d->getClass( s.object().uri().toString() ) );
137  rc->addParentResource( d->getClass( s.object().uri().toString() ) );
138  rc->setGenerateClass( true );
139  }
140  else if( s.predicate().uri() == Soprano::Vocabulary::RDF::type() ) {
141  if( s.object().uri().toString().endsWith( "#Class" ) )
142  d->getClass( s.subject().uri().toString() )->setGenerateClass( true );
143  }
144  else if( s.predicate().uri() == Soprano::Vocabulary::RDFS::domain() ) {
145  ResourceClass* rc = d->getClass( s.object().uri().toString() );
146  Property* p = d->getProperty( s.subject().uri().toString() );
147  p->setDomain( rc );
148  if ( !rc->allProperties().contains( p ) )
149  rc->addProperty( p );
150  rc->setGenerateClass( true );
151  }
152  else if( s.predicate().uri() == Soprano::Vocabulary::RDFS::range() ) {
153  Property* prop = d->getProperty(s.subject().uri().toString());
154  QUrl type = s.object().uri();
155  if( type.toString().contains( "XMLSchema" ) ) {
156  prop->setLiteralRange( d->xmlSchemaTypes[type] );
157  }
158  else if( type == Soprano::Vocabulary::RDFS::Literal() ) {
159  prop->setLiteralRange( "QString" );
160  }
161  else {
162  prop->setRange( d->getClass(s.object().uri()) );
163  }
164  }
165  else if( s.predicate().uri() == Soprano::Vocabulary::NRL::maxCardinality() ||
166  s.predicate().uri() == Soprano::Vocabulary::NRL::cardinality() ) {
167  d->getProperty(s.subject().uri())->setIsList( s.object().literal().toInt() > 1 );
168  }
169  else if( s.predicate().uri() == Soprano::Vocabulary::RDFS::comment() ) {
170  d->comments[s.subject().uri()] = s.object().literal().toString();
171  }
172  else if ( s.predicate().uri() == Soprano::Vocabulary::NRL::inverseProperty() ) {
173  d->getProperty(s.subject().uri())->setInverseProperty( d->getProperty(s.object().uri()) );
174  d->getProperty(s.object().uri())->setInverseProperty( d->getProperty(s.subject().uri()) );
175  }
176  }
177 
178  // determine the reverse properties
179  for( QMap<QUrl, Property>::iterator propIt = d->properties.begin();
180  propIt != d->properties.end(); ++propIt ) {
181  Property& p = propIt.value();
182  if( p.range() ) {
183  if ( !quiet )
184  qDebug() << "Setting reverse property " << p.uri() << " on type " << p.range()->uri() << endl;
185  if ( !p.range()->allReverseProperties().contains( &p ) )
186  p.range()->addReverseProperty( &p );
187  }
188  if ( !p.domain() ) {
189  p.setDomain( d->getClass(Soprano::Vocabulary::RDFS::Resource()) );
190  }
191 
192  Q_ASSERT( d->properties.count( propIt.key() ) == 1 );
193  }
194 
195  // now assign the comments to resources and properties
196  QMapIterator<QUrl, QString> commentsIt( d->comments );
197  while( commentsIt.hasNext() ) {
198  commentsIt.next();
199  if( d->resources.contains( commentsIt.key() ) )
200  d->resources[commentsIt.key()].setComment( commentsIt.value() );
201  else if( d->properties.contains( commentsIt.key() ) )
202  d->properties[commentsIt.key()].setComment( commentsIt.value() );
203  }
204 
205  // testing stuff
206  for( QMap<QUrl, ResourceClass>::iterator it = d->resources.begin();
207  it != d->resources.end(); ++it ) {
208  if( !it->parentClass(false) ) {
209  it->setParentResource( d->getClass(Soprano::Vocabulary::RDFS::Resource()) );
210  }
211  if ( !quiet )
212  qDebug() << "Resource: " << (*it).name()
213  << "[" << (*it).uri() << "]"
214  << " (->" << (*it).parentClass(false)->name() << ")"
215  << ( (*it).generateClass() ? " (will be generated)" : " (will not be generated)" )
216  << endl;
217 
218  Q_ASSERT( d->resources.count( it.key() ) == 1 );
219 
220  QListIterator<const Property*> propIt( (*it).allProperties() );
221  while( propIt.hasNext() ) {
222  const Property* p = propIt.next();
223  if ( !quiet )
224  qDebug() << " " << p->uri() << " (->" << p->typeString() << ")" << ( p->isList() ? QString("+") : QString("1") ) << endl;
225  }
226  }
227 
228  return success;
229 }
230 
231 
232 QList<ResourceClass*> OntologyParser::parsedClasses() const
233 {
234  QList<ResourceClass*> rl;
235  for( QMap<QUrl, ResourceClass>::iterator it = d->resources.begin();
236  it != d->resources.end(); ++it )
237  rl << &(*it);
238  return rl;
239 }
resourceclass.h
Property::isList
bool isList() const
Returns whether the property is a list of values.
Definition: rcgen/property.cpp:88
ResourceClass::addProperty
void addProperty(Property *property)
Adds a new property to the resource.
Definition: resourceclass.cpp:92
Property::range
ResourceClass * range() const
Returns the scope of the property.
Definition: rcgen/property.cpp:63
OntologyParser::OntologyParser
OntologyParser()
Definition: ontologyparser.cpp:92
ResourceClass::setUri
void setUri(const QUrl &uri)
Sets the uri of the resource.
Definition: resourceclass.cpp:38
Property::uri
QUrl uri() const
Returns the uri of the property.
Definition: rcgen/property.cpp:48
ResourceClass::uri
QUrl uri() const
Returns the uri of the resource.
Definition: resourceclass.cpp:43
ResourceClass::parentClass
ResourceClass * parentClass(bool considerGenerateClass=true) const
Returns the parent resource of the resource.
Definition: resourceclass.cpp:63
Property::setUri
void setUri(const QUrl &uri)
Creates a new property of a given type and with a given uri.
Definition: rcgen/property.cpp:43
QHash
ontologyparser.h
ResourceClass::allProperties
Property::ConstPtrList allProperties() const
Returns the list of all properties of the resource.
Definition: resourceclass.cpp:97
quiet
bool quiet
Definition: rcgen.cpp:29
OntologyParser::parse
bool parse(const QString &filename, const QString &serialization=QString())
Definition: ontologyparser.cpp:105
ResourceClass
Represents a resource.
Definition: resourceclass.h:30
OntologyParser::~OntologyParser
~OntologyParser()
Definition: ontologyparser.cpp:99
OntologyParser::parsedClasses
QList< ResourceClass * > parsedClasses() const
Definition: ontologyparser.cpp:232
Property::setLiteralRange
void setLiteralRange(const QString &range)
Set the literal range of the property.
Definition: rcgen/property.cpp:58
ResourceClass::allReverseProperties
Property::ConstPtrList allReverseProperties() const
Returns the list of all reverse properties of the resource.
Definition: resourceclass.cpp:107
ResourceClass::setGenerateClass
void setGenerateClass(bool generate)
Sets whether code for this class shall be generated.
Definition: resourceclass.cpp:112
ResourceClass::addReverseProperty
void addReverseProperty(Property *property)
Adds a reverse property to the resource.
Definition: resourceclass.cpp:102
Property::domain
ResourceClass * domain(bool onlyReturnGeneratedClass=false) const
Returns the domain resource the property belongs to.
Definition: rcgen/property.cpp:98
Property
Represents the property of a resource.
Definition: rcgen/property.h:29
ResourceClass::setParentResource
void setParentResource(ResourceClass *parent)
Sets the parent resource of the resource.
Definition: resourceclass.cpp:58
Property::setDomain
void setDomain(ResourceClass *domain)
Sets the domain the property belongs to.
Definition: rcgen/property.cpp:93
property.h
ResourceClass::addParentResource
void addParentResource(ResourceClass *parent)
Adds a parent resource to the resource.
Definition: resourceclass.cpp:82
Property::typeString
QString typeString(bool simple=false, const QString &nameSpace=QString()) const
Retrieve a string representation of the range.
Definition: rcgen/property.cpp:132
Property::setRange
void setRange(ResourceClass *type)
Sets the type of the property.
Definition: rcgen/property.cpp:53
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