Development/Tutorials/Metadata/Nepomuk/TipsAndTricks: Difference between revisions

From KDE TechBase
(the first 4 nepomuk tips and tricks)
 
No edit summary
Line 48: Line 48:
     "select ?r where { ?r nao:hasTag ?tag . \
     "select ?r where { ?r nao:hasTag ?tag . \
                       ?tag nao:prefLabel 'foobar'^^xsd:string . }"
                       ?tag nao:prefLabel 'foobar'^^xsd:string . }"
</code>
To monitor all statements that are added and removed from the Nepomuk storage one would simply use the following command (as with ''list'' one can specify a filter to only list the added and removed statements one is interested in):
<code>
# sopranocmd --dbus org.kde.NepomukStorage --model main monitor
</code>
</code>



Revision as of 08:08, 24 June 2009


Development/Tutorials/Metadata/Nepomuk/TipsAndTricks


Nepmuk Tips and Tricks
Tutorial Series   Nepomuk
Previous   None
What's Next   n/a
Further Reading   Resource Handling with Nepomuk,

Advanced Queries with SPARQL, RDF and Ontologies in Nepomuk

Always initialize Nepomuk

Make sure that somewhere in the initialization code of your application or library Nepomuk is initialized via:

Nepomuk::ResourceManager::instance()->init();


Using ontology URIs in your code

One often needs the URI of a specific class or a specific property in ones code. And not all ontologies are provided by the very convenient Soprano::Vocabulary namespace.

The solution is rather simple: create your own vocbulary namespaces by using Soprano's own onto2vocabularyclass command line tool. It can generate convenient vocabulary namespaces for you. The Soprano documentation shows how to use it manually or even simpler with a simple CMake macro.


Debugging the created data

When using Nepomuk one creates a lot of RDF statements in the Nepomuk RDF storage. It is often of interest to check which data has been created, if statements have been correctly created or simply look at existing data.

Soprano provides a nice command line client to do all this called sopranocmd. It provides all the features one needs to debug data: it can add and remove statements, list and query them, import and export whole RDF files, and even monitor for statementAdded and statementRemoved events.

To access the Nepomuk storage one would typically use the D-Bus interface:

  1. sopranocmd --dbus org.kde.NepomukStorage --model main <command> \
   <parameters>

If one wanted to list all the resources that have been tagged with the tag whose resource URI is nepomuk:/foobar one would use the following command:

  1. sopranocmd --dbus org.kde.NepomukStorage --model main list \
   "" "" "<nepomuk:/foobar>"

or one would use a SPARQL query (sopranocmd supports the standard URI prefixes out of the box):

  1. sopranocmd --dbus org.kde.NepomukStorage --model main query \
   "select ?r where { ?r nao:hasTag ?tag . \
                      ?tag nao:prefLabel 'foobar'^^xsd:string . }"

To monitor all statements that are added and removed from the Nepomuk storage one would simply use the following command (as with list one can specify a filter to only list the added and removed statements one is interested in):

  1. sopranocmd --dbus org.kde.NepomukStorage --model main monitor

# sopranocmd --help is your friend for all details.


Constructing SPARQL queries

Whenever doing something a bit fancier with Nepomuk one has to use SPARQL queries via Nepomuk::ResourceManager::instance()->mainModel()

   ->executeQuery( myQueryString, 
                   Soprano::Query::QueryLanguageSparql );

Constructing these queries can be a bit cumbersome since one has to use a lot of class and property URIs from different ontologies. Also literals have to be formatted according to the N3 syntax used in SPARQL. Luckily Soprano provides the necessary tools to do exactly that: Soprano::Node::toN3, Soprano::Node::resourceToN3, and Soprano::Node::literalToN3 take care of all formatting and percent-encoding you need. Using those methods the code to create queries might look ugly but the resulting queries are more likely to be correctly encoded and introduce less code duplication.

Typically one would use QString::arg like so (be aware that the standard prefixes are NOT supported out-of-the-box as with sopranocmd):

using namespace Soprano;

QString myQuery

    = QString("select ?r where { "
              "?r %1 ?v . "
              "?v %2 %3 . }")
      .arg(Node::resourceToN3(Vocabulary::NAO::hasTag()))
      .arg(Node::resourceToN3(Vocabulary::NAO::prefLabel()))
      .arg(Node::literalToN3("foobar")));

This will create the same query we used above only using no hard-coded components whatsoever.