英文:
How to update RDF graph by instantiating the variables existing in triples with values?
问题
Here are the translations for the code-related parts of your text:
我有一个如下所示的知识库:
<!-- language: lang-none -->
@prefix ex: <http://example.org/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
ex:a a ex:C .
ex:C rdfs:subClassOf ex:D .
ex:D rdfs:subClassOf ex:E .
而且我有以下规则:
<!-- language: lang-none -->
s rdf.type X, X rdfs.subClassOf Y -> s rdf.type Y
其中三元组 `s type Y` 是结论,而三元组 `s type X`,`X subClassOf Y` 是前提。
## 我的目标
我有结论 `a type E`。我必须将结论与上述规则进行模式匹配并找到前提。在这个示例中,如果我模式匹配结论 `a type E`,那么前提应该如下所示:
<!-- language: lang-none -->
a type ?X, ?X subClassOf E.
在这种情况下,我尝试创建前提的图形,如下所示:
<!-- language: lang-none -->
@prefix ex: <http://example.org/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
ex:a rdf:type _:X .
_:X rdfs:subClassOf ex:E .
然后,我检查是否可能将前提与知识库的任何元素进行模式匹配(通过迭代处理前提)。在这个示例中,可以模式匹配第一个三元组 `ex.a rdf.type ?X`,因为 `a type ?X` 与知识库的元素 `a type C` 模式匹配。所以,`?X = C`。然后,我为前提中的所有 `?X` 实例化 `C`,其中三元组应更新如下:`a type C`,`C subClassOf E`。因此,我的更新后的前提图应如下所示:
<!-- language: lang-none -->
@prefix ex: <http://example.org/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
ex:a rdf:type ex:C .
ex:C rdfs:subClassOf ex:E .
## 到目前为止我的代码
我实现了以下方式来创建前提图的Python代码:
```python
consequent = (ex.a, rdf.type, ex.E)
# 为前提创建一个新的RDF图形
antecedent_graph = Graph()
def serialize_antecedent_graph():
# antecedent_graph.serialize() 返回一个字符串
print(antecedent_graph.serialize(format='turtle'))
antecedent_graph.bind('ex', ex)
antecedent_graph.bind('rdf', rdf)
antecedent_graph.bind('rdfs', rdfs)
# --- R5 ---
if (consequent[1].eq(URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"))):
X = BNode()
antecedent_graph.add((consequent[0], rdf.type, X))
antecedent_graph.add((X, rdfs.subClassOf, consequent[2]))
serialize_antecedent_graph()
我的问题
-
如何按照上述所示更新我的前提图?
-
目前我在前提图中引入变量的方式如下:
X = BNode()
antecedent_graph.add((consequent[0], rdf.type, X))
这种方法是否正确?是否有更高效和替代的方法?
Please note that I've provided translations for the code-related sections only. If you have any further questions or need assistance with the code, feel free to ask.
<details>
<summary>英文:</summary>
I have a knowledge base that looks as follows:
<!-- language: lang-none -->
@prefix ex: <http://example.org/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
ex:a a ex:C .
ex:C rdfs:subClassOf ex:D .
ex:D rdfs:subClassOf ex:E .
And I have the following rule:
<!-- language: lang-none -->
s rdf.type X, X rdfs.subClassOf Y -> s rdf.type Y
where the triple `s type Y` is a consequent and the triples `s type X`, `X subClassOf Y` are antecedents.
## My goal
I have the consequent `a type E`. I have to pattern match the consequent with the rule given above and find the antecedents. In this example, if I pattern match the consequent `a type E`, then the antecedents should look like this:
<!-- language: lang-none -->
a type ?X, ?X subClassOf E.
In this case, I try to make a graph for antecedents where it looks like the following:
<!-- language: lang-none -->
@prefix ex: <http://example.org/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
ex:a rdf:type _:X .
_:X rdfs:subClassOf ex:E .
Then I check if it possible to pattern match the antecedents with any element of KB (by iteratively going through the antecedents). In this example, it is possible to pattern match the first triple `ex.a rdf.type ?X` because `a type ?X` is pattern matched with the element of the KB `a type C`. So, `?X = C`. Then I instantiate all `?X` for all triples in the antecedents with `C` where the triples should be updated this way: `a type C`, `C subClassOf E`. Therefore, my updated antecedent graph should look like the following:
<!-- language: lang-none -->
@prefix ex: <http://example.org/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
ex:a rdf:type ex:C .
ex:C rdfs:subClassOf ex:E .
## My code so far
I implemented the Python code to create the antecedent graph the following way:
consequent = (ex.a, rdf.type, ex.E)
Create a new RDF graph for the antecedents
antecedent_graph = Graph()
def serialize_antecedent_graph():
# antecedent_graph.serialize() returns a string
print(antecedent_graph.serialize(format='turtle'))
antecedent_graph.bind('ex', ex)
antecedent_graph.bind('rdf', rdf)
antecedent_graph.bind('rdfs', rdfs)
--- R5 ---
if (consequent[1].eq(URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"))):
X = BNode()
antecedent_graph.add((consequent[0], rdf.type, X))
antecedent_graph.add((X, rdfs.subClassOf, consequent[2]))
serialize_antecedent_graph()
## My questions
1\. How can I update my antecedent graph in the way that is shown above?
2\. Currently I'm introducing variable in my antecedent graph the following way:
X = BNode()
antecedent_graph.add((consequent[0], rdf.type, X))
Is this approach correct? Is there an efficient and alternative way to do it?
</details>
# 答案1
**得分**: 1
The simplest way to achieve your goal is to use SPARQL for pattern matching.
```python
antecedents = knowledge_base.query("""
CONSTRUCT {
?s rdf:type ?x . ?x rdfs:subClassOf ?y
}
WHERE {
VALUES (?s ?y) {(%s %s)}
?s rdf:type ?x . ?x rdfs:subClassOf ?y
}
""" %(consequent[0].n3(), consequent[2].n3()) )
for antecedent in antecedents:
antecedent_graph.add(antecedent)
Note that multiple sets of antecedents are possible.
Your idea to use triples containing blank nodes as basic graph patterns is correct. In essence, blank nodes are variables. However, blank nodes cannot be predicates. It wouldn't be possible to analyze rules like this:
?s ?p ?o, ?p rdfs:domain ?c -> ?s rdf:type ?c
英文:
The simplest way to achieve your goal is to use SPARQL for pattern matching.
antecedents = knowledge_base.query("""
CONSTRUCT {
?s rdf:type ?x . ?x rdfs:subClassOf ?y
}
WHERE {
VALUES (?s ?y) {(%s %s)}
?s rdf:type ?x . ?x rdfs:subClassOf ?y
}
""" %(consequent[0].n3(), consequent[2].n3()) )
for antecedent in antecedents:
antecedent_graph.add(antecedent)
Note that multiple sets of antecedents are possible.
Your idea to use triples containing blank nodes as basic graph patterns is correct. In essense, blank nodes are variables. Howewer, blank nodes can not be predicates. It wouldn't be possible to analyse rules like this:
?s ?p ?o, ?p rdfs:domain ?c -> ?s rdf:type ?c
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论