Skip to content

Commit

Permalink
[ontology] Made SOMA-HOME the default ontology
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigul committed Jun 3, 2024
1 parent 09929a6 commit abdc978
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/pycram/ontology/ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class OntologyManager(object, metaclass=Singleton):
Singleton class as the adapter accessing data of an OWL ontology, largely based on owlready2.
"""

def __init__(self, main_ontology_iri: str = "", ontology_search_path: str = ""):
def __init__(self, main_ontology_iri: str = None, ontology_search_path: str = ""):
"""
Create the singleton object of OntologyManager class
Expand All @@ -51,26 +51,30 @@ def __init__(self, main_ontology_iri: str = "", ontology_search_path: str = ""):
#: The main ontology instance as the result of an ontology loading operation
self.main_ontology: Optional[owlready2.Ontology] = None

#: The SOMA ontology instance, referencing :attr:`ontology` in case of ontology loading from `SOMA.owl`. Ref: http://www.ease-crc.org/ont/SOMA.owl
#: The SOMA ontology instance, referencing :attr:`ontology` in case of ontology loading from `SOMA.owl`.
# Ref: http://www.ease-crc.org/ont/SOMA.owl
self.soma: Optional[owlready2.Ontology] = None

#: The DUL ontology instance, referencing :attr:`ontology` in case of ontology loading from `DUL.owl`. Ref: http://www.ease-crc.org/ont/DUL.owl
#: The DUL ontology instance, referencing :attr:`ontology` in case of ontology loading from `DUL.owl`.
# Ref: http://www.ease-crc.org/ont/DUL.owl
self.dul: Optional[owlready2.Ontology] = None

#: Ontology world, the placeholder of triples stored by owlready2. Ref: https://owlready2.readthedocs.io/en/latest/world.html
#: Ontology world, the placeholder of triples stored by owlready2.
# Ref: https://owlready2.readthedocs.io/en/latest/world.html
self.ontology_world: Optional[owlready2.World] = None

#: Ontology IRI (Internationalized Resource Identifier), either a URL to a remote OWL file or the full name path of a local one
self.main_ontology_iri: str = main_ontology_iri
# Ontology IRI (Internationalized Resource Identifier), either a URL to a remote OWL file or the full
# name path of a local one
self.main_ontology_iri: str = main_ontology_iri if main_ontology_iri else SOMA_HOME_ONTOLOGY_IRI

#: Namespace of the main ontology
self.main_ontology_namespace: Optional[owlready2.Namespace] = None

# Create an ontology world with parallelized file parsing enabled
self.ontology_world = World(filename=f"{ontology_search_path}/{Path(main_ontology_iri).stem}.sqlite3",
self.ontology_world = World(filename=f"{ontology_search_path}/{Path(self.main_ontology_iri).stem}.sqlite3",
exclusive=False, enable_thread_parallelism=True)

self.main_ontology, self.main_ontology_namespace = self.load_ontology(main_ontology_iri)
self.main_ontology, self.main_ontology_namespace = self.load_ontology(self.main_ontology_iri)
if self.main_ontology.loaded:
self.soma = self.ontologies.get(SOMA_ONTOLOGY_NAMESPACE)
self.dul = self.ontologies.get(DUL_ONTOLOGY_NAMESPACE)
Expand Down Expand Up @@ -178,7 +182,8 @@ def save(self, target_filename: str = "", overwrite: bool = False) -> bool:
else f"{Path(self.ontology_world.filename).parent.absolute()}/{Path(self.main_ontology_iri).stem}.owl"
save_to_same_file = is_current_ontology_local and (target_filename == current_ontology_filename)
if save_to_same_file and not overwrite:
rospy.logerr(f"Ontologies cannot be saved to the originally loaded [{target_filename}] if not by overwriting")
rospy.logerr(
f"Ontologies cannot be saved to the originally loaded [{target_filename}] if not by overwriting")
return False
else:
save_filename = target_filename if target_filename else current_ontology_filename
Expand All @@ -205,7 +210,7 @@ def create_ontology_concept_class(self, class_name: str,

with self.main_ontology:
return types.new_class(class_name, (owlready2.Thing, ontology_parent_concept_class,)
if inspect.isclass(ontology_parent_concept_class) else (owlready2.Thing,))
if inspect.isclass(ontology_parent_concept_class) else (owlready2.Thing,))

@staticmethod
def create_ontology_property_class(class_name: str,
Expand Down Expand Up @@ -252,7 +257,8 @@ def get_ontology_classes_by_condition(self, condition: Callable, first_match_onl
return out_classes

@staticmethod
def get_ontology_class_by_ontology(ontology: owlready2.Ontology, class_name: str) -> Optional[Type[owlready2.Thing]]:
def get_ontology_class_by_ontology(ontology: owlready2.Ontology, class_name: str) -> Optional[
Type[owlready2.Thing]]:
"""
Get an ontology class if it exists in a given ontology
Expand Down Expand Up @@ -395,7 +401,8 @@ def create_ontology_linked_designator_by_concept(self, designator_class: Type[De
"""
ontology_concept_name = f'{object_name}_concept'
if len(OntologyConceptHolderStore().get_designators_of_ontology_concept(ontology_concept_name)) > 0:
rospy.logerr(f"A designator named [{object_name}] is already created for ontology concept [{ontology_concept_name}]")
rospy.logerr(
f"A designator named [{object_name}] is already created for ontology concept [{ontology_concept_name}]")
return None

# Create a designator of `designator_class`
Expand All @@ -410,7 +417,8 @@ def create_ontology_linked_designator_by_concept(self, designator_class: Type[De
designator = designator_class()

# Link designator with an ontology concept of `ontology_concept_class`
ontology_concept_holder = OntologyConceptHolderStore().get_ontology_concept_holder_by_name(ontology_concept_name)
ontology_concept_holder = OntologyConceptHolderStore().get_ontology_concept_holder_by_name(
ontology_concept_name)
if ontology_concept_holder is None:
ontology_concept_holder = OntologyConceptHolder(ontology_concept_class(name=ontology_concept_name,
namespace=self.main_ontology))
Expand Down

0 comments on commit abdc978

Please sign in to comment.