[Nepomuk] Re: counting literal values

Robby Stephenson robby at periapsis.org
Wed Dec 1 06:08:43 CET 2010


Maybe I'm still missing something? The following code doesn't work for me:

  QUrl subject("http://example.com/subject");
  QUrl property("http://example.com/number");
  Soprano::LiteralValue value(1);

  Nepomuk::Resource res2(subject);
  res2.addProperty(property, Nepomuk::Variant(1));

  Nepomuk::Query::ComparisonTerm term(Nepomuk::Types::Property(property),
                                      Nepomuk::Query::LiteralTerm(value),
                                      Nepomuk::Query::ComparisonTerm::Equal);
  QString queryString = 
Nepomuk::Query::Query(term).toSparqlQuery(Nepomuk::Query::Query::CreateCountQuery);
Soprano::QueryResultIterator it = Nepomuk::ResourceManager::instance()-
>mainModel()->executeQuery(queryString, 
Soprano::Query::QueryLanguageSparql);
  int cnt = it.next() ? it.binding(0).literal().toInt() : 0;
  QCOMPARE(cnt, 1);

Robby

On Monday 29 November 2010, Sebastian Trüg wrote:
> 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::Lit
> >                                                                          
eralTerm(value),
> >                                                                          
Nepomuk::Query::Co
> >                                                                          
mparisonTerm::Equal
> >                                                                          
);
> >   
> >   QString queryString =
> > 
> > Nepomuk::Query::Query(term).toSparqlQuery(Nepomuk::Query::Query::Create
> > CountQuery);
> > 
> >   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#DiscardableIn
> > stanceBase> . } . }"
> > 
> > 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