我已成功加载rdf文件。即使我也尝试使用相同的rdf文件将此查询转换为protege。它运行正常。但是,当我在日食中尝试它时,它给了我例外。
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.util.FileManager;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
sparqltest();}
static void sparqltest()
{
FileManager.get().addLocatorClassLoader(Test.class.getClassLoader());
Model model= FileManager.get().loadModel("C:/Users/avg/workspacejena32/Jena/data.rdf");
String queryString="PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> " +
"PREFIX foaf:<http://xmlns.com/foaf/0.1/> " +
"SELECT ?y ?x" +
"WHERE " +
"{?y foaf:z ?x}";
Query query= QueryFactory.create(queryString);
QueryExecution qexec=QueryExecutionFactory.create(query, model);
try {
ResultSet results = qexec.execSelect();while ( results.hasNext()){
QuerySolution soln = results.nextSolution();
Literal name = soln.getLiteral("x");
System.out.println(name);
}
}
finally {
qexec.close();
}}}
我得到以下例外......
Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Encountered " "-" "- "" at line 1, column 130.
Was expecting one of:
"graph" ...
"optional" ...
"minus" ...
"bind" ...
"service" ...
"filter" ...
"{" ...
"}" ...
";" ...
"," ...
"." ...
at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.perform(ParserSPARQL11.java:87)
at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.parse(ParserSPARQL11.java:40)
at com.hp.hpl.jena.query.QueryFactory.parse(QueryFactory.java:132)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:69)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:40)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:28)
at Test.sparqltest(Test.java:31)
at Test.main(Test.java:16)
答案 0 :(得分:0)
删除&#39; - &#39;它给了我无效的结果。
null
null
null
null
null
null
null
null
null
如果我在查询中做了一些更改,例如
String queryString="PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> " +
"PREFIX foaf:<http://xmlns.com/foaf/0.1/> " +
"SELECT ?y ?x" +
"WHERE " +
"{?y foaf:Studies ?x}";
它什么都没有。空白结果。
答案 1 :(得分:0)
String queryString="PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>" +
"PREFIX foaf:<http://xmlns.com/foaf/0.1/> " +
"SELECT * WHERE {" +
"?person foaf:name ?x .}";
我的结果如下......
Charlie
Mary
John
George V