Skip to content

Commit

Permalink
[ontology_query_interface] Added a fn for retrieving all subjects and…
Browse files Browse the repository at this point in the history
… objects for a given property
  • Loading branch information
alex-mitrevski committed Mar 8, 2020
1 parent 199012c commit c6e6e46
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions common/mas_knowledge_utils/ontology_query_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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".
Expand Down

0 comments on commit c6e6e46

Please sign in to comment.