2

I can retrieve individuals from my ontology using following query:

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

I get results such as Apple, Orange, etc., but when I wrote this query in Java, I get the following exception:

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)

My code is:

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){
}
Joshua Taylor
  • 80,876
  • 9
  • 135
  • 306
Rosh
  • 699
  • 1
  • 11
  • 30

1 Answers1

2

That is illegal SPARQL syntax. You want something like:

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

RDF is triples; the triple pattern of interest is where the predciate is rdf:type. Spaces to separate the 3 parts of the pattern are necessary.

AndyS
  • 14,989
  • 15
  • 20
  • Now I did not get any exceptions. But I did not get any results... Do you have any idea about why is this? – Rosh Oct 21 '12 at 15:30
  • 2
    Probably because either there are no individuals directly asserted to be of type ont:Fruit. Further if you've said that A, B, & C are Apples, and All Apples are Fruit, you need to make sure you're using some sort of reasoner to get back the fact that A, B, & C are Fruit by virtue of the fact of they are Apples. – Michael Oct 22 '12 at 12:09