在 rdflib 中使用上下文

发布于 2024-08-12 09:20:46 字数 118 浏览 4 评论 0原文

我无法找到一个清晰、合理的 rdflib 上下文使用示例。 ConjunctiveGraph 不接受上下文,并且 Graph 已被弃用。我应该如何在同一个全局 ConjunctiveGraph 中的不同上下文上创建和操作?

I am having trouble finding a clear, sensible example of usage of context with rdflib.
ConjunctiveGraph does not accept contexts, and Graph is deprecated. How am I supposed to create and operate on different contexts within the same global ConjunctiveGraph ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

就像说晚安 2024-08-19 09:20:46

是的。这是代码

import rdflib
from rdflib.Graph import Graph

conj=rdflib.ConjunctiveGraph()

NS=rdflib.Namespace("http://example.com/#")
NS_CTX=rdflib.Namespace("http://example.com/context/#")

alice=NS.alice
bob=NS.bob
charlie=NS.charlie

pizza=NS.pizza
meat=NS.meat
chocolate=NS.chocolate

loves=NS.loves
hates=NS.hates
likes=NS.likes
dislikes=NS.dislikes

love_ctx=Graph(conj.store, NS_CTX.love)
food_ctx=Graph(conj.store, NS_CTX.food)

love_ctx.add( (alice, loves, bob) )
love_ctx.add( (alice, loves, charlie) )
love_ctx.add( (bob, hates, charlie) )
love_ctx.add( (charlie, loves, bob) )

food_ctx.add( (alice, likes, chocolate) )
food_ctx.add( (alice, likes, meat) )
food_ctx.add( (alice, dislikes, pizza) )

print "Full context"
for t in conj:
    print t

print ""
print "Contexts"
for c in conj.contexts():
    print c

print "love context"
for t in love_ctx:
    print t

print "food context"
for t in food_ctx:
    print t

这是输出

Full context
(rdflib.URIRef('http://example.com/#bob'), rdflib.URIRef('http://example.com/#hates'), rdflib.URIRef('http://example.com/#charlie'))
(rdflib.URIRef('http://example.com/#alice'), rdflib.URIRef('http://example.com/#likes'), rdflib.URIRef('http://example.com/#chocolate'))
(rdflib.URIRef('http://example.com/#alice'), rdflib.URIRef('http://example.com/#likes'), rdflib.URIRef('http://example.com/#meat'))
(rdflib.URIRef('http://example.com/#alice'), rdflib.URIRef('http://example.com/#dislikes'), rdflib.URIRef('http://example.com/#pizza'))
(rdflib.URIRef('http://example.com/#alice'), rdflib.URIRef('http://example.com/#loves'), rdflib.URIRef('http://example.com/#bob'))
(rdflib.URIRef('http://example.com/#alice'), rdflib.URIRef('http://example.com/#loves'), rdflib.URIRef('http://example.com/#charlie'))
(rdflib.URIRef('http://example.com/#charlie'), rdflib.URIRef('http://example.com/#loves'), rdflib.URIRef('http://example.com/#bob'))

Contexts
<http://example.com/context/#food> a rdfg:Graph;rdflib:storage [a rdflib:Store;rdfs:label 'IOMemory'].
<http://example.com/context/#love> a rdfg:Graph;rdflib:storage [a rdflib:Store;rdfs:label 'IOMemory'].
love context
(rdflib.URIRef('http://example.com/#bob'), rdflib.URIRef('http://example.com/#hates'), rdflib.URIRef('http://example.com/#charlie'))
(rdflib.URIRef('http://example.com/#alice'), rdflib.URIRef('http://example.com/#loves'), rdflib.URIRef('http://example.com/#bob'))
(rdflib.URIRef('http://example.com/#alice'), rdflib.URIRef('http://example.com/#loves'), rdflib.URIRef('http://example.com/#charlie'))
(rdflib.URIRef('http://example.com/#charlie'), rdflib.URIRef('http://example.com/#loves'), rdflib.URIRef('http://example.com/#bob'))
food context
(rdflib.URIRef('http://example.com/#alice'), rdflib.URIRef('http://example.com/#likes'), rdflib.URIRef('http://example.com/#chocolate'))
(rdflib.URIRef('http://example.com/#alice'), rdflib.URIRef('http://example.com/#likes'), rdflib.URIRef('http://example.com/#meat'))
(rdflib.URIRef('http://example.com/#alice'), rdflib.URIRef('http://example.com/#dislikes'), rdflib.URIRef('http://example.com/#pizza'))

