0

Given a subject with multiple occurrences of the same property, is it possible to retrieve all the objects/values they point to, without specifying in advance their number?

This question resembles this one.

Example data (taken from link above):

@prefix example: <http://example.org/> .
example:subject1 example:property example:object1 .
example:subject1 example:property example:object2 .
example:subject1 example:property example:object3 .
...

It is possible to get a defined number of objects via:

select ?subject ?object1/2/n 
where {?subject example:property ?object1, ?object2 .}
filter( ?object1 != ?object2 ).

But how to do if we do not know the number of occurrences of the property in advance? (besides programmatically). Thank you.

Community
  • 1
  • 1
ecrin
  • 91
  • 1
  • 7

1 Answers1

1

I suppose, you are a bit confused by the Turtle syntax.

In fact,

SELECT ?object1
   WHERE {
   ?subject example:property ?object1, ?object2 .
   FILTER ( ?object1 != ?object2 ).
   }

is just a syntactic sugar for:

SELECT ?object1
   WHERE {
   ?subject example:property ?object1 .
   ?subject example:property ?object2 .
   FILTER ( ?object1 != ?object2 ).
   }

This pattern matches all the triples which are different from each other only by their objects.
I suppose, "a subject with multiple occurrences of the same property" (from your question) means "there exist different objects for the same subject and the same property".

The pattern is symmetric, i.e. if :object1 is bound to ?object1 in a result, then :object1 will be bound to ?object2 in another result. It doesn't matter for this pattern whether any third triple with different object :object3 exists, but if it exists, :object3 will be bound to ?object1 in yet another result.

You can try also something like this for better understanding:

SELECT DISTINCT ?object1
   WHERE {
   ?subject example:property ?object1 .
   FILTER EXISTS {
      ?subject example:property ?object2 .
      FILTER ( ?object1 != ?object2 )
      }
   }
Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58
  • 1
    Thank you for the answer. Yes, I exactly meant "there exist different objects for the same subject and the same property". I understand the syntactic sugar but did not know/realize that the filtered pattern could catch all different objects by only forcing 2 triples to be different. If I understand well: whatever triple which matches the subject and the property and has a different object from another previous matching triple will be returned (this is actually the very essence of patterns, they fit multiple cases (!)). Thanks, now it's clearer! – ecrin May 18 '17 at 14:49