Fix variable capture in indirect instance retrieval SPARQL (#575) - #631
Merged
Merged
Conversation
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
marked this pull request as ready for review
August 14, 2026 08:54
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
TripleStoreReasoner.instances(ontolearn/triple_store.py) rewrote every?x aoccurrence in the generated SPARQL query to the same?some_clsvariable when doing indirect (direct=False) instance retrieval.(¬Daughter) ⊓ Female, which puts one occurrence inside aFILTER NOT EXISTSblock 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).?some_cls_Nvariable viare.subwith an incrementing counter, instead ofstr.replacewith a fixed replacement string.tests/test_triplestore_indirect_instances_sparql.py, which mocks the HTTP layer (matching the existingtests/test_triplestore_sparql_optimizations.pypattern) to assert on the generated SPARQL directly, without needing a live triplestore.Fixes #575.
Test plan
tests/test_triplestore_indirect_instances_sparql.pycovers this directly (no live triplestore needed): asserts the two occurrences in(¬Daughter) ⊓ Femaleget distinct?some_cls_Nvariables, that a single occurrence is still rewritten correctly, and thatdirect=Trueleaves the query untouched.?some_clsvariable) and pass against the fix, by temporarily checking out the parent commit'striple_store.pyand 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