Comparing classes or construction object

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

Comparing classes or construction object

fengbozheng
Hi Jiba,

Is it possible to compare two classes separately? --To see if one's definition is more general than the other one's (e.g., "has_ingredient: seed" is more general than "has_ingredient: Maize seed"). Just like classification process fulfilled by the reasoner. More specifically, I want to compare the generality between two construction object (e.g. And or Or). Is there a way for me to achieve this?

Thanks,
Fengbo
Reply | Threaded
Open this post in threaded view
|

Re: Comparing classes or construction object

William
Hi

It is possible to compare two atomic classes with A in B.is_a, where A need not be atomic. If B is not atomic, you may have to check it with your logic knowledge.

I hope that the developer would define a helper function is_a(A, B) to compare all kind of classes. (or use an operator such as A<B.)
Reply | Threaded
Open this post in threaded view
|

Re: Comparing classes or construction object

fengbozheng
Yeah, there should be a general need of checking whether a set of logical expressions is more general than the other one, either for ontology construction or validation purpose. When it comes to "A in B.is_a", it is quite limited as A and B are required to be classes. Of course, I could add them as new classes, run the reasoner, and then see if the new classes for B is a subclass of the new class for A, but it is quite awkward and time consuming (running the reasoner for whole ontology, remove classes after, etc. ). Just need a more elegant way to do this.
Reply | Threaded
Open this post in threaded view
|

Re: Comparing classes or construction object

William
Hi ~

I defined two functions. I hope that they would be helpful. They works well in most cases.

def is_instance_of(i, c):
    # i: Thing, c:Class
    if isinstance(c, And):
        return all(is_instance_of(i, cc) for cc in c.Classes)
    elif isinstance(c, Or):
        return any(is_instance_of(i, cc) for cc in c.Classes)
    else:
        return c in i.is_instance_of or any(is_a(y, c) for y in i.INDIRECT_is_instance_of if hasattr(y, 'is_a'))

def is_a(x, c):
    # x,c: Class
    if isinstance(c, And):
        return all(is_a(i, cc) for cc in c.Classes)
    elif isinstance(c, Or):
        return any(is_a(i, cc) for cc in c.Classes)
    else:
        return c in x.is_a or any(is_a(y, c) for y in x.is_a if hasattr(y, 'is_a'))
Reply | Threaded
Open this post in threaded view
|

Re: Comparing classes or construction object

Jiba
Administrator
In reply to this post by fengbozheng
Hi,

To compare class, you can use the issubclass() Python function (reimplemented in Owlready2, so be sure to import it from owlready2). However, to fully consider OWL constructor such as Or and And, you need to perform reasoning before (with sync_reasoner()).

Jiba
Reply | Threaded
Open this post in threaded view
|

Re: Comparing classes or construction object

fengbozheng
Thanks for your reply.
For the first case, I tried the function issubclass(), it worked fine (owlready2.issubclass(ClassA, ClassB)).

For the second case, I'm not sure. Do I need to treat/create constructor as new class?
Could you explain more or provide a concrete example? Suppose I have two constructor (either And or Or) ConstructorA and ConstructorB, how could I use owlready to compare them?

Thanks,
Fengbo
Reply | Threaded
Open this post in threaded view
|

Re: Comparing classes or construction object

Jiba
Administrator
issubclass() works only for "real" classes, sorry. In Owlready, And and Or are not "real" Python classes, because they are usually not directly used as classes in ontology (although they are theorically classes, indeed).

A possible solution (but not "light") is to create classes equivalent to your And and Or, because calling the reasoner.

Jiba