2

After I query dbpedia over its Sparql endpoint, I get results as Jena ResourceImpl objects. Then how I can get details of this resource? For example if this resource is a person; how can I get his/her birthDate?

I tried this one; but it always returns null.

QuerySolution querySolution = resultSet.next();
RDFNode x = querySolution.get("x");
ResourceImpl resource = (ResourceImpl) x;
Property property = new PropertyImpl("http://dbpedia.org/property/birthDate");
Resource propertyResourceValue = resource.getPropertyResourceValue(property); // NULL
talha06
  • 6,206
  • 21
  • 92
  • 147

2 Answers2

2

Likely you will need to make a subsequent SPARQL query if you want to get further details about the resource. E.g.,

String nextQuery = "DESCRIBE " + FmtUtils.stringForNode(resource.asNode(), (SerializationContext)null);
Query describeQuery = QueryFactory.create(nextQuery);
QueryExecution exec = QueryExecutionFactory.sparqlService("http://endpoint", describeQuery);
Model m = exec.execDescribe();

You should then be able to use the resource API over the resulting model to get the information you want.

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
RobV
  • 28,022
  • 11
  • 77
  • 119
  • 1
    Wouldn't a `CONSTRUCT` query in the first place be more efficient? – Ben Companjen Apr 09 '13 at 22:20
  • Potentially yes, the OP didn't show his initial query so we have no idea if he's bringing back lots of resources or a single resources, or whether he is selecting sufficient information to make `CONSTRUCT` viable since you'd have to then be sure to select all the triples you might later want to look at – RobV Apr 09 '13 at 23:43
1

Assuming your SPARQL query is a SELECT ..., the ResultSet is a table and each QuerySolution is a row in that table. When you get the Resource from such a row, you only have a Resource; the properties are not automatically attached. Hence, getting a property value on the Resource returns null.

It looks like RDFOutput does what you expected: transform a SPARQL query ResultSet to an RDF Model.

Ben Companjen
  • 1,417
  • 10
  • 24
  • 1
    `RDFOutput` won't help here, it merely translates the `ResultSet` into an RDF encoding of said `ResultSet` so `getPropertyResourceValue()` will still likely not return anything – RobV Apr 09 '13 at 17:45
  • I haven't used it, but will `RDFOutput.toModel()` not connect the properties to `Resource`s? Anyway, I just realised that the birthDate value is probably a Literal, but `getPropertyResourceValue` returns (if I understand the docs correctly) a Resource connected by the given property - and only when there is one. `listProperties(Property)` or `getProperty(Property)` would make more sense here, wouldn't they? – Ben Companjen Apr 09 '13 at 22:15