def rdf(self): """ RDF """ if self._rdf: return self._rdf data = self.context if not hasattr(data, 'read'): if callable(data.data): data = StringInputSource(data.data()) else: data = StringInputSource(data.data) try: self._rdf = ConjunctiveGraph().parse(data) except Exception: s = data.getByteStream() # this is a StringIO instance s.seek(0) with tempfile.NamedTemporaryFile(prefix="rdflib_staff_log", delete=False) as f: f.write(s.read()) self.context.error_log.raising(sys.exc_info()) self.validDatas = False self._rdf = ConjunctiveGraph() return self._rdf
def test_turtle_namespace_prefixes(self): g = ConjunctiveGraph() n3 = \ """ @prefix _9: <http://data.linkedmdb.org/resource/movie/> . @prefix p_9: <urn:test:> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . p_9:a p_9:b p_9:c . <http://data.linkedmdb.org/resource/director/1> a <http://data.linkedmdb.org/resource/movie/director>; rdfs:label "Cecil B. DeMille (Director)"; _9:director_name "Cecil B. DeMille" .""" g.parse(data=n3, format='n3') turtle = g.serialize(format="turtle") # Check round-tripping, just for kicks. g = ConjunctiveGraph() g.parse(data=turtle, format='turtle') # Shouldn't have got to here s = g.serialize(format="turtle") self.assertTrue(b('@prefix _9') not in s)
def parse_and_serialize(input_files, input_format, guess, outfile, output_format, ns_bindings, store_conn="", store_type=None): if store_type: store = plugin.get(store_type, Store)() store.open(store_conn) graph = ConjunctiveGraph(store) else: store = None graph = ConjunctiveGraph() for prefix, uri in list(ns_bindings.items()): graph.namespace_manager.bind(prefix, uri, override=False) for fpath in input_files: use_format, kws = _format_and_kws(input_format) if fpath == '-': fpath = sys.stdin elif not input_format and guess: use_format = guess_format(fpath) or DEFAULT_INPUT_FORMAT graph.parse(fpath, format=use_format, **kws) if outfile: output_format, kws = _format_and_kws(output_format) kws.setdefault('base', None) graph.serialize(destination=outfile, format=output_format, **kws) if store: store.rollback()
def read(x): store = OrderedStore() gg = Graph(store=store, identifier=dddd) g = ConjunctiveGraph(store=store, identifier=dddd) g.default_union = False gg.parse(x, format='n3') ostore = OrderedStore() return g, ConjunctiveGraph(store=ostore, identifier=dddd)
def delete(self, model): uri = model.identifier idb = ConjunctiveGraph(self.db.store, uri) if not idb: abort(404, "Resource does not exist or is not deletable.") idb.remove((None, None, None)) g = ConjunctiveGraph(self.db.store) g.remove((uri, None, None)) g.remove((None, None, uri)) self.delete(model.identifier)
def load_cache(cache_filename): cache = {'urls': set(), 'g': ConjunctiveGraph()} if not cache_filename: return cache if not os.path.exists(cache_filename): return cache f = open(cache_filename, "rb") cache = pickle.load(f) g_serialized = cache['g'] g = ConjunctiveGraph() g.parse(data=g_serialized) cache['g'] = g f.close() return cache
def testSingleQuotedLiterals(self): test_data = ["""@prefix : <#> . :s :p 'o' .""", """@prefix : <#> . :s :p '''o''' ."""] for data in test_data: # N3 doesn't accept single quotes around string literals g = ConjunctiveGraph() self.assertRaises(BadSyntax, g.parse, data=data, format='n3') g = ConjunctiveGraph() g.parse(data=data, format='turtle') self.assertEqual(len(g), 1) for _, _, o in g: self.assertEqual(o, Literal('o'))
def test_escaping_of_triple_doublequotes(): """ Issue 186 - Check escaping of multiple doublequotes. A serialization/deserialization roundtrip of a certain class of Literals fails when there are both, newline characters and multiple subsequent quotation marks in the lexical form of the Literal. In this case invalid N3 is emitted by the serializer, which in turn cannot be parsed correctly. """ g = ConjunctiveGraph() g.add((URIRef('http://foobar'), URIRef('http://fooprop'), Literal('abc\ndef"""""'))) # assert g.serialize(format='n3') == '@prefix ns1: <http:// .\n\nns1:foobar ns1:fooprop """abc\ndef\\"\\"\\"\\"\\"""" .\n\n' g2 = ConjunctiveGraph() g2.parse(data=g.serialize(format='n3'), format='n3') assert g.isomorphic(g2) is True
def infer(graph: ConjunctiveGraph, rules: ConjunctiveGraph): """ returns new graph of inferred statements. """ log.info( f'Begin inference of graph len={len(graph)} with rules len={len(rules)}:' ) workingSet = ConjunctiveGraph() workingSet.addN(graph.quads()) implied = ConjunctiveGraph() delta = 1 while delta > 0: delta = -len(implied) for r in rules: if r[1] == LOG['implies']: containsSetup = all(st in workingSet for st in r[0]) if containsSetup: log.info(f' Rule {r[0]} -> present={containsSetup}') for st in r[0]: log.info( f' {st[0].n3()} {st[1].n3()} {st[2].n3()}') log.info(f' ...implies {len(r[2])} statements') if containsSetup: for st in r[2]: workingSet.add(st) implied.add(st) else: log.info(f' {r}') delta += len(implied) log.info(f' this inference round added {delta} more implied stmts') log.info(f'{len(implied)} stmts implied:') for st in implied: log.info(f' {st}') return implied # based on fuxi/tools/rdfpipe.py target = Graph() tokenSet = generateTokenSet(graph) with _dontChangeRulesStore(rules): network = ReteNetwork(rules, inferredTarget=target) network.feedFactsToAdd(tokenSet) return target
def check_n3_serialize(fpath, fmt, verbose=False): g = ConjunctiveGraph() _parse_or_report(verbose, g, fpath, format=fmt) if verbose: for t in g: print t print "========================================" print "Parsed OK!" s = g.serialize(format='n3') if verbose: print s g2 = ConjunctiveGraph() _parse_or_report(verbose, g2, data=s, format='n3') if verbose: print g2.serialize() crapCompare(g,g2)
def deserialize( self, stream, rdf_format="trig", relation_mapper=relation_mapper, predicate_mapper=predicate_mapper, **kwargs, ): """ Deserialize from the `PROV-O <https://www.w3.org/TR/prov-o/>`_ representation to a :class:`~prov.model.ProvDocument` instance. :param stream: Input data. :param rdf_format: The RDF format of the input data, default: TRiG. """ newargs = kwargs.copy() newargs["format"] = rdf_format container = ConjunctiveGraph() container.parse(stream, **newargs) document = pm.ProvDocument() self.document = document self.decode_document( container, document, relation_mapper=relation_mapper, predicate_mapper=predicate_mapper, ) return document
def create_ontology(self, tr, predicate, subClass, address, booktitle): LDT = Namespace("http://www.JceFinalProjectOntology.com/") ut = Namespace("http://www.JceFinalProjectOntology.com/subject/#") usubClass = URIRef("http://www.JceFinalProjectOntology.com/subject/" + subClass.strip() + '#') #LDT.subClass=LDT[subClass] print(ut) print(usubClass) store = IOMemory() sty = LDT[predicate] g = rdflib.Graph(store=store, identifier=LDT) t = ConjunctiveGraph(store=store, identifier=ut) print('Triples in graph before add: ', len(t)) #g.add((LDT,RDF.type,RDFS.Class)) g.add((URIRef(LDT), RDF.type, RDFS.Class)) g.add((URIRef(LDT), RDFS.label, Literal("JFPO"))) g.add((URIRef(LDT), RDFS.comment, Literal('class of all properties'))) for v in self.symbols.values(): if self.if_compoTerm(v) == True: vs = self.splitTerms(v)[0] else: vs = v g.add((LDT[vs], RDF.type, RDF.Property)) g.add((LDT[vs], RDFS.label, Literal('has' + vs))) g.add((LDT[vs], RDFS.comment, Literal(v))) g.add((LDT[vs], RDFS.range, OWL.Class)) g.add((LDT[vs], RDFS.domain, Literal(vs))) g.bind('JFPO', LDT) #g.commit() g.serialize('trtst.rdf', format='turtle') t.add((ut[tr], RDF.type, OWL.Class)) t.add((ut[tr], RDFS.subClassOf, OWL.Thing)) t.add((ut[tr], RDFS.label, Literal(tr))) t.add((ut[tr], DC.title, Literal(booktitle))) t.add((ut[tr], DC.source, Literal(address))) t.add((ut[tr], DC[predicate], URIRef(usubClass))) t.add((ut[tr], LDT[predicate], RDF.Property)) t.add((ut[tr], DC[predicate], URIRef(usubClass))) t.add((ut[tr], DC[predicate], URIRef(usubClass))) relation = 'has' + predicate t.add((ut[tr], LDT.term(predicate), URIRef(usubClass))) t.add((usubClass, RDF.type, OWL.Class)) t.add((usubClass, RDFS.subClassOf, OWL.Thing)) t.add((usubClass, RDFS.subClassOf, URIRef(sty))) t.add((usubClass, RDFS.label, Literal(subClass))) #tc=Graph(store=store,identifier=usubClass) t.bind("dc", "http://http://purl.org/dc/elements/1.1/") t.bind('JFPO', LDT) t.commit() #print(t.serialize(format='pretty-xml')) t.serialize('test2.owl', format='turtle')
def parse(self, source, sink, **kwargs): # TODO: docstring w. args and return value encoding = kwargs.get('encoding') or 'utf-8' if encoding not in ('utf-8', 'utf-16'): warnings.warn("JSON should be encoded as unicode. " + "Given encoding was: %s" % encoding) base = kwargs.get('base') or sink.absolutize( source.getPublicId() or source.getSystemId() or "") context_data = kwargs.get('context') if not context_data and isinstance(source, URLInputSource): context_data = context_from_urlinputsource(source) produce_generalized_rdf = kwargs.get('produce_generalized_rdf', False) data = source_to_json(source) # NOTE: A ConjunctiveGraph parses into a Graph sink, so no sink will be # context_aware. Keeping this check in case RDFLib is changed, or # someone passes something context_aware to this parser directly. if not sink.context_aware: conj_sink = ConjunctiveGraph(store=sink.store, identifier=sink.identifier) else: conj_sink = sink to_rdf(data, conj_sink, base, context_data)
def parse_n3(term_n3): ''' Disclaimer: Quick and dirty hack using the n3 parser. ''' prepstr = ("@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n" "<urn:no_use> <urn:no_use> %s.\n" % term_n3) g = ConjunctiveGraph() g.parse(data=prepstr, format='n3') return [t for t in g.triples((None, None, None))][0][2]
def has_correct_hash(self, resource): f = RdfUtils.get_format(resource.get_filename()) cg = ConjunctiveGraph() cg.parse(data=resource.get_content(), format=f) quads = RdfUtils.get_quads(cg) h = RdfHasher.make_hash(quads, resource.get_hashstr()) return resource.get_hashstr() == h
def testNG4j(self): g = ConjunctiveGraph() trix_path = os.path.relpath( os.path.join(TEST_DIR, 'trix/ng4jtest.trix'), os.curdir) g.parse(trix_path, format="trix")
def test_transitive_dep_null_context_triples_no_imports(custom_bundle): dep_dep_desc = Descriptor.load(''' id: dep_dep includes: - http://example.com/ctx ''') dep_desc = Descriptor.load(''' id: dep dependencies: - dep_dep ''') test_desc = Descriptor.load(''' id: test dependencies: - dep ''') depgraph = ConjunctiveGraph() ctx_graph = depgraph.get_context('http://example.com/ctx') quad = (URIRef('http://example.org/sub'), URIRef('http://example.org/prop'), URIRef('http://example.org/obj'), ctx_graph) depgraph.add(quad) with custom_bundle(dep_dep_desc, graph=depgraph) as depdepbun, \ custom_bundle(dep_desc, bundles_directory=depdepbun.bundles_directory) as depbun, \ custom_bundle(test_desc, bundles_directory=depbun.bundles_directory) as testbun, \ Bundle('test', bundles_directory=testbun.bundles_directory) as bnd: assert set([quad[:3]]) == set(bnd.rdf.triples((None, None, None)))
def test_01_query(self): g = ConjunctiveGraph(self.store) count = 0 for statement in g.triples((None, None, None)): count += 1 break assert count == 1, "Should have found at least one triple"
def test_add_to_graph_not_supported(custom_bundle): dep_desc = Descriptor.load(''' id: dep includes: - http://example.com/ctx ''') test_desc = Descriptor.load(''' id: test dependencies: - dep ''') depgraph = ConjunctiveGraph() ctx_graph = depgraph.get_context('http://example.com/ctx') quad = (URIRef('http://example.org/sub'), URIRef('http://example.org/prop'), URIRef('http://example.org/obj'), ctx_graph) depgraph.add(quad) with custom_bundle(dep_desc, graph=depgraph) as depbun, \ custom_bundle(test_desc, bundles_directory=depbun.bundles_directory) as testbun, \ Bundle('test', bundles_directory=testbun.bundles_directory) as bnd: with pytest.raises(ZODB.POSException.ReadOnlyError): with transaction.manager: bnd.rdf.add((URIRef('http://example.org/sub'), URIRef('http://example.org/prop'), URIRef('http://example.org/obj')))
def parse(self, source, sink, **kwargs): # TODO: docstring w. args and return value encoding = kwargs.get("encoding") or "utf-8" if encoding not in ("utf-8", "utf-16"): warnings.warn( "JSON should be encoded as unicode. " + "Given encoding was: %s" % encoding ) base = kwargs.get("base") or sink.absolutize( source.getPublicId() or source.getSystemId() or "" ) context_data = kwargs.get("context") if not context_data and isinstance(source, URLInputSource): context_data = context_from_urlinputsource(source) try: version = float(kwargs.get("version", "1.0")) except ValueError: version = None generalized_rdf = kwargs.get("generalized_rdf", False) data = source_to_json(source) # NOTE: A ConjunctiveGraph parses into a Graph sink, so no sink will be # context_aware. Keeping this check in case RDFLib is changed, or # someone passes something context_aware to this parser directly. if not sink.context_aware: conj_sink = ConjunctiveGraph(store=sink.store, identifier=sink.identifier) else: conj_sink = sink to_rdf(data, conj_sink, base, context_data, version, generalized_rdf)
def main(): parser = argparse.ArgumentParser( description='OMIA integration test', formatter_class=argparse.RawTextHelpFormatter) parser.add_argument( '--input', '-i', type=str, required=True, help='Location of input ttl file') args = parser.parse_args() graph = ConjunctiveGraph() graph.parse(args.input, format=rdflib_util.guess_format(args.input)) model_of = URIRef('http://purl.obolibrary.org/obo/RO_0003301') models = graph.subject_objects(model_of) model_len = len(list(models)) if model_len < EXPECTED_PAIRS: logger.error("Not enough model_of predicates in graph:" " {} expected {} check omia log for" " warnings".format(model_len, EXPECTED_PAIRS)) exit(1) else: logger.info("PASSED")
def __init__(self, dir, f='index.rdf', uri=None): self.graph = ConjunctiveGraph() self._uri = uri self.dir = dir self.filename = os.path.join(dir, f) print(self.filename) self.graph.parse(self.filename) self.terms = [] self.uterms = [] # should also load translations here? # and deal with a base-dir? ##if f != None: ## self.index() self.ns_list = { "http://www.w3.org/1999/02/22-rdf-syntax-ns#": "rdf", "http://www.w3.org/2000/01/rdf-schema#": "rdfs", "http://www.w3.org/2002/07/owl#": "owl", "http://www.w3.org/2001/XMLSchema#": "xsd", "http://rdfs.org/sioc/ns#": "sioc", "http://xmlns.com/foaf/0.1/": "foaf", "http://purl.org/dc/elements/1.1/": "dc", "http://purl.org/dc/terms/": "dct", "http://usefulinc.com/ns/doap#": "doap", "http://www.w3.org/2003/06/sw-vocab-status/ns#": "status", "http://purl.org/rss/1.0/modules/content/": "content", "http://www.w3.org/2003/01/geo/wgs84_pos#": "geo", "http://www.w3.org/2004/02/skos/core#": "skos", "http://purl.org/NET/c4dm/event.owl#": "event", "http://www.w3.org/2004/03/trix/rdfg-1/": "rdfg", "http://purl.org/net/provenance/ns#": "prv", "http://www.ontologydesignpatterns.org/ont/web/irw.owl#": "irw", }
def make_rdf_graph(movies): mg = ConjunctiveGraph() mg.bind('fb', FB) mg.bind('dc', DC) for movie in movies: # Make a movie node movie_node = IVA_MOVIE[movie['id']] mg.add((movie_node, DC['title'], Literal(movie['title']))) # Make the director node, give it a name and link it to the movie dir_node = IVA_PERSON[movie['director']['id']] mg.add((movie_node, FB['film.film.directed_by'], dir_node)) mg.add((dir_node, DC['title'], Literal(movie['director']['name']))) for actor in movie['actors']: # The performance node is a blank node -- it has no URI performance = BNode() # The performance is connected to the actor and the movie actor_node = IVA_PERSON[actor['id']] mg.add((actor_node, DC['title'], Literal(actor['name']))) mg.add((performance, FB['film.performance.actor'], actor_node)) # If you had the name of the role, you could also add it to the # performance node, e.g. # mg.add((performance,FB['film.performance.role'],Literal('Carrie Bradshaw'))) mg.add((movie_node, FB['film.film.performances'], performance)) return mg
def configure_database(self): """ Database configuration should be set here """ self.NS = NS self.NS.local = rdflib.Namespace(self.config['lod_prefix']+'/') self.admin_db = database.engine_from_config(self.config, "admin_") self.db = database.engine_from_config(self.config, "knowledge_") self.db.app = self self.vocab = ConjunctiveGraph() #print URIRef(self.config['vocab_file']) default_vocab = Graph(store=self.vocab.store) default_vocab.parse("default_vocab.ttl", format="turtle", publicID=str(self.NS.local)) custom_vocab = Graph(store=self.vocab.store) custom_vocab.parse(self.config['vocab_file'], format="turtle", publicID=str(self.NS.local)) self.datastore = WhyisUserDatastore(self.admin_db, {}, self.config['lod_prefix']) self.security = Security(self, self.datastore, register_form=ExtendedRegisterForm) self.file_depot = DepotManager.get('files') if self.file_depot is None: DepotManager.configure('files', self.config['file_archive']) self.file_depot = DepotManager.get('files') if DepotManager.get('nanopublications') is None: DepotManager.configure('nanopublications', self.config['nanopub_archive'])
def get_conjunctivegraph(quads): cg = ConjunctiveGraph() # for (c, s, p, o) in quads: # cg.default_context = Graph(store=cg.store, identifier=c) # cg.add((s, p, o)) cg.addN([(s, p, o, Graph(store=cg.store, identifier=c)) for (c, s, p, o) in quads]) return cg
def update(nanopub_uri): '''gets called whenever there is a change in the knowledge graph. Performs a breadth-first knowledge expansion of the current change.''' #print "Updating on", nanopub_uri #if not app.nanopub_manager.is_current(nanopub_uri): # print("Skipping retired nanopub", nanopub_uri) # return nanopub = app.nanopub_manager.get(nanopub_uri) nanopub_graph = ConjunctiveGraph(nanopub.store) if 'inferencers' in self.config: for name, service in list(self.config['inferencers'].items()): service.app = self if service.query_predicate == self.NS.whyis.updateChangeQuery: if service.getInstances(nanopub_graph): print("invoking", name, nanopub_uri) process_nanopub.apply_async(kwargs={ 'nanopub_uri': nanopub_uri, 'service_name': name }, priority=1) for name, service in list(self.config['inferencers'].items()): service.app = self if service.query_predicate == self.NS.whyis.globalChangeQuery: process_resource.apply_async( kwargs={'service_name': name}, priority=5)
def test_issue_250(self): """ https://github.com/RDFLib/rdflib/issues/250 When I have a ConjunctiveGraph with the default namespace set, for example import rdflib g = rdflib.ConjunctiveGraph() g.bind(None, "http://defaultnamespace") then the Trix serializer binds the default namespace twice in its XML output, once for the Trix namespace and once for the namespace I used: print(g.serialize(format='trix').decode('UTF-8')) <?xml version="1.0" encoding="utf-8"?> <TriX xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="http://defaultnamespace" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns="http://www.w3.org/2004/03/trix/trix-1/" /> """ graph = ConjunctiveGraph() graph.bind(None, "http://defaultnamespace") sg = graph.serialize(format="trix") self.assertTrue('xmlns="http://defaultnamespace"' not in sg, sg) self.assertTrue('xmlns="http://www.w3.org/2004/03/trix/trix-1/' in sg, sg)
def configure_database(self): """ Database configuration should be set here """ self.NS = NS self.NS.local = rdflib.Namespace(self.config['lod_prefix'] + '/') self.admin_db = database.engine_from_config(self.config, "admin_") self.db = database.engine_from_config(self.config, "knowledge_") self.db.app = self self.vocab = ConjunctiveGraph() #print URIRef(self.config['vocab_file']) default_vocab = Graph(store=self.vocab.store) default_vocab.parse(source=os.path.abspath( os.path.join(os.path.dirname(__file__), "default_vocab.ttl")), format="turtle", publicID=str(self.NS.local)) custom_vocab = Graph(store=self.vocab.store) custom_vocab.parse(self.config['vocab_file'], format="turtle", publicID=str(self.NS.local)) self.datastore = WhyisUserDatastore(self.admin_db, {}, self.config['lod_prefix']) self.security = Security(self, self.datastore, register_form=ExtendedRegisterForm)
def testSpec(self): g = ConjunctiveGraph() trix_path = os.path.relpath( os.path.join(TEST_DIR, 'trix/nokia_example.trix'), os.curdir) g.parse(trix_path, format="trix")
def compute_obsels(self, computed_trace, from_scratch=False): """I implement :meth:`.interface.IMethod.compute_obsels`. """ diag = Diagnosis("sparql.compute_obsels") source = computed_trace.source_traces[0] parameters = computed_trace.parameters_as_dict parameters["__destination__"] = computed_trace.uri parameters["__source__"] = source.uri scope = parameters.get('scope', 'trace') try: if scope == 'store': data = ConjunctiveGraph(source.service.store) elif scope == 'base': data = PrefixConjunctiveView(source.base.uri, source.service.store) else: # scope == 'trace' data = source.obsel_collection.get_state({"refresh": "no"}) sparql = parameters["sparql"] % parameters result = data.query(sparql, base=source.obsel_collection.uri).graph replace_obsels(computed_trace, result, ("inherit" in parameters)) except Exception, exc: LOG.warn(traceback.format_exc()) diag.append(unicode(exc))