[docs]classCapabilitySkillOntology:""" This class contains related methods of the Capability-Skill ontology. """def__init__(self):self.ontology:Ontology=None
[docs]asyncdefinitialize_ontology(self):""" This method initializes the Capability-Skill ontology loading the definition of the OWL ontology in the file stored in the config folder. """# Import only ontologies in RDF/XML, OWL/XML or NTriples format (others do not work, e.g. Turtle)self.ontology=get_ontology(CapabilitySkillOntologyUtils.get_ontology_file_path())try:self.ontology.load()exceptFileNotFoundErrorase:self.ontology=NoneraiseCriticalError("The OWL file of the ontology does not exist.")exceptOwlReadyOntologyParsingErrorase:if'NTriples'ine.args[0]:raiseCriticalError("ERROR: The OWL file is invalid (only RDF/XML, OWL/XML or NTriples format are accepted).")if'Cannot download'ine.args[0]:raiseCriticalError("ERROR: The OWL has defined imported ontologies that cannot be downloaded.")self.ontology=Nonereturn# The ontology has been loaded_logger.info("CSS ontology initialized")
[docs]asyncdefexecute_ontology_reasoner(self,debug=False):""" This method executes the reasoner for the ontology. Args: debug (bool): if it is true, the inconsistency of the ontology is explained. """debug_value=1ifdebugisTrue:debug_value=2try:withself.ontology:sync_reasoner_pellet(debug=debug_value)exceptOwlReadyInconsistentOntologyErrorase:print("ERROR: INCONSISTENT ONTOLOGY!")print(e)raiseOwlReadyInconsistentOntologyError(e)
[docs]asyncdefget_ontology_class_by_iri(self,class_iri):""" This method gets the class within the ontology by its IRI. Args: class_iri (str): the IRI of the class to be found. Returns: object: ontology class object. """result_classes=self.ontology.search(iri=class_iri)iflen(result_classes)==0:_logger.error("ERROR: class not found with IRI [{}]".format(class_iri))returnNoneiflen(result_classes)>1:_logger.warning("WARNING: THERE IS MORE THAN ONE CLASS WITH IRI [{}], BE CAREFUL".format(class_iri))returnresult_classes[0]
[docs]asyncdefcreate_ontology_object_instance(self,class_object,instance_name):""" This method creates a new object instance (individual) within the ontology. To this end, a ThingClass is required. Args: class_object (ThingClass): ontology class of the instance. instance_name (str): name of the instance. Returns: ThingClass: created instance object. """ifnotisinstance(class_object,ThingClass):raiseOntologyInstanceCreationError("ERROR: the instance cannot be created because the given constructor"" is not of ThingClass.")else:# The object of the class is used as constructor of the instancereturnclass_object(instance_name)
[docs]asyncdefget_ontology_instance_by_name(self,instance_name):""" This method returns an object instance within the ontology by its name. Args: instance_name (str): name of the instance. Returns: ThingClass: class of the instance to be found (None if it is not found). """forinstance_classinself.ontology.individuals():ifinstance_class.name==instance_name:returninstance_classreturnNone
[docs]asyncdefget_ontology_instances_by_class_iri(self,class_iri):""" This method gets all the instances within the ontology with a given class IRI. Args: class_iri (str): the IRI of the class to be found. Returns: list: list of ontology instance objects. """instances_list=[]forinstanceinself.ontology.individuals():forparent_classininstance.is_a:ifparent_class.iri==class_iri:instances_list.append(instance)iflen(instances_list)==0:returnNonereturninstances_list
[docs]asyncdefadd_object_property_to_instances_by_names(self,object_property_name,domain_object_name,range_object_name):""" This method adds a new ObjectProperty to link two instances within the ontology. Checks are performed to ensure that the ObjectProperty is valid to link the instances. Args: object_property_name (str): name of the ObjectProperty to be added. domain_object_name (str): name of the instance to which the ObjectProperty will be added. range_object_name (str): name of the instance to be added to the domain instance within the given property. """# Preliminary checks are performedifdomain_object_nameisNoneorrange_object_nameisNone:raiseOntologyReadingError("The object property cannot be added because the domain or range are invalid.")domain_instance=awaitself.get_ontology_instance_by_name(domain_object_name)range_instance=awaitself.get_ontology_instance_by_name(range_object_name)try:getattr(domain_instance,object_property_name).append(range_instance)exceptAttributeErrorase:raiseOntologyReadingError("ERROR: The class {} does not have the attribute {}".format(domain_instance,object_property_name))
[docs]asyncdefcheck_instance_by_name(self,instance_name):""" This method checks an instance and removes the invalid objects. In case the instance itself is invalid, it is removed from the ontology. If any of the properties are invalid they are removed from this instance. Args: instance_name (str): name of the instance to be checked. """instance_object=awaitself.get_ontology_instance_by_name(instance_name)ifinstance_objectisNone:returntry:instance_object.check_instance()exceptOntologyCheckingAttributeErrorase:destroy_entity(e.invalid_instance)exceptOntologyCheckingPropertyErrorase:getattr(instance_object,e.concerned_property_name).remove(e.invalid_instance)
[docs]asyncdefget_all_subclasses_iris_of_class(self,owl_class):""" This method gets all IRIs of the subclasses of a given OWL class. Args: owl_class (ThingClass): OWL class of which the subclasses are to be found. Returns: list(str): list of IRIs of the subclasses of the given OWL class. """subclasses=list(owl_class.subclasses())all_subclasses_iris=[subclass.iriforsubclassinsubclasses]# The method need to be executed recursively to get all existing subclassesforsubclassinsubclasses:all_subclasses_iris.extend(awaitself.get_all_subclasses_iris_of_class(subclass))returnall_subclasses_iris