0

I have a file describing people with the help of FOAF. In Jena I am trying to parse all ages from the profiles and noticed that my listStatements and listObjectsOfProperty gives me different results. I could not easily find any help from the javadocs or other documentation.

I have the following code for querying with listStatments:

StmtIterator iter = this.foafmodel.listStatements(
     (Resource) null, 
     this.foafmodel.createProperty("http://xmlns.com/foaf/0.1/age"), 
     (RDFNode) null);

And this is the code for listObjectsOfProperty:

Property foafAge = this.foafmodel.createProperty("http://xmlns.com/foaf/0.1/age");
NodeIterator iter = this.foafmodel.listObjectsOfProperty(foafAge);

In this case listStatements iterator iterates 38 times while the listObjectsOfProperty only 20 times. Can someone explain to me what is the difference between these two implementations?

ksnabb
  • 183
  • 2
  • 8

1 Answers1

2

Let us assume that your data contains multiple triples with your property :p referencing the same object :o, like so:

:s1 :p :o .
:s2 :p :o .

At first glance, it would appear to me that listObjectsOfProperty are giving you all individuals that are referenced by your property without duplication. We can confirm this by digging into the implementation that depends on GraphUtil#listObjects(...). The code uses a Set<Node> to aggregate all of the objects. As a result, you should only get back a single iteration with the element :o.

The other method, listStatements should return to you a statement/triple for every time that property is used. In the example model above, you would/should get two results, one for each statements containing :p.

Rob Hall
  • 2,693
  • 16
  • 22
  • 1
    Thanks! I see no duplicates when using listObjectsOfProperty which means when trying to calculate e.g. average age it's the wrong function to use. – ksnabb Apr 14 '14 at 17:22
  • 1
    @ksnabb Note that you can use SPARQL to get some answers for those types of questions in Jena (through ARQ). For example, you can use the [average](http://www.w3.org/TR/sparql11-query/#defn_aggAvg) aggregate function as is done in [this example](http://www.w3.org/TR/sparql11-query/#groupby) – Rob Hall Apr 14 '14 at 17:46