Open rsgoncalves opened 6 years ago
While the line of code above (alone) doesn't solve the issue, adding another statement that mentions the ontology IRI will set the ontology IRI as I wanted. I added an annotation assertion on the ontology (IRI) and then the IRI appears correctly:
g.add((ontology.identifier, RDFS.comment, Literal("generated using rdflib", datatype=XSD.string)))
I'm using RDFLib to generate triples that represent some spreadsheet data. I'd like the resulting output to contain the ontology IRI. I've tried to wrap the generated graph triples in an ontology object, like so:
ontology = Ontology(identifier=URIRef("http://my.ontology.iri"), graph=self.g)
Where
g
is the RDF graph containing generated triples. In doing the above I see no changes in the output. How can I specify the ontology IRI such that it shows up when the document is opened in Protégé, for example.
I got this error:
NameError Traceback (most recent call last)
C:\LOCAL\TEMP/ipykernel_18924/1102890418.py in
NameError: name 'self' is not defined
This issue tracker is only for technical issues related to RDFLib.
General RDFLib questions and/or support requests are best directed to Stackoverflow with the [rdflib] tag.
Any help would be appreciated : https://stackoverflow.com/questions/72050219/how-to-add-an-ontology-iri-with-the-python-rdflib-library
Okay, after ferreting around in the docs, I believe this issue can reasonably be re-opened with a different context, to wit: “There is no documentation for using RDFLib to create and work with ontologies.”
As an interim work-around ...
baseuri = rdflib.URIRef("http://purl.org/net/bel-epa/polti.owl")
nsuri = rdflib.URIRef(str(baseuri) + '#')
POLTI = rdflib.Namespace(nsuri)
g = rdflib.Graph(identifier=baseuri)
g.bind('polti', POLTI)
o = rdflib.infixowl.Ontology(
identifier=nsuri,
graph=g,
comment=rdflib.Literal(
"Georges Polti‘s Thirty-Six Dramatic Situations.", lang="en"
),
)
o.setVersion(rdflib.Literal("0.1.0", lang="en"))
print(g.serialize(format='pretty-xml'))
Which produces:
<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
>
<owl:Ontology rdf:about="http://purl.org/net/bel-epa/polti.owl#">
<rdfs:comment xml:lang="en">Georges Polti‘s Thirty-Six Dramatic Situations.</rdfs:comment>
<owl:versionInfo xml:lang="en">0.1.0</owl:versionInfo>
</owl:Ontology>
</rdf:RDF>
which, if saved to a file, can be loaded into Protege's desktop app:
Okay, after ferreting around in the docs, I believe this issue can reasonably be re-opened with a different context, to wit: “There is no documentation for using RDFLib to create and work with ontologies.”
As an interim work-around ...
baseuri = rdflib.URIRef("http://purl.org/net/bel-epa/polti.owl") nsuri = rdflib.URIRef(str(baseuri) + '#') POLTI = rdflib.Namespace(nsuri) g = rdflib.Graph(identifier=baseuri) g.bind('polti', POLTI) o = rdflib.infixowl.Ontology( identifier=nsuri, graph=g, comment=rdflib.Literal( "Georges Polti‘s Thirty-Six Dramatic Situations.", lang="en" ), ) o.setVersion(rdflib.Literal("0.1.0", lang="en")) print(g.serialize(format='pretty-xml'))
Thanks for the help... I got this error:
AttributeError Traceback (most recent call last)
C:\LOCAL\TEMP/ipykernel_18924/1345067266.py in
AttributeError: module 'rdflib' has no attribute 'infixowl'
Thanks for the help... I got this error: AttributeError: module 'rdflib' has no attribute 'infixowl'
So import Ontology
from rdflb.infixowl
and change
o = rdflib.infixowl.Ontology(
to just
o = Ontology(
It worked with :
from rdflib.extras.infixowl import Ontology
Thanks !
I'm using RDFLib to generate triples that represent some spreadsheet data. I'd like the resulting output to contain the ontology IRI. I've tried to wrap the generated graph triples in an ontology object, like so:
ontology = Ontology(identifier=URIRef("http://my.ontology.iri"), graph=self.g)
Where
g
is the RDF graph containing generated triples. In doing the above I see no changes in the output. How can I specify the ontology IRI such that it shows up when the document is opened in Protégé, for example.