From c6e6e467b0a6cf8ce8fd046c14b4fdfe55d61426 Mon Sep 17 00:00:00 2001 From: Alex Mitrevski Date: Sun, 8 Mar 2020 18:45:35 +0100 Subject: [PATCH] [ontology_query_interface] Added a fn for retrieving all subjects and objects for a given property --- .../ontology_query_interface.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/common/mas_knowledge_utils/ontology_query_interface.py b/common/mas_knowledge_utils/ontology_query_interface.py index 2d6c3d8..f3578c9 100644 --- a/common/mas_knowledge_utils/ontology_query_interface.py +++ b/common/mas_knowledge_utils/ontology_query_interface.py @@ -121,6 +121,23 @@ def get_objects_of(self, prop, subject): object_list = [self.__extract_obj_name(x[0]) for x in query_result] return object_list + def get_all_subjects_and_objects(self, prop): + '''Returns a list of pairs in which the first element is the subject + and the second element is the object of each (subject prop object) + triple in the ontology. + + Keyword arguments: + prop: str -- name of a property + + ''' + rdf_property = self.__format_class_name(prop) + query_result = self.knowledge_graph.query('SELECT ?subj ?obj ' + + 'WHERE {?subj ' + rdf_property + ' ?obj}') + subj_obj_pairs = [(self.__extract_obj_name(x[0]), + self.__extract_obj_name(x[1])) + for x in query_result] + return subj_obj_pairs + def __format_class_name(self, class_name): '''Returns a string of the format "self.class_prefix:class_name".