本文大多內容來自Joshua Taylor的回答
https://stackoverflow.com/users/1281433/joshua-taylor
查詢子類或等價關系
https://stackoverflow.com/questions/21092246/sparql-query-subclass-or-equivalentto/21093154#21093154
even though owl:equivalentClass
is a symmetric property (i.e., from a owl:equivalentClass b
we can infer b owl:equivalentClass a
), the triple might be present in only one direction in the data
在數據里面等價關系是單向表示的,因此查詢等價類的語句為
?myClass (owl:equivalentClass|^owl:equivalentClass)* :MyClass
查詢等價屬性的語句為
?p (owl:equivalentProperty|^ owl:equivalentProperty)* :order_no
查詢<http://class/加工過程> class的property
SELECT ?subclass WHERE { ?subclass rdfs:domain <http://class/加工過程>.#加工過程類的所有屬性 }
查詢<http://class/加工過程>的所有屬性的所有數據(s p o)
SELECT ?s ?subclass ?o WHERE { ?subclass rdfs:domain <http://class/加工過程>.#加工過程類的所有屬性 ?s ?subclass ?o#返回屬性關聯的所有數據 } LIMIT 100
查詢所有實例及其對應類型
SELECT ?instance ?s WHERE { ?instance rdf:type ?s. #找出實例的類型 }
查詢值為1600KN20173的所有數據
SELECT ?instance ?p WHERE {
?instance ?p “1600KN20173”. #找出屬性值為V101208的所有實例
}
如何獲取實例對應本體
本體之間存在關系 Human ----(hasPizza)---> Pizzas
為本體添加實例 Human:Jim ----(hasPizza)---> Pizzas:cheesePizza
執行語句:
select ?x ?y where { ?x hasPizza ?y }
返回?x=Jim
和 ?y=cheesePizza
如何得到實例對應本體?
用turtle表示rdf
@prefix : <http://example.org/pizzas#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix pizzas: <http://example.org/pizzas#> . @prefix owl: <http://www.w3.org/2002/07/owl#> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . pizzas:Jim a pizzas:Human , owl:NamedIndividual ; pizzas:hasPizza pizzas:CheesePizza . pizzas:hasPizza a owl:ObjectProperty ; rdfs:domain pizzas:Human ; rdfs:range pizzas:Pizza . pizzas:Human a owl:Class . pizzas:Pizza a owl:Class . <http://example.org/pizzas> a owl:Ontology . pizzas:CheesePizza a pizzas:Pizza , owl:NamedIndividual .
可以看到在模型定義中,
pizzas:hasPizza a owl:ObjectProperty ; rdfs:domain pizzas:Human ; rdfs:range pizzas:Pizza .
因此可以利用屬性hasPizza的rdfs:domain和rdfs:range兩個關系來獲得
查詢語句為:
prefix : <http://example.org/pizzas#> prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> select ?domain ?range where { :hasPizza rdfs:domain ?domain ; rdfs:range ?range . }
返回:
------------------- | domain | range | =================== | :Human | :Pizza | -------------------
2.利用rdf:type獲取
SELECT DISTINCT ?s WHERE { ?instance rdf:type ?s. #找出所有三元組的類型 }
返回了本體和實例數據的類型,混在一起了,因為本體和數據都是用三元組表示的
所以限定到具體數據,返回類型
SELECT DISTINCT ?s WHERE { ?instance ?p "三月". ?instance rdf:type ?s. #找出實例的類型 }
實例的rdf:
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:j.0="http://class/" xmlns:j.1="http://dataproperty/" xmlns:owl="http://www.w3.org/2002/07/owl#" xmlns:j.2="http://objectproperty/" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:xsd="http://www.w3.org/2001/XMLSchema#"> <j.0:維保 rdf:about="http://individual/維保/1600KN20173"> <j.1:保養人>empty</j.1:保養人> <j.1:實際保養日期>empty</j.1:實際保養日期> <j.1:所需工時_人數_時間>2*0.1</j.1:所需工時_人數_時間> <j.1:計划保養時間>3月5日——3月11日</j.1:計划保養時間> <j.1:計划保養周>第10周</j.1:計划保養周> <j.1:設備保養級別>1級</j.1:設備保養級別> <j.1:月份>三月</j.1:月份> <j.1:維保計划編號>1600KN20173</j.1:維保計划編號> </j.0:維保>
查詢結果:
上述查詢語句是對整個數據庫進行查詢,所以查詢了所有路徑下的數據:
"http://class/"
"http://dataproperty/"
"http://objectproperty/"
等等
可以指定PREFIX : <http://class/> 減小查詢范圍
比如下面語句
就指定了在‘:’的范圍中查找等價屬性
PREFIX : <http://www.siemens.com/semantic_web#> PREFIX owl: <http://www.w3.org/2002/07/owl#> SELECT ?property WHERE { ? property (owl:equivalentProperty|^owl:equivalentProperty)* :order_id. }