From 48b7eaaf57579233070c782cd6d2490316c8aecd Mon Sep 17 00:00:00 2001 From: Alex Mitrevski Date: Sun, 8 Mar 2020 19:21:28 +0100 Subject: [PATCH] [ontology_query_interface] Added a fn for retrieving the domain and range of a property --- .../ontology_query_interface.py | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/common/mas_knowledge_utils/ontology_query_interface.py b/common/mas_knowledge_utils/ontology_query_interface.py index f3578c9..169429e 100644 --- a/common/mas_knowledge_utils/ontology_query_interface.py +++ b/common/mas_knowledge_utils/ontology_query_interface.py @@ -6,6 +6,8 @@ class URIRefConstants(object): RDF_TYPE = rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type') OWL_OBJECT_PROPERTY = rdflib.term.URIRef('http://www.w3.org/2002/07/owl#ObjectProperty') + PROPERTY_DOMAIN = rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#domain') + PROPERTY_RANGE = rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#range') class OntologyQueryInterface(object): '''Defines an interface for interacting with an OWL knowledge base. @@ -127,7 +129,7 @@ def get_all_subjects_and_objects(self, prop): triple in the ontology. Keyword arguments: - prop: str -- name of a property + @param prop: str -- name of a property ''' rdf_property = self.__format_class_name(prop) @@ -138,6 +140,27 @@ def get_all_subjects_and_objects(self, prop): for x in query_result] return subj_obj_pairs + def get_property_domain_range(self, prop): + '''Returns a pair in which the first element is the domain of the + given property and the second element is its range. + + Keyword arguments: + @param prop: str -- name of a property + + ''' + prop_domain = None + prop_range = None + rdf_property = rdflib.URIRef(self.__format_class_name(prop)) + for triple in self.knowledge_graph[:]: + if triple[0] == rdf_property: + if triple[1] == URIRefConstants.PROPERTY_DOMAIN: + prop_domain = self.__extract_obj_name(triple[2]) + elif triple[1] == URIRefConstants.PROPERTY_RANGE: + prop_range = self.__extract_obj_name(triple[2]) + if prop_domain and prop_range: + break + return (prop_domain, prop_range) + def __format_class_name(self, class_name): '''Returns a string of the format "self.class_prefix:class_name".