Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在SPARQL查询中使用DataTypeProperty文字无效_Java_Sparql_Rdf_Jena - Fatal编程技术网

Java 在SPARQL查询中使用DataTypeProperty文字无效

Java 在SPARQL查询中使用DataTypeProperty文字无效,java,sparql,rdf,jena,Java,Sparql,Rdf,Jena,我试图执行一个非常简单的SPARQL查询,根据查询字符串中传递的名称检索一个人的国籍,我不明白为什么它不起作用。以下是相关的RDF规则 <owl:DatatypeProperty rdf:ID="name"> <rdfs:domain rdf:resource="#NobelWinner"/> <rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/> <rdfs:commen

我试图执行一个非常简单的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>

所有获奖者都有一个名字。
获奖者获奖时与一个国家有关联。
代码位于Javaservlet中,看起来像这样,第42行是SPARQL查询

              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                 }
试试看{
34字符串prefix1=“前缀nob:”;
35字符串prefix2=“PREFIX rdfs:”;
36字符串前缀x3=“前缀rdf:”;
37字符串prefix4=“前缀nobdat:”;
38字符串前缀=prefix1+prefix2+prefix3+prefix4;
39字符串winnerName=request.getParameter(“名称”);
40
41//插入查询
42字符串queryString=前缀+
43“选择纳特{s nob:name“+winnerName+”?s nob:national?nat.}”;
44
45字符串ttlLoc=“/my/ttl/path/loc.ttl”;
47 Store Store=SDBFactory.connectStore(ttlLoc);
48数据集ds=DatasetStore.create(store);
49
50//创建查询
51 Query Query=QueryFactory.create(queryString);
52 QueryExecution qe=QueryExecutionFactory.create(query,ds);
while(rs.hasNext()){
66 QuerySolution qs=rs.nextSolution();
67字符串国籍=qs.getLiteral(“?nat”).toString();
68.println(“

国籍:“+国籍+”

”); 69 }

我也无法获得在Protege IDE中使用字符串文字代替名称的查询或类似查询。有什么想法吗?谢谢各位。

winnerName
需要是SPARQL常量,即带引号的-可以使用单引号:

.... ?s nob:name '" + winnerName + "' . ?s nob:nationality
使用数据集ds=SBDFcatoryconnectDataset(ttlloc);


从长远来看,考虑在Servlet初始化中创建datset,而不是每个请求。

Andy,你是对的,这是一个错误,但修复后仍然没有结果。此外,我在Protege编辑器中使用引号,也没有得到任何结果。