Skip to content

Commit ba8ddfc

Browse files
authored
Merge pull request #621 from dice-group/worktree-fix-abox-refactor
Deduplicate abox() triple traversal across native/iri/axiom modes
2 parents 7d5d7c8 + 225825f commit ba8ddfc

3 files changed

Lines changed: 44 additions & 34 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ date) and use that section as the basis for the GitHub release notes.
2828
`tests/test_nces2.py`, and `tests/test_nces_trainer.py` from the test run.
2929
These intermittently hang for 1h+ (pretrained-model loading), stalling CI
3030
until manually cancelled, unrelated to the code under test. See #622.
31+
- `KnowledgeBase.abox()` (`ontolearn/knowledge_base.py`) repeated the same
32+
rdf:type/data-property/object-property triple traversal once per
33+
`mode in {"native", "iri", "axiom"}`, differing only in output formatting.
34+
Factored into a shared `_abox_native_triples` generator with per-mode
35+
formatter methods applied on top.
3136
- `ModifiedCELOERefinement._setup()` (`ontolearn/refinement_operators.py`, used by
3237
`CELOE`/`OCEL`/`ExpressRefinement`) eagerly swept every object property against
3338
every individual in the knowledge base at construction time, purely to precompute

‎Improvements.md‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@ remain open as of this writing.
1515

1616
## 2. Code duplication
1717

18-
- **`ontolearn/knowledge_base.py:191-262`** — `abox()` repeats the same
19-
triple-traversal ~70 lines of near-identical branching, once per
20-
`mode in {"native", "iri", "axiom"}`, differing only in output formatting.
21-
Factor a single internal `(subject, predicate, object)` generator with
22-
per-mode formatters applied on top.
18+
All code duplication issues originally found in this pass have been fixed;
19+
none remain open as of this writing.
2320

2421
## 3. Error handling & logging
2522

‎ontolearn/knowledge_base.py‎

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)