Using contexts in rdflib(在rdflib中使用上下文)
本文介绍了在rdflib中使用上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很难找到一个清晰、合理的使用rdflib上下文的示例。 ConunctiveGraph不接受上下文,因此Graph已弃用。我应该如何在同一全局连接图中的不同上下文中创建和操作?
推荐答案
是。这是代码
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'))
这篇关于在rdflib中使用上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:在rdflib中使用上下文


基础教程推荐
猜你喜欢
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 包装空间模型 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01