20 #include <soprano/soprano.h>
22 #include <QtCore/QMap>
23 #include <QtCore/QStringList>
24 #include <QtCore/QFile>
25 #include <QtCore/QHash>
26 #include <QtCore/QDebug>
27 #include <QtCore/QDir>
33 class OntologyParser::Private
38 resources.insert( Soprano::Vocabulary::RDFS::Resource(),
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" );
62 QStringList classesToGenerate;
68 QMap<QUrl, ResourceClass> resources;
69 QMap<QUrl, Property> properties;
70 QMap<QUrl, QString> comments;
71 const Soprano::Parser* rdfParser;
84 Property* getProperty(
const QUrl& uri ) {
107 Soprano::RdfSerialization serialization = Soprano::SerializationUnknown;
108 if( !serializationMimeType.isEmpty() ) {
109 serialization = Soprano::mimeTypeToSerialization( serializationMimeType );
111 else if ( filename.endsWith(
"trig" ) ) {
112 serialization = Soprano::SerializationTrig;
115 serialization = Soprano::SerializationRdfXml;
118 if ( !( d->rdfParser = Soprano::PluginManager::instance()->discoverParserForSerialization( serialization, serializationMimeType ) ) ) {
123 qDebug() <<
"(OntologyParser) Parsing " << filename << endl;
125 Soprano::StatementIterator it = d->rdfParser->parseFile( filename,
126 QUrl(
"http://org.kde.nepomuk/dummybaseuri"),
128 serializationMimeType );
132 const Soprano::Statement& s = *it;
134 if( s.predicate().uri() == Soprano::Vocabulary::RDFS::subClassOf() ) {
135 ResourceClass* rc = d->getClass( s.subject().uri().toString() );
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 );
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() );
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" ) ) {
158 else if( type == Soprano::Vocabulary::RDFS::Literal() ) {
162 prop->
setRange( d->getClass(s.object().uri()) );
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 );
169 else if( s.predicate().uri() == Soprano::Vocabulary::RDFS::comment() ) {
170 d->comments[s.subject().uri()] = s.object().literal().toString();
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()) );
179 for( QMap<QUrl, Property>::iterator propIt = d->properties.begin();
180 propIt != d->properties.end(); ++propIt ) {
184 qDebug() <<
"Setting reverse property " << p.
uri() <<
" on type " << p.
range()->
uri() << endl;
189 p.
setDomain( d->getClass(Soprano::Vocabulary::RDFS::Resource()) );
192 Q_ASSERT( d->properties.count( propIt.key() ) == 1 );
196 QMapIterator<QUrl, QString> commentsIt( d->comments );
197 while( commentsIt.hasNext() ) {
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() );
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()) );
212 qDebug() <<
"Resource: " << (*it).name()
213 <<
"[" << (*it).uri() <<
"]"
214 <<
" (->" << (*it).parentClass(
false)->name() <<
")"
215 << ( (*it).generateClass() ?
" (will be generated)" :
" (will not be generated)" )
218 Q_ASSERT( d->resources.count( it.key() ) == 1 );
220 QListIterator<const Property*> propIt( (*it).allProperties() );
221 while( propIt.hasNext() ) {
224 qDebug() <<
" " << p->
uri() <<
" (->" << p->
typeString() <<
")" << ( p->
isList() ? QString(
"+") : QString(
"1") ) << endl;
234 QList<ResourceClass*> rl;
235 for( QMap<QUrl, ResourceClass>::iterator it = d->resources.begin();
236 it != d->resources.end(); ++it )
bool isList() const
Returns whether the property is a list of values.
void addProperty(Property *property)
Adds a new property to the resource.
ResourceClass * range() const
Returns the scope of the property.
void setUri(const QUrl &uri)
Sets the uri of the resource.
QUrl uri() const
Returns the uri of the property.
QUrl uri() const
Returns the uri of the resource.
ResourceClass * parentClass(bool considerGenerateClass=true) const
Returns the parent resource of the resource.
void setUri(const QUrl &uri)
Creates a new property of a given type and with a given uri.
Property::ConstPtrList allProperties() const
Returns the list of all properties of the resource.
bool parse(const QString &filename, const QString &serialization=QString())
QList< ResourceClass * > parsedClasses() const
void setLiteralRange(const QString &range)
Set the literal range of the property.
Property::ConstPtrList allReverseProperties() const
Returns the list of all reverse properties of the resource.
void setGenerateClass(bool generate)
Sets whether code for this class shall be generated.
void addReverseProperty(Property *property)
Adds a reverse property to the resource.
ResourceClass * domain(bool onlyReturnGeneratedClass=false) const
Returns the domain resource the property belongs to.
Represents the property of a resource.
void setParentResource(ResourceClass *parent)
Sets the parent resource of the resource.
void setDomain(ResourceClass *domain)
Sets the domain the property belongs to.
void addParentResource(ResourceClass *parent)
Adds a parent resource to the resource.
QString typeString(bool simple=false, const QString &nameSpace=QString()) const
Retrieve a string representation of the range.
void setRange(ResourceClass *type)
Sets the type of the property.