本文整理汇总了Python中rdflib.ConjunctiveGraph.remove_context方法的典型用法代码示例。如果您正苦于以下问题:Python ConjunctiveGraph.remove_context方法的具体用法?Python ConjunctiveGraph.remove_context怎么用?Python ConjunctiveGraph.remove_context使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rdflib.ConjunctiveGraph
的用法示例。
在下文中一共展示了ConjunctiveGraph.remove_context方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __store_graph
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import remove_context [as 别名]
def __store_graph(cur_g, rdf_iri_string, d_dir):
try:
res_dir, dest_file = \
find_paths(rdf_iri_string, args.base + os.sep, "https://w3id.org/oc/corpus/", 10000, 1000)
dest_dir = res_dir.replace(args.base + os.sep, d_dir + os.sep)
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
cur_file = dest_file.replace(res_dir, dest_dir)
if os.path.exists(cur_file):
c_graph = __load_graph(cur_file)
else:
c_graph = ConjunctiveGraph()
c_graph.remove_context(c_graph.get_context(cur_g.identifier))
c_graph.addN([item + (cur_g.identifier,) for item in list(cur_g)])
with open(dest_file.replace(res_dir, dest_dir), "w") as f:
cur_json_ld = json.loads(c_graph.serialize(format="json-ld", context=context_json))
cur_json_ld["@context"] = context_path
json.dump(cur_json_ld, f, indent=4)
# repok.add_sentence("File '%s' added." % cur_file)
return dest_file
except Exception as e:
reperr.add_sentence("[5] It was impossible to store the RDF statements in %s. %s" %
(dest_file, str(e)))
示例2: GraphCache
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import remove_context [as 别名]
class GraphCache(object):
def __init__(self, cachedir):
self.graph = ConjunctiveGraph()
self.mtime_map = {}
self.cachedir = cachedir
if not os.path.isdir(cachedir):
os.makedirs(cachedir)
def load(self, url):
src = VOCAB_SOURCE_MAP.get(str(url), url)
if os.path.isfile(url):
context_id = create_input_source(url).getPublicId()
last_vocab_mtime = self.mtime_map.get(url)
vocab_mtime = os.stat(url).st_mtime
if not last_vocab_mtime or last_vocab_mtime < vocab_mtime:
logger.debug("Parse file: '%s'", url)
self.mtime_map[url] = vocab_mtime
# use CG as workaround for json-ld always loading as dataset
graph = ConjunctiveGraph()
graph.parse(src, format=guess_format(src))
self.graph.remove_context(context_id)
for s, p, o in graph:
self.graph.add((s, p, o, context_id))
return graph
else:
context_id = url
if any(self.graph.triples((None, None, None), context=context_id)):
logger.debug("Using context <%s>" % context_id)
return self.graph.get_context(context_id)
cache_path = self.get_fs_path(url)
if os.path.exists(cache_path):
logger.debug("Load local copy of <%s> from '%s'", context_id, cache_path)
return self.graph.parse(cache_path, format='turtle', publicID=context_id)
else:
logger.debug("Fetching <%s> to '%s'", context_id, cache_path)
graph = self.graph.parse(src,
format='rdfa' if url.endswith('html') else None)
with open(cache_path, 'w') as f:
graph.serialize(f, format='turtle')
return graph
def get_fs_path(self, url):
return os.path.join(self.cachedir, quote(url, safe="")) + '.ttl'
示例3: ContextTestCase
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import remove_context [as 别名]
#.........这里部分代码省略.........
# 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)
# print("Graph", graph.identifier, graph.serialize(format="nt"))
# print("Selfgraph", self.graph.identifier,
# self.graph.serialize(format="nt"))
self.assertEquals(len(self.graph.store), len(graph.store))
def testAdd(self):
self.addStuff()
def testRemove(self):
self.addStuff()
self.removeStuff()
def testLenInOneContext(self):
c1 = self.c1
# make sure context is empty
self.graph.remove_context(self.get_context(c1))
graph = Graph(self.graph.store, c1)
oldLen = len(self.graph)
for i in range(0, 10):
graph.add((BNode(), self.hates, self.hates))
self.assertEquals(len(graph), oldLen + 10)
self.assertEquals(len(self.get_context(c1)), oldLen + 10)
self.graph.remove_context(self.get_context(c1))
self.assertEquals(len(self.graph), oldLen)
self.assertEquals(len(graph), 0)
def testLenInMultipleContexts(self):
oldLen = len(self.graph.store)
print("Original", oldLen, self.graph.store)
self.addStuffInMultipleContexts()
newLen = len(self.graph.store)
print("MultipleContexts", newLen, self.graph.store)
# addStuffInMultipleContexts is adding the same triple to
# three different contexts. So it's only + 1
print("No context", len(list(self.graph.triples((None, None, None)))))
print("Context context-1", len(
list(self.graph.triples((None, None, None), context=self.c1))))
print("Context context-2", len(
list(self.graph.triples((None, None, None), context=self.c2))))
self.assertEquals(len(self.graph.store), oldLen + 1,
[self.graph.store, oldLen + 1])
graph = Graph(self.graph.store, self.c1)
self.assertEquals(len(graph.store), oldLen + 1,
[graph.store, oldLen + 1])
def testRemoveInMultipleContexts(self):
示例4: ContextTestCase
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import remove_context [as 别名]
#.........这里部分代码省略.........
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):
if self.store == "SQLite":
raise SkipTest("Skipping known issue with __len__")
self.addStuffInMultipleContexts()
triple = (self.pizza, self.likes, self.pizza)
# add to context 1
graph = Graph(self.graph.store, self.c1)
graph.add(triple)
self.assertEqual(len(self.graph), len(graph))
def testAdd(self):
self.addStuff()
def testRemove(self):
self.addStuff()
self.removeStuff()
def testLenInOneContext(self):
c1 = self.c1
# make sure context is empty
self.graph.remove_context(self.graph.get_context(c1))
graph = Graph(self.graph.store, c1)
oldLen = len(self.graph)
for i in range(0, 10):
graph.add((BNode(), self.hates, self.hates))
self.assertEqual(len(graph), oldLen + 10)
self.assertEqual(len(self.graph.get_context(c1)), oldLen + 10)
self.graph.remove_context(self.graph.get_context(c1))
self.assertEqual(len(self.graph), oldLen)
self.assertEqual(len(graph), 0)
def testLenInMultipleContexts(self):
if self.store == "SQLite":
raise SkipTest("Skipping known issue with __len__")
oldLen = len(self.graph)
self.addStuffInMultipleContexts()
# addStuffInMultipleContexts is adding the same triple to
# three different contexts. So it's only + 1
self.assertEqual(len(self.graph), oldLen + 1)
graph = Graph(self.graph.store, self.c1)
self.assertEqual(len(graph), oldLen + 1)
def testRemoveInMultipleContexts(self):
c1 = self.c1
c2 = self.c2
triple = (self.pizza, self.hates, self.tarek) # revenge!
self.addStuffInMultipleContexts()
# triple should be still in store after removing it from c1 + c2
示例5: ContextTestCase
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import remove_context [as 别名]
#.........这里部分代码省略.........
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()
def testRemove(self):
self.addStuff()
self.removeStuff()
def testLenInOneContext(self):
c1 = self.c1
# make sure context is empty
self.graph.remove_context(self.get_context(c1))
graph = Graph(self.graph.store, c1)
oldLen = len(self.graph)
for i in range(0, 10):
graph.add((BNode(), self.hates, self.hates))
self.assertEquals(len(graph), oldLen + 10)
self.assertEquals(len(self.get_context(c1)), oldLen + 10)
self.graph.remove_context(self.get_context(c1))
self.assertEquals(len(self.graph), oldLen)
self.assertEquals(len(graph), 0)
def testLenInMultipleContexts(self):
oldLen = len(self.graph)
self.addStuffInMultipleContexts()
# addStuffInMultipleContexts is adding the same triple to
# three different contexts. So it's only + 1
self.assertEquals(len(self.graph), oldLen + 1)
graph = Graph(self.graph.store, self.c1)
self.assertEquals(len(graph), oldLen + 1)
def testRemoveInMultipleContexts(self):
c1 = self.c1
c2 = self.c2
triple = (self.pizza, self.hates, self.tarek) # revenge!
self.addStuffInMultipleContexts()
# triple should be still in store after removing it from c1 + c2
self.assert_(triple in self.graph)
graph = Graph(self.graph.store, c1)
示例6: ContextTestCase
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import remove_context [as 别名]
#.........这里部分代码省略.........
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()
def testRemove(self):
self.addStuff()
self.removeStuff()
def testLenInOneContext(self):
c1 = self.c1
# make sure context is empty
self.graph.remove_context(self.get_context(c1))
graph = Graph(self.graph.store, c1)
oldLen = len(self.graph)
for i in range(0, 10):
graph.add((BNode(), self.hates, self.hates))
self.assertEquals(len(graph), oldLen + 10)
self.assertEquals(len(self.get_context(c1)), oldLen + 10)
self.graph.remove_context(self.get_context(c1))
self.assertEquals(len(self.graph), oldLen)
self.assertEquals(len(graph), 0)
def testLenInMultipleContexts(self):
oldLen = len(self.graph)
self.addStuffInMultipleContexts()
# addStuffInMultipleContexts is adding the same triple to
# three different contexts. So it's only + 1
self.assertEquals(len(self.graph), oldLen + 1)
graph = Graph(self.graph.store, self.c1)
self.assertEquals(len(graph), oldLen + 1)
def testRemoveInMultipleContexts(self):
c1 = self.c1
c2 = self.c2
triple = (self.pizza, self.hates, self.tarek) # revenge!
self.addStuffInMultipleContexts()
# triple should be still in store after removing it from c1 + c2
self.assert_(triple in self.graph)
graph = Graph(self.graph.store, c1)
示例7: Store
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import remove_context [as 别名]
class Store(object):
def __init__(self, store='default'):
self.graphs = ConjunctiveGraph(store)
self._lock = threading.RLock()
@locked
def _print(self):
print "Graph %r" % self.graphs
@locked
def get_graph_uris(self):
return [ n.identifier for n in self.graphs.contexts() ]
def __write_graph(self, graph):
self._lock.acquire()
while True:
#new_uri = URIRef('http://otsopack/%s' % random.randint(0, 1000))
new_id = str( random.randint(0, 1000) )
if new_id in filter(lambda n: n.identifier!=new_id, self.graphs.contexts()):
continue
gr = self.graphs.get_context(new_id) #Graph(self.graphs.store, new_uri)
gr += graph
return new_id
@locked
def write(self, triples):
if not isinstance(triples, Graph):
raise Exception("'triples' must be a Graph.")
new_uri = self.__write_graph(triples)
self._lock.release()
return new_uri
@locked
def read_uri(self, uri):
ret = Graph(self.graphs.store, uri)
return deepcopy(ret) if ret else None
@locked
def read_wildcard(self, subject, predicate, obj):
gr = self._find_graph(subject, predicate, obj)
return deepcopy(gr)
@locked
def read_sparql(self, query):
gr = self._find_graph_sparql(query)
return deepcopy(gr)
@locked
def take_uri(self, uri):
ret = None
try:
#context = URIRef(uri)
#ret = Graph(self.graphs.store, uri)
to_delete = self.graphs.get_context(uri)
ret = deepcopy(to_delete)
self.graphs.remove_context(to_delete)
except KeyError:
return None
return ret if len(ret)>0 else None
@locked
def take_wildcard(self, subject, predicate, obj):
ret = None
try:
to_delete = self._find_graph(subject, predicate, obj)
if to_delete is not None:
ret = deepcopy(to_delete)
self.graphs.remove_context(to_delete)
except KeyError:
return None
return ret
@locked
def take_sparql(self, query):
ret = None
try:
to_delete = self._find_graph_sparql(query)
if to_delete is not None:
ret = deepcopy(to_delete)
self.graphs.remove_context(to_delete)
except KeyError:
return None
return ret
@locked
def query_wildcard(self, subject, predicate, obj):
ret = Graph()
for t in self.graphs.triples((subject, predicate, obj)):
ret.add(t)
return ret if len(ret)>0 else None
@locked
def query_sparql(self, query):
ret = Graph()
for t in self.graphs.query( query ):
ret.add(t)
return ret if len(ret)>0 else None
#.........这里部分代码省略.........
示例8: ContextTestCase
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import remove_context [as 别名]
#.........这里部分代码省略.........
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()
def testRemove(self):
self.addStuff()
self.removeStuff()
def testLenInOneContext(self):
c1 = self.c1
# make sure context is empty
self.graph.remove_context(self.get_context(c1))
graph = Graph(self.graph.store, c1)
oldLen = len(self.graph)
for i in range(0, 10):
graph.add((BNode(), self.hates, self.hates))
self.assertEquals(len(graph), oldLen + 10)
self.assertEquals(len(self.get_context(c1)), oldLen + 10)
self.graph.remove_context(self.get_context(c1))
self.assertEquals(len(self.graph), oldLen)
self.assertEquals(len(graph), 0)
def testLenInMultipleContexts(self):
oldLen = len(self.graph)
self.addStuffInMultipleContexts()
# addStuffInMultipleContexts is adding the same triple to
# three different contexts. So it's only + 1
self.assertEquals(len(self.graph), oldLen + 1)
graph = Graph(self.graph.store, self.c1)
self.assertEquals(len(graph), oldLen + 1)
def testRemoveInMultipleContexts(self):
c1 = self.c1
c2 = self.c2
triple = (self.pizza, self.hates, self.tarek) # revenge!
self.addStuffInMultipleContexts()
# triple should be still in store after removing it from c1 + c2
self.assert_(triple in self.graph)
graph = Graph(self.graph.store, c1)