Skip to content

Fix variable capture in indirect instance retrieval SPARQL (#575) - #631

Merged
Demirrr merged 2 commits into
developfrom
worktree-fix-issue-575
Aug 14, 2026
Merged

Demirrr merged 2 commits into
developfrom
worktree-fix-issue-575

Conversation

@Demirrr

@Demirrr Demirrr commented Aug 14, 2026

Copy link
Copy Markdown
Member

Summary

  • TripleStoreReasoner.instances (ontolearn/triple_store.py) rewrote every ?x a occurrence in the generated SPARQL query to the same ?some_cls variable when doing indirect (direct=False) instance retrieval.
  • For class expressions that place more than one such occurrence in different scopes — e.g. (¬Daughter) ⊓ Female, which puts one occurrence inside a FILTER NOT EXISTS block and one outside it — the shared variable name let the two bind to each other instead of being scoped independently, producing incorrect results (exactly the scenario described in Issue with indirect instance retrieval on TripleStore #575).
  • Each occurrence now gets its own uniquely numbered ?some_cls_N variable via re.sub with an incrementing counter, instead of str.replace with a fixed replacement string.
  • Added tests/test_triplestore_indirect_instances_sparql.py, which mocks the HTTP layer (matching the existing tests/test_triplestore_sparql_optimizations.py pattern) to assert on the generated SPARQL directly, without needing a live triplestore.

Fixes #575.

Test plan

  • New tests/test_triplestore_indirect_instances_sparql.py covers this directly (no live triplestore needed): asserts the two occurrences in (¬Daughter) ⊓ Female get distinct ?some_cls_N variables, that a single occurrence is still rewritten correctly, and that direct=True leaves the query untouched.
  • Confirmed the new tests fail against the pre-fix code (both occurrences share the bare ?some_cls variable) and pass against the fix, by temporarily checking out the parent commit's triple_store.py and re-running.
  • tests/test_triplestore.py (the existing suite) requires a live Apache Jena Fuseki instance with the Mutagenesis ontology loaded (per CI setup), not available in this environment — could not run it here.

🤖 Generated with Claude Code

Demirrr and others added 2 commits August 14, 2026 08:49
TripleStoreReasoner.instances rewrote every "?x a " occurrence in the
generated SPARQL to the same "?some_cls" variable. For class expressions
placing multiple such occurrences in different scopes (e.g. inside vs.
outside a FILTER NOT EXISTS block, as with (¬Daughter) ⊓ Female), the
shared variable name let occurrences bind to each other instead of being
scoped independently, producing incorrect results. Each occurrence now
gets its own uniquely numbered ?some_cls_N variable.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
tests/test_triplestore_indirect_instances_sparql.py mocks the HTTP layer
(matching the existing tests/test_triplestore_sparql_optimizations.py
pattern) so it exercises TripleStoreReasoner.instances' query-construction
logic without needing a live triplestore. Verified the tests fail against
the pre-fix code (both occurrences share "?some_cls") and pass against the
fix (each gets its own "?some_cls_N").

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@Demirrr
Demirrr marked this pull request as ready for review August 14, 2026 08:54
@Demirrr
Demirrr merged commit 69240d8 into develop Aug 14, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Issue with indirect instance retrieval on TripleStore

1 participant