• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 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." );
Nepomuk::File
A Nepomuk resource representing a file.
Definition: file.h:42

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 );
Nepomuk::Tag
A Tag can be assigned to any Thing.
Definition: tag.h:39

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();
Nepomuk::Resource
Resource is the central object type in Nepomuk.
Definition: resource.h:95

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;
}
Nepomuk::Types::Entity::label
QString label(const QString &language=KGlobal::locale() ->language())
Retrieve the label of the entity (rdfs:label)
Nepomuk::Types::Property
A property is a resource of type rdf:Property which relates a domain with a range.
Definition: property.h:53

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 );
Nepomuk::Query::LiteralTerm
Match literal properties via full text.
Definition: literalterm.h:87
Nepomuk::Query::Query
A Nepomuk desktop query.
Definition: query.h:77

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 );
Nepomuk::Query::ComparisonTerm
A term matching the value of a property.
Definition: comparisonterm.h:71

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 );
Nepomuk::Query::ResourceTerm
Matches exactly one resource.
Definition: resourceterm.h:53

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) );
Nepomuk::Query::AndTerm
Match resource that match all sub terms.
Definition: andterm.h:44

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 );
Nepomuk::Query::FileQuery
A Nepomuk desktop query specialized for file searches.
Definition: filequery.h:45

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 );
Nepomuk::Query::GroupTerm::addSubTerm
void addSubTerm(const Term &term)
Add a sub term to the list of terms that are combined in this group.
Nepomuk::Query::ResourceTypeTerm
Matching resources by type.
Definition: resourcetypeterm.h:49

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 ) );
Nepomuk::Query::NegationTerm::negateTerm
static Term negateTerm(const Term &term)
Negate term.
Nepomuk::Query::Term
The base class for all term types.
Definition: term.h:65

List all query results in a KDirModel:

KDirModel* model = getFancyDirModel();
Nepomuk::Query::Query query = buildFancyQuery();
KUrl searchUrl = query.toSearchUrl();
model->dirLister()->open( searchUrl );
Nepomuk::Query::Query::toSearchUrl
KUrl toSearchUrl(SparqlFlags flags=NoFlags) const
Convert the query into a URL which can be listed using KIO::DirLister.

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-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 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-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 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
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
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