2009-11-24 33 views
6

Tôi gặp khó khăn khi tìm một ví dụ rõ ràng, hợp lý về việc sử dụng ngữ cảnh với rdflib. ConjunctiveGraph không chấp nhận ngữ cảnh và Biểu đồ không được chấp nhận. Làm thế nào tôi có thể tạo ra và hoạt động trên các bối cảnh khác nhau trong cùng một ConjunctiveGraph toàn cầu?Sử dụng ngữ cảnh trong rdflib

+0

có lẽ tôi đã nhận nó ... chúng ta hãy xem nếu tôi có thể trả lời câu hỏi của riêng tôi .. –

Trả lời

11

Có. Đây là mã

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 

Và đây là sản phẩm

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')) 
Các vấn đề liên quan