本文整理汇总了Python中rdflib.graph.ConjunctiveGraph.open方法的典型用法代码示例。如果您正苦于以下问题:Python ConjunctiveGraph.open方法的具体用法?Python ConjunctiveGraph.open怎么用?Python ConjunctiveGraph.open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rdflib.graph.ConjunctiveGraph
的用法示例。
在下文中一共展示了ConjunctiveGraph.open方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: update_list
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import open [as 别名]
def update_list():
"""
Reads the triple store looking for twitter usernames and adds
them to the dev8d list.
"""
g = ConjunctiveGraph("Sleepycat")
g.open("store")
# get oauth client in order
credentials = get_credentials()
consumer = oauth.Consumer(credentials['key'], credentials['secret'])
client = oauth.Client(consumer, credentials['access_token'])
list_update_url = 'http://api.twitter.com/1/%s/%s/members.json' % \
(credentials['list_owner'], credentials['list_name'])
# create the list if necessary
create_list(credentials['list_owner'], credentials['list_name'], client)
# look at all the twitter usernames and add them to the list
for twitter_username in g.objects(predicate=w['Twitter']):
id = twitter_user_id(twitter_username, client)
if id:
logging.info("adding %s (%s) to list" % (twitter_username, id))
body = "id=%s" % id
resp, content = client.request(list_update_url, 'POST', body=body)
else:
logging.error("unable to get twitter id for %s" % twitter_username)
g.close()
示例2: test_issue_4
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import open [as 别名]
def test_issue_4(self):
ident = URIRef("rdflib_test")
g = Graph(store="SQLAlchemy", identifier=ident)
rt = g.open(self.uri, create=True)
if rt == NO_STORE:
g.open(self.uri, create=True)
else:
assert rt == VALID_STORE, "The underlying store is corrupt"
g.destroy(self.uri)
示例3: TestBDBTransactions
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import open [as 别名]
class TestBDBTransactions(unittest.TestCase):
non_core = True
def setUp(self):
self.graph = ConjunctiveGraph(store="BerkeleyDB")
self.path = mkdtemp()
self.graph.open(self.path, create=True)
def tearDown(self):
self.graph.close()
def get_context(self, identifier):
assert isinstance(identifier, URIRef) or \
isinstance(identifier, BNode), type(identifier)
return Graph(store=self.graph.store, identifier=identifier,
namespace_manager=self)
def __manyOpsManyThreads(self, worker, workers=10, triples=1000, input=[]):
all_ops = []
pool = []
for i in range(0, workers):
t = Thread(target=worker, args=(all_ops, self.graph, triples), kwargs={'input':input})
pool.append(t)
t.start()
for t in pool:
t.join()
return all_ops
def testAddManyManyThreads(self):
# TODO: sometimes this test leads to TypeError exceptions?
w = 4
t = 1000
self.__manyOpsManyThreads(worker_add, workers=w, triples=t)
#print "graph size after finish: ", len(self.graph)
self.failUnless(len(self.graph) == w*t)
def testRemove(self):
ops = self.__manyOpsManyThreads(worker_add, workers=1, triples=10)
self.__manyOpsManyThreads(worker_remove, workers=1, triples=7, input=ops)
#print "graph size after finish: ", len(self.graph)
self.failUnless(len(self.graph) == 3)
def testRemoveAll(self):
ops = self.__manyOpsManyThreads(worker_add, workers=1, triples=10)
try:
self.graph.remove((None, None, None))
except Exception, e:
#print "Could not remove all: ", e
raise e
#print "graph size after finish: ", len(self.graph)
self.failUnless(len(self.graph) == 0)
示例4: setUp
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import open [as 别名]
class TestBDBOptimized:
non_core = True
bsddb = True
def setUp(self):
self.graph = ConjunctiveGraph(store="BDBOptimized")
self.path = mkdtemp()
self.graph.open(self.path, create=True)
def tearDown(self):
self.graph.close()
示例5: test_issue_4
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import open [as 别名]
def test_issue_4(self):
from rdflib.graph import ConjunctiveGraph as Graph
from rdflib.store import NO_STORE, VALID_STORE
from rdflib.term import URIRef
ident = URIRef("rdflib_test")
g = Graph(store="SQLAlchemy", identifier=ident)
rt = g.open(self.uri, create=True)
if rt == NO_STORE:
g.open(self.uri, create=True)
else:
assert rt == VALID_STORE, "The underlying store is corrupt"
g.destroy(self.uri)
示例6: init_store
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import open [as 别名]
def init_store(path):
"""
Initialize the Conjunctive Graph
"""
global store
if store is not None:
raise RuntimeError("Don't initialize the graph more than once")
store = plugin.get('Sleepycat', Store)('rdfstore')
graph = ConjunctiveGraph(store=store, identifier=graph_uri)
rt = graph.open(path)
if rt == NO_STORE:
graph.open(path, create=True)
else:
assert rt == VALID_STORE
示例7: rdf_description
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import open [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()
示例8: TestIssue11
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import open [as 别名]
class TestIssue11(unittest.TestCase):
debug = True
def setUp(self):
self.graph = ConjunctiveGraph(store="SQLite")
fp, path = tempfile.mkstemp(suffix='.sqlite')
self.graph.open(path, create=True)
def testSPARQL_SQLite_lessthan_filter_a(self):
self.graph.parse(data=testgraph1, format="n3", publicID=NS)
rt = self.graph.query(good_testquery,
initNs={'rdf': RDF, 'xsd': XSD}, DEBUG=True)
# rt = self.graph.query(good_testquery, DEBUG=True)
# assert str(list(rt)[0][0]) == "http://example.org/bar", list(rt)
assert len(list(rt)) == 1, list(rt)
def testSPARQL_SQLite_lessthan_filter_b(self):
self.graph.parse(data=testgraph2, format="n3", publicID=NS)
# Counter-example demo
rt = self.graph.query(bad_testquery,
initNs={'rdf': RDF, 'xsd': XSD}, DEBUG=True)
# rt = self.graph.query(bad_testquery, DEBUG=True)
assert len(list(rt)) == 3, list(rt)
示例9: ContextTestCase
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import open [as 别名]
class ContextTestCase(unittest.TestCase):
#store = 'Memory'
store = 'default'
slow = True
def setUp(self):
self.graph = ConjunctiveGraph(store=self.store)
if self.store == "MySQL":
from mysql import configString
from rdflib.store.MySQL import MySQL
path=configString
MySQL().destroy(path)
else:
path = a_tmp_dir = mkdtemp()
self.graph.open(path, create=True)
self.michel = URIRef(u'michel')
self.tarek = URIRef(u'tarek')
self.bob = URIRef(u'bob')
self.likes = URIRef(u'likes')
self.hates = URIRef(u'hates')
self.pizza = URIRef(u'pizza')
self.cheese = URIRef(u'cheese')
self.c1 = URIRef(u'context-1')
self.c2 = URIRef(u'context-2')
# delete the graph for each test!
self.graph.remove((None, None, None))
def tearDown(self):
self.graph.close()
def get_context(self, identifier):
assert isinstance(identifier, URIRef) or \
isinstance(identifier, BNode), type(identifier)
return Graph(store=self.graph.store, identifier=identifier,
namespace_manager=self)
def addStuff(self):
tarek = self.tarek
michel = self.michel
bob = self.bob
likes = self.likes
hates = self.hates
pizza = self.pizza
cheese = self.cheese
c1 = self.c1
graph = Graph(self.graph.store, c1)
graph.add((tarek, likes, pizza))
graph.add((tarek, likes, cheese))
graph.add((michel, likes, pizza))
graph.add((michel, likes, cheese))
graph.add((bob, likes, cheese))
graph.add((bob, hates, pizza))
graph.add((bob, hates, michel)) # gasp!
def removeStuff(self):
tarek = self.tarek
michel = self.michel
bob = self.bob
likes = self.likes
hates = self.hates
pizza = self.pizza
cheese = self.cheese
c1 = self.c1
graph = Graph(self.graph.store, c1)
graph.remove((tarek, likes, pizza))
graph.remove((tarek, likes, cheese))
graph.remove((michel, likes, pizza))
graph.remove((michel, likes, cheese))
graph.remove((bob, likes, cheese))
graph.remove((bob, hates, pizza))
graph.remove((bob, hates, michel)) # gasp!
def addStuffInMultipleContexts(self):
c1 = self.c1
c2 = self.c2
triple = (self.pizza, self.hates, self.tarek) # revenge!
# add to default context
self.graph.add(triple)
# add to context 1
graph = Graph(self.graph.store, c1)
graph.add(triple)
# add to context 2
graph = Graph(self.graph.store, c2)
graph.add(triple)
def testConjunction(self):
self.addStuffInMultipleContexts()
triple = (self.pizza, self.likes, self.pizza)
# add to context 1
graph = Graph(self.graph.store, self.c1)
graph.add(triple)
self.assertEquals(len(self.graph), len(graph))
def testAdd(self):
self.addStuff()
#.........这里部分代码省略.........
示例10: open_graph
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import open [as 别名]
def open_graph(self):
graph = ConjunctiveGraph(store='hstore')
graph.open(connection_uri, create=True)
self.graphs.append(graph)
return graph
示例11: rdf_all
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import open [as 别名]
def rdf_all(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 IOMemory 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:
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' ):
#.........这里部分代码省略.........
示例12: Graph
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import open [as 别名]
import rdflib
from rdflib.graph import ConjunctiveGraph as Graph
#from rdflib.graph import Graph
configString = "/tmp/rdfstore12"
graph = Graph(store="KyotoCabinet")
print graph.store
path = configString
rt = graph.open(path, create=False)
print "Triples in graph: ", len(graph)
print "first 20 are as follows:"
i=20
for subj, pred, obj in graph:
print(subj, pred, obj)
i-=1
if i==0:
break
graph.close()
示例13: Namespace
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import open [as 别名]
#!/usr/bin/env python
from rdflib.graph import ConjunctiveGraph
from rdflib.namespace import Namespace
dct = Namespace('http://purl.org/dc/terms/')
g = ConjunctiveGraph('Sleepycat')
g.open('store')
subjects = {}
for o in g.objects(predicate=dct['subject']):
sub = o.split('/')[-1]
subjects[sub] = subjects.get(sub, 0) + 1
sorted_keys = subjects.keys()
sorted_keys.sort(lambda a, b: cmp(subjects[b], subjects[a]))
for subject in sorted_keys:
print subject, "\t", subjects[subject]
示例14: Graph
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import open [as 别名]
import rdflib
from rdflib.graph import ConjunctiveGraph as Graph
from rdflib import plugin
from rdflib.store import Store
configString = "/tmp/rdfstore"
store = plugin.get('KyotoCabinet', Store)('rdfstore')
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")
for subj, pred, obj in graph:
if (subj, pred, obj) not in graph:
raise Exception("It better be!")
graph.add((subj, pred, obj))
graph.commit()
print "Triples in graph after add: ", len(graph)
graph.close()
示例15: ContextTest
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import open [as 别名]
class ContextTest(test.TestCase):
"""
Testing different contexts.
Heavily based on https://github.com/RDFLib/rdflib-postgresql/blob/master/test/context_case.py
"""
store_name = "Django"
storetest = True
path = ""
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')
c1 = URIRef(u'context-1')
c2 = URIRef(u'context-2')
def setUp(self):
self.graph = ConjunctiveGraph(store=self.store_name)
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 get_context(self, identifier):
assert isinstance(identifier, URIRef) or isinstance(identifier, BNode), type(identifier)
return Graph(store=self.graph.store, identifier=identifier, namespace_manager=self)
def addStuff(self):
tarek = self.tarek
michel = self.michel
bob = self.bob
likes = self.likes
hates = self.hates
pizza = self.pizza
cheese = self.cheese
c1 = self.c1
graph = Graph(self.graph.store, c1)
graph.add((tarek, likes, pizza))
graph.add((tarek, likes, cheese))
graph.add((michel, likes, pizza))
graph.add((michel, likes, cheese))
graph.add((bob, likes, cheese))
graph.add((bob, hates, pizza))
graph.add((bob, hates, michel))
def removeStuff(self):
tarek = self.tarek
michel = self.michel
bob = self.bob
likes = self.likes
hates = self.hates
pizza = self.pizza
cheese = self.cheese
c1 = self.c1
graph = Graph(self.graph.store, c1)
graph.remove((tarek, likes, pizza))
graph.remove((tarek, likes, cheese))
graph.remove((michel, likes, pizza))
graph.remove((michel, likes, cheese))
graph.remove((bob, likes, cheese))
graph.remove((bob, hates, pizza))
graph.remove((bob, hates, michel))
def addStuffInMultipleContexts(self):
c1 = self.c1
c2 = self.c2
triple = (self.pizza, self.hates, self.tarek)
# add to default context
self.graph.add(triple)
# add to context 1
graph = Graph(self.graph.store, c1)
graph.add(triple)
# add to context 2
graph = Graph(self.graph.store, c2)
graph.add(triple)
def testConjunction(self):
self.addStuffInMultipleContexts()
triple = (self.pizza, self.likes, self.pizza)
# add to context 1
graph = Graph(self.graph.store, self.c1)
graph.add(triple)
self.assertEquals(len(graph), 2)
self.assertEquals(len(self.graph), 2)
def testAdd(self):
self.addStuff()
def testRemove(self):
#.........这里部分代码省略.........