Yes. This is the code

import rdflib
from rdflib.Graph import Graph

conj=rdflib.ConjunctiveGraph()

NS=rdflib.Namespace("http://example.com/#")
NS_CTX=rdflib.Namespace("http://example.com/context/#")

alice=NS.alice
bob=NS.bob
charlie=NS.charlie

pizza=NS.pizza
meat=NS.meat
chocolate=NS.chocolate

loves=NS.loves
hates=NS.hates
likes=NS.likes
dislikes=NS.dislikes

love_ctx=Graph(conj.store, NS_CTX.love)
food_ctx=Graph(conj.store, NS_CTX.food)

love_ctx.add( (alice, loves, bob) )
love_ctx.add( (alice, loves, charlie) )
love_ctx.add( (bob, hates, charlie) )
love_ctx.add( (charlie, loves, bob) )

food_ctx.add( (alice, likes, chocolate) )
food_ctx.add( (alice, likes, meat) )
food_ctx.add( (alice, dislikes, pizza) )

print "Full context"
for t in conj:
    print t

print ""
print "Contexts"
for c in conj.contexts():
    print c

print "love context"
for t in love_ctx:
    print t

print "food context"
for t in food_ctx:
    print t

And this is the output

Full context
(rdflib.URIRef('http://example.com/#bob'), rdflib.URIRef('http://example.com/#hates'), rdflib.URIRef('http://example.com/#charlie'))
(rdflib.URIRef('http://example.com/#alice'), rdflib.URIRef('http://example.com/#likes'), rdflib.URIRef('http://example.com/#chocolate'))
(rdflib.URIRef('http://example.com/#alice'), rdflib.URIRef('http://example.com/#likes'), rdflib.URIRef('http://example.com/#meat'))
(rdflib.URIRef('http://example.com/#alice'), rdflib.URIRef('http://example.com/#dislikes'), rdflib.URIRef('http://example.com/#pizza'))
(rdflib.URIRef('http://example.com/#alice'), rdflib.URIRef('http://example.com/#loves'), rdflib.URIRef('http://example.com/#bob'))
(rdflib.URIRef('http://example.com/#alice'), rdflib.URIRef('http://example.com/#loves'), rdflib.URIRef('http://example.com/#charlie'))
(rdflib.URIRef('http://example.com/#charlie'), rdflib.URIRef('http://example.com/#loves'), rdflib.URIRef('http://example.com/#bob'))

Contexts
<http://example.com/context/#food> a rdfg:Graph;rdflib:storage [a rdflib:Store;rdfs:label 'IOMemory'].
<http://example.com/context/#love> a rdfg:Graph;rdflib:storage [a rdflib:Store;rdfs:label 'IOMemory'].
love context
(rdflib.URIRef('http://example.com/#bob'), rdflib.URIRef('http://example.com/#hates'), rdflib.URIRef('http://example.com/#charlie'))
(rdflib.URIRef('http://example.com/#alice'), rdflib.URIRef('http://example.com/#loves'), rdflib.URIRef('http://example.com/#bob'))
(rdflib.URIRef('http://example.com/#alice'), rdflib.URIRef('http://example.com/#loves'), rdflib.URIRef('http://example.com/#charlie'))
(rdflib.URIRef('http://example.com/#charlie'), rdflib.URIRef('http://example.com/#loves'), rdflib.URIRef('http://example.com/#bob'))
food context
(rdflib.URIRef('http://example.com/#alice'), rdflib.URIRef('http://example.com/#likes'), rdflib.URIRef('http://example.com/#chocolate'))
(rdflib.URIRef('http://example.com/#alice'), rdflib.URIRef('http://example.com/#likes'), rdflib.URIRef('http://example.com/#meat'))
(rdflib.URIRef('http://example.com/#alice'), rdflib.URIRef('http://example.com/#dislikes'), rdflib.URIRef('http://example.com/#pizza'))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文