例如,当我有一个人图时,例如约翰和约翰有一个工作地址家庭地址电话号码关系等。
是否有可能在不知道它是什么的情况下检索与 john 和 john 的子类相关的所有内容?
这样我就可以检索例如以下内容
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.
Run Code Online (Sandbox Code Playgroud)
还是我需要知道图表?
你的术语有点不对劲,因为地址、手机号码等不是 John 的子类。RDF 是关于三元组(标记为有向边),而约翰的地址是具有形式 的三元组的对象John hasAddress AddressOfJohn
。用基于图的术语来说,听起来您是在问是否可以检索从 John 开始的某个有向路径可到达的所有事物。使用具体数据更容易,所以让我们假设您有这些数据:
@prefix : <http://stackoverflow.com/q/20878795/1281433/> .
:john :hasAddress :addressOfJohn ;
:hasMobileNumber :mobileNumber ;
:hasCompany :company .
:addressOfJohn :hasHouseNumber :houseNumber .
:company :hasAddress :addressOfCompany ;
:hasFunction :function .
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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
}
Run Code Online (Sandbox Code Playgroud)
---------------------
| object |
=====================
| :company |
| :function |
| :addressOfCompany |
| :mobileNumber |
| :addressOfJohn |
| :houseNumber |
---------------------
Run Code Online (Sandbox Code Playgroud)