• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

Nepomuk

Nepomuk Examples

Overview | Using | Examples | Desktop Ontologies | Resource Generator

Resource Handling Examples

Add an annotation (a comment) to a file.

Nepomuk::File f( "/home/foo/bar.txt" );
f.setAnnotation( "This is just a test file that contains nothing of interest." );

The following example creates a new tag. The tag can be accessed through its name which works as an identifier. Nepomuk automatically creates a new unique URI if this tag does not already exist.

Nepomuk::Tag tag( "MyTag" );
Nepomuk::File f( "/home/foo/bar.txt" );
f.addTag( tag );

Reading the information using plain Resource methods:

Nepomuk::Resource f( "/home/foo/bar.txt" );
QString annotation = f.getProperty( Soprano::Vocabulary::NAO::decription() ).toString();
QList<Resource> tags = f.getProperty( Soprano::Vocabulary::NAO::hasTag() ).toResourceList();
QListIterator<Resource> it( tags );
while( it.hasNext() )
kdDebug() << "File tagged with tag: "
<< it.next().genericLabel();

Reading the information using the convenience classes (be aware that these classes need to be generated from an ontology using the Resource Generator):

Nepomuk::File f( "/home/foo/bar.txt" );
QString description = f.description();
QList<Tag> tags = f.getTags();
QListIterator<Tag> it( tags );
while( it.hasNext() )
kdDebug() << "File tagged with tag: " << it.next().genericLabel();

Present all defined properties of an arbitrary resource to the user including internationalized labels:

Nepomuk::Resource f( "/home/foo/bar.txt" );
QHash<QUrl, Variant> properties = f.properties();
QHashIterator<QUrl, Variant> it( properties );
while( it.hasNext() ) {
it.next();
kdDebug() << Nepomuk::Types::Property( it.key() ).label() << ": " << it.value().toString() << endl;
}

Query Examples

Perform a simple full text query that looks for any resource containing the value 'nepomuk':

Nepomuk::Query::LiteralTerm nepomukTerm("nepomuk");
Nepomuk::Query::Query query( nepomukTerm );

Create a query that lists all resources tagged with a tag whose name matches 'nepomuk':

Nepomuk::Query::ComparisonTerm term( Soprano::Vocabulary::NAO::hasTag(),
Nepomuk::Query::LiteralTerm("nepomuk") );
Nepomuk::Query::Query query( term );

Create a query that lists all resources tagged with a specific tag:

Nepomuk::Tag tag = getFancyTag();
Nepomuk::Query::ComparisonTerm term( Soprano::Vocabulary::NAO::hasTag(),
Nepomuk::Query::ResourceTerm( tag ) );
Nepomuk::Query::Query query( term );

Create a query that lists all resource tagged with both the tags used above:

Nepomuk::Tag tag = getFancyTag();
Nepomuk::Query::ComparisonTerm fancyTagTerm( Soprano::Vocabulary::NAO::hasTag(),
Nepomuk::Query::ResourceTerm( tag ) );
Nepomuk::Query::ComparisonTerm nepomukTagTerm( Soprano::Vocabulary::NAO::hasTag(),
Nepomuk::Query::LiteralTerm("nepomuk") );
Nepomuk::Query::Query query( Nepomuk::Query::AndTerm(fancyTagTerm, nepomukTagTerm) );

Create a query that lists all files tagged with a specific tag:

Nepomuk::Tag tag = getFancyTag();
Nepomuk::Query::ComparisonTerm term( Soprano::Vocabulary::NAO::hasTag(),
Nepomuk::Query::ResourceTerm( tag ) );
Nepomuk::Query::FileQuery fileQuery( term );

Any other term can be used as sub term in a ComparisonTerm (Vocabulary namespace generated via The Nepomuk Resource Generator). The following query does return all resources related to person contacts that are tagged with tag.

Nepomuk::Query::AndTerm andTerm;
andTerm.addSubTerm( Nepomuk::Query::ResourceTypeTerm( Nepomuk::Vocabulary::NCO::PersonContact() ) );
andTerm.addSubTerm( Nepomuk::Query::ComparisonTerm( Soprano::Vocabulary::NAO::hasTag(),
Nepomuk::Query::ResourceTerm( tag ) ) );
Nepomuk::Query::ComparisonTerm cterm( Nepomuk::Vocabulary::NAO::isRelated(),
andTerm );

To make matters even more complex the above ComparisonTerm can be inverted:

cterm.setInverted(true);

This will not match resources related to some tagged person contact but match resources that some tagged person contact is related to (sounds confusing but remember that the properties are not symmetric by default, i.e. the graph of data is directed.)

Listing Files

Restrict the search to a specific folder:

fileQuery.addIncludeFolder( KUrl("/home/foobar/thegoodstuff") );

Restrict the search to files-only (Vocabulary namespace generated via onto2vocabularyclass using ${SHAREDDESKTOPONTOLOGIES_ROOT_DIR}/nie/nfo.trig):

Nepomuk::Query::Term folderTerm = Nepomuk::Query::ResourceTypeTerm( Nepomuk::Vocabulary::NFO::Folder() );
Nepomuk::Query::Term noFolderTerm = Nepomuk::Query::NegationTerm::negateTerm(folderTerm);
fileQuery.setTerm( Nepomuk::Query::AndTerm( fileQuery.term(), noFolderTerm ) );

List all query results in a KDirModel:

KDirModel* model = getFancyDirModel();
Nepomuk::Query::Query query = buildFancyQuery();
KUrl searchUrl = query.toSearchUrl();
model->dirLister()->open( searchUrl );

KIO will use the nepomuksearch:/ slave to list search results as file entries.

Misc Query Examples

Match all EmailAddress instances with a specific email address:

Nepomuk::Query::ComparisonTerm email( Nepomuk::Vocabulary::NCO::emailAddress(), Soprano::LiteralValue( "trueg@kde.org" ) );

Match all nco:PersonContact instances:

Nepomuk::Query::ResourceTypeTerm type( Nepomuk::Vocabulary::NCO::PersonContact() );

Match all person contacts with a specific email address:

Nepomuk::Query::AndTerm( type, Nepomuk::Query::ComparisonTerm( Nepomuk::Vocabulary::NCO::hasEmailAddress(), email ) );
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Nepomuk

Skip menu "Nepomuk"
  • 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