Can't create a subclass into a loaded OWL class

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

Can't create a subclass into a loaded OWL class

gbaesso
Dear Mr Jean-Baptiste Lamy,

First of all, congratulations for what you have accomplished with owlready! I was looking for a way to work with ontologies directly from python and it fitted like a glove! It will probably be a key tool on my Masters degree essay, so I´ll definetily cite and exalt your work.

Righ now I´m working on the "conversion" of some JSON schemas into a very basic ontology that I will enrich afterwards, and load data to it, but I am facing some challenges here. I will really apreciate some help from you or the community.

If I load an OWL file, even one created with owlready, and I try to create a new subclass, python says the main class is not defined.

Eg: My ontology has a main class named "Fruit"
I load the ontology, and try

with onto:
   class Apple(Fruit): pass

Python error is:
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
NameError: name 'Fruit' is not defined

But if I redefine 'Fruit' within python, I can then create all the subclasses.

What am I doing wrong?

Thanks in advance for your attention!

Best Regards,
GB

Reply | Threaded
Open this post in threaded view
|

Re: Can't create a subclass into a loaded OWL class

Jiba
Administrator
Hello,

You need to prefix Fruit with the ontology name, for example:

onto = get_ontology("...<IRI>...").load()

with onto:
    class Apple(onto.Fruit): pass


The ontology acts like a Python module.

Best regards,
Jean-Baptiste Lamy
MCF, LIMICS, Université Paris 13

> Dear Mr Jean-Baptiste Lamy,
>
> First of all, congratulations for what you have accomplished with owlready!
> I was looking for a way to work with ontologies directly from python and it
> fitted like a glove! It will probably be a key tool on my Masters degree
> essay, so I´ll definetily cite and exalt your work.
>
> Righ now I´m working on the "conversion" of some JSON schemas into a very
> basic ontology that I will enrich afterwards, and load data to it, but I am
> facing some challenges here. I will really apreciate some help from you or
> the community.
>
> If I load an OWL file, even one created with owlready, and a try to create a
> new subclass, python says the main class is not defined.
>
> Eg: My ontology has a main class named "Fruit"
> I load the ontology, and try
>
> with onto:
>    class Apple(Fruit): pass
>
> Python error is:
> Traceback (most recent call last):
>   File "<stdin>", line 2, in <module>
> NameError: name 'Fruit' is not defined
>
> But if I redefine 'Fruit' within python, I can then create all the
> subclasses.
>
> What am I doing wrong?
>
> Thanks in advance for your attention!
>
> Best Regards,
> GB
>
>
>
>
>
> _______________________________________________
> If you reply to this email, your message will be added to the discussion below:
> http://owlready.8326.n8.nabble.com/Can-t-create-a-subclass-into-a-loaded-OWL-class-tp41.html
> To start a new topic under Owlready, email [hidden email]
> To unsubscribe from Owlready, visit
Reply | Threaded
Open this post in threaded view
|

Re: Can't create a subclass into a loaded OWL class

gbaesso
Mr. Lamy,

Thank you very much for your fast response!

When I use the ontology name it gives me a different error:

>>> with onto:
...     class Apple(onto.Fruit): pass
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: NoneType takes no arguments

And then again, if I do a "class Fruit(Thing): pass" first, the previous command works flawlesly.

:/


2017-11-14 6:21 GMT-02:00 Jiba [via Owlready] <[hidden email]>:
Hello,

You need to prefix Fruit with the ontology name, for example:

onto = get_ontology("...<IRI>...").load()

with onto:
    class Apple(onto.Fruit): pass


The ontology acts like a Python module.

Best regards,
Jean-Baptiste Lamy
MCF, LIMICS, Université Paris 13

