本文整理汇总了Python中rdflib.ConjunctiveGraph.namespace_manager方法的典型用法代码示例。如果您正苦于以下问题:Python ConjunctiveGraph.namespace_manager方法的具体用法?Python ConjunctiveGraph.namespace_manager怎么用?Python ConjunctiveGraph.namespace_manager使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rdflib.ConjunctiveGraph
的用法示例。
在下文中一共展示了ConjunctiveGraph.namespace_manager方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _populate_graph
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import namespace_manager [as 别名]
def _populate_graph(self):
graph = ConjunctiveGraph(identifier=self._generate_namedgraph_uri())
graph.namespace_manager = namespace_manager
subject = URIRef(self._get_document_uri())
graph.add((subject, RDFS.isDefinedBy, URIRef(self._generate_about_uri())))
self._add_about_triples(graph)
graph.add((subject, RDF.type, self.ns[self.get_rdf_type()]))
if self.source_uri and self.source_uri != self.document_uri:
graph.add((subject, OWL.sameAs, URIRef(self.source_uri)))
for key, value in self.get_graph_mapping().items():
if isinstance(key, URIRef):
predicate = key
elif isinstance(key, str) and key.startswith('http://'):
predicate = URIRef(key)
elif isinstance(key, str) and ":" in key:
ns, label = key.split(":")
ns = self.ns_dict.get(ns)
predicate = URIRef("{}/{}".format(str(ns).rstrip('/'), label))
else:
raise ValueError("unknown predicate key in mapping dict: {} => ".format(key, value))
if type(value) in [str, float, int] and value:
if isinstance(value, str) and any([value.startswith(uri_prefix) for uri_prefix in ["http", "urn"]]):
value = URIRef(value)
else:
value = Literal(value)
elif type(value) in [Literal, URIRef]:
value = value
else:
logger.warn("Unsupported datatype {} for value {}".format(type(value), value))
if value:
graph.add((subject, predicate, value))
graph.namespace_manager = namespace_manager
return graph
示例2: get_graph_from_sparql_results
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import namespace_manager [as 别名]
def get_graph_from_sparql_results(sparql_json, named_graph=None):
if len(sparql_json['results']['bindings']) == 0:
return ConjunctiveGraph(), 0
sparql_vars = sparql_json['head']['vars']
if 'g' in sparql_vars:
if not named_graph:
named_graph = sparql_json['results']['bindings'][0]['g']['value']
sparql_vars.remove('g')
triple_levels = RDFModel.get_context_triples(sparql_json['head']['vars'])
nr_levels = len(triple_levels)
if named_graph:
named_graph = URIRef(named_graph)
graph = ConjunctiveGraph(identifier=named_graph)
graph.namespace_manager = namespace_manager
for binding in sparql_json['results']['bindings']:
binding_levels = RDFModel.get_context_levels(len(binding.keys()))
for s, p, o in triple_levels[:binding_levels]:
subject = URIRef(binding[s]['value'])
if binding[s]['type'] == 'bnode':
subject = BNode(binding[s]['value'])
predicate = URIRef(binding[p]['value'])
obj = RDFModel.get_object_from_sparql_result(binding[o])
graph.add((subject, predicate, obj))
# materialize inferences
for subject, obj in graph.subject_objects(
predicate=URIRef("http://www.openarchives.org/ore/terms/isAggregatedBy")):
graph.add((obj, URIRef("http://www.openarchives.org/ore/terms/aggregates"), subject))
graph.remove((subject, URIRef("http://www.openarchives.org/ore/terms/isAggregatedBy"), obj))
return graph, nr_levels
示例3: get_graph
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import namespace_manager [as 别名]
def get_graph(self, with_mappings=False, include_mapping_target=False, acceptance=False, target_uri=None):
"""Get Graph instance of this EDMRecord.
:param target_uri: target_uri if you want a sub-selection of the whole graph
:param acceptance: if the acceptance data should be listed
:param include_mapping_target: Boolean also include the mapping target triples in graph
:param with_mappings: Boolean integrate the ProxyMapping into the graph
"""
rdf_string = self.source_rdf
if acceptance and self.acceptance_rdf:
rdf_string = self.acceptance_rdf
graph = ConjunctiveGraph(identifier=self.named_graph)
graph.namespace_manager = namespace_manager
graph.parse(data=rdf_string, format='nt')
if with_mappings:
proxy_resources, graph = ProxyResource.update_proxy_resource_uris(self.dataset, graph)
self.proxy_resources.add(*proxy_resources)
for proxy_resource in proxy_resources:
graph = graph + proxy_resource.to_graph(include_mapping_target=include_mapping_target)
if target_uri and not target_uri.endswith("/about") and target_uri != self.document_uri:
g = Graph(identifier=URIRef(self.named_graph))
subject = URIRef(target_uri)
for p, o in graph.predicate_objects(subject=subject):
g.add((subject, p, o))
graph = g
return graph
示例4: parse
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import namespace_manager [as 别名]
def parse(self, source, graph, encoding="utf-8"):
if encoding not in [None, "utf-8"]:
raise Exception(
("TriG files are always utf-8 encoded, ",
"I was passed: %s") % encoding)
# we're currently being handed a Graph, not a ConjunctiveGraph
assert graph.store.context_aware, "TriG Parser needs a context-aware store!"
conj_graph = ConjunctiveGraph(store=graph.store, identifier=graph.identifier)
conj_graph.default_context = graph # TODO: CG __init__ should have a
# default_context arg
# TODO: update N3Processor so that it can use conj_graph as the sink
conj_graph.namespace_manager = graph.namespace_manager
sink = RDFSink(conj_graph)
baseURI = conj_graph.absolutize(
source.getPublicId() or source.getSystemId() or "")
p = TrigSinkParser(sink, baseURI=baseURI, turtle=True)
p.loadStream(source.getByteStream())
for prefix, namespace in p._bindings.items():
conj_graph.bind(prefix, namespace)
示例5: __init__
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import namespace_manager [as 别名]
def __init__(self, endpoint):
graph = ConjunctiveGraph('SPARQLStore')
graph.open(endpoint)
graph.namespace_manager = ns_mgr
self.graph = graph
self.default_graph = \
'http://vitro.mannlib.cornell.edu/default/vitro-kb-2'
示例6: catalyst_graph_for
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import namespace_manager [as 别名]
def catalyst_graph_for(file):
if file.startswith("/"):
file = "file://" + file
logging.info("InferenceStore catalyst_graph_for started")
# quads = jsonld.to_rdf(file, {'format': 'application/nquads'})
logging.info("InferenceStore JSON-LD loaded")
g = ConjunctiveGraph()
g.namespace_manager = namespace_manager
# g.parse(data=quads, format='nquads')
g.load(file, format="json-ld")
logging.info("InferenceStore base graph loaded")
f = FuXiInferenceStore.get_instance()
# get the inference engine
cl = f.get_inference(g)
logging.info("InferenceStore inference graph loaded")
union_g = rdflib.ConjunctiveGraph()
for s, p, o in g.triples((None, None, None)):
union_g.add((s, p, o))
for s, p, o in cl.triples((None, None, None)):
union_g.add((s, p, o))
logging.info("InferenceStore union graph prepared")
return union_g
示例7: init_graph
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import namespace_manager [as 别名]
def init_graph(graph_type=None):
"""
Helper to initialize a VIVO graph with
namespace manager.
"""
if graph_type == 'conjunctive':
g = ConjunctiveGraph()
else:
g = Graph()
g.namespace_manager = ns_mgr
return g
示例8: catalyst_graph_for
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import namespace_manager [as 别名]
def catalyst_graph_for(file):
if file.startswith('/'):
file = 'file://'+file
logging.info("InferenceStore catalyst_graph_for started")
# quads = jsonld.to_rdf(file, {'format': 'application/nquads'})
logging.info("InferenceStore JSON-LD loaded")
g = ConjunctiveGraph()
g.namespace_manager = namespace_manager
# g.parse(data=quads, format='nquads')
g.load(file, format="json-ld")
logging.info("InferenceStore base graph loaded")
# get the inference engine
get_inference_store().get_inference(g)
logging.info("InferenceStore inference graph loaded")
return g
示例9: in_range
# 需要导入模块: from rdflib import ConjunctiveGraph [as 别名]
# 或者: from rdflib.ConjunctiveGraph import namespace_manager [as 别名]
if domain and not in_range(s, domain):
print("Not in domain: ", s, p, o)
if range_ and not in_range(o, range_):
print("Not in range: ", s, p, o)
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument('--check_properties', '-p', action='store_true',
help="check property domain and range against the ontology. Slow.")
parser.add_argument('input_fname', help="the input file")
args = parser.parse_args()
json = load(open(args.input_fname))
context = load(open(join(dirname(__file__), 'context.jsonld')))
suspicious = list(check_keys(json, context['@context']))
suspicious.sort()
if suspicious:
print("Suspicious keys:")
for key in suspicious:
print(key)
if args.check_properties:
from pyld import jsonld
quads = jsonld.to_rdf('file:'+args.input_fname, {'format': 'application/nquads'})
ontology = load_ontology()
g = ConjunctiveGraph()
g.namespace_manager = ontology.namespace_manager
g.parse(data=quads, format='nquads')
for c in g.contexts():
c.namespace_manager = ontology.namespace_manager
check_props(g, ontology)