Difference between revisions of "Projects/Nepomuk/SparqlQueries"

Jump to: navigation, search
(Prepare for translation)
(Marked this version for translation)
 
(One intermediate revision by one user not shown)
Line 2: Line 2:
 
<translate>
 
<translate>
  
== Running Sparql Queries ==
+
__TOC__
 +
== Running Sparql Queries == <!--T:1-->
  
 +
<!--T:2-->
 
Depending on the needs of your application you may need to directly run sparql queries.
 
Depending on the needs of your application you may need to directly run sparql queries.
  
 +
<!--T:3-->
 
'''Nepomuk''' uses one main [http://soprano.sourceforge.net/apidox/stable/classSoprano_1_1Model.html Soprano model] which is accessed through the [http://api.kde.org/4.x-api/kdelibs-apidocs/nepomuk-core/html/classNepomuk_1_1ResourceManager.html ResourceManager]:
 
'''Nepomuk''' uses one main [http://soprano.sourceforge.net/apidox/stable/classSoprano_1_1Model.html Soprano model] which is accessed through the [http://api.kde.org/4.x-api/kdelibs-apidocs/nepomuk-core/html/classNepomuk_1_1ResourceManager.html ResourceManager]:
  
 +
<!--T:4-->
 
<syntaxhighlight lang="cpp-qt">
 
<syntaxhighlight lang="cpp-qt">
 
     Soprano::Model* model = Nepomuk::ResourceManager::instance()->mainModel();
 
     Soprano::Model* model = Nepomuk::ResourceManager::instance()->mainModel();
Line 13: Line 17:
  
  
 +
<!--T:5-->
 
The queries can be run via this model -
 
The queries can be run via this model -
 
<syntaxhighlight lang="cpp-qt">
 
<syntaxhighlight lang="cpp-qt">
Line 19: Line 24:
 
     Soprano::QueryResultIterator it = model->executeQuery( query, Soprano::Query::QueryLanguageSparql );
 
     Soprano::QueryResultIterator it = model->executeQuery( query, Soprano::Query::QueryLanguageSparql );
  
     while( it.next() ) {
+
     <!--T:6-->
 +
while( it.next() ) {
 
         kDebug() << it["r"].uri();
 
         kDebug() << it["r"].uri();
 
     }
 
     }
Line 25: Line 31:
  
  
=== Sparql Prefixes ===
+
=== Sparql Prefixes === <!--T:7-->
  
 +
<!--T:8-->
 
When writing SPARQL queries, according to the standard one needs to prepend the queries with the prefixes that have been used. For example -  
 
When writing SPARQL queries, according to the standard one needs to prepend the queries with the prefixes that have been used. For example -  
 
<syntaxhighlight lang="text">
 
<syntaxhighlight lang="text">
Line 33: Line 40:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
<!--T:9-->
 
When running queries in Nepomuk, you do not need to explicitly add the prefix. The prefix will automatically be added by the storage service. In the future we will try to make virtuoso automatically understand all prefixes.
 
When running queries in Nepomuk, you do not need to explicitly add the prefix. The prefix will automatically be added by the storage service. In the future we will try to make virtuoso automatically understand all prefixes.
  
=== Full Text Indexing ===
+
=== Full Text Indexing === <!--T:10-->
  
 +
<!--T:11-->
 
'''Nepomuk''' depends on [http://soprano.sourceforge.net/apidox/trunk/soprano_backend_virtuoso.html Virtuoso] for data storage. '''Virtuoso''' brings a lot of nice [http://docs.openlinksw.com/virtuoso/rdfsparql.html#sparqlextensions extensions to SPARQL]. Most importantly the full text search which is used through the artificial ''bif:contains'' property.  
 
'''Nepomuk''' depends on [http://soprano.sourceforge.net/apidox/trunk/soprano_backend_virtuoso.html Virtuoso] for data storage. '''Virtuoso''' brings a lot of nice [http://docs.openlinksw.com/virtuoso/rdfsparql.html#sparqlextensions extensions to SPARQL]. Most importantly the full text search which is used through the artificial ''bif:contains'' property.  
  
 +
<!--T:12-->
 
This allows to combine graph queries with full text queries in a nice way:
 
This allows to combine graph queries with full text queries in a nice way:
  
 +
<!--T:13-->
 
<syntaxhighlight lang="text">
 
<syntaxhighlight lang="text">
 
select ?r where { ?r nao:prefLabel ?label .
 
select ?r where { ?r nao:prefLabel ?label .
Line 46: Line 57:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
<!--T:14-->
 
The query above will find any resources that contain ''nepomuk'' in their label.
 
The query above will find any resources that contain ''nepomuk'' in their label.
  
 +
<!--T:15-->
 
Of course wildcards are supported, too. However, be aware that when using wildcards the expression itself needs to be enclosed in quotes as follows:
 
Of course wildcards are supported, too. However, be aware that when using wildcards the expression itself needs to be enclosed in quotes as follows:
  
 +
<!--T:16-->
 
<syntaxhighlight lang="text">
 
<syntaxhighlight lang="text">
 
select ?r where { ?r nao:prefLabel ?label .
 
select ?r where { ?r nao:prefLabel ?label .
Line 55: Line 69:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
<!--T:17-->
 
Please note the virtuoso '''requires a minimum of 4 leading characters''' in order to use wildcards.
 
Please note the virtuoso '''requires a minimum of 4 leading characters''' in order to use wildcards.
  
 +
<!--T:18-->
 
[[Category:Documentation]]
 
[[Category:Documentation]]
 
[[Category:Development]]
 
[[Category:Development]]
 
[[Category:Tutorials]]
 
[[Category:Tutorials]]
 
</translate>
 
</translate>

Latest revision as of 14:47, 12 December 2012

Other languages:English 100%

Contents

[edit] Running Sparql Queries

Depending on the needs of your application you may need to directly run sparql queries.

Nepomuk uses one main Soprano model which is accessed through the ResourceManager:

    Soprano::Model* model = Nepomuk::ResourceManager::instance()->mainModel();


The queries can be run via this model -

    QString query = QString::fromLatin1("select ?r where { ?r a nco:Contact . }");
    Soprano::Model* model = Nepomuk2::ResourceManager::instance()->mainModel();
    Soprano::QueryResultIterator it = model->executeQuery( query, Soprano::Query::QueryLanguageSparql );
 
    while( it.next() ) {
        kDebug() << it["r"].uri();
    }


[edit] Sparql Prefixes

When writing SPARQL queries, according to the standard one needs to prepend the queries with the prefixes that have been used. For example -

PREFIX nco: <http://www.semanticdesktop.org/ontologies/2007/03/22/nco#> .
select ?r ?name where { ?r a nco:Contact . ?r nco:fullname ?name . }

When running queries in Nepomuk, you do not need to explicitly add the prefix. The prefix will automatically be added by the storage service. In the future we will try to make virtuoso automatically understand all prefixes.

[edit] Full Text Indexing

Nepomuk depends on Virtuoso for data storage. Virtuoso brings a lot of nice extensions to SPARQL. Most importantly the full text search which is used through the artificial bif:contains property.

This allows to combine graph queries with full text queries in a nice way:

select ?r where { ?r nao:prefLabel ?label .
                  ?label bif:contains 'nepomuk' . }

The query above will find any resources that contain nepomuk in their label.

Of course wildcards are supported, too. However, be aware that when using wildcards the expression itself needs to be enclosed in quotes as follows:

select ?r where { ?r nao:prefLabel ?label .
                  ?label bif:contains "'nepomuk*'" . }

Please note the virtuoso requires a minimum of 4 leading characters in order to use wildcards.


This page was last modified on 12 December 2012, at 14:47. This page has been accessed 1,906 times. Content is available under Creative Commons License SA 3.0 as well as the GNU Free Documentation License 1.2.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal