Create individuals without specifying the class

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

Create individuals without specifying the class

martingrant
Hi there I'm wondering if the following is possible:

I want to create individuals and add them to an ontology without having to declare their class in Python first. Currently I have to do for example:

class Laptop(Thing):
    pass:

...

newLaptop = Laptop(...)

But what I need is to be able to create individuals of any class specified in the ontology without having to declare them in Python too. Is this possible using Owlready?

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: Create individuals without specifying the class

William
If you mean creating classes dynamically, then use type or types.new_class
Reply | Threaded
Open this post in threaded view
|

Re: Create individuals without specifying the class

martingrant
Hi William,

Thanks for your reply. I was trying that but couldn't get it to work at first (had never used the types module) but got there in the end!

My code looks like this:

with onto:
        class Entity(Thing):
                pass

        for x in onto.classes():
                if x._name == "laptop":
                        NewClass = type(x._name, (Entity,), {"namespace" : onto})

                        newLaptop = NewClass(name = "laptop5", hasModel="toshiba")
                        print(newLaptop)
Reply | Threaded
Open this post in threaded view
|

Re: Create individuals without specifying the class

martingrant
In reply to this post by William
I have came across another problem, hopefully someone can help. My class hierarchy looks like:

Thing:
    Object:
        MechanicalObject:
        ElectronicObject:
            Laptop:
        OrganicObject:

Using the method in my last post I can successfully create an instance of Laptop by declaring the class Object as a subclass of the overall Thing class. This is fine however when I save and look at the ontology in Protege it places Laptop as a direct subclass of both Object and ElectronicObject, but I only want it to be a direct subclass of ElectronicObject.

Does this mean I would have to iteratively recreate the whole class hierarchy in Python to achieve this?

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: Create individuals without specifying the class

Jiba
Administrator
Hi,

I discovered the same problem yesterday and I fixed it in the development version. It is caused by the fact of redefining the class : in this case, Owlready "continue" and extend  the previous definition, possibly adding Thing in the superclasses if the new definition has Thing as a parent class.

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

Re: Create individuals without specifying the class

toflynn
Hi,
I have a related question to create Individuals without a class. I am a student and learning, checking if this should be possible?

What I trying to do is the following.
In protege I can create an Individual without specifying a class.
I can add object and data properties in the instance data.

I can start the reasoner and it can infer the class by the object and data properties.
Synching the reasoner updates the individuals with the infer classes.

What I am trying to do is mimic this behavior programmatically.
Is this possible using the current implementation.
Any advice welcome
Tim
Reply | Threaded
Open this post in threaded view
|

Re: Create individuals without specifying the class

Jiba
Administrator
Hi,

I think you can just use the Thing class for creating your individuals. It is the most general class and all individuals must belong to it (or they are inconsistent).

Jiba