Projects/Nepomuk/QueryingMethods

From KDE TechBase
Revision as of 11:43, 23 August 2012 by Vishesh (talk | contribs) (Code examples)

There isn't much point of pushing all your data into Nepomuk, if you cannot query it. This article mentions the different ways you can query Nepomuk and the advantages and disadvantages of each.

QueryServiceClient

The Nepomuk QueryServiceClient is an asynchronous method of running queries in Nepomuk. It accepts both Nepomuk Queries and direct SPARQL queries. It provides convenient signal which your application can connect to in order to receive the results.

Please note that the asynchronous nature of the QueryServiceClient is achieved by transmitting the query over dbus to the storage service, where a new thread is spawned, the query is run on that thread, and the results are returned over dbus. This is fine for casual use, but when you care about high performance, it is recommended that you avoid the QueryServiceClient.

Example -

    Nepomuk2::Query::Query query( .. build Query .. );
    Nepomuk2::Query::QueryServiceClient* client = new Nepomuk2::Query::QueryServiceClient( this );
    client->query( query );
    connect( client, SIGNAL(newEntries(QList<Nepomuk2::Query::Result>)),
             this, SLOT(newEntries(QList<Nepomuk2::Query::Result>)) );
    
    void TestObject::newEntries(const QList< Nepomuk2::Query::Result >& list)
    {
        foreach( const Nepomuk2::Query::Result &r, list ) {
            Nepomuk2::Resource res = r.resource();
            QHash<QUrl, Nepomuk2::Variant> prop = res.properties();
            QHashIterator<QUrl, Nepomuk2::Variant> it( prop );
            while( it.hasNext() ) {
                it.next();
                kDebug() << it.key() << " " << it.value();
            }
            kDebug() << "--------------------------";
        }
    }

Result Iterator

The ResultIterator can take either a Nepomuk or SPARQL query and provides an iterator which you can use to get the results. The iterator is obviously blocking, so you may want to put it another thread, depending on the kind of query.

Example -

    Nepomuk2::Query::Query query( .. build Query .. );
    Nepomuk::Query::ResultIterator it( query );
    while( it.next() ) {
            Nepomuk2::Resource res = it.current().resource();
            QHash<QUrl, Nepomuk2::Variant> prop = res.properties();
            QHashIterator<QUrl, Nepomuk2::Variant> it( prop );

            while( it.hasNext() ) {
                it.next();
                kDebug() << it.key() << " " << it.value();
            }
            kDebug() << "--------------------------";
        }
    }

Soprano::Model

The underlying Soprano model can also be used to directly run the queries. You can only run SPARQL queries on the model. Use this method when you're writing a custom query. The underlying Model also provides an iterator similar to that of the ResultIterator.

Example -

    QString query = QString::fromLatin1("select ?r where { ?r a nco:Contact . }");
    Soprano::Model* model = Nepomuk2::ResourceManager::instance()->mainModel();
    Soprano::QueryResultIterator it = model->executeQuery( query, Soprano::Query::QueryLanguageSparql );

    while( it.next() ) {
        kDebug() << it["r"].uri();
    }