Combining ontologies and SWRL rules across multiple ontologies

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

Combining ontologies and SWRL rules across multiple ontologies

henry1412
Hello,

I have been following the Ontologies in Python textbook and have two questions:

1. Even though I have imported an ontology into another, I still cannot access the classes from the imported ontology (when I list the classes after the merge, it still only shows the original classes before the merge, but when I list the imported ontologies it shows up). Is there a way to combine ontologies so that all classes can be easily accessible just like they were created under one ontology?

2. Basically, all I am trying to do is write SWRL rules across multiple ontologies. For instance, a building ontology consists of multiple ontologies like structure, HVAC, and office space... and I want to write SWRL rules so that they can interact with each other. But I don't know how to specify namespace under SWRL rules in Owlready2, perhaps I am missing something, or is there a better way to approach it?

Thanks in advance.

Here is my script for combining ontologies:

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

with onto_person:
    class Person(Thing):
        pass

    class weight(Person >> float, FunctionalProperty):
        pass

    class size(Person >> float, FunctionalProperty):
        pass

    class bmi(Person >> float, FunctionalProperty):
        pass

    class Obese(Person):
        equivalent_to = [Person & (bmi >= 30.0)]

    imp = Imp()
    imp.set_as_rule("Person(?x), weight(?x,?w), size(?x,?s), multiply(?s2, ?s, ?s), divide(?b, ?w, ?s2) -> bmi(?x, ?b)")

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

with onto_food:
    class Food(Thing):
        pass

    class price(Food >> float, FunctionalProperty):
        pass

    class calo(Food >> float, FunctionalProperty):
        pass

    class weight(Food >> float, FunctionalProperty):
        pass

with onto_food:
    class Expensive(Food):
        equivalent_to = [Food & (price >= 30.0)]

onto_food.imported_ontologies.append(onto_person)
onto_food.save(r'Path\onto_food.owl')
onto_food = get_ontology(r'Path\onto_food.owl').load()
list(onto_food.classes())
onto_food.imported_ontologies
Reply | Threaded
Open this post in threaded view
|

Re: Combining ontologies and SWRL rules across multiple ontologies

Jiba
Administrator
Hi,

For question 1, you can use indirectly_imported_ontologies() to obtain all ontologies, and then ask for their classes, e.g.:

classes = [c for o in onto.indirectly_imported_ontologies() for c in o.classes()]


For question 2, when using set_as_rule(), you can use the optional "namespaces" arguments to pass a list of namespaces (such as ontologies) where the elements of the rules are searched for, e.g.:

    imp = Imp()
    imp.set_as_rule("Person(?x), weight(?x,?w), size(?x,?s), multiply(?s2, ?s, ?s), divide(?b, ?w, ?s2) -> price(?x, ?b)", namespaces = [onto_person, onto_food])

Jiba
Reply | Threaded
Open this post in threaded view
|

Re: Combining ontologies and SWRL rules across multiple ontologies

henry1412
Thanks, Jiba, for replying in such a timely manner! Your suggestions work, perhaps I don't need to combine the ontologies after all, thanks again, this helps a lot.

Best,
Li