user641687
user641687

Reputation:

Using DataTypeProperty literal in SPARQL query not working

I'm trying to execute a very simple SPARQL query to retrieve the nationality of a person based on a name passed in the query string, and I don't understand why it's not working. Here are the relevant RDF rules....

<owl:DatatypeProperty rdf:ID="name">
<rdfs:domain rdf:resource="#NobelWinner"/>
<rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
<rdfs:comment rdf:datatype="http://www.w3.org/2001/XMLSchema#string">All laureates have a name.</rdfs:comment>
</owl:DatatypeProperty>

<owl:DatatypeProperty rdf:ID="nationality">
<rdfs:domain rdf:resource="#PersonWinner"/>
<rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
<rdfs:comment rdf:datatype="http://www.w3.org/2001/XMLSchema#string">
Person laureates were associated with a nation when they won the prize.
</rdfs:comment>
</owl:DatatypeProperty>

The code is in a Java servlet and looks something like this, with the SPARQL query on line 42....

              try {
 34             String prefix1 = "PREFIX nob:<http://swat.cse.lehigh.edu/resources/onto/nobel.owl#> ";
 35             String prefix2 = "PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> ";
 36             String prefix3 = "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> ";
 37             String prefix4 = "PREFIX nobdat:<http://swat.cse.lehigh.edu/resources/data/nobel/nobeldata.owl#> ";
 38             String prefix = prefix1+prefix2+prefix3+prefix4;
 39             String winnerName = request.getParameter("name");
 40
 41             // INSERT QUERY
 42             String queryString  = prefix +
 43                 "SELECT ?nat { ?s nob:name " + winnerName + ". ?s nob:nationality ?nat. }";
 44
 45             String ttlLoc = "/my/ttl/path/loc.ttl";
 47             Store store = SDBFactory.connectStore(ttlLoc);
 48             Dataset ds = DatasetStore.create(store) ;
 49
 50             // CREATE QUERY
 51             Query query = QueryFactory.create(queryString);
 52             QueryExecution qe = QueryExecutionFactory.create(query, ds);


                    while (rs.hasNext()) {
 66                     QuerySolution qs = rs.nextSolution();
 67                     String nationality = qs.getLiteral("?nat").toString();
 68                     out.println("<P>Nationality: " + nationality + "</P>");
 69                 }

I can't get the query or anything like it that would use a string literal in place of a name to work in the Protege IDE, either. Any ideas? Thanks guys.

Upvotes: 3

Views: 579

Answers (1)

AndyS
AndyS

Reputation: 16630

winnerName needs to be a SPARQL constant i.e. with quotes - you can use single quotes:

.... ?s nob:name '" + winnerName + "' . ?s nob:nationality

Use Dataset ds = SBDFcatoryconnectDataset(ttlloc) ;

Longer term, think about creating the datset ins ervlet initialization, not each request.

Upvotes: 2

Related Questions