How do you define a new class which is not a subclass of Thing

classic Classic list List threaded Threaded
4 messages Options
gp
Reply | Threaded
Open this post in threaded view
|

How do you define a new class which is not a subclass of Thing

gp
Hello !

I have been struggling with the following usecase:

I am extending some existing ontology by adding among other new subclasses, the resulting ontology is then imported in some more fundamental one.

Problem: the following won't work
with newOnto:
    someClass = IRIS['some IRI Of The Higher Ontology']
    class someSubClass(someClass):
        blabla

newOnto.save(path)
which makes sense as newOnto does not know about someClass.

So currently I have to do the following:
with newOnto:
    class someClass(Thing):
          pass
    someClass.iri = 'some IRI Of The Higher Ontology'

    class someSubClass(someClass):
        blabla

newOnto.save(path)

import rdflib
from rdflib.namespace import RDFS, RDF, OWL


g = rdflib.Graph()
g.parse(path)


someClass2 = rdflib.URIRef('some other IRI Of The Higher Ontology')
identifier = rdflib.URIRef('some IRI Of The Higher Ontology')
g.add((identifier,RDFS.subClassOf, someClass2))

g.remove((rdflib.URIRef('some IRI Of The Higher Ontology'),
      RDFS.subClassOf,
      OWL.Thing))

[save updated graph]
Or something similar depending on the case, ie. manually editing the graph which is cumbersome.

Is there a better way of doing this?

Cheers,
GP
Reply | Threaded
Open this post in threaded view
|

Re: How do you define a new class which is not a subclass of Thing

Jiba
Administrator
Hello,

In order to be able to inherited from someClass, someClass must be an OWL class and, in Owlready, to be an OWL class it must inherit (directly or indirectly) from Thing. This is required because many stuff regarding classes are located in the Thing class: a class that does not inherit from Thing will not behave like a class in Python.

The best solution is probably to import the higher ontology, which should define someClass as a descendant of Thing.

If imports are broken (cf your other post), we should fix that :)

Best regards,
Jiba
gp
Reply | Threaded
Open this post in threaded view
|

Re: How do you define a new class which is not a subclass of Thing

gp
Well, I actually want to extend the higher ontology (in a modular fashion) by importing the newly developed one.

Would not it be that importing the higher ontology would result in the addition of all corresponding triplets in the ontology being developed ?
Also I guess it would lead to issues as this would result in a circular reference schema, am I wrong?

Effectively it is only the triplet representation of the new class which I need to change. Can I change this on the fly (ie. force the definition my new class as subClass of some IRI rather a subClass of Thing)? Is it possible to modify on the fly the graph representation of some entity for instance, or the graph is first created upon serialization?

Cheers,
Guillaume
gp
Reply | Threaded
Open this post in threaded view
|

Re: How do you define a new class which is not a subclass of Thing

gp
I could find some workaround which I will document here.

It involves simply removing the "x rdfs:subClassOf owl:Thing " triplets and storing the resulting graph as a rdf file.
The inconvenience here is that we do not have a real owl file anymore, but this is good enough for my purpose.

Some code for inspiration:
# assuming we import the resulting file in some ontology where cl1, cl2, ... are defined
classesToModify = [cl1, cl2, ...]

g = exportWorld.as_rdflib_graph()

for cl in classesToModify:
    uriRef = rdflib.URIRef(cl.iri)

    g.remove((uriRef,
              rdflib.RDFS.subClassOf,
              rdflib.OWL.Thing))

savePath = '../path/to/export.rdf'
g.serialize(destination=savePath,
            format="n3")