在SPARQL查询中使用DataTypeProperty字面量无法工作

3

我正在尝试执行一个非常简单的SPARQL查询,根据传递在查询字符串中的姓名检索一个人的国籍,但我不明白为什么它不能工作。以下是相关的RDF规则...

<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>

代码位于Java servlet中,类似于以下内容,其中SPARQL查询在第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                 }

我在Protege IDE中无法使用查询或任何类似的东西,该查询将字符串字面量替换为名称。有什么想法吗?谢谢大家。

1个回答

2

winnerName需要是SPARQL常量,即必须加上引号 - 你可以使用单引号:


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

使用Dataset ds = SBDFcatoryconnectDataset(ttlloc) ;

从长远来看,考虑在Servlet初始化时创建数据集,而不是每个请求都创建。


安迪,你说得对,那是个错误,但修复后仍然没有任何结果。此外,我在Protege编辑器中使用引号也没有得到任何东西。 - user641687

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接