2011-04-11 3 views

답변

3

당신이 찾고있는 그래프에 컨텍스트를 첨부하는 것입니다. 그것은 서브 그래프를 파싱하여 그래프를 작성하는 것과 같습니다. 각 서브 그래프에는 이름이 있습니다 (예 : URIRef, rdflib의 경우). dataB.nt

<http://data.org/inst1> <http://xmlns.com/foaf/0.1/knows> <http://data.org/inst2> . 
<http://data.org/inst2> <http://xmlns.com/foaf/0.1/knows> <http://data.org/inst3> . 
<http://data.org/inst3> <http://xmlns.com/foaf/0.1/knows> <http://data.org/inst1> . 

dataA.nt

<http://data.org/inst1> <http://xmlns.com/foaf/0.1/name> "david" . 
<http://data.org/inst2> <http://xmlns.com/foaf/0.1/name> "luis" . 
<http://data.org/inst3> <http://xmlns.com/foaf/0.1/name> "max" . 

그리고 다음 코드 조각 :

는 다음 두 파일로 표현 그래프에있는 상상

import rdflib 

g = rdflib.ConjunctiveGraph("IOMemory",) 

#g is made of two sub-graphs or triples gathered in two different contexts. 
#the second paramaters identifies the URIRef for each subgraph. 
g.parse("dataA.nt",rdflib.URIRef("http://mygraphs.org/names"),format="n3") 
g.parse("dataB.nt",rdflib.URIRef("http://mygraphs.org/relations"),format="n3") 

print "traverse all contexts and all triples for each context" 
for subgraph in g.contexts(): 
    print "Graph name",subgraph.identifier 
    for triple in subgraph.triples((None,None,None)): 
     print triple 

print "traverse all contexts where a triple appears" 
for subgraph in g.contexts(triple=(rdflib.URIRef('http://data.org/inst1'),rdflib.URIRef("http://xmlns.com/foaf/0.1/name"),rdflib.Literal(u'david'))): 
    print "Graph name",subgraph.identifier 
    for triple in subgraph.triples((None,None,None)): 
     print triple 

print "traverse a triple pattern regardless the context is in" 
for t in g.triples((None,rdflib.URIRef("http://xmlns.com/foaf/0.1/name"),None)): 
    print t