Java - Jena QueryParseException

时间:2018-05-28 13:49:56

标签: java eclipse http sparql jena

我是Jena和SPARQL的新手。我正在尝试使用以下请求和代码在Eclipse中运行Jena。我得到QueryParseException,我知道其他人对未定义的rdfs前缀有同样的问题,但这里有不同。

例外:

  

线程“main”中的异常org.apache.jena.query.QueryParseException:第1行,第134列:未解析的前缀名称:http:

查询:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?subject ?fsn
WHERE {
  ?subject rdfs:label ?fsn.
  ?subject rdfs:subClassOf+ http://snomed.info/id/410607006
}

代码:

import java.util.HashMap;
import org.apache.jena.query.*;

public class SnomedQuery {
  private String serviceURI;
  private String query;

  //Constructor with SPARQL endpoint and query
  public SnomedQuery(String URI, String serviceURI){
    this.serviceURI = serviceURI;
    this.query = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>" +
              " SELECT ?subject ?fsn WHERE {?subject rdfs:label ?fsn. ?subject rdfs:subClassOf+ "+URI+"}";
  }

  //return SPARQL endpoint
  public String getServiceURI() {
    return this.serviceURI;
  }

  //return query
  public String getQuery() {
    return this.query;
  }

  /*
  * purpose: This function is used to retrieve all child of a concept and the concept itself
  * @param
  * @return
  *   Hashmap with URI as key and corresponding term as value
  */
  public HashMap<String, String> getFSNChildPlus(){
    HashMap<String, String> output = new HashMap<String, String>();

    QueryExecution q = QueryExecutionFactory.sparqlService(getServiceURI(), getQuery());
    ResultSet results = q.execSelect();

    while (results.hasNext()) {
        QuerySolution answer = results.nextSolution();
        String subject = answer.get("subject").toString();
        String fsn = answer.get("fsn").toString();

        output.put(subject, fsn);
    }
    return null;
  }
}

感谢您的帮助

1 个答案:

答案 0 :(得分:3)

在SPARQL中,需要标记URI。

所以http://snomed.info/id/410607006错了,但<http://snomed.info/id/410607006>没问题。

以下是您的查询:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?subject ?fsn
WHERE {
  ?subject rdfs:label ?fsn.
  ?subject rdfs:subClassOf+ <http://snomed.info/id/410607006>
}