• Skip to content
  • Skip to link menu
KDE 4.6 API Reference
  • KDE API Reference
  • kdelibs
  • 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 ) );

Nepomuk

Skip menu "Nepomuk"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.7.3
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal