@@ -161,6 +161,36 @@ def individuals(self, concept: Optional[OWLClassExpression] = None, implicit_ind
161161 else :
162162 return frozenset (self .ontology .individuals_in_signature ())
163163
164+ def _abox_native_triples (self , i : OWLNamedIndividual ):
165+ """Yield native (subject, predicate, object) abox triples for a single individual:
166+ rdf:type assertions, then data- and object-property assertions. Shared by every
167+ 'native'/'iri'/'axiom' formatter in :meth:`abox`.
168+ """
169+ # For now, 'rdfs:type' predicate will be represented as an IRI
170+ rdf_type = IRI .create ("http://www.w3.org/1999/02/22-rdf-syntax-ns#type" )
171+ yield from ((i , rdf_type , t ) for t in self .get_types (ind = i , direct = True ))
172+ for dp in self .get_data_properties_for_ind (ind = i ):
173+ yield from ((i , dp , literal ) for literal in self .get_data_property_values (i , dp ))
174+ for op in self .get_object_properties_for_ind (ind = i ):
175+ yield from ((i , op , ind ) for ind in self .get_object_property_values (i , op ))
176+
177+ @staticmethod
178+ def _abox_triple_as_native (s , p , o ):
179+ return s , p , o
180+
181+ @staticmethod
182+ def _abox_triple_as_iri (s , p , o ):
183+ return s .str , p .str , o .get_literal () if isinstance (o , OWLLiteral ) else o .str
184+
185+ @staticmethod
186+ def _abox_triple_as_axiom (s , p , o ):
187+ if isinstance (p , IRI ) and isinstance (o , OWLClass ):
188+ return OWLClassAssertionAxiom (s , o )
189+ elif isinstance (o , OWLLiteral ):
190+ return OWLDataPropertyAssertionAxiom (s , p , o )
191+ else :
192+ return OWLObjectPropertyAssertionAxiom (s , p , o )
193+
164194 def abox (self , individual : Union [OWLNamedIndividual , Iterable [OWLNamedIndividual ]] = None , mode = 'native' ): # pragma: no cover
165195 """
166196 Get all the abox axioms for a given individual. If no individual is given, get all abox axioms
@@ -188,36 +218,14 @@ def abox(self, individual: Union[OWLNamedIndividual, Iterable[OWLNamedIndividual
188218 else :
189219 inds = self .individuals ()
190220
221+ triple_formatters = {"native" : self ._abox_triple_as_native ,
222+ "iri" : self ._abox_triple_as_iri ,
223+ "axiom" : self ._abox_triple_as_axiom }
224+
191225 for i in inds :
192- if mode == "native" :
193- # Obtain all class assertion triples/axioms
194- # For now, 'rdfs:type' predicate will be represented as an IRI
195- yield from ((i , IRI .create ("http://www.w3.org/1999/02/22-rdf-syntax-ns#type" ), t ) for t in
196- self .get_types (ind = i , direct = True ))
197-
198- # Obtain all property assertion triples/axioms
199- for dp in self .get_data_properties_for_ind (ind = i ):
200- yield from ((i , dp , literal ) for literal in self .get_data_property_values (i , dp ))
201-
202- for op in self .get_object_properties_for_ind (ind = i ):
203- yield from ((i , op , ind ) for ind in self .get_object_property_values (i , op ))
204- elif mode == "iri" :
205- yield from ((i .str , "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" ,
206- t .str ) for t in self .get_types (ind = i , direct = True ))
207- for dp in self .get_data_properties_for_ind (ind = i ):
208- yield from ((i .str , dp .str , literal .get_literal ()) for literal in
209- self .get_data_property_values (i , dp ))
210- for op in self .get_object_properties_for_ind (ind = i ):
211- yield from ((i .str , op .str , ind .str ) for ind in
212- self .get_object_property_values (i , op ))
213- elif mode == "axiom" :
214- yield from (OWLClassAssertionAxiom (i , t ) for t in self .get_types (ind = i , direct = True ))
215- for dp in self .get_data_properties_for_ind (ind = i ):
216- yield from (OWLDataPropertyAssertionAxiom (i , dp , literal ) for literal in
217- self .get_data_property_values (i , dp ))
218- for op in self .get_object_properties_for_ind (ind = i ):
219- yield from (OWLObjectPropertyAssertionAxiom (i , op , ind ) for ind in
220- self .get_object_property_values (i , op ))
226+ if mode in triple_formatters :
227+ formatter = triple_formatters [mode ]
228+ yield from (formatter (s , p , o ) for s , p , o in self ._abox_native_triples (i ))
221229 elif mode == "expression" :
222230 object_restrictions_quantifiers = dict ()
223231 # To no return duplicate objects.
0 commit comments