> Dear Mr Jean-Baptiste Lamy,
>
> First of all, congratulations for what you have accomplished with owlready!
> I was looking for a way to work with ontologies directly from python and it
> fitted like a glove! It will probably be a key tool on my Masters degree
> essay, so I´ll definetily cite and exalt your work.
>
> Righ now I´m working on the "conversion" of some JSON schemas into a very
> basic ontology that I will enrich afterwards, and load data to it, but I am
> facing some challenges here. I will really apreciate some help from you or
> the community.
>
> If I load an OWL file, even one created with owlready, and a try to create a
> new subclass, python says the main class is not defined.
>
> Eg: My ontology has a main class named "Fruit"
> I load the ontology, and try
>
> with onto:
>    class Apple(Fruit): pass
>
> Python error is:
> Traceback (most recent call last):
>   File "<stdin>", line 2, in <module>
> NameError: name 'Fruit' is not defined
>
> But if I redefine 'Fruit' within python, I can then create all the
> subclasses.
>
> What am I doing wrong?
>
> Thanks in advance for your attention!
>
> Best Regards,
> GB
>
>
>
>
>
> _______________________________________________
> If you reply to this email, your message will be added to the discussion below:
> http://owlready.8326.n8.nabble.com/Can-t-create-a-subclass-into-a-loaded-OWL-class-tp41.html
> To start a new topic under Owlready, email [hidden email]
> To unsubscribe from Owlready, visit


To unsubscribe from Can't create a subclass into a loaded OWL class, click here.
NAML

Reply | Threaded
Open this post in threaded view
|

Re: Can't create a subclass into a loaded OWL class

Jiba
Administrator
Hello,

Can you check that onto.Fruit really exists ? If it cannot be found, it returns None, and causes the error you get.
If onto.Fruit is None, there can be several possible explanation :
 * it does not exist in the ontology
 * the ontology has not been loaded
 * it is not located in the same namespace that the ontology (in this case, you need a namespace for accessing it)

The program should look like this:


onto = get_ontology("xxx.owl").load() # Do not forget to load the ontology

print(onto.Fruit)

with onto:
     class T(onto.Fruit): pass


Kind regards,
Jean-Baptiste Lamy
MCF, LIMICS, Université Paris 13

> Mr. Lamy,
>
> Thank you very much for your fast response!
>
> When I use the ontology name it gives me a different error:
>
> >>> with onto:  
> ...     class Apple(onto.Fruit): pass
> ...
> Traceback (most recent call last):
>   File "<stdin>", line 2, in <module>
> TypeError: NoneType takes no arguments
>
> And then again, if I do a "class Fruit(Thing): pass" first, the previous
> command works flawlesly.
>
> :/
>
>
> 2017-11-14 6:21 GMT-02:00 Jiba [via Owlready] <[hidden email]>
> :
>
> > Hello,
> >
> > You need to prefix Fruit with the ontology name, for example:
> >
> > onto = get_ontology("...<IRI>...").load()
> >
> > with onto:
> >     class Apple(onto.Fruit): pass
> >
> >
> > The ontology acts like a Python module.
> >
> > Best regards,
> > Jean-Baptiste Lamy
> > MCF, LIMICS, Université Paris 13
> >  
> > > Dear Mr Jean-Baptiste Lamy,
> > >
> > > First of all, congratulations for what you have accomplished with  
> > owlready!  
> > > I was looking for a way to work with ontologies directly from python and  
> > it  
> > > fitted like a glove! It will probably be a key tool on my Masters degree
> > > essay, so I´ll definetily cite and exalt your work.
> > >
> > > Righ now I´m working on the "conversion" of some JSON schemas into a  
> > very  
> > > basic ontology that I will enrich afterwards, and load data to it, but I  
> > am  
> > > facing some challenges here. I will really apreciate some help from you  
> > or  
> > > the community.
> > >
> > > If I load an OWL file, even one created with owlready, and a try to  
> > create a  
> > > new subclass, python says the main class is not defined.
> > >
> > > Eg: My ontology has a main class named "Fruit"
> > > I load the ontology, and try
> > >
> > > with onto:
> > >    class Apple(Fruit): pass
> > >
> > > Python error is:
> > > Traceback (most recent call last):
> > >   File "<stdin>", line 2, in <module>
> > > NameError: name 'Fruit' is not defined
> > >
> > > But if I redefine 'Fruit' within python, I can then create all the
> > > subclasses.
> > >
> > > What am I doing wrong?
> > >
> > > Thanks in advance for your attention!
> > >
> > > Best Regards,
> > > GB
> > >
> > >
> > >
> > >
> > >
> > > _______________________________________________
> > > If you reply to this email, your message will be added to the discussion  
> > below:  
> > > http://owlready.8326.n8.nabble.com/Can-t-create-a- 
> > subclass-into-a-loaded-OWL-class-tp41.html  
> > > To start a new topic under Owlready, email [hidden email]  
> > <http:///user/SendEmail.jtp?type=node&node=42&i=0>  
> > > To unsubscribe from Owlready, visit  
> >
> > ------------------------------
> > If you reply to this email, your message will be added to the discussion
> > below:
> > http://owlready.8326.n8.nabble.com/Can-t-create-a-
> > subclass-into-a-loaded-OWL-class-tp41p42.html
> > To unsubscribe from Can't create a subclass into a loaded OWL class, click
> > here
> > <
> > .
> > NAML
> > <
http://owlready.8326.n8.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
> >  
>
>
>
>
> _______________________________________________
> If you reply to this email, your message will be added to the discussion below:
> http://owlready.8326.n8.nabble.com/Can-t-create-a-subclass-into-a-loaded-OWL-class-tp41p43.html
> To start a new topic under Owlready, email [hidden email]
> To unsubscribe from Owlready, visit
Reply | Threaded
Open this post in threaded view
|

