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 vocabulary 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.

Mind the Difference between QString and QUrl

The latter one is really simple: the given URI is used as the resource URI. If the resource exists, its data is used, otherwise it will be created with exactly that URI.

The QString one is a bit trickier. It will try to be clever about the parameter and see if it is a URI. If no resource with that URI (if it is a URI) exists, it is interpreted as an identifier (nao:identifier). Resource checks if a resource with that identifier exists. If so, its data is loaded, if not, a new resource with a random URI and that string as identifier is created.

However, be aware that nothing is written to Nepomuk until the first writing call to Resource such as setProperty or addType.

Debugging the created data

Using sopranocmd

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:

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):

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

# sopranocmd --help

is your friend for all details.

nepomukcmd

As a shortcut add the following to your .bashrc to avoid having to type in the dbus and model parameters all the time:

(Be aware that the --nrl parameter is only available in Soprano 2.3.63 and above.)

Using Konqueror

In the Nepomuk playground repository lives a KIO slave which can handle the nepomuk:/ protocol. It will display all properties of a Nepomuk resource including its links to other resources and the backlinks. This is a convenient way of looking at the Nepomuk data. The KIO slave even support removal of resources.

Using NepomukShell

NepomukShell is a maintenance and debugging tool, which lives in its own git repository at nepomukshell. It is a simple tool that let's one browse all resources in Nepomuk. Additionally it allows to create subclasses and properties (Caution: do only create subclasses and properties from PIMO classes and properties!) and remove resources.

Constructing SPARQL queries

Hint: In most cases the Nepomuk Query API should be enough and prevent you from writing your own SPARQL which is hard to debug.

Whenever doing something a bit fancier with Nepomuk one has to use SPARQL queries via

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):

Listening to changes in the database

Nepomuk's database does only contain statements, i.e. quadruples. To date Soprano's Model does provide four signals which can be used to monitor new and removed statements: statementsAdded and statementsRemoved as well as their counterparts which have as paramter the added or removed statement.

It is recommended to use Soprano's SignalCacheModel when listening to changes to prevent a slowdown of the whole system as the signals are emitted for each statement:

Starting Nepomuk Sever from the Trunk in Ubuntu

Note: Starting with (K)ubuntu 10.10 (Maverick Meerkat), virtuoso-t is in /usr/bin. So the work around described below is no longer necessary.

Ubuntu packages virtuoso slightly differently. It provides a package called virtuoso-nepomuk which installs the executable virtuoso-t in the /usr/lib/virtuoso/ directory for security purposes.

When running Nepomuk from the trunk, the nepomukserver is unable to find the virtuoso-t executable, and therefore the NepomukStorage Service fails to initialize. One way to fix this is to adjust the PATH environment variable.

PATH=/usr/lib/virtuoso:$PATH
export PATH

Debugging virtuoso-t

If virtuoso-t consumes a lot of CPU resources but there are no active queries analysis has to go a bit deeper. Virtuoso is started through Soprano with certain parameters which are set in a temporary ini-file (/tmp/virtuoso_XXXX.ini). Soprano needs to be modified manually to start Virtuoso with different parameters in the ini-file, e.g. to improve virtuoso-t's behaviour by modifying backends/virtuoso/virtuosocontroller.cpp (Soprano) and setting NumberOfBuffers to 40000 (line 344) and SchedulerInterval to 0 (line 350).

After re-compiling soprano one has to attach gdb to virtuoso-t as soon as it starts consuming CPU and create a full threaded backtrace:

set logging file /tmp/virtuoso-t.out
set logging on
thread apply all bt full