RDFLib / rdflib

RDFLib is a Python library for working with RDF, a simple yet powerful language for representing information.
https://rdflib.readthedocs.org
BSD 3-Clause "New" or "Revised" License
2.11k stars 546 forks source link

No documentation for programmatically adding IRI to ontology #817

Open rsgoncalves opened 6 years ago

rsgoncalves commented 6 years ago

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.

rsgoncalves commented 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)))

lbocken commented 2 years ago

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 ----> 1 ontology = Ontology(identifier=URIRef("http://my.ontology.iri"), graph=self.g)

NameError: name 'self' is not defined

ghost commented 2 years ago

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.

lbocken commented 2 years ago

Any help would be appreciated : https://stackoverflow.com/questions/72050219/how-to-add-an-ontology-iri-with-the-python-rdflib-library

ghost commented 2 years ago

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: polti-00

lbocken commented 2 years ago

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 9 g.bind('polti', POLTI) 10 ---> 11 o = rdflib.infixowl.Ontology( 12 identifier=nsuri, 13 graph=g,

AttributeError: module 'rdflib' has no attribute 'infixowl'

ghost commented 2 years ago

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(

lbocken commented 2 years ago

It worked with :

from rdflib.extras.infixowl import Ontology Thanks !