sparql complete tree from subject

2020-07-25 23:43发布

When I have for example a person graph e.g. John and john has a work adress home adress phone numbers relations etc.

Is it possible to retrieve everything that is connected to john and to the subclasses of john without knowing what it is?

So that I can retrieve for example the following

John < address < house_number
     < mobile_number
     < company < address
               < function < office number < etc...

And retrieve this via:
     "John" rdfs:everything ?everything ... as deep as the tree goes.

Or do I need to know the graph?

标签: tree rdf sparql
1条回答
时光不老,我们不散
2楼-- · 2020-07-25 23:57

Your terminology is a bit off, as things like addresses, mobile numbers, etc., aren't subclasses of John. RDF is about triples (labeled directed edges), and John's address is the object of a triple that has the form John hasAddress AddressOfJohn. Speaking in graph based terms, it sounds like you're asking whether it's possible to retrieve all the things that are reachable by some directed path beginning at John. It's easier to work with concrete data, so let's suppose that you've got this data:

@prefix : <http://stackoverflow.com/q/20878795/1281433/> .

:john :hasAddress :addressOfJohn ;
      :hasMobileNumber :mobileNumber ;
      :hasCompany :company .

:addressOfJohn :hasHouseNumber :houseNumber .

:company :hasAddress :addressOfCompany ;
         :hasFunction :function .

SPARQL 1.1 added support for Property Paths, which a sort of like a regular expression language for sequences of properties, including typical operators like * (any number of repetition) and + (one or more). Unfortunately, you can't use variables in property paths, so you can't just do

:john ?p+ ?object

However, as noted in a similar question on answers.semanticweb.com SPARQL: property paths without specified predicates, you can construct a disjunction that will match every property. For instance, since : is a defined prefix and is thus an IRI, the pattern :|!: will match any property (since everything is either : or it isn't). That mans that (:|!:)+ is a genuine wildcard path. Thus, we can write a query like the following and get the corresponding results:

prefix : <http://stackoverflow.com/q/20878795/1281433/>

select ?object where { 
  :john (:|!:)+ ?object 
}
---------------------
| object            |
=====================
| :company          |
| :function         |
| :addressOfCompany |
| :mobileNumber     |
| :addressOfJohn    |
| :houseNumber      |
---------------------
查看更多
登录 后发表回答