如何使用SPARQL查询从owl中检索个人?

时间:2012-10-21 04:59:15

标签: rdf sparql jena

我可以使用以下查询从我的本体中检索个人:

SELECT ?indiv WHERE { ?indiv rdf:type:Fruit  } 

我得到的结果如Apple,Orange等,但是当我用Java编写这个查询时,我得到以下异常:

Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Encountered " "}" "} "" at line 4, column 41.
Was expecting one of:
    <IRIref> ...
    <PNAME_NS> ...
    <PNAME_LN> ...
    <BLANK_NODE_LABEL> ...
    <VAR1> ...
    <VAR2> ...
    "true" ...
    "false" ...
    <INTEGER> ...
    <DECIMAL> ...
    <DOUBLE> ...
    <INTEGER_POSITIVE> ...
    <DECIMAL_POSITIVE> ...
    <DOUBLE_POSITIVE> ...
    <INTEGER_NEGATIVE> ...
    <DECIMAL_NEGATIVE> ...
    <DOUBLE_NEGATIVE> ...
    <STRING_LITERAL1> ...
    <STRING_LITERAL2> ...
    <STRING_LITERAL_LONG1> ...
    <STRING_LITERAL_LONG2> ...
    "(" ...
    <NIL> ...
    "{" ...
    "[" ...
    <ANON> ...
    "+" ...
    "*" ...
    "/" ...
    "|" ...
    "?" ...

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)

我的代码是:

String queryString = " PREFIX ont: <http://www.owl-ontologies.com/fruitOntology.owl#> 
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
SELECT ?indiv WHERE { ?indiv ont:Fruit  } ";

Query query = QueryFactory.create(queryString) ;
QueryExecution qexec = QueryExecutionFactory.create(query, m) ;
try {
  ResultSet results = qexec.execSelect() ;
  for ( ; results.hasNext() ; )
  {
    QuerySolution soln = results.nextSolution() ;
    Resource y = soln.getResource("y") ; 
    Resource x = soln.getLiteral("x") ;   
    System.out.println(y.getLocalName()+" = "+x.getString()) ;
  }
}
catch(Exception e){
}

1 个答案:

答案 0 :(得分:2)

这是非法的SPARQL语法。你想要这样的东西:

PREFIX ....
SELECT ?indiv WHERE { ?indiv rdf:type ont:Fruit  }

RDF是三倍;感兴趣的三重模式是预测为rdf:type的地方。需要用于分隔图案的3个部分的空间。