本文整理汇总了Python中rdflib.graph.ConjunctiveGraph.commit方法的典型用法代码示例。如果您正苦于以下问题:Python ConjunctiveGraph.commit方法的具体用法?Python ConjunctiveGraph.commit怎么用?Python ConjunctiveGraph.commit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rdflib.graph.ConjunctiveGraph
的用法示例。
在下文中一共展示了ConjunctiveGraph.commit方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import commit [as 别名]
def main(fd, store_type=None, store_id=None, graph_id=None, gzipped=False):
"""
Converts MARC21 data stored in fd to a RDFlib graph.
"""
from rdflib import plugin
if store_type:
msg = "Need a {} identifier for a disk-based store."
assert store_id, msg.format('store')
assert graph_id, msg.format('graph')
store = plugin.get(store_type, Store)(store_id)
else:
store = 'default'
graph = Graph(store=store, identifier=graph_id)
try:
records = MARCReader(open(fd))
for i, triple in enumerate(process_records(records)):
graph.add(triple)
if i % 100 == 0:
graph.commit()
if i % 10000 == 0:
print i
finally:
graph.commit()
return graph
示例2: create_ontology
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import commit [as 别名]
def create_ontology(self,tr,predicate,subClass,address,booktitle):
LDT= Namespace("http://www.JceFinalProjectOntology.com/")
ut=Namespace("http://www.JceFinalProjectOntology.com/subject/#")
usubClass=URIRef("http://www.JceFinalProjectOntology.com/subject/"+subClass.strip()+'#')
#LDT.subClass=LDT[subClass]
print(ut)
print(usubClass)
store=IOMemory()
sty=LDT[predicate]
g = rdflib.Graph(store=store,identifier=LDT)
t = ConjunctiveGraph(store=store,identifier=ut)
print ('Triples in graph before add: ', len(t))
#g.add((LDT,RDF.type,RDFS.Class))
g.add((URIRef(LDT),RDF.type,RDFS.Class))
g.add((URIRef(LDT),RDFS.label,Literal("JFPO")))
g.add((URIRef(LDT),RDFS.comment,Literal('class of all properties')))
for v in self.symbols.values():
if self.if_compoTerm(v)==True:
vs=self.splitTerms(v)[0]
else:
vs =v
g.add((LDT[vs],RDF.type,RDF.Property))
g.add((LDT[vs],RDFS.label,Literal('has'+vs)))
g.add((LDT[vs],RDFS.comment,Literal(v)))
g.add((LDT[vs],RDFS.range,OWL.Class))
g.add((LDT[vs],RDFS.domain,Literal(vs)))
g.bind('JFPO',LDT)
#g.commit()
g.serialize('trtst.rdf',format='turtle')
t.add( (ut[tr], RDF.type,OWL.Class) )
t.add((ut[tr],RDFS.subClassOf,OWL.Thing))
t.add((ut[tr],RDFS.label,Literal(tr)))
t.add((ut[tr],DC.title,Literal(booktitle)))
t.add((ut[tr],DC.source,Literal(address)))
t.add((ut[tr],DC[predicate],URIRef(usubClass)))
t.add((ut[tr],LDT[predicate],RDF.Property))
t.add((ut[tr],DC[predicate],URIRef(usubClass)))
t.add((ut[tr],DC[predicate],URIRef(usubClass)))
relation='has'+predicate
t.add((ut[tr],LDT.term(predicate),URIRef(usubClass)))
t.add( (usubClass,RDF.type,OWL.Class))
t.add((usubClass,RDFS.subClassOf,OWL.Thing))
t.add((usubClass,RDFS.subClassOf,URIRef(sty)))
t.add((usubClass,RDFS.label,Literal(subClass)))
#tc=Graph(store=store,identifier=usubClass)
t.bind("dc", "http://http://purl.org/dc/elements/1.1/")
t.bind('JFPO',LDT)
t.commit()
#print(t.serialize(format='pretty-xml'))
t.serialize('test2.owl',format='turtle')
示例3: write_graph
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import commit [as 别名]
def write_graph(data_handle, out_handle, format='n3'):
graph = Graph()
count = 0
for record in generate_records(data_handle):
count += 1
if count % 1000:
sys.stderr.write(".")
else:
sys.stderr.write(str(count))
for triple in get_triples(record):
graph.add(triple)
graph.commit()
current_site = Site.objects.get_current()
domain = 'https://%s' % current_site.domain
out_handle.write(graph.serialize(format=format, base=domain, include_base=True))
return count
示例4: rdf_description
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import commit [as 别名]
def rdf_description(name, notation='xml' ):
"""
Funtion takes title of node, and rdf notation.
"""
valid_formats = ["xml", "n3", "ntriples", "trix"]
default_graph_uri = "http://gstudio.gnowledge.org/rdfstore"
configString = "/var/tmp/rdfstore"
# Get the Sleepycat plugin.
store = plugin.get('Sleepycat', Store)('rdfstore')
# Open previously created store, or create it if it doesn't exist yet
graph = Graph(store="Sleepycat",
identifier = URIRef(default_graph_uri))
path = mkdtemp()
rt = graph.open(path, create=False)
if rt == NO_STORE:
#There is no underlying Sleepycat infrastructure, create it
graph.open(path, create=True)
else:
assert rt == VALID_STORE, "The underlying store is corrupt"
# Now we'll add some triples to the graph & commit the changes
rdflib = Namespace('http://sbox.gnowledge.org/gstudio/')
graph.bind("gstudio", "http://gnowledge.org/")
exclusion_fields = ["id", "rght", "node_ptr_id", "image", "lft", "_state", "_altnames_cache", "_tags_cache", "nid_ptr_id", "_mptt_cached_fields"]
node=Objecttype.objects.get(title=name)
node_dict=node.__dict__
subject=str(node_dict['id'])
for key in node_dict:
if key not in exclusion_fields:
predicate=str(key)
pobject=str(node_dict[predicate])
graph.add((rdflib[subject], rdflib[predicate], Literal(pobject)))
graph.commit()
print graph.serialize(format=notation)
graph.close()
示例5: rdf_all
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import commit [as 别名]
#.........这里部分代码省略.........
graph.open(path, create=True)
else:
assert rt == VALID_STORE, "The underlying store is corrupt"
# Now we'll add some triples to the graph & commit the changes
graph.bind("gstudio", "http://gnowledge.org/")
exclusion_fields = ["id", "rght", "node_ptr_id", "image", "lft", "_state", "_altnames_cache", "_tags_cache", "nid_ptr_id", "_mptt_cached_fields"]
for node in NID.objects.all():
node_dict=node.ref.__dict__
node_type = node.reftype
try:
if (node_type=='Gbobject'):
node=Gbobject.objects.get(title=node)
elif (node_type=='None'):
node=Gbobject.objects.get(title=node)
elif (node_type=='Processes'):
node=Gbobject.objects.get(title=node)
elif (node_type=='System'):
node=Gbobject.objects.get(title=node)
rdflib=link(node)
url_addr=link1(node)
a=fstore_dump(url_addr)
elif (node_type=='Objecttype'):
node=Objecttype.objects.get(title=node)
elif (node_type=='Attributetype'):
node=Attributetype.objects.get(title=node)
elif (node_type=='Complement'):
node=Complement.objects.get(title=node)
elif (node_type=='Union'):
node=Union.objects.get(title=node)
elif (node_type=='Intersection'):
node=Intersection.objects.get(title=node)
elif (node_type=='Expression'):
node=Expression.objects.get(title=node)
elif (node_type=='Processtype'):
node=Processtype.objects.get(title=node)
elif (node_type=='Systemtype'):
node=Systemtype.objects.get(title=node)
elif (node_type=='AttributeSpecification'):
node=AttributeSpecification.objects.get(title=node)
elif (node_type=='RelationSpecification'):
node=RelationSpecification.objects.get(title=node)
rdflib=link(node)
url_addr=link1(node)
a=fstore_dump(url_addr)
if(node_type=='Attribute'):
node=Attribute.objects.get(title=node)
rdflib = Namespace('http://sbox.gnowledge.org/gstudio/')
elif(node_type=='Relationtype' ):
node=Relationtype.objects.get(title=node)
rdflib = Namespace('http://sbox.gnowledge.org/gstudio/')
elif(node_type=='Metatype'):
node=Metatype.objects.get(title=node)
rdflib = Namespace('http://sbox.gnowledge.org/gstudio/')
url_addr='http://sbox.gnowledge.org/gstudio/'
a=fstore_dump(url_addr)
except:
if(node_type=='Attribute'):
rdflib= Namespace('http://sbox.gnowledge.org/gstudio/')
if(node_type=='Relationtype' ):
rdflib= Namespace('http://sbox.gnowledge.org/gstudio/')
if(node_type=='Metatype'):
rdflib= Namespace('http://sbox.gnowledge.org/gstudio/')
subject=str(node_dict['id'])
for key in node_dict:
if key not in exclusion_fields:
predicate=str(key)
pobject=str(node_dict[predicate])
graph.add((rdflib[subject], rdflib[predicate], Literal(pobject)))
rdf_code=graph.serialize(format=notation)
#path to store the rdf in a file
#x = os.path.join(os.path.dirname(__file__), 'rdffiles.rdf')
graph.commit()
graph.close()
示例6: TestKyotoCabinetConjunctiveGraphCore
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import commit [as 别名]
class TestKyotoCabinetConjunctiveGraphCore(unittest.TestCase):
def setUp(self):
store = "KyotoCabinet"
self.graph = ConjunctiveGraph(store=store)
self.path = configString
self.graph.open(self.path, create=True)
def tearDown(self):
self.graph.destroy(self.path)
try:
self.graph.close()
except:
pass
if getattr(self, "path", False) and self.path is not None:
if os.path.exists(self.path):
if os.path.isdir(self.path):
for f in os.listdir(self.path):
os.unlink(self.path + "/" + f)
os.rmdir(self.path)
elif len(self.path.split(":")) == 1:
os.unlink(self.path)
else:
os.remove(self.path)
def test_namespaces(self):
self.graph.bind("dc", "http://http://purl.org/dc/elements/1.1/")
self.graph.bind("foaf", "http://xmlns.com/foaf/0.1/")
self.assert_(len(list(self.graph.namespaces())) == 5)
self.assert_(("foaf", rdflib.term.URIRef(u"http://xmlns.com/foaf/0.1/")) in list(self.graph.namespaces()))
def test_play_journal(self):
self.assertRaises(NotImplementedError, self.graph.store.play_journal, {"graph": self.graph})
def test_readable_index(self):
print(readable_index(111))
def test_triples_context_reset(self):
michel = rdflib.URIRef(u"michel")
likes = rdflib.URIRef(u"likes")
pizza = rdflib.URIRef(u"pizza")
cheese = rdflib.URIRef(u"cheese")
self.graph.add((michel, likes, pizza))
self.graph.add((michel, likes, cheese))
self.graph.commit()
ntriples = self.graph.triples((None, None, None), context=self.graph.store)
self.assert_(len(list(ntriples)) == 2)
def test_remove_context_reset(self):
michel = rdflib.URIRef(u"michel")
likes = rdflib.URIRef(u"likes")
pizza = rdflib.URIRef(u"pizza")
cheese = rdflib.URIRef(u"cheese")
self.graph.add((michel, likes, pizza))
self.graph.add((michel, likes, cheese))
self.graph.commit()
self.graph.store.remove((michel, likes, cheese), self.graph.store)
self.graph.commit()
self.assert_(len(list(self.graph.triples((None, None, None), context=self.graph.store))) == 1)
def test_remove_db_exception(self):
michel = rdflib.URIRef(u"michel")
likes = rdflib.URIRef(u"likes")
pizza = rdflib.URIRef(u"pizza")
cheese = rdflib.URIRef(u"cheese")
self.graph.add((michel, likes, pizza))
self.graph.add((michel, likes, cheese))
self.graph.commit()
self.graph.store.__len__(context=self.graph.store)
self.assert_(len(list(self.graph.triples((None, None, None), context=self.graph.store))) == 2)
示例7: Graph
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import commit [as 别名]
default_graph_uri = "http://id.southampton.ac.uk/dataset/places"
configString =
# Get the mysql plugin. You may have to install the python mysql libraries
store = plugin.get('MySQL', Store)('rdfstore')
# Open previously created store, or create it if it doesn't exist yet
rt = store.open(configString,create=False)
if rt == NO_STORE:
# There is no underlying MySQL infrastructure, create it
store.open(configString,create=True)
else:
assert rt == VALID_STORE,"There underlying store is corrupted"
# There is a store, use it
graph = Graph(store, identifier = URIRef(default_graph_uri))
print("Triples in graph before add: %s" % len(graph))
# Now we'll add some triples to the graph & commit the changes
rdflib = Namespace('http://rdflib.net/test/')
graph.add((rdflib['pic:1'], rdflib['name'], Literal('Jane & Bob')))
graph.add((rdflib['pic:2'], rdflib['name'], Literal('Squirrel in Tree')))
graph.commit()
print("Triples in graph after add: %" % len(graph))
# display the graph in RDF/XML
print(graph.serialize())
示例8: __init__
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import commit [as 别名]
class Project:
def __init__(self, uri, storePath):
if (uri[-1] != "/"):
raise SemPipeException("A Module must be a directory and its URI must end with a /")
self.n = URIRef(uri)
self.g = ConjunctiveGraph('IOMemory')
self.storePath = storePath
if storePath and os.path.exists(storePath+"/store.nquads"):
self.g.parse(storePath + "/store.nq", format='nquads')
self.confGraph = self.g.get_context(URIRef("sempipe:confgraph"))
#self.storePath = storePath
## Get the Sleepycat plugin.
#self.store = plugin.get('Sleepycat', Store)('rdfstore')
## Open previously created store, or create it if it doesn't exist yet
#self.g = ConjunctiveGraph(store="Sleepycat",
# identifier = URIRef(self.default_graph_uri))
##path = mkdtemp()
#rt = self.g.open(self.storePath, create=False)
#if rt == NO_STORE:
# # There is no underlying Sleepycat infrastructure, create it
# self.g.open(self.storePath, create=True)
#else:
# assert rt == VALID_STORE, "The underlying store is corrupt"
else:
#Aggregate graphs
self.confGraph = self.g.get_context(URIRef("sempipe:confgraph"))
self._loadconf()
for graph in self.confGraph.objects(self.n, semp.dataGraph):
self.loadData(graph)
for updateList in self.confGraph.objects(self.n, semp.update):
for updateInstruction in Collection(self.confGraph, updateList):
self.updateGraph(str(updateInstruction))
self.commit()
# Cache HostedSpaces
self.hostedSpaces = []
res = self.confGraph.query("""
SELECT ?baseURI ?mapTo ?index ?htaccess {
?baseURI a semp:HostedSpace ;
semp:mapTo ?mapTo ;
semp:mapIndexTo ?index ;
semp:mapHTAccessTo ?htaccess .
}
""", initNs={"semp": semp})
for s in res:
self.hostedSpaces.append(HostedSpace._make(s))
def __str__(self):
return str(self.n)
def __repr__(self):
return "{0}({1},{2})".format(self.__class__.__name__, repr(self.n), repr(self.storePath))
def _loadconf(self, uri=None):
"""Loads a graph and all config-graphs it references as configuration graphs
@param uri: a URIRef, defaults to self.n+SempPipe.conffilename"""
uri = uri or URIRef(self.n + conffilename)
if self.g.get_context(uri):
print("ConfGraph {} already in database".format(uri), file=sys.stderr)
return
print("Loading {} as config graph".format(uri), file=sys.stderr)
newgraph = self.g.parse(uri, format="n3")
self.confGraph += newgraph
self.confGraph.add((uri, rdf.type, semp.ConfGraph))
imports = set(newgraph.objects(uri, semp.confGraph))
imports |= set(newgraph.objects(self.n, semp.confGraph))
imports = filter(lambda x: not self.g.get_context(x), imports)
#Recursively load additional graphs
for imp in imports:
self._loadconf(imp)
def loadData(self, url):
"""Loads a data graph"""
return parse(self.g, url)
def updateGraph(self, sparql):
try:
self.g.update(sparql)
except:
raise SemPipeException("Update instruction failed:\n{}".format(str(sparql)))
def hostedSpace(self, resource, reverse=False):
"""Picks the best matching hostedSpace for the given resource.
If reverse is set, resource is considered to be a path
relative to the buildDir and the corresponding URIRef is
returned."""
if reverse:
hostedSpaces = filter(lambda s: resource.startswith(self.buildDir + s.mapTo), self.hostedSpaces)
else:
hostedSpaces = filter(lambda s: resource.startswith(s.baseURI), self.hostedSpaces)
# Find the best match, which is the most specific one:
try:
return max(hostedSpaces, key=lambda s: len(s.baseURI))
#.........这里部分代码省略.........
示例9: rdf_description
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import commit [as 别名]
#.........这里部分代码省略.........
"""
name='student'
valid_formats = ["xml", "n3", "ntriples", "trix"]
default_graph_uri = "http://gstudio.gnowledge.org/rdfstore"
# default_graph_uri = "http://example.com/"
configString = "/var/tmp/rdfstore"
# Get the Sleepycat plugin.
store = plugin.get('IOMemory', Store)('rdfstore')
# Open previously created store, or create it if it doesn't exist yet
graph = Graph(store="IOMemory",
identifier = URIRef(default_graph_uri))
path = mkdtemp()
rt = graph.open(path, create=False)
if rt == NO_STORE:
#There is no underlying Sleepycat infrastructure, create it
graph.open(path, create=True)
else:
assert rt == VALID_STORE, "The underlying store is corrupt"
# Now we'll add some triples to the graph & commit the changes
# rdflib = Namespace('http://sbox.gnowledge.org/gstudio/')
graph.bind("gstudio", "http://gnowledge.org/")
exclusion_fields = ["id", "rght", "node_ptr_id", "image", "lft", "_state", "_altnames_cache", "_tags_cache", "nid_ptr_id", "_mptt_cached_fields"]
node_type=get_nodetype()
if (node_type=='gbobject'):
node=Gbobject.objects.get(title=name)
elif (node_type=='objecttype'):
node=Objecttype.objects.get(title=name)
elif (node_type=='metatype'):
node=Metatype.objects.get(title=name)
elif (node_type=='attributetype'):
node=Attributetype.objects.get(title=name)
elif (node_type=='relationtype'):
node=Relationtype.objects.get(title=name)
elif (node_type=='attribute'):
node=Attribute.objects.get(title=name)
elif (node_type=='complement'):
node=Complement.objects.get(title=name)
elif (node_type=='union'):
node=Union.objects.get(title=name)
elif (node_type=='intersection'):
node=Intersection.objects.get(title=name)
elif (node_type=='expression'):
node=Expression.objects.get(title=name)
elif (node_type=='processtype'):
node=Processtype.objects.get(title=name)
elif (node_type=='systemtype'):
node=Systemtype.objects.get(title=name)
node_url=node.get_absolute_url()
site_add= node.sites.all()
a = site_add[0]
host_name =a.name
#host_name=name
link='http://'
#Concatenating the above variables will give the url address.
url_add=link+host_name+node_url
rdflib = Namespace(url_add)
# node=Objecttype.objects.get(title=name)
node_dict=node.__dict__
subject=str(node_dict['id'])
for key in node_dict:
if key not in exclusion_fields:
predicate=str(key)
pobject=str(node_dict[predicate])
graph.add((rdflib[subject], rdflib[predicate], Literal(pobject)))
rdf_code=graph.serialize(format=notation)
#graph = rdflib.Graph("IOMemory")
#graph.open("store", create=True)
#graph.parse(rdf_code)
# print out all the triples in the graph
# for subject, predicate, object in graph:
# print subject, predicate, object
# store.add(self,(subject, predicate, object),context)
graph.commit()
print rdf_code
graph.close()