Re: Can't create a subclass into a loaded OWL class

gbaesso
This post was updated on .
Bonjour Mr. Lamy,

Looks like it´s related to the namespace, because when I try
to print(onto.Fruit) it says "None".

I have started from scratch to test this. Let me show you how I am creating
the ontology:

from owlready2 import *

onto = get_ontology("fruits_example.owl") # Does not exist yet

with onto:
    class Fruits(Thing): pass   # There's no difference if I use the
namespace attribute

onto.save() # Create the file from scratch

It results in the following RDF exerpt:

  <owl:Class rdf:about="#Fruits">
    <rdfs:subClassOf rdf:resource="http://www.w3.org/2002/07/owl#Thing"/>
  </owl:Class>

The rdf:resource should be my own owl, not the w3.org Thing, right? I still
don´t get what I´m doing wrong.

I´m really sorry for this very basic questions. I am fairly new to
ontologies and my python programming is a little rusty. I am studying hard
everyday to accomplish my goals, and your help is from inestimate value.

Thanks again for your attention!

Kind Regards,
Guilherme
Reply | Threaded
Open this post in threaded view
|

Re: Can't create a subclass into a loaded OWL class

Jiba
Administrator
Hello,

You need to specify a full IRI for your ontology, for example:

onto = get_ontology("http://test.org/fruits_example.owl")

and not just "fruits_example.owl".
Using a full IRI, w3.org should no longer appear in the class identifiers.

Best regards,
Jean-Baptiste Lamy
MCF, LIMICS, Université Paris 13
Reply | Threaded
Open this post in threaded view
|

Re: Can't create a subclass into a loaded OWL class

gbaesso
This post was updated on .
Thank you very much Mr. Lamy! That did the job (but it stills reference Thing to w3.org)!  :)

I still don´t understand IRI completely. Is it "just" a name and the URL style is just a way to help making it unique? Or the URL is supposed to be available under some circumstances?

Although owlready is an ingenious approach to the ontology handling, for people less skilled in the topic, like me, it can bring some confusion when associating with pyhton classes. For example, I undertsand that a subclass will inherit all attributes and methods from the parent class. But when dealing with an ontology, will a subclass inherit the parent class relations, as well (I mean, the axioms)?

Would you have a suggestion of a didatic reading to get a deeper and practical understanding of ontologies?

Regarding the code, as I told in my initial message, I am converting JSON schema and data to an ontology model. So, I am creating classes and subclasses dinamically. I know I could use the sintax below to create classes based on a variable name...

>>> NewClass = types.new_class("NewClassName", (SuperClass,), kwds = { "namespace" : my_ontology })

But I still have to explicitly define the SuperClass, and that wont work for my use case. So, I am using the following work around to circunvent the problem:

def add_class(name, parent, onto):
    with onto:
        exec('class {}({}): pass'.format(name, parent))

This is probably not a good practice. It would be elegant to have a method to create new classes passing dynamically its own name and its parent name.

