本文整理汇总了Python中rdflib.graph.ConjunctiveGraph.get_context方法的典型用法代码示例。如果您正苦于以下问题:Python ConjunctiveGraph.get_context方法的具体用法?Python ConjunctiveGraph.get_context怎么用?Python ConjunctiveGraph.get_context使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rdflib.graph.ConjunctiveGraph
的用法示例。
在下文中一共展示了ConjunctiveGraph.get_context方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from rdflib.graph import ConjunctiveGraph [as 别名]
# 或者: from rdflib.graph.ConjunctiveGraph import get_context [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))
#.........这里部分代码省略.........