SPARQL:查询链接到一个 URL 但不链接到另一个的所有三元组
SPARQL: querying all triples linked to one URL, but not to another
根据一个包含关于多种化合物的陈述的 RDF 文件,我想为每种化合物创建一个 RDF 文件。
为了做到这一点,我开始寻找一个 SPARQL 查询,它可以隔离所有链接到某个 URL 的三元组,无论中间有多少节点。
我从一个非常有趣的 SPARQL 查询 () 开始,这样它将 return(希望)所有三元组链接到我数据集中的化合物 :d1
,但是不是关于另一种化合物 :d10
:
CONSTRUCT {
:d1 ?prop ?val .
?child ?childProp ?childPropVal .
?someSubj ?incomingChildProp ?child .
}
WHERE {
:d1 ?prop ?val ;
(:overrides|!:overrides)+ ?child .
?child ?childProp ?childPropVal.
?someSubj ?incomingChildProp ?child.
}
但是,当我在我的简化测试用例 (python) 中 运行 时:
rdf = """<?xml version="1.0"?>
<!DOCTYPE rdf:RDF [
<!ENTITY owl "http://www.w3.org/2002/07/owl#" >
<!ENTITY owl11 "http://www.w3.org/2006/12/owl11#" >
<!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
<!ENTITY owl11xml "http://www.w3.org/2006/12/owl11-xml#" >
<!ENTITY carcinogenesis "http://dl-learner.org/carcinogenesis#" >
<!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
<!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
]>
<rdf:RDF xmlns="http://dl-learner.org/carcinogenesis#"
xml:base="http://dl-learner.org/carcinogenesis"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:owl11="http://www.w3.org/2006/12/owl11#"
xmlns:carcinogenesis="http://dl-learner.org/carcinogenesis#"
xmlns:owl11xml="http://www.w3.org/2006/12/owl11-xml#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<owl:Ontology rdf:about=""/>
<owl:ObjectProperty rdf:about="#hasAtom">
<rdfs:domain rdf:resource="#Compound"/>
<rdfs:range rdf:resource="#Atom"/>
</owl:ObjectProperty>
<owl:ObjectProperty rdf:about="#hasBond">
<rdfs:domain rdf:resource="#Compound"/>
<rdfs:range rdf:resource="#Bond"/>
</owl:ObjectProperty>
<owl:ObjectProperty rdf:about="#hasStructure">
<rdfs:domain rdf:resource="#Compound"/>
<rdfs:range rdf:resource="#Structure"/>
</owl:ObjectProperty>
<owl:ObjectProperty rdf:about="#inBond">
<rdfs:range rdf:resource="#Atom"/>
<rdfs:domain rdf:resource="#Bond"/>
</owl:ObjectProperty>
<owl:DatatypeProperty rdf:about="#charge">
<rdfs:domain rdf:resource="#Atom"/>
<rdfs:range rdf:resource="&xsd;double"/>
</owl:DatatypeProperty>
<owl:DatatypeProperty rdf:about="#isMutagenic">
<rdfs:domain rdf:resource="#Compound"/>
<rdfs:range rdf:resource="&xsd;boolean"/>
</owl:DatatypeProperty>
<owl:Class rdf:about="#Atom"/>
<owl:Class rdf:about="#Bond">
<owl:disjointWith rdf:resource="#Structure"/>
<owl:disjointWith rdf:resource="#Atom"/>
</owl:Class>
<owl:Class rdf:about="#Bond-7">
<rdfs:subClassOf rdf:resource="#Bond"/>
</owl:Class>
<owl:Class rdf:about="#Carbon-22">
<rdfs:subClassOf rdf:resource="#Carbon"/>
</owl:Class>
<owl:Class rdf:about="#Compound">
<owl:disjointWith rdf:resource="#Structure"/>
<owl:disjointWith rdf:resource="#Atom"/>
<owl:disjointWith rdf:resource="#Bond"/>
</owl:Class>
<owl:Class rdf:about="#Six_ring">
<rdfs:subClassOf rdf:resource="#Ring"/>
</owl:Class>
<owl:Class rdf:about="#Ring">
<rdfs:subClassOf rdf:resource="#Structure"/>
</owl:Class>
<owl:Class rdf:about="#Structure">
<owl:disjointWith rdf:resource="#Atom"/>
</owl:Class>
<Compound rdf:about="#d1">
<hasBond rdf:resource="#bond1"/>
<hasAtom rdf:resource="#d1_2"/>
<hasAtom rdf:resource="#d1_3"/>
<hasStructure rdf:resource="#six_ring-1"/>
<isMutagenic rdf:datatype="&xsd;boolean">false</isMutagenic>
</Compound>
<Bond-7 rdf:about="#bond1">
<inBond rdf:resource="#d1_3"/>
<inBond rdf:resource="#d1_2"/>
</Bond-7>
<Carbon-22 rdf:about="#d1_2">
<charge rdf:datatype="&xsd;double">-0.133</charge>
</Carbon-22>
<Carbon-22 rdf:about="#d1_3">
<charge rdf:datatype="&xsd;double">-0.0030</charge>
</Carbon-22>
<Six_ring rdf:about="#six_ring-1"/>
<Compound rdf:about="#d10">
<hasBond rdf:resource="#bond40"/>
<hasAtom rdf:resource="#d10_12"/>
<hasAtom rdf:resource="#d10_13"/>
<isMutagenic rdf:datatype="&xsd;boolean">false</isMutagenic>
<hasStructure rdf:resource="#six_ring-9"/>
</Compound>
<Bond-1 rdf:about="#bond40">
<inBond rdf:resource="#d10_12"/>
<inBond rdf:resource="#d10_13"/>
</Bond-1>
<Six_ring rdf:about="#six_ring-9"/>
<Nitrogen-32 rdf:about="#d10_12">
<charge rdf:datatype="&xsd;double">-0.313</charge>
</Nitrogen-32>
<Nitrogen-32 rdf:about="#d10_13">
<charge rdf:datatype="&xsd;double">-0.313</charge>
</Nitrogen-32>
</rdf:RDF>
"""
# Inspired by
sparql_query = """CONSTRUCT {
:d1 ?prop ?val .
?child ?childProp ?childPropVal .
?someSubj ?incomingChildProp ?child .
}
WHERE {
:d1 ?prop ?val ;
(:overrides|!:overrides)+ ?child .
?child ?childProp ?childPropVal.
?someSubj ?incomingChildProp ?child.
}
"""
# Trying this query out:
import rdflib
import logging
logger = logging.getLogger()
logger.setLevel("INFO")
graph = rdflib.Graph()
graph.parse(data=rdf, format='xml')
result = graph.query(sparql_query)
for s, p, o in result:
print(s, p, o)
if s.endswith('#d10') or s.endswith('#bond40') or s.endswith('#six_ring-9') or s.endswith('#d10_12') or s.endswith('#d10_13'):
logging.error('This triple should not be in the results! => {0} {1} {2}.'.format(s, p, o))
我遇到了两个我想避免的错误:
ERROR:root:This triple should not be in the results! => http://dl-learner.org/carcinogenesis#six_ring-9 http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://dl-learner.org/carcinogenesis#Six_ring.
ERROR:root:This triple should not be in the results! => http://dl-learner.org/carcinogenesis#d10 http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://dl-learner.org/carcinogenesis#Compound.
我在添加我已经注意到的额外内容时找到了这个问题的答案。
因为 :d1 rdf:type :Compound
和 :d10 rdf:type :Compound
,实际上有一条从 :d1
到 :d10
的路径,因此关于 :d10
的三倍也在我的查询中结束结果 - 这当然不是我想要的。
查看查询,我认为添加 ?child
不能是 :d10
的附加约束是有意义的。而且,考虑到 的发帖人解释他的查询的方式,我还应该排除 ?someSubj
或 ?childPropVal
可以是 :d10
:
CONSTRUCT {
:d1 ?prop ?val .
?child ?childProp ?childPropVal .
?someSubj ?incomingChildProp ?child .
}
WHERE {
:d1 (:overrides|!:overrides)+ ?child .
?child ?childProp ?childPropVal.
?someSubj ?incomingChildProp ?child.
FILTER (?child != :d10)
FILTER (?childPropVal != :d10)
FILTER (?someSubj != :d10)
}
这从我的查询结果中删除了 :d10
。太棒了!
但错误 ERROR:root:This triple should not be in the results! => http://dl-learner.org/carcinogenesis#six_ring-9 http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://dl-learner.org/carcinogenesis#Six_ring.
仍然存在。
我尝试删除一些可能导致此问题的三元组,发现三元组 { Six_ring rdf:about="#six_ring-9" }
是查询结果中仍然有 #six_ring-9
的原因。因此,再次是 rdf:type
(隐含在 rdf:about
中)导致了问题。
理想情况下,我们需要在 SPARQL 中描述我们有兴趣知道链接到 :d1
的对象的 rdf:types
是什么(例如 :Compound
、:Six_Ring
) ,但不是其他对象也链接到这些对象类型。这将解决我们最初通过查询检测到的两个问题。
所以这个查询解决了这个问题:
CONSTRUCT {
:d1 ?prop ?val .
?child ?childProp ?childPropVal .
?someSubj ?incomingChildProp ?child .
}
WHERE {
:d1 (:overrides|!:overrides)+ ?child .
?child ?childProp ?childPropVal.
?someSubj ?incomingChildProp ?child.
FILTER (?incomingChildProp != rdf:type)
}
根据一个包含关于多种化合物的陈述的 RDF 文件,我想为每种化合物创建一个 RDF 文件。
为了做到这一点,我开始寻找一个 SPARQL 查询,它可以隔离所有链接到某个 URL 的三元组,无论中间有多少节点。
我从一个非常有趣的 SPARQL 查询 () 开始,这样它将 return(希望)所有三元组链接到我数据集中的化合物 :d1
,但是不是关于另一种化合物 :d10
:
CONSTRUCT {
:d1 ?prop ?val .
?child ?childProp ?childPropVal .
?someSubj ?incomingChildProp ?child .
}
WHERE {
:d1 ?prop ?val ;
(:overrides|!:overrides)+ ?child .
?child ?childProp ?childPropVal.
?someSubj ?incomingChildProp ?child.
}
但是,当我在我的简化测试用例 (python) 中 运行 时:
rdf = """<?xml version="1.0"?>
<!DOCTYPE rdf:RDF [
<!ENTITY owl "http://www.w3.org/2002/07/owl#" >
<!ENTITY owl11 "http://www.w3.org/2006/12/owl11#" >
<!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
<!ENTITY owl11xml "http://www.w3.org/2006/12/owl11-xml#" >
<!ENTITY carcinogenesis "http://dl-learner.org/carcinogenesis#" >
<!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
<!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
]>
<rdf:RDF xmlns="http://dl-learner.org/carcinogenesis#"
xml:base="http://dl-learner.org/carcinogenesis"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:owl11="http://www.w3.org/2006/12/owl11#"
xmlns:carcinogenesis="http://dl-learner.org/carcinogenesis#"
xmlns:owl11xml="http://www.w3.org/2006/12/owl11-xml#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<owl:Ontology rdf:about=""/>
<owl:ObjectProperty rdf:about="#hasAtom">
<rdfs:domain rdf:resource="#Compound"/>
<rdfs:range rdf:resource="#Atom"/>
</owl:ObjectProperty>
<owl:ObjectProperty rdf:about="#hasBond">
<rdfs:domain rdf:resource="#Compound"/>
<rdfs:range rdf:resource="#Bond"/>
</owl:ObjectProperty>
<owl:ObjectProperty rdf:about="#hasStructure">
<rdfs:domain rdf:resource="#Compound"/>
<rdfs:range rdf:resource="#Structure"/>
</owl:ObjectProperty>
<owl:ObjectProperty rdf:about="#inBond">
<rdfs:range rdf:resource="#Atom"/>
<rdfs:domain rdf:resource="#Bond"/>
</owl:ObjectProperty>
<owl:DatatypeProperty rdf:about="#charge">
<rdfs:domain rdf:resource="#Atom"/>
<rdfs:range rdf:resource="&xsd;double"/>
</owl:DatatypeProperty>
<owl:DatatypeProperty rdf:about="#isMutagenic">
<rdfs:domain rdf:resource="#Compound"/>
<rdfs:range rdf:resource="&xsd;boolean"/>
</owl:DatatypeProperty>
<owl:Class rdf:about="#Atom"/>
<owl:Class rdf:about="#Bond">
<owl:disjointWith rdf:resource="#Structure"/>
<owl:disjointWith rdf:resource="#Atom"/>
</owl:Class>
<owl:Class rdf:about="#Bond-7">
<rdfs:subClassOf rdf:resource="#Bond"/>
</owl:Class>
<owl:Class rdf:about="#Carbon-22">
<rdfs:subClassOf rdf:resource="#Carbon"/>
</owl:Class>
<owl:Class rdf:about="#Compound">
<owl:disjointWith rdf:resource="#Structure"/>
<owl:disjointWith rdf:resource="#Atom"/>
<owl:disjointWith rdf:resource="#Bond"/>
</owl:Class>
<owl:Class rdf:about="#Six_ring">
<rdfs:subClassOf rdf:resource="#Ring"/>
</owl:Class>
<owl:Class rdf:about="#Ring">
<rdfs:subClassOf rdf:resource="#Structure"/>
</owl:Class>
<owl:Class rdf:about="#Structure">
<owl:disjointWith rdf:resource="#Atom"/>
</owl:Class>
<Compound rdf:about="#d1">
<hasBond rdf:resource="#bond1"/>
<hasAtom rdf:resource="#d1_2"/>
<hasAtom rdf:resource="#d1_3"/>
<hasStructure rdf:resource="#six_ring-1"/>
<isMutagenic rdf:datatype="&xsd;boolean">false</isMutagenic>
</Compound>
<Bond-7 rdf:about="#bond1">
<inBond rdf:resource="#d1_3"/>
<inBond rdf:resource="#d1_2"/>
</Bond-7>
<Carbon-22 rdf:about="#d1_2">
<charge rdf:datatype="&xsd;double">-0.133</charge>
</Carbon-22>
<Carbon-22 rdf:about="#d1_3">
<charge rdf:datatype="&xsd;double">-0.0030</charge>
</Carbon-22>
<Six_ring rdf:about="#six_ring-1"/>
<Compound rdf:about="#d10">
<hasBond rdf:resource="#bond40"/>
<hasAtom rdf:resource="#d10_12"/>
<hasAtom rdf:resource="#d10_13"/>
<isMutagenic rdf:datatype="&xsd;boolean">false</isMutagenic>
<hasStructure rdf:resource="#six_ring-9"/>
</Compound>
<Bond-1 rdf:about="#bond40">
<inBond rdf:resource="#d10_12"/>
<inBond rdf:resource="#d10_13"/>
</Bond-1>
<Six_ring rdf:about="#six_ring-9"/>
<Nitrogen-32 rdf:about="#d10_12">
<charge rdf:datatype="&xsd;double">-0.313</charge>
</Nitrogen-32>
<Nitrogen-32 rdf:about="#d10_13">
<charge rdf:datatype="&xsd;double">-0.313</charge>
</Nitrogen-32>
</rdf:RDF>
"""
# Inspired by
sparql_query = """CONSTRUCT {
:d1 ?prop ?val .
?child ?childProp ?childPropVal .
?someSubj ?incomingChildProp ?child .
}
WHERE {
:d1 ?prop ?val ;
(:overrides|!:overrides)+ ?child .
?child ?childProp ?childPropVal.
?someSubj ?incomingChildProp ?child.
}
"""
# Trying this query out:
import rdflib
import logging
logger = logging.getLogger()
logger.setLevel("INFO")
graph = rdflib.Graph()
graph.parse(data=rdf, format='xml')
result = graph.query(sparql_query)
for s, p, o in result:
print(s, p, o)
if s.endswith('#d10') or s.endswith('#bond40') or s.endswith('#six_ring-9') or s.endswith('#d10_12') or s.endswith('#d10_13'):
logging.error('This triple should not be in the results! => {0} {1} {2}.'.format(s, p, o))
我遇到了两个我想避免的错误:
ERROR:root:This triple should not be in the results! => http://dl-learner.org/carcinogenesis#six_ring-9 http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://dl-learner.org/carcinogenesis#Six_ring.
ERROR:root:This triple should not be in the results! => http://dl-learner.org/carcinogenesis#d10 http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://dl-learner.org/carcinogenesis#Compound.
我在添加我已经注意到的额外内容时找到了这个问题的答案。
因为 :d1 rdf:type :Compound
和 :d10 rdf:type :Compound
,实际上有一条从 :d1
到 :d10
的路径,因此关于 :d10
的三倍也在我的查询中结束结果 - 这当然不是我想要的。
查看查询,我认为添加 ?child
不能是 :d10
的附加约束是有意义的。而且,考虑到 的发帖人解释他的查询的方式,我还应该排除 ?someSubj
或 ?childPropVal
可以是 :d10
:
CONSTRUCT {
:d1 ?prop ?val .
?child ?childProp ?childPropVal .
?someSubj ?incomingChildProp ?child .
}
WHERE {
:d1 (:overrides|!:overrides)+ ?child .
?child ?childProp ?childPropVal.
?someSubj ?incomingChildProp ?child.
FILTER (?child != :d10)
FILTER (?childPropVal != :d10)
FILTER (?someSubj != :d10)
}
这从我的查询结果中删除了 :d10
。太棒了!
但错误 ERROR:root:This triple should not be in the results! => http://dl-learner.org/carcinogenesis#six_ring-9 http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://dl-learner.org/carcinogenesis#Six_ring.
仍然存在。
我尝试删除一些可能导致此问题的三元组,发现三元组 { Six_ring rdf:about="#six_ring-9" }
是查询结果中仍然有 #six_ring-9
的原因。因此,再次是 rdf:type
(隐含在 rdf:about
中)导致了问题。
理想情况下,我们需要在 SPARQL 中描述我们有兴趣知道链接到 :d1
的对象的 rdf:types
是什么(例如 :Compound
、:Six_Ring
) ,但不是其他对象也链接到这些对象类型。这将解决我们最初通过查询检测到的两个问题。
所以这个查询解决了这个问题:
CONSTRUCT {
:d1 ?prop ?val .
?child ?childProp ?childPropVal .
?someSubj ?incomingChildProp ?child .
}
WHERE {
:d1 (:overrides|!:overrides)+ ?child .
?child ?childProp ?childPropVal.
?someSubj ?incomingChildProp ?child.
FILTER (?incomingChildProp != rdf:type)
}