Projects/Nepomuk/QuickStart: Difference between revisions

From KDE TechBase
m (typo--)
(Removed page from translation)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
<languages />
<translate>


== Scope of this page == <!--T:1-->
== Scope of this page ==


<!--T:2-->
This page serves as a very simple overview to '''Nepomuk'''. It focuses on getting started with '''Nepomuk''' and tries not to deal with internal '''Nepomuk''' concepts. Please keep in the mind that the processes described here are targeted to the general use case. If you have a more specialized use case in mind, this might not be the best way to use the '''Nepomuk''' libraries.
This page serves as a very simple overview to '''Nepomuk'''. It focuses on getting started with '''Nepomuk''' and tries not to deal with internal '''Nepomuk''' concepts. Please keep in the mind that the processes described here are targeted to the general use case. If you have a more specialized use case in mind, this might not be the best way to use the '''Nepomuk''' libraries.


==Libraries and Headers== <!--T:3-->
==Libraries and Headers==


<!--T:4-->
Most of the non graphic parts of '''Nepomuk''' are present in the [https://projects.kde.org/projects/kde/kdelibs/nepomuk-core NepomukCore repository]. It is shipped with KDE 4.9, and should be provided by your distribution. When starting with '''Nepomuk''' development, it would be better to stick with your distribution version.
Most of the non graphic parts of '''Nepomuk''' are present in the [https://projects.kde.org/projects/kde/kdelibs/nepomuk-core NepomukCore repository]. It is shipped with KDE 4.9, and should be provided by your distribution. When starting with '''Nepomuk''' development, it would be better to stick with your distribution version.


<!--T:5-->
With KDE 4.9, the '''Nepomuk''' libraries moved to the Nepomuk2 namespace. The headers also follow a similar convention.
With KDE 4.9, the '''Nepomuk''' libraries moved to the Nepomuk2 namespace. The headers also follow a similar convention.


=== Headers === <!--T:6-->
=== Headers ===


<!--T:7-->
All the '''Nepomuk''' headers in are preceded by Nepomuk2. Here are some common '''Nepomuk''' includes that are frequently used -
All the '''Nepomuk''' headers in are preceded by Nepomuk2. Here are some common '''Nepomuk''' includes that are frequently used -


<!--T:8-->
<syntaxhighlight lang="cpp-qt">
<syntaxhighlight lang="cpp-qt">
#include <Nepomuk2/Tag>
#include <Nepomuk2/Tag>
Line 28: Line 21:
</syntaxhighlight>
</syntaxhighlight>


=== CMake === <!--T:9-->
=== CMake ===


<!--T:10-->
KDE Projects generally use cmake as a build tool. '''Nepomuk''' is no different. In order to use '''Nepomuk''' you need to add the relevant macros for '''Nepomuk''' in your CMakeLists.txt.
KDE Projects generally use cmake as a build tool. '''Nepomuk''' is no different. In order to use '''Nepomuk''' you need to add the relevant macros for '''Nepomuk''' in your CMakeLists.txt.


<!--T:11-->
<syntaxhighlight lang="cmake">
<syntaxhighlight lang="cmake">
find_package(NepomukCore REQUIRED)
find_package(NepomukCore REQUIRED)
Line 39: Line 30:
</syntaxhighlight>
</syntaxhighlight>


== Basic Terminology == <!--T:12-->
== Basic Terminology ==


<!--T:13-->
At the lowest level '''Nepomuk''' is just a database which many applications use to store data. The main difference is that the data is not stored in rows and columns like traditional relational databases. Instead '''Nepomuk''' can be viewed as something similar to an object or document store. We store a large number of objects, and each of these objects have (key, value) pairs attached to them.
At the lowest level '''Nepomuk''' is just a database which many applications use to store data. The main difference is that the data is not stored in rows and columns like traditional relational databases. Instead '''Nepomuk''' can be viewed as something similar to an object or document store. We store a large number of objects, and each of these objects have (key, value) pairs attached to them.


<!--T:14-->
Each object in our database is called a Resource. Everything in '''Nepomuk''' revolves around resources. All the higher level concepts such as files, tags, contacts, music albums, actors, etc. they are all Resources. Each of these resources has a number of (key, value) pairs associated with it. In the '''Nepomuk''' world, the keys are called properties or predicates, and have already been defined.
Each object in our database is called a Resource. Everything in '''Nepomuk''' revolves around resources. All the higher level concepts such as files, tags, contacts, music albums, actors, etc. they are all Resources. Each of these resources has a number of (key, value) pairs associated with it. In the '''Nepomuk''' world, the keys are called properties or predicates, and have already been defined.


<!--T:15-->
'''Nepomuk''' is also based around a concept called the Semantic Web. However, understanding the ideas and details behind the Semantic Web is not necessary.
'''Nepomuk''' is also based around a concept called the Semantic Web. However, understanding the ideas and details behind the Semantic Web is not necessary.


== Tags and Ratings == <!--T:16-->
== Tags and Ratings ==


<!--T:17-->
Every tag in '''Nepomuk''' is a Resource. In fact the [http://api.kde.org/4.x-api/kdelibs-apidocs/nepomuk-core/html/classNepomuk2_1_1Tag.html Tag class] is also derived from the [http://api.kde.org/4.x-api/kdelibs-apidocs/nepomuk-core/html/classNepomuk2_1_1Resource.html Resource class].
Every tag in '''Nepomuk''' is a Resource. In fact the [http://api.kde.org/4.x-api/kdelibs-apidocs/nepomuk-core/html/classNepomuk2_1_1Tag.html Tag class] is also derived from the [http://api.kde.org/4.x-api/kdelibs-apidocs/nepomuk-core/html/classNepomuk2_1_1Resource.html Resource class].


=== Setting Tags === <!--T:18-->
=== Setting Tags ===


<!--T:19-->
<syntaxhighlight lang="cpp-qt">
<syntaxhighlight lang="cpp-qt">
Nepomuk2::Tag tag( "Awesome-Tag-Name" );
Nepomuk2::Tag tag( "Awesome-Tag-Name" );
Line 64: Line 50:
</syntaxhighlight>
</syntaxhighlight>


<!--T:20-->
The [http://api.kde.org/4.x-api/kdelibs-apidocs/nepomuk-core/html/classNepomuk2_1_1Tag.html <tt>Nepomuk2::Tag</tt>] class will automatically look for a tag called "Awesome-Tag-Name", if it finds it then [http://api.kde.org/4.x-api/kdelibs-apidocs/nepomuk-core/html/classNepomuk2_1_1Resource.html#a3c58ca2096f8424e12f31ddd73369597 Tag::exists()] will return true. Otherwise, the tag will be saved the first time the tag is used. In our case, when we add the tag to the Resource via [http://api.kde.org/4.x-api/kdelibs-apidocs/nepomuk-core/html/classNepomuk2_1_1Resource.html#a649e935b3557d0c5015ab7728f623f66 Resource::addTag()], the tag will be saved.
The [http://api.kde.org/4.x-api/kdelibs-apidocs/nepomuk-core/html/classNepomuk2_1_1Tag.html <tt>Nepomuk2::Tag</tt>] class will automatically look for a tag called "Awesome-Tag-Name", if it finds it then [http://api.kde.org/4.x-api/kdelibs-apidocs/nepomuk-core/html/classNepomuk2_1_1Resource.html#a3c58ca2096f8424e12f31ddd73369597 Tag::exists()] will return true. Otherwise, the tag will be saved the first time the tag is used. In our case, when we add the tag to the Resource via [http://api.kde.org/4.x-api/kdelibs-apidocs/nepomuk-core/html/classNepomuk2_1_1Resource.html#a649e935b3557d0c5015ab7728f623f66 Resource::addTag()], the tag will be saved.


=== Retrieving Tags === <!--T:21-->
=== Retrieving Tags ===


<!--T:22-->
<syntaxhighlight lang="cpp-qt">
<syntaxhighlight lang="cpp-qt">
using namespace Nepomuk2;
using namespace Nepomuk2;


<!--T:23-->
Resource res( url );
Resource res( url );
QList<Tag> tags = res.tags();
QList<Tag> tags = res.tags();
Line 80: Line 63:
</syntaxhighlight>
</syntaxhighlight>


<!--T:24-->
Every resource has a predefined function for retrieving all the Tags any resource is tagged with - [http://api.kde.org/4.x-api/kdelibs-apidocs/nepomuk-core/html/classNepomuk2_1_1Resource.html#a286c2e37f975e58e38aaa83903040910 Resource::tags()]. The [http://api.kde.org/4.x-api/kdelibs-apidocs/nepomuk-core/html/classNepomuk2_1_1Resource.html#ad8d16485bdc9788de60bf5784141bcac Resource::genericLabel()] function tries to find a presentable name for any resource. In the case of tags, it returns the tags name.
Every resource has a predefined function for retrieving all the Tags any resource is tagged with - [http://api.kde.org/4.x-api/kdelibs-apidocs/nepomuk-core/html/classNepomuk2_1_1Resource.html#a286c2e37f975e58e38aaa83903040910 Resource::tags()]. The [http://api.kde.org/4.x-api/kdelibs-apidocs/nepomuk-core/html/classNepomuk2_1_1Resource.html#ad8d16485bdc9788de60bf5784141bcac Resource::genericLabel()] function tries to find a presentable name for any resource. In the case of tags, it returns the tags name.


=== Listing Tags === <!--T:25-->
=== Listing Tags ===


<!--T:26-->
The simplest way to list all the tags that are present in '''Nepomuk''' is via a static function - [http://api.kde.org/4.x-api/kdelibs-apidocs/nepomuk-core/html/classNepomuk2_1_1Tag.html#a03bc6a42a14003aca17e8c3e02f43d17 Tag::allTags()]. You, however, need to be careful using this in a production environment as it executes a blocking query in the background. Depending on the number of tags on the system, it could take some time.  
The simplest way to list all the tags that are present in '''Nepomuk''' is via a static function - [http://api.kde.org/4.x-api/kdelibs-apidocs/nepomuk-core/html/classNepomuk2_1_1Tag.html#a03bc6a42a14003aca17e8c3e02f43d17 Tag::allTags()]. You, however, need to be careful using this in a production environment as it executes a blocking query in the background. Depending on the number of tags on the system, it could take some time.  


<!--T:27-->
<syntaxhighlight lang="cpp-qt">
<syntaxhighlight lang="cpp-qt">
using namespace Nepomuk2;
using namespace Nepomuk2;


<!--T:28-->
QList<Tag> tags = Tag::allTags();
QList<Tag> tags = Tag::allTags();
foreach(const Tag& tag, tags)
foreach(const Tag& tag, tags)
Line 98: Line 77:
</syntaxhighlight>
</syntaxhighlight>


<!--T:29-->
There are also convenience functions to list all the resources that have been assigned a particular tag - [http://api.kde.org/4.x-api/kdelibs-apidocs/nepomuk-core/html/classNepomuk2_1_1Tag.html#a04256e4dda504e5b021afcab0ba4b714 Tag::tagOf]
There are also convenience functions to list all the resources that have been assigned a particular tag - [http://api.kde.org/4.x-api/kdelibs-apidocs/nepomuk-core/html/classNepomuk2_1_1Tag.html#a04256e4dda504e5b021afcab0ba4b714 Tag::tagOf]


<!--T:30-->
<syntaxhighlight lang="cpp-qt">
<syntaxhighlight lang="cpp-qt">
using namespace Nepomuk2;
using namespace Nepomuk2;


<!--T:31-->
Tag tag("Awesome-Tag-Name");
Tag tag("Awesome-Tag-Name");
QList<Resoruce> taggedResources = tag.tagOf();
QList<Resource> taggedResources = tag.tagOf();


<!--T:32-->
foreach(const Resource& res, taggedResources)
foreach(const Resource& res, taggedResources)
     kDebug() << res.genericLabel();
     kDebug() << res.genericLabel();
</syntaxhighlight>
</syntaxhighlight>


=== Ratings === <!--T:33-->
=== Ratings ===


<!--T:34-->
Every resource in Nepomuk can be given a numeric rating. It is generally done on a scale of 0-10.
Every resource in Nepomuk can be given a numeric rating. It is generally done on a scale of 0-10.


<!--T:35-->
<syntaxhighlight lang="cpp-qt">
<syntaxhighlight lang="cpp-qt">
using namespace Nepomuk2;
using namespace Nepomuk2;


<!--T:36-->
File fileRes( urlOfTheFile );
File fileRes( urlOfTheFile );
int rating = fileRes.rating();
int rating = fileRes.rating();


<!--T:37-->
rating = rating + 1;
rating = rating + 1;
fileRes.setRating( rating );
fileRes.setRating( rating );
</syntaxhighlight>
</syntaxhighlight>


== Dealing with files == <!--T:38-->
== Dealing with files ==


<!--T:39-->
Every file in Nepomuk is represented as a resource. Since dealing with files is very common, we provide a special [http://api.kde.org/4.x-api/kdelibs-apidocs/nepomuk-core/html/classNepomuk2_1_1File.html File class] which is derived from the Resource class. We also provide convenience functions for checking if a resource is a file.
Every file in Nepomuk is represented as a resource. Since dealing with files is very common, we provide a special [http://api.kde.org/4.x-api/kdelibs-apidocs/nepomuk-core/html/classNepomuk2_1_1File.html File class] which is derived from the Resource class. We also provide convenience functions for checking if a resource is a file.


<!--T:40-->
<syntaxhighlight lang="cpp-qt">
<syntaxhighlight lang="cpp-qt">
using namespace Nepomuk2;
using namespace Nepomuk2;


<!--T:41-->
File fileRes( urlOfTheFile );
File fileRes( urlOfTheFile );
kDebug() << fileRes.url();
kDebug() << fileRes.url();


<!--T:42-->
Resource res( urlOfTheFile );
Resource res( urlOfTheFile );
if( res.isFile() )
if( res.isFile() )
     kDebug() << res.toFile().url();
     kDebug() << res.toFile().url();


<!--T:43-->
</syntaxhighlight>
</syntaxhighlight>




<!--T:44-->
Internally Nepomuk Files are not very different from other Resources.
Internally Nepomuk Files are not very different from other Resources.


== Working Example == <!--T:45-->
== Working Example ==


<!--T:46-->
We understand that using a new framework such as '''Nepomuk''' can be quite intimidating at times. Therefore, we though it would be nice to have a working example for you to get started with - [http://quickgit.kde.org/index.php?p=kdeexamples.git&a=tree&h=eddcb1707a9e600db49fafdcb5ed8e8a195bcaba&hb=80a322af2bc5975c01c704173e937ccb8df605e3&f=nepomuk%2Ftest kdeexamples/nepomuk/test]. This example does nothing but it creates an executable called <code>nepomuk-test</code>. It serves as a simple starting point to '''Nepomuk''' where all the boiler plate code has been taken care of.
We understand that using a new framework such as '''Nepomuk''' can be quite intimidating at times. Therefore, we though it would be nice to have a working example for you to get started with - [http://quickgit.kde.org/index.php?p=kdeexamples.git&a=tree&h=eddcb1707a9e600db49fafdcb5ed8e8a195bcaba&hb=80a322af2bc5975c01c704173e937ccb8df605e3&f=nepomuk%2Ftest kdeexamples/nepomuk/test]. This example does nothing but it creates an executable called <code>nepomuk-test</code>. It serves as a simple starting point to '''Nepomuk''' where all the boiler plate code has been taken care of.


<!--T:47-->
[[Category: Documentation]]
[[Category: Documentation]]
[[Category: Development]]
[[Category: Development]]
[[Category: Tutorials]]
[[Category: Tutorials]]
</translate>

Latest revision as of 12:35, 9 February 2018

Scope of this page

This page serves as a very simple overview to Nepomuk. It focuses on getting started with Nepomuk and tries not to deal with internal Nepomuk concepts. Please keep in the mind that the processes described here are targeted to the general use case. If you have a more specialized use case in mind, this might not be the best way to use the Nepomuk libraries.

Libraries and Headers

Most of the non graphic parts of Nepomuk are present in the NepomukCore repository. It is shipped with KDE 4.9, and should be provided by your distribution. When starting with Nepomuk development, it would be better to stick with your distribution version.

With KDE 4.9, the Nepomuk libraries moved to the Nepomuk2 namespace. The headers also follow a similar convention.

Headers

All the Nepomuk headers in are preceded by Nepomuk2. Here are some common Nepomuk includes that are frequently used -

#include <Nepomuk2/Tag>
#include <Nepomuk2/Resource>
#include <Nepomuk2/ResourceManager>
#include <Nepomuk2/File>

CMake

KDE Projects generally use cmake as a build tool. Nepomuk is no different. In order to use Nepomuk you need to add the relevant macros for Nepomuk in your CMakeLists.txt.

find_package(NepomukCore REQUIRED)
target_link_libraries(myfile ${NEPOMUK_CORE_LIBRARY} )

Basic Terminology

At the lowest level Nepomuk is just a database which many applications use to store data. The main difference is that the data is not stored in rows and columns like traditional relational databases. Instead Nepomuk can be viewed as something similar to an object or document store. We store a large number of objects, and each of these objects have (key, value) pairs attached to them.

Each object in our database is called a Resource. Everything in Nepomuk revolves around resources. All the higher level concepts such as files, tags, contacts, music albums, actors, etc. they are all Resources. Each of these resources has a number of (key, value) pairs associated with it. In the Nepomuk world, the keys are called properties or predicates, and have already been defined.

Nepomuk is also based around a concept called the Semantic Web. However, understanding the ideas and details behind the Semantic Web is not necessary.

Tags and Ratings

Every tag in Nepomuk is a Resource. In fact the Tag class is also derived from the Resource class.

Setting Tags

Nepomuk2::Tag tag( "Awesome-Tag-Name" );
Nepomuk2::Resource res( url );
res.addTag( tag );

The Nepomuk2::Tag class will automatically look for a tag called "Awesome-Tag-Name", if it finds it then Tag::exists() will return true. Otherwise, the tag will be saved the first time the tag is used. In our case, when we add the tag to the Resource via Resource::addTag(), the tag will be saved.

Retrieving Tags

using namespace Nepomuk2;

Resource res( url );
QList<Tag> tags = res.tags();
foreach(const Tag& tag, tags)
    kDebug() << tag.genericLabel();

Every resource has a predefined function for retrieving all the Tags any resource is tagged with - Resource::tags(). The Resource::genericLabel() function tries to find a presentable name for any resource. In the case of tags, it returns the tags name.

Listing Tags

The simplest way to list all the tags that are present in Nepomuk is via a static function - Tag::allTags(). You, however, need to be careful using this in a production environment as it executes a blocking query in the background. Depending on the number of tags on the system, it could take some time.

using namespace Nepomuk2;

QList<Tag> tags = Tag::allTags();
foreach(const Tag& tag, tags)
    kDebug() << tag.genericLabel();

There are also convenience functions to list all the resources that have been assigned a particular tag - Tag::tagOf

using namespace Nepomuk2;

Tag tag("Awesome-Tag-Name");
QList<Resource> taggedResources = tag.tagOf();

foreach(const Resource& res, taggedResources)
    kDebug() << res.genericLabel();

Ratings

Every resource in Nepomuk can be given a numeric rating. It is generally done on a scale of 0-10.

using namespace Nepomuk2;

File fileRes( urlOfTheFile );
int rating = fileRes.rating();

rating = rating + 1;
fileRes.setRating( rating );

Dealing with files

Every file in Nepomuk is represented as a resource. Since dealing with files is very common, we provide a special File class which is derived from the Resource class. We also provide convenience functions for checking if a resource is a file.

using namespace Nepomuk2;

File fileRes( urlOfTheFile );
kDebug() << fileRes.url();

Resource res( urlOfTheFile );
if( res.isFile() )
    kDebug() << res.toFile().url();


Internally Nepomuk Files are not very different from other Resources.

Working Example

We understand that using a new framework such as Nepomuk can be quite intimidating at times. Therefore, we though it would be nice to have a working example for you to get started with - kdeexamples/nepomuk/test. This example does nothing but it creates an executable called nepomuk-test. It serves as a simple starting point to Nepomuk where all the boiler plate code has been taken care of.