Thanks again for your time and patience,

Kind Regards,
Guilherme

Reply | Threaded
Open this post in threaded view
|

Re: Can't create a subclass into a loaded OWL class

Jiba
Administrator
Bonjour,

> I still don´t understand URI completely. Is it "just" a name and the URL
> style is just a way to help making it unique? Or the URL is supposed to be
> available under some circumstances?

The URL of an ontology is used to obtain the OWL file when it is not available locally. If the file is available locally, the URL is only used as a name.


> I undertsand that a subclass will inherit all attributes and methods from
> the parent class. But when dealing with an ontology, will a subclass inherit
> the parent class relations, as well (I mean, the axioms)?

Yes, all axioms are inherited.


> Would you have a suggestion of a didatic reading to get a deeper and
> practical understanding of ontologies?

You could look at the "pizza" tutorial, or its French below:

        https://protegewiki.stanford.edu/wiki/Protege4Pizzas10Minutes
        https://www.researchgate.net/publication/318208041_Creation_d%27une_ontologie_en_OWL_avec_Protege_45


> Would you have a suggestion of a didatic reading to get a deeper and
> practical understanding of ontologies?

> But I still have to explicitly define the SuperClass, and that wont work for
> my use case. So, I am using the following work around to circunvent the
> problem:
>
> def add_class(name, parent, onto):
>     with onto:
>         exec('class {}({}): pass'.format(name, parent))
>
> This is probably not a good practice. It would be elegant to have a method
> to create new classes passing dynamically its own name and its parent name.

You can use the getattr() Python function to obtain a Class via its name:

        getattr(onto, "class_name")


Kind regards,
Jean-Baptiste Lamy
MCF, LIMICS, Université Paris 13

> Thank you very much Mr. Lamy! That did the job! :)
>
> I still don´t understand URI completely. Is it "just" a name and the URL
> style is just a way to help making it unique? Or the URL is supposed to be
> available under some circumstances?
>
> Although owlready is an ingenious approach to the ontology handling, for
> people less skilled in the topic, like me, it can bring some confusion when
> associating with pyhton classes.
>
> I undertsand that a subclass will inherit all attributes and methods from
> the parent class. But when dealing with an ontology, will a subclass inherit
> the parent class relations, as well (I mean, the axioms)?
>
> Would you have a suggestion of a didatic reading to get a deeper and
> practical understanding of ontologies?
>
> Regarding the code, as I told in my initial message, I am converting JSON
> schema and data to an ontology model. So, I am creating classes and
> subclasses dinamically. I know I could use the sintax below to create
> classes based on a variable name...
>
> >>> NewClass = types.new_class("NewClassName", (SuperClass,), kwds = {
> >>> "namespace" : my_ontology })  
>
> But I still have to explicitly define the SuperClass, and that wont work for
> my use case. So, I am using the following work around to circunvent the
> problem:
>
> def add_class(name, parent, onto):
>     with onto:
>         exec('class {}({}): pass'.format(name, parent))
>
> This is probably not a good practice. It would be elegant to have a method
> to create new classes passing dynamically its own name and its parent name.
>
> Thanks again for your time and pattience,
>
> Kind Regards,
> Guilherme
>
>
>
>
>
> _______________________________________________
> If you reply to this email, your message will be added to the discussion below:
> http://owlready.8326.n8.nabble.com/Can-t-create-a-subclass-into-a-loaded-OWL-class-tp41p47.html
> To start a new topic under Owlready, email [hidden email]
> To unsubscribe from Owlready, visit
Reply | Threaded
Open this post in threaded view
|

Re: Can't create a subclass into a loaded OWL class

gbaesso
Thank you, Mr. Lamy!
Reply | Threaded
Open this post in threaded view
|

Re: Can't create a subclass into a loaded OWL class

jui gram
In reply to this post by gbaesso
Hi GB,

I am also looking at converting JSON schemas into ontology.Can you please guide me towards what kind of mapping rules you used.

I have used Owlready2 before to convert relational database into owl which I did based on some research papers which mapped between the two.

I am kind of looking into similar  help for doing JSON to OWL.

I would really appreciate your help.

Regards,
Jui