0

I'm using Jena TDB for store a dataset of triples from a file. I have a problem when I try to send a SPARQL query to TDB using filter. For example the following query works:

select ?ob where {
  ?ob rdfs:label "NameOfLabel"@language .
}

but this doesn't:

select ?ob where {
  ?ob rdfs:label ?pr .
  filter( ?pr = "NameOfLabel" ) .
} 

The purpose of this query is to find an ?ob from a "NameOfLabel" (ignoring the language). I've tried regex and str(?pr), and some other things, but these haven't worked. How can I do this?

Update (based on answer)

When I try using filter( str(?pr) = "NameOfLabel" ), I get an exception. Here's the stack trace:

Exception in thread "main" java.lang.IllegalArgumentException: getLow: Empty RecordBuffer
    at com.hp.hpl.jena.tdb.base.buffer.RecordBuffer.getLow(RecordBuffer.java:59)
    at com.hp.hpl.jena.tdb.base.recordbuffer.RecordRangeIterator.hasNext(RecordRangeIterator.java:112)
    at org.apache.jena.atlas.iterator.Iter$4.hasNext(Iter.java:317)
    at com.hp.hpl.jena.tdb.sys.DatasetControlMRSW$IteratorCheckNotConcurrent.hasNext(DatasetControlMRSW.java:119)
    at org.apache.jena.atlas.iterator.Iter$4.hasNext(Iter.java:317)
    at org.apache.jena.atlas.iterator.Iter$3.hasNext(Iter.java:200)
    at org.apache.jena.atlas.iterator.Iter.hasNext(Iter.java:915)
    at org.apache.jena.atlas.iterator.RepeatApplyIterator.hasNext(RepeatApplyIterator.java:59)
    at com.hp.hpl.jena.tdb.solver.SolverLib$IterAbortable.hasNext(SolverLib.java:191)
    at org.apache.jena.atlas.iterator.Iter$4.hasNext(Iter.java:317)
    at com.hp.hpl.jena.sparql.engine.iterator.QueryIterPlainWrapper.hasNextBinding(QueryIterPlainWrapper.java:54)
    at com.hp.hpl.jena.sparql.engine.iterator.QueryIteratorBase.hasNext(QueryIteratorBase.java:112)
    at com.hp.hpl.jena.sparql.engine.iterator.QueryIterConvert.hasNextBinding(QueryIterConvert.java:59)
    at com.hp.hpl.jena.sparql.engine.iterator.QueryIteratorBase.hasNext(QueryIteratorBase.java:112)
    at com.hp.hpl.jena.sparql.engine.iterator.QueryIterDistinctReduced.hasNextBinding(QueryIterDistinctReduced.java:54)
    at com.hp.hpl.jena.sparql.engine.iterator.QueryIteratorBase.hasNext(QueryIteratorBase.java:112)
    at com.hp.hpl.jena.sparql.engine.iterator.QueryIteratorWrapper.hasNextBinding(QueryIteratorWrapper.java:40)
    at com.hp.hpl.jena.sparql.engine.iterator.QueryIteratorBase.hasNext(QueryIteratorBase.java:112)
    at com.hp.hpl.jena.sparql.engine.iterator.QueryIteratorWrapper.hasNextBinding(QueryIteratorWrapper.java:40)
    at com.hp.hpl.jena.sparql.engine.iterator.QueryIteratorBase.hasNext(QueryIteratorBase.java:112)
    at com.hp.hpl.jena.sparql.engine.iterator.QueryIteratorWrapper.hasNextBinding(QueryIteratorWrapper.java:40)
    at com.hp.hpl.jena.sparql.engine.iterator.QueryIteratorBase.hasNext(QueryIteratorBase.java:112)
    at com.hp.hpl.jena.sparql.engine.ResultSetStream.hasNext(ResultSetStream.java:75)

EDIT II:

Dataset dataset = TDBFactory.createDataset(DIRECTORY);
dataset.begin(ReadWrite.READ);

QueryExecution qExec = QueryExecutionFactory.create(query, dataset) ;
ResultSet risultati = qExec.execSelect();
while(risultati.hasNext()){
    system.out.println(risultati.next());
}
  • Thanks for the update with the stack trace, but what's the code where this happens? – Joshua Taylor Jul 28 '14 at 21:10
  • I edited another time with code – fortunato.rosa Jul 28 '14 at 21:20
  • 2
    OK, quick response is that the code looks OK. You didn't show the query string, so it's possible that there's still something wrong there, but this *looks* like it should be OK. It might be worthwhile to take this to the jena mailing lists, because it's possible that it's a bug. You'll want to be able to provide a complete working example there though, so recreate a minimal amount of a data, and full Java class that loads the dataset and runs the query. – Joshua Taylor Jul 28 '14 at 21:22

1 Answers1

4

The plain literal "NameOfLabel" is not the same as the literal with a language tag "NameOfLabel"@en. If

?ob rdfs:label "NameOfLabel"@en           # (1)

works, then so should

?ob rdfs:label ?label .
filter ( ?label = "NameOfLabel"@en )      # (2)

If you want to compare the string content in a filter without comparing the language, just do:

?ob rdfs:label ?label .
filter ( str(?label) = "NameOfLabel" )    # (3)

Note that (2) is really not very good practice. It doesn't make much sense to filter on exact values, because you can just use (1) instead. (3) is OK, because you do actually need to use the str function. If you want to specify some exact values at run time, and have variables in the query, you also have the option of

values ?label { "NameOfObject"@en }
?ob rdfs:label ?label .

which has the advantage of being able to specify multiple values for ?label. If you're using Jena, you can also use a ParameterizedSparqlString and just have the pattern

?ob rdfs:label ?label .

but replace ?label when you have the value that you want. See my answer to get latitude and longitude of a place dbpedia for an example of ParameterizedSparqlStrings.

Community
  • 1
  • 1
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • The problem is : if i run your #3 query and i print the result with a hasNext() iterator on resultSet , i get the get low: Empty RecordBuffer exception. – fortunato.rosa Jul 28 '14 at 20:18
  • 1
    I'm not sure what you mean by "low:". (3) should work, unless I'm misunderstanding something. Can you show the code you're using, and the exception with stacktrace that you get? Do be aware that most result sets can only be iterated once—if you print them and then try to iterate again, there won't be anything to iterate over. – Joshua Taylor Jul 28 '14 at 20:39
  • I write stack trace in edit. Thx in advance for help ! – fortunato.rosa Jul 28 '14 at 21:10
  • 3
    It looks like you have corrupted the database at some time in teh past by not using transactions when creating the database and not calling TDB.sync(dataset). – AndyS Jul 28 '14 at 22:17