Re: Problem when instantiating sub-class of owl.Thing

Posted by cracoras on
URL: http://owlready.306.s1.nabble.com/Problem-when-instantiating-sub-class-of-owl-Thing-tp1659p1671.html

Hi Jiba.

I figured out the 1st solution and was able to move forward it, but I like the second one better as it makes the instantiation of the subclasses simpler.

So I made the following changes in my code.


from owlready2 import *

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

with onto:
    class BusinessDocument(Thing):
        @staticmethod
        def get_class(doc_type):
            switch = {
                'MasterData': MasterData,
                'Transactional': Transactional
            }
            cls = switch.get(doc_type, lambda: "Invalid Noun Type")
            return cls
       
        def __new__ (cls, doc_id, location, color, size, doc_type):
            Thing.__new__(cls)
       
        def __init__ (self, doc_id, location, color, size, doc_type):
            self.doc_id = doc_id,
            self.location = location
            self.color = color
            self.size = size
            self.doc_type = doc_type
           
        def get_type(self):
            return self.doc_type

           
    class MasterData(BusinessDocument):  
       
        def __new__ (cls, doc_id, location, color, size):
            BusinessDocument.__new__(cls, doc_id, location, color, size, 'MasterData')
       
        def __init__ (self, doc_id, location, color, size):
            BusinessDocument.__init__(doc_id, location, color, size, 'MasterData')

    class Transactional(BusinessDocument):
       
        def __new__ (cls, doc_id, location, color, size):
            BusinessDocument.__new__(cls, doc_id, location, color, size, 'Transactional')

        def __init__ (self, doc_id, location, color, size):
            BusinessDocument.__init__(doc_id, location, color, size, 'Transactional')


    class NounClass():
        @staticmethod
        def get_class(doc_name, doc_type):
            return type(doc_name, (BusinessDocument.get_class(doc_type), ),dict())

        def __new__ (cls, doc_id, location, color, size):
            super().__new__(cls, doc_id, location, color, size)

       
        def __init__(self, doc_id, location, color, size):
            super().__init__(doc_id, location, color, size)


I am able to successfully get the new class type in the following line:

invoice_class = NounClass.get_class('Invoice', 'Transactional')

When I print the mro for it I get what seems to be the correct inheritance chain.

print(invoice_class.__mro__)

(onto.Invoice, onto.Transactional, onto.BusinessDocument, owl.Thing, <class 'object'>)


Then I try to create the new invoice object:
my_obj = invoice_class('IN-001', 'London', "Red", "Small")

But it sees like that object is None, but I get the following error when I call the type or __mro__ property for it.
print(my_obj.__mro__)

AttributeError                            Traceback (most recent call last)
<ipython-input-80-e4b41e54bf70> in <module>
     60 print(invoice_class.__mro__)
     61 my_obj = invoice_class('IN-001', 'London', "Red", "Small")
---> 62 print(my_obj.__mro__)
     63

AttributeError: 'NoneType' object has no attribute '__mro__'

Thank you in advance for any help.


MD.