[Nepomuk] counting literal values

Robby Stephenson robby at periapsis.org
Sat Nov 27 08:09:22 CET 2010


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


More information about the Nepomuk mailing list