本文整理汇总了Python中rdflib.Graph.commit方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.commit方法的具体用法?Python Graph.commit怎么用?Python Graph.commit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rdflib.Graph
的用法示例。
在下文中一共展示了Graph.commit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: DeepGraphStore
# 需要导入模块: from rdflib import Graph [as 别名]
# 或者: from rdflib.Graph import commit [as 别名]
class DeepGraphStore():
store_name = settings.DEEPGRAPHS_DEFAULT_STORAGE
def __init__(self, create=True):
self.create = create
self.path = "databases/" + random_file_generating()
def setUp(self):
self.graph = Graph(store=self.store_name)
self.graph.open(self.path, create=self.create)
if self.create:
self.graph.parse("http://njh.me/foaf.rdf", format='xml')
self.graph.commit()
def open(self, path):
self.graph = Graph(self.store_name).open(path, False)
return self.graph.__len__
def query(self, sparql_query):
return self.graph.query(sparql_query)
def parse(self, path_to_file_):
self.graph.parse(path_to_file_)
def load(self, triples):
self.graph.load(triples)
def close(self):
self.graph.close()
def size(self):
size = self.graph.__len__
self.close()
return size
示例2: CoreSQLiteStoreTestCase
# 需要导入模块: from rdflib import Graph [as 别名]
# 或者: from rdflib.Graph import commit [as 别名]
class CoreSQLiteStoreTestCase(unittest.TestCase):
"""
Test case for SQLite core.
"""
store = "SQLite"
path = None
storetest = True
def setUp(self):
self.graph = Graph(store=self.store)
fp, self.path = tempfile.mkstemp(suffix=".sqlite")
self.graph.open(self.path, create=True)
def tearDown(self):
# TODO: delete a_tmp_dir
self.graph.close()
del self.graph
if hasattr(self, "path") 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_escape_quoting(self):
test_string = "This's a Literal!!"
self.graph.add((URIRef("http://example.org/foo"), RDFS.label, Literal(test_string, datatype=XSD.string)))
self.graph.commit()
assert b("This's a Literal!!") in self.graph.serialize(format="xml")
示例3: TestAuditableStoreEmbeded
# 需要导入模块: from rdflib import Graph [as 别名]
# 或者: from rdflib.Graph import commit [as 别名]
class TestAuditableStoreEmbeded(BaseTestAuditableStore):
def setUp(self):
self.g = Graph()
self.g.add((EX.s0, EX.p0, EX.o0))
self.g.add((EX.s0, EX.p0, EX.o0bis))
self.t1 = Graph(AuditableStore(self.g.store),
self.g.identifier)
self.t1.add((EX.s1, EX.p1, EX.o1))
self.t1.remove((EX.s0, EX.p0, EX.o0bis))
self.t2 = Graph(AuditableStore(self.t1.store),
self.t1.identifier)
self.t2.add((EX.s2, EX.p2, EX.o2))
self.t2.remove((EX.s1, EX.p1, EX.o1))
def test_commit_commit(self):
self.assert_graph_equal(self.t2, [
(EX.s0, EX.p0, EX.o0),
(EX.s2, EX.p2, EX.o2),
])
self.t2.commit()
self.assert_graph_equal(self.t1, [
(EX.s0, EX.p0, EX.o0),
(EX.s2, EX.p2, EX.o2),
])
self.t1.commit()
self.assert_graph_equal(self.g, [
(EX.s0, EX.p0, EX.o0),
(EX.s2, EX.p2, EX.o2),
])
def test_commit_rollback(self):
self.t2.commit()
self.t1.rollback()
self.assert_graph_equal(self.g, [
(EX.s0, EX.p0, EX.o0),
(EX.s0, EX.p0, EX.o0bis),
])
def test_rollback_commit(self):
self.t2.rollback()
self.assert_graph_equal(self.t1, [
(EX.s0, EX.p0, EX.o0),
(EX.s1, EX.p1, EX.o1),
])
self.t1.commit()
self.assert_graph_equal(self.g, [
(EX.s0, EX.p0, EX.o0),
(EX.s1, EX.p1, EX.o1),
])
def test_rollback_rollback(self):
self.t2.rollback()
self.t1.rollback()
self.assert_graph_equal(self.g, [
(EX.s0, EX.p0, EX.o0),
(EX.s0, EX.p0, EX.o0bis),
])
示例4: removeFromGraph
# 需要导入模块: from rdflib import Graph [as 别名]
# 或者: from rdflib.Graph import commit [as 别名]
def removeFromGraph(timestamp, graphURI = "http://example.com/g1", db_conf={"dbname" : "postgres", "user" : "postgres", "password" : "admin" }):
configString = ("dbname=postgres user=waccess password=write")
#configString = ("dbname=" + db_conf['dbname'] + "user="+ db_conf['user'] + " password=" + db_conf['password'])
graph = Graph('PostgreSQL', identifier=URIRef(graphURI))
graph.open(configString, create=False)
results = graph.query(queries.getEvents(timestamp))
print len(results)
for result in results:
for node in result:
graph.remove((node, None, None))
# Commit and close the graph
graph.commit()
graph.close()
示例5: TestAuditableStoreEmptyGraph
# 需要导入模块: from rdflib import Graph [as 别名]
# 或者: from rdflib.Graph import commit [as 别名]
class TestAuditableStoreEmptyGraph(BaseTestAuditableStore):
def setUp(self):
self.g = Graph()
self.t = Graph(AuditableStore(self.g.store),
self.g.identifier)
def test_add_commit(self):
self.t.add((EX.s1, EX.p1, EX.o1))
self.assert_graph_equal(self.t, [
(EX.s1, EX.p1, EX.o1),
])
self.t.commit()
self.assert_graph_equal(self.g, [
(EX.s1, EX.p1, EX.o1),
])
def test_add_rollback(self):
self.t.add((EX.s1, EX.p1, EX.o1))
self.t.rollback()
self.assert_graph_equal(self.g, [
])
示例6: GraphTestCase
# 需要导入模块: from rdflib import Graph [as 别名]
# 或者: from rdflib.Graph import commit [as 别名]
class GraphTestCase(unittest.TestCase):
store_name = 'default'
path = None
storetest = True
create = True
michel = URIRef(u'michel')
tarek = URIRef(u'tarek')
bob = URIRef(u'bob')
likes = URIRef(u'likes')
hates = URIRef(u'hates')
pizza = URIRef(u'pizza')
cheese = URIRef(u'cheese')
def setUp(self):
store = plugin.get('MySQL', Store)(identifier="rdflib_test")
store.destroy(self.path)
store.open(self.path, create=True)
self.graph = Graph(store)
self.graph.destroy(self.path)
self.graph.open(self.path, create=self.create)
def tearDown(self):
self.graph.destroy(self.path)
self.graph.close()
def addStuff(self):
tarek = self.tarek
michel = self.michel
bob = self.bob
likes = self.likes
hates = self.hates
pizza = self.pizza
cheese = self.cheese
self.graph.add((tarek, likes, pizza))
self.graph.add((tarek, likes, cheese))
self.graph.add((michel, likes, pizza))
self.graph.add((michel, likes, cheese))
self.graph.add((bob, likes, cheese))
self.graph.add((bob, hates, pizza))
self.graph.add((bob, hates, michel)) # gasp!
self.graph.commit()
def removeStuff(self):
tarek = self.tarek
michel = self.michel
bob = self.bob
likes = self.likes
hates = self.hates
pizza = self.pizza
cheese = self.cheese
self.graph.remove((tarek, likes, pizza))
self.graph.remove((tarek, likes, cheese))
self.graph.remove((michel, likes, pizza))
self.graph.remove((michel, likes, cheese))
self.graph.remove((bob, likes, cheese))
self.graph.remove((bob, hates, pizza))
self.graph.remove((bob, hates, michel)) # gasp!
def testAdd(self):
self.addStuff()
def testRemove(self):
self.addStuff()
self.removeStuff()
def testTriples(self):
tarek = self.tarek
michel = self.michel
bob = self.bob
likes = self.likes
hates = self.hates
pizza = self.pizza
cheese = self.cheese
asserte = self.assertEquals
triples = self.graph.triples
Any = None
self.addStuff()
# unbound subjects
asserte(len(list(triples((Any, likes, pizza)))), 2)
asserte(len(list(triples((Any, hates, pizza)))), 1)
asserte(len(list(triples((Any, likes, cheese)))), 3)
asserte(len(list(triples((Any, hates, cheese)))), 0)
# unbound objects
asserte(len(list(triples((michel, likes, Any)))), 2)
asserte(len(list(triples((tarek, likes, Any)))), 2)
asserte(len(list(triples((bob, hates, Any)))), 2)
asserte(len(list(triples((bob, likes, Any)))), 1)
# unbound predicates
asserte(len(list(triples((michel, Any, cheese)))), 1)
asserte(len(list(triples((tarek, Any, cheese)))), 1)
asserte(len(list(triples((bob, Any, pizza)))), 1)
asserte(len(list(triples((bob, Any, michel)))), 1)
# unbound subject, objects
#.........这里部分代码省略.........
示例7: GraphTestCase
# 需要导入模块: from rdflib import Graph [as 别名]
# 或者: from rdflib.Graph import commit [as 别名]
class GraphTestCase(unittest.TestCase):
store_name = 'default'
path = None
storetest = True
create = True
michel = URIRef(u'michel')
tarek = URIRef(u'tarek')
bob = URIRef(u'bob')
likes = URIRef(u'likes')
hates = URIRef(u'hates')
pizza = URIRef(u'pizza')
cheese = URIRef(u'cheese')
def setUp(self):
self.graph = Graph(store=self.store_name)
self.graph.destroy(self.path)
if isinstance(self.path, type(None)):
if self.store_name == "SQLite":
self.path = mkstemp(prefix='test',dir='/tmp')
else:
self.path = mkdtemp(prefix='test',dir='/tmp')
self.graph.open(self.path, create=self.create)
def tearDown(self):
self.graph.destroy(self.path)
try:
self.graph.close()
except:
pass
import os
if hasattr(self,'path') 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 addStuff(self):
tarek = self.tarek
michel = self.michel
bob = self.bob
likes = self.likes
hates = self.hates
pizza = self.pizza
cheese = self.cheese
self.graph.add((tarek, likes, pizza))
self.graph.add((tarek, likes, cheese))
self.graph.add((michel, likes, pizza))
self.graph.add((michel, likes, cheese))
self.graph.add((bob, likes, cheese))
self.graph.add((bob, hates, pizza))
self.graph.add((bob, hates, michel)) # gasp!
self.graph.commit()
def removeStuff(self):
tarek = self.tarek
michel = self.michel
bob = self.bob
likes = self.likes
hates = self.hates
pizza = self.pizza
cheese = self.cheese
self.graph.remove((tarek, likes, pizza))
self.graph.remove((tarek, likes, cheese))
self.graph.remove((michel, likes, pizza))
self.graph.remove((michel, likes, cheese))
self.graph.remove((bob, likes, cheese))
self.graph.remove((bob, hates, pizza))
self.graph.remove((bob, hates, michel)) # gasp!
def testAdd(self):
self.addStuff()
def testRemove(self):
self.addStuff()
self.removeStuff()
def testTriples(self):
tarek = self.tarek
michel = self.michel
bob = self.bob
likes = self.likes
hates = self.hates
pizza = self.pizza
cheese = self.cheese
asserte = self.assertEquals
triples = self.graph.triples
Any = None
self.addStuff()
# unbound subjects
asserte(len(list(triples((Any, likes, pizza)))), 2)
asserte(len(list(triples((Any, hates, pizza)))), 1)
asserte(len(list(triples((Any, likes, cheese)))), 3)
#.........这里部分代码省略.........
示例8: GraphTestCase
# 需要导入模块: from rdflib import Graph [as 别名]
# 或者: from rdflib.Graph import commit [as 别名]
class GraphTestCase(unittest.TestCase):
store_name = 'default'
path = None
storetest = True
create = True
michel = URIRef(u'michel')
tarek = URIRef(u'tarek')
bob = URIRef(u'bob')
likes = URIRef(u'likes')
hates = URIRef(u'hates')
pizza = URIRef(u'pizza')
cheese = URIRef(u'cheese')
def setUp(self):
self.graph = Graph(store=self.store_name)
self.graph.destroy(self.path)
if isinstance(self.path, type(None)):
if self.store_name == "SQLite":
self.path = mkstemp(prefix='test', dir='/tmp')
else:
self.path = mkdtemp(prefix='test', dir='/tmp')
self.graph.open(self.path, create=self.create)
def tearDown(self):
self.graph.destroy(self.path)
try:
self.graph.close()
except:
pass
import os
if hasattr(self, 'path') and self.path is not None:
if os.path.exists(self.path):
if os.path.isdir(self.path):
shutil.rmtree(self.path)
elif len(self.path.split(':')) == 1:
os.unlink(self.path)
else:
os.remove(self.path)
def addStuff(self):
tarek = self.tarek
michel = self.michel
bob = self.bob
likes = self.likes
hates = self.hates
pizza = self.pizza
cheese = self.cheese
self.graph.add((tarek, likes, pizza))
self.graph.add((tarek, likes, cheese))
self.graph.add((michel, likes, pizza))
self.graph.add((michel, likes, cheese))
self.graph.add((bob, likes, cheese))
self.graph.add((bob, hates, pizza))
self.graph.add((bob, hates, michel)) # gasp!
self.graph.commit()
def removeStuff(self):
tarek = self.tarek
michel = self.michel
bob = self.bob
likes = self.likes
hates = self.hates
pizza = self.pizza
cheese = self.cheese
self.graph.remove((tarek, likes, pizza))
self.graph.remove((tarek, likes, cheese))
self.graph.remove((michel, likes, pizza))
self.graph.remove((michel, likes, cheese))
self.graph.remove((bob, likes, cheese))
self.graph.remove((bob, hates, pizza))
self.graph.remove((bob, hates, michel)) # gasp!
def testAdd(self):
self.addStuff()
def testRemove(self):
self.addStuff()
self.removeStuff()
def testTriples(self):
tarek = self.tarek
michel = self.michel
bob = self.bob
likes = self.likes
hates = self.hates
pizza = self.pizza
cheese = self.cheese
asserte = self.assertEquals
triples = self.graph.triples
Any = None
self.addStuff()
# unbound subjects
asserte(len(list(triples((Any, likes, pizza)))), 2)
asserte(len(list(triples((Any, hates, pizza)))), 1)
asserte(len(list(triples((Any, likes, cheese)))), 3)
asserte(len(list(triples((Any, hates, cheese)))), 0)
#.........这里部分代码省略.........
示例9: TestAuditableStore
# 需要导入模块: from rdflib import Graph [as 别名]
# 或者: from rdflib.Graph import commit [as 别名]
class TestAuditableStore(BaseTestAuditableStore):
def setUp(self):
self.g = Graph()
self.g.add((EX.s0, EX.p0, EX.o0))
self.g.add((EX.s0, EX.p0, EX.o0bis))
self.t = Graph(AuditableStore(self.g.store),
self.g.identifier)
def test_add_commit(self):
self.t.add((EX.s1, EX.p1, EX.o1))
self.assert_graph_equal(self.t, [
(EX.s0, EX.p0, EX.o0),
(EX.s0, EX.p0, EX.o0bis),
(EX.s1, EX.p1, EX.o1),
])
self.t.commit()
self.assert_graph_equal(self.g, [
(EX.s0, EX.p0, EX.o0),
(EX.s0, EX.p0, EX.o0bis),
(EX.s1, EX.p1, EX.o1),
])
def test_remove_commit(self):
self.t.remove((EX.s0, EX.p0, EX.o0))
self.assert_graph_equal(self.t, [
(EX.s0, EX.p0, EX.o0bis),
])
self.t.commit()
self.assert_graph_equal(self.g, [
(EX.s0, EX.p0, EX.o0bis),
])
def test_multiple_remove_commit(self):
self.t.remove((EX.s0, EX.p0, None))
self.assert_graph_equal(self.t, [
])
self.t.commit()
self.assert_graph_equal(self.g, [
])
def test_noop_add_commit(self):
self.t.add((EX.s0, EX.p0, EX.o0))
self.assert_graph_equal(self.t, [
(EX.s0, EX.p0, EX.o0),
(EX.s0, EX.p0, EX.o0bis),
])
self.t.commit()
self.assert_graph_equal(self.g, [
(EX.s0, EX.p0, EX.o0),
(EX.s0, EX.p0, EX.o0bis),
])
def test_noop_remove_commit(self):
self.t.add((EX.s0, EX.p0, EX.o0))
self.assert_graph_equal(self.t, [
(EX.s0, EX.p0, EX.o0),
(EX.s0, EX.p0, EX.o0bis),
])
self.t.commit()
self.assert_graph_equal(self.g, [
(EX.s0, EX.p0, EX.o0),
(EX.s0, EX.p0, EX.o0bis),
])
def test_add_remove_commit(self):
self.t.add((EX.s1, EX.p1, EX.o1))
self.t.remove((EX.s1, EX.p1, EX.o1))
self.assert_graph_equal(self.t, [
(EX.s0, EX.p0, EX.o0),
(EX.s0, EX.p0, EX.o0bis),
])
self.t.commit()
self.assert_graph_equal(self.g, [
(EX.s0, EX.p0, EX.o0),
(EX.s0, EX.p0, EX.o0bis),
])
def test_remove_add_commit(self):
self.t.remove((EX.s1, EX.p1, EX.o1))
self.t.add((EX.s1, EX.p1, EX.o1))
self.assert_graph_equal(self.t, [
(EX.s0, EX.p0, EX.o0),
(EX.s0, EX.p0, EX.o0bis),
(EX.s1, EX.p1, EX.o1),
])
self.t.commit()
self.assert_graph_equal(self.g, [
(EX.s0, EX.p0, EX.o0),
(EX.s0, EX.p0, EX.o0bis),
(EX.s1, EX.p1, EX.o1),
])
def test_add_rollback(self):
self.t.add((EX.s1, EX.p1, EX.o1))
self.t.rollback()
self.assert_graph_equal(self.g, [
(EX.s0, EX.p0, EX.o0),
(EX.s0, EX.p0, EX.o0bis),
#.........这里部分代码省略.........
示例10: Graph
# 需要导入模块: from rdflib import Graph [as 别名]
# 或者: from rdflib.Graph import commit [as 别名]
import rdflib
#from rdflib.graph import ConjunctiveGraph as Graph
from rdflib import Graph
configString = "/tmp/rdfstore12"
graph = Graph(store="KyotoCabinet")
path = configString
rt = graph.open(path, create=True)
print "Triples in graph before add: ", len(graph)
graph.parse("http://130.88.198.11/co-ode-files/ontologies/pizza.owl", format="xml")
graph.parse("20140114.rdf", format="xml")
graph.commit()
print "Triples in graph after add: ", len(graph)
graph.close()
示例11: addToGraph
# 需要导入模块: from rdflib import Graph [as 别名]
# 或者: from rdflib.Graph import commit [as 别名]
def addToGraph(event, graphURI = "http://example.com/g1", db_conf={"dbname" : "postgres", "user" : "postgres", "password" : "admin" }):
#configString = ("dbname=postgres user=waccess password=write")
configString = ("dbname=" + db_conf['dbname'] + " user="+ db_conf['user'] + " password=" + db_conf['password'])
#print configString
graph = Graph('PostgreSQL', identifier=URIRef(graphURI))
graph.open(configString, create=False)
graph.bind('ssn', SSN)
graph.bind('geo', GEO)
graph.bind('dul', DUL)
observation = BNode();
oTime = BNode();
# Observation
graph.add((observation, RDF.type, SSN.Observation))
graph.add((oTime, RDF.type, DUL.TimeInterval))
graph.add((observation, SSN.observationSamplingTime, oTime))
# Time
date = parser.parse(event['pickup_datetime'])
t = Literal(date.strftime("%Y-%m-%dT%H:%M:%S"), datatype=XSD.dateTime)
graph.add((oTime, DUL.hasRegionDataValue, t))
# SensorOutput
sensorOutput = BNode();
graph.add((sensorOutput, RDF.type, SSN.SensorOutput))
graph.add((observation, SSN.observationResult, sensorOutput))
# ObservationValue
observationValue = BNode()
startLocation = BNode()
endLocation = BNode()
graph.add((observationValue, RDF.type, SSN.ObservationValue))
graph.add((sensorOutput, SSN.hasValue, observationValue))
# Start and End Location
graph.add((observationValue, SSN.hasStartLocation, startLocation))
graph.add((observationValue, SSN.hasEndLocation, endLocation))
graph.add((startLocation, RDF.type, GEO.location))
graph.add((endLocation, RDF.type, GEO.location))
# Start Location
lat = Literal(event['pickup_latitude'], datatype=XSD.float)
long = Literal(event['pickup_longitude'], datatype=XSD.float)
# Adding the start location
graph.add((startLocation, GEO.lat, lat))
graph.add((startLocation, GEO.long, long))
# End Location
lat = Literal(event['dropoff_latitude'], datatype=XSD.float)
long = Literal(event['dropoff_longitude'], datatype=XSD.float)
# Adding the start location
graph.add((endLocation, GEO.lat, lat))
graph.add((endLocation, GEO.long, long))
#Duration
date1 = parser.parse(event['dropoff_datetime'])
date2 = parser.parse(event['pickup_datetime'])
dur = date1 - date2
duration = Literal(str(dur), datatype=XSD.float)
graph.add((observation, SSN.hasDuration, duration))
#print str(graph.__len__() / 11)
#Commit and close the graph
graph.commit()
graph.close()
示例12: int
# 需要导入模块: from rdflib import Graph [as 别名]
# 或者: from rdflib.Graph import commit [as 别名]
k=-1
brands =[]
playcounts = []
for row in g.query(query):
print "brand %s played artist %s times<br>" % row
k = k+1
t = int(row[1])
brands.append((row[0],t))
brands.sort(key=operator.itemgetter(1))
brands.reverse()
for (brand,pc) in brands:
#print "brand %s played artist %s times" % (brand,pc)
#print brand
g.load(fix_uri(brand))
g.commit()
q = "SELECT ?e WHERE {<%s> <http://purl.org/ontology/po/episode> ?e.}" % brand
#print q
for e in g.query(q):
#print e
g.load(fix_uri(e[0]))
g.commit()
q2 = "SELECT ?v WHERE {<%s> <http://purl.org/ontology/po/version> ?v.}" % e
for v in g.query(q2):
#print v
g.load(fix_uri(v[0]))
g.commit()
if service==None:
q3 = "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> SELECT ?start ?dur ?service WHERE {?broadcast <http://purl.org/ontology/po/broadcast_of> <%s>; <http://purl.org/NET/c4dm/event.owl#time> ?time. ?time <http://purl.org/NET/c4dm/timeline.owl#start> ?start; <http://purl.org/NET/c4dm/timeline.owl#duration> ?dur. ?broadcast <http://purl.org/ontology/po/broadcasted_on> ?service.}" % v
else :
q3 = "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> SELECT ?start ?dur WHERE {?broadcast <http://purl.org/ontology/po/broadcast_of> <%s>; <http://purl.org/NET/c4dm/event.owl#time> ?time. ?time <http://purl.org/NET/c4dm/timeline.owl#start> ?start; <http://purl.org/NET/c4dm/timeline.owl#duration> ?dur. ?broadcast <http://purl.org/ontology/po/broadcasted_on> <http://bbc-programmes.dyndns.org/%s>.}" % (v[0],service)
示例13: StoreTestCase
# 需要导入模块: from rdflib import Graph [as 别名]
# 或者: from rdflib.Graph import commit [as 别名]
class StoreTestCase(unittest.TestCase):
"""
Test case for testing store performance... probably should be
something other than a unit test... but for now we'll add it as a
unit test.
"""
store = 'IOMemory'
path = None
storetest = True
performancetest = True
def setUp(self):
self.gcold = gc.isenabled()
gc.collect()
gc.disable()
self.graph = Graph(store=self.store, identifier='main')
if not self.path:
path = mkdtemp()
else:
path = self.path
self.path = path
self.exists=exists=os.path.exists(path)
print ("Exists:", exists, path)
self.graph.open(self.path, create=not exists)
self.input = Graph()
def tearDown(self):
self.graph.close()
if self.gcold:
gc.enable()
# TODO: delete a_tmp_dir
self.graph.close()
"""
del self.graph
if hasattr(self,'path') 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 testTime(self):
# number = 1
if self.exists:
print("Graph does exist. Do not import anything.")
else:
print('"%s": [' % self.store)
for i in ['500triples', '1ktriples', '2ktriples',
'3ktriples', '5ktriples', '10ktriples',
'25ktriples']:
inputloc = os.getcwd()+'/sp2b/%s.n3' % i
res = self._testInput(inputloc)
print("%s," % res.strip())
print("],")
self.graph.commit()
cnt=0
for s,r,o in self.graph:
cnt+=1
print("Count of triples:%d" % cnt)
def _testInput(self, inputloc):
number = 1
store = self.graph
self.input.parse(location=inputloc, format="n3")
def add_from_input():
for t in self.input:
store.add(t)
it = itertools.repeat(None, number)
t0 = time()
for _i in it:
add_from_input()
t1 = time()
return "%.3g " % (t1 - t0)
示例14: GraphTestCase
# 需要导入模块: from rdflib import Graph [as 别名]
# 或者: from rdflib.Graph import commit [as 别名]
class GraphTestCase(unittest.TestCase):
storetest = True
identifier = URIRef("rdflib_test")
michel = URIRef(u"michel")
tarek = URIRef(u"tarek")
bob = URIRef(u"bob")
likes = URIRef(u"likes")
hates = URIRef(u"hates")
pizza = URIRef(u"pizza")
cheese = URIRef(u"cheese")
def setUp(self, uri="sqlite://", storename=None):
store = plugin.get(storename, Store)(identifier=self.identifier)
self.graph = Graph(store, identifier=self.identifier)
self.graph.open(uri, create=True)
def tearDown(self, uri="sqlite://"):
self.graph.destroy(uri)
try:
self.graph.close()
except:
pass
def addStuff(self):
tarek = self.tarek
michel = self.michel
bob = self.bob
likes = self.likes
hates = self.hates
pizza = self.pizza
cheese = self.cheese
self.graph.add((tarek, likes, pizza))
self.graph.add((tarek, likes, cheese))
self.graph.add((michel, likes, pizza))
self.graph.add((michel, likes, cheese))
self.graph.add((bob, likes, cheese))
self.graph.add((bob, hates, pizza))
self.graph.add((bob, hates, michel)) # gasp!
self.graph.commit()
def removeStuff(self):
tarek = self.tarek
michel = self.michel
bob = self.bob
likes = self.likes
hates = self.hates
pizza = self.pizza
cheese = self.cheese
self.graph.remove((tarek, likes, pizza))
self.graph.remove((tarek, likes, cheese))
self.graph.remove((michel, likes, pizza))
self.graph.remove((michel, likes, cheese))
self.graph.remove((bob, likes, cheese))
self.graph.remove((bob, hates, pizza))
self.graph.remove((bob, hates, michel)) # gasp!
def testAdd(self):
self.addStuff()
def testRemove(self):
self.addStuff()
self.removeStuff()
def testTriples(self):
tarek = self.tarek
michel = self.michel
bob = self.bob
likes = self.likes
hates = self.hates
pizza = self.pizza
cheese = self.cheese
asserte = self.assertEquals
triples = self.graph.triples
Any = None
self.addStuff()
# unbound subjects
asserte(len(list(triples((Any, likes, pizza)))), 2)
asserte(len(list(triples((Any, hates, pizza)))), 1)
asserte(len(list(triples((Any, likes, cheese)))), 3)
asserte(len(list(triples((Any, hates, cheese)))), 0)
# unbound objects
asserte(len(list(triples((michel, likes, Any)))), 2)
asserte(len(list(triples((tarek, likes, Any)))), 2)
asserte(len(list(triples((bob, hates, Any)))), 2)
asserte(len(list(triples((bob, likes, Any)))), 1)
# unbound predicates
asserte(len(list(triples((michel, Any, cheese)))), 1)
asserte(len(list(triples((tarek, Any, cheese)))), 1)
asserte(len(list(triples((bob, Any, pizza)))), 1)
asserte(len(list(triples((bob, Any, michel)))), 1)
# unbound subject, objects
asserte(len(list(triples((Any, hates, Any)))), 2)
#.........这里部分代码省略.........
示例15: GraphTestCase
# 需要导入模块: from rdflib import Graph [as 别名]
# 或者: from rdflib.Graph import commit [as 别名]
class GraphTestCase(unittest.TestCase):
storetest = True
store_name = "default"
# create = True
michel = URIRef(u'michel')
tarek = URIRef(u'tarek')
bob = URIRef(u'bob')
likes = URIRef(u'likes')
hates = URIRef(u'hates')
pizza = URIRef(u'pizza')
cheese = URIRef(u'cheese')
def setUp(self):
self.graph = Graph(self.store_name, self.identifier)
self.graph.open(self.tmppath, create=self.create)
# self.store = plugin.get(self.store_name, store.Store)(
# configuration=self.tmppath, identifier=self.identifier)
# self.graph = Graph(self.store, self.identifier)
# self.graph.destroy(self.tmppath)
# self.graph.open(self.tmppath, create=self.create)
def tearDown(self):
self.graph.destroy(self.tmppath)
try:
self.graph.close()
except:
pass
if os.path.exists(self.tmppath):
os.unlink(self.tmppath)
def addStuff(self):
tarek = self.tarek
michel = self.michel
bob = self.bob
likes = self.likes
hates = self.hates
pizza = self.pizza
cheese = self.cheese
self.graph.add((tarek, likes, pizza))
self.graph.add((tarek, likes, cheese))
self.graph.add((michel, likes, pizza))
self.graph.add((michel, likes, cheese))
self.graph.add((bob, likes, cheese))
self.graph.add((bob, hates, pizza))
self.graph.add((bob, hates, michel)) # gasp!
self.graph.commit()
def removeStuff(self):
tarek = self.tarek
michel = self.michel
bob = self.bob
likes = self.likes
hates = self.hates
pizza = self.pizza
cheese = self.cheese
self.graph.remove((tarek, likes, pizza))
self.graph.remove((tarek, likes, cheese))
self.graph.remove((michel, likes, pizza))
self.graph.remove((michel, likes, cheese))
self.graph.remove((bob, likes, cheese))
self.graph.remove((bob, hates, pizza))
self.graph.remove((bob, hates, michel)) # gasp!
def testAdd(self):
self.addStuff()
def testRemove(self):
self.addStuff()
self.removeStuff()
def testTriples(self):
tarek = self.tarek
michel = self.michel
bob = self.bob
likes = self.likes
hates = self.hates
pizza = self.pizza
cheese = self.cheese
asserte = self.assertEquals
triples = self.graph.triples
Any = None
self.addStuff()
# unbound subjects
asserte(len(list(triples((Any, likes, pizza)))), 2)
asserte(len(list(triples((Any, hates, pizza)))), 1)
asserte(len(list(triples((Any, likes, cheese)))), 3)
asserte(len(list(triples((Any, hates, cheese)))), 0)
# unbound objects
asserte(len(list(triples((michel, likes, Any)))), 2)
asserte(len(list(triples((tarek, likes, Any)))), 2)
asserte(len(list(triples((bob, hates, Any)))), 2)
asserte(len(list(triples((bob, likes, Any)))), 1)
# unbound predicates
#.........这里部分代码省略.........