[Nepomuk] Re: counting literal values

Sebastian Trüg trueg at kde.org
Mon Nov 29 09:42:42 CET 2010


Hi Robby,

your problem is that Nepomuk makes a few assumptions on the way the data
is stored[1]. You add a triple in the empty graph, thus it is ignored by
the query system.
You should rather use

Nepomuk::Resource res( subject );
res.addProperty( predicate, value );

Then your query will work. Using the model directly is discouraged. I
should actually add that to the API docs.

This week I will start work on an API in between the Nepomuk::Resource
and the Model API allowing lower level data manipulations without having
to care about the format of the data beyond what you already did.

Hope this helps,
Cheers,
Sebastian

On 11/27/2010 08:09 AM, Robby Stephenson wrote:
> Hi All,
> 
> I'm getting my feet a little wet with RDF and SPARQL, along with Nepomuk and 
> Nepomuk Query.  One of my little test examples did not work qute as 
> expected, so I was hoping someone might point out what I was doing wrong or 
> where my incorrect assumption might be.
> 
> I added a statement setting an example predicate property to a literal 
> value. Then, I want to count the number of statements with that literal 
> value. Here's what I tried first, which doesn't seem to work:
> 
>   QUrl subject("http://example.com/subject");
>   QUrl predicate("http://example.com/number");
>   Soprano::LiteralValue value(1);
>   model->addStatement(subject, property, value);
>   Nepomuk::Query::ComparisonTerm term(Nepomuk::Types::Property(predicate),
>                                                                          Nepomuk::Query::LiteralTerm(value),
>                                                                          Nepomuk::Query::ComparisonTerm::Equal);
>   QString queryString = 
> Nepomuk::Query::Query(term).toSparqlQuery(Nepomuk::Query::Query::CreateCountQuery);
>   Soprano::QueryResultIterator it = model->executeQuery(queryString, 
> Soprano::Query::QueryLanguageSparql);
>   it.next();
>   int count = it.binding(0).literal().toInt();
> 
> I expect count to equal 1, since there is at least one matching statement in 
> the model. But it equals 0. Looking at the query string, I see:
> 
> select count(distinct ?r) as ?cnt where { ?r <http://example.com/number> ?v1 
> . ?v1 ?v2 "1"^^<http://www.w3.org/2001/XMLSchema#int> . ?v2 
> <http://www.w3.org/2000/01/rdf-schema#subPropertyOf> 
> <http://www.w3.org/2000/01/rdf-schema#label> . graph ?v4 { ?r a ?v3 . } . { 
> ?v4 a 
> <http://www.semanticdesktop.org/ontologies/2007/08/15/nrl#InstanceBase> . } 
> UNION { ?v4 a 
> <http://www.semanticdesktop.org/ontologies/2007/08/15/nrl#DiscardableInstanceBase> 
> . } . }"
> 
> Is it a problem with my use of Types::Property? Is there a better or "more 
> correct" way to use the Query API to get a count of literal values?
> 
> It works as expected if I hand-write the query as:
> 
> select count(distinct ?r) as ?cnt where {
>   ?r <http://example.com/number> "1"^^<http://www.w3.org/2001/XMLSchema#int> 
> .
> }
> 
> This is with KDE 4.5.3. Thanks!
> Robby
> _______________________________________________
> Nepomuk mailing list
> Nepomuk at kde.org
> https://mail.kde.org/mailman/listinfo/nepomuk
> 


More information about the Nepomuk mailing list