简体   繁体   English

如何在不使用SPARQL的情况下使用rdflib模式匹配三元组

[英]how to pattern match triples using rdflib without using SPARQL

I want to pattern match triples of an existing RDF graph. 我想对现有RDF图的三元组进行模式匹配。 The triples() function of the RDFLib module looks like it could do the job, but I cant get it to work. RDFLib模块的Triples()函数看起来可以完成这项工作,但我无法使其正常工作。

Can someone provide an example? 有人可以提供例子吗?

Consider the following graph. 考虑下图。

@prefix ex: <http://example.com/family/> .

ex:Bob a ex:Person.
ex:Bob ex:Occupation ex:Doctor.
ex:Bob ex:Country ex:USA.
ex:Bob ex:Age ex:32.

I would like to pattern match all triples with subject ex:Bob. 我想用主题ex:Bob匹配所有三元组。

I have a hunch I am using the wrong function to begin with. 我有一种直觉,就是我使用了错误的功能。 Can someone explain? 有人可以解释吗? Thanks 谢谢

import rdflib
g = rdflib.Graph()
g.parse("bob.ttl", format="turtle")

bob = rdflib.URIRef("http://example.com/family/Bob")

for triple in g.triples( (bob, None, None) ):
    print(triple)

family = rdflib.Namespace('http://example.com/family/')
bob = family.Bob

for triple in g.triples( (bob, None, None) ):
    print(triple)

Note that triple patterns, as well as triples themselves, are Python tuples in RDFLib; 注意,三重模式以及三重模式本身是RDFLib中的Python元组。 hence one more pair of parentheses. 因此又要加上一对括号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM