Checking a class is primitive or a defined class

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

Checking a class is primitive or a defined class

GoMa
Hi Jiba,

I am facing some difficulties to find out whether a class is a primitive or a defined class.

In the ontology I have created two classes to demonstrate:



    <owl:Class rdf:about="http://www.semanticweb.org/ontologies/2019/9/untitled-ontology-6#DefinedClass">
        <owl:equivalentClass>
            <owl:Restriction>
                <owl:onProperty rdf:resource="http://www.semanticweb.org/ontologies/2019/9/untitled-ontology-6#hasRelation"/>
                <owl:someValuesFrom rdf:resource="http://www.semanticweb.org/ontologies/2019/9/untitled-ontology-6#PrimitiveClass"/>
            </owl:Restriction>
        </owl:equivalentClass>
    </owl:Class>
   

   

    <owl:Class rdf:about="http://www.semanticweb.org/ontologies/2019/9/untitled-ontology-6#PrimitiveClass">
        <rdfs:subClassOf>
            <owl:Restriction>
                <owl:onProperty rdf:resource="http://www.semanticweb.org/ontologies/2019/9/untitled-ontology-6#hasRelation"/>
                <owl:someValuesFrom rdf:resource="http://www.semanticweb.org/ontologies/2019/9/untitled-ontology-6#TestClass"/>
            </owl:Restriction>
        </rdfs:subClassOf>
    </owl:Class>


The defined class is described by <owl:equivalentClass> whereas the primitive class is described by <rdfs:subClassOf>.

When I try in owlready to access this property by
Onto.DefinedClass.defined_class it returns False

For
Onto.PrimitiveClass.defined_class it returns False as well.

What am I doing wrong? Is there another way to find out whether it is a defined or a primitive class?

Thanks a lot!
Reply | Threaded
Open this post in threaded view
|

Re: Checking a class is primitive or a defined class

Jiba
Administrator
Hi,

To check whether a class has a formal defition, you can check whether Class.equivalent_to is empty or not.

Class.defined_class is used to indicate to Owlready that you consider a class as defined. If set to True, when setting class properties, Owlready will add them to equivalent_to instead of is_a. For example :

If Class.defined_class is False, Class.property.append(value) will add property.some(value) to is_a.
If Class.defined_class is True, Class.property.append(value) will add property.some(value) to equivalent_to.

Jiba
Reply | Threaded
Open this post in threaded view
|

Re: Checking a class is primitive or a defined class

GoMa
Thanks Jiba, the clue to check whether Class.equivalent_to is empty or not works fine.
Reply | Threaded
Open this post in threaded view
|

Re: Checking a class is primitive or a defined class

mareikep@cs.uni-bremen.de
In reply to this post by Jiba
Hey,
I tried something similar but came across a different problem: Once I save an ontology containing restricted classes and load it again (in another interpreter), I am facing an empty equivalent_to, even though it was shown when creating the class/setting the restrictions. The owl file looks fine so far, and Protege also shows the respective equivalent_to constraints, but I cannot access them via code. What am I doing wrong?
I prepared a minimal example here:

# in first python interpreter
from owlready2 import Thing, ConstrainedDatatype, DataProperty, FunctionalProperty, get_ontology
import functools
import types
import operator

path = '/path/to/minimal.owl'
onto = get_ontology("http://www.semanticweb.org/mareike/ontologies/2020/2/minimal#")
with onto:

    # add two exemplary properties
    prop_a = types.new_class('propA', (DataProperty, FunctionalProperty))
    prop_a.range = [float]
    prop_b = types.new_class('propB', (DataProperty, FunctionalProperty))
    prop_b.range = [float]

    # create Test class and restrictions
    tc = types.new_class('TestClass', (Thing,))
    restrictions = [functools.reduce(operator.and_, [prop_a.some(ConstrainedDatatype(float, **{'max_inclusive': 0.4, 'min_inclusive': 0.1})),
                                                     prop_b.some(ConstrainedDatatype(float, **{'min_exclusive': 0.3}))])]
    tc.set_equivalent_to(tc, restrictions)

    print('equivalent_to', list(tc.get_equivalent_to(tc)))
onto.save(file=path)

# in second python interpreter
from owlready2 import get_ontology
path = '/path/to/minimal.owl'
onto = get_ontology(path).load()
print('equivalent_to', list(onto['TestClass'].get_equivalent_to(onto['TestClass'])))

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

Re: Checking a class is primitive or a defined class

Jiba
Administrator
Hi,

The problem here is that you must avoid calling get_equivalent_to() or set_equivalent_to(), because they are defined both at the Class and the individual level. Here, you call get_equivalent_to() with a class, but actually the individual method is called (Python gives it priority).

You should rather use the .equivalent_to attribute, for example:

print('equivalent_to', onto['TestClass'].equivalent_to)

(which is also much simpler!)

Jiba
Reply | Threaded
Open this post in threaded view
|

Re: Checking a class is primitive or a defined class

mareikep@cs.uni-bremen.de
That worked like a charm, thanks! And you're right, it is much simpler and easier to read.

Cheers,
Mareike