Add RDF List operators and WOQL.localize #458
Merged
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.
This PR aligns the Python client's RDFList predicates and
WOQL.localizefunctionality with the JavaScript client implementation, ensuring consistent variable scoping behavior across both clients.Background
The JavaScript client has comprehensive support for RDF List operations through a
WOQLLibraryclass withrdflist_*methods, and alocalizemethod for variable scoping. The Python client was missing these features, creating an inconsistency between the two client libraries.Changes
New Classes and Functions
VarsUnique - Generates unique variable names to prevent collisions in library functions:
WOQLQuery.localize() - Creates localized variable scopes to prevent local variables from leaking to outer scope:
WOQLQuery.lib() - Returns WOQLLibrary instance for RDFList operations:
WOQLLibrary RDFList Operations
All RDFList predicates from the JavaScript client are now available in Python:
rdflist_list(cons_subject, list_var)rdflist_peek(cons_subject, value_var)rdflist_last(cons_subject, value_var)rdflist_nth0(cons_subject, index, value_var)rdflist_nth1(cons_subject, index, value_var)rdflist_member(cons_subject, value)rdflist_length(cons_subject, length_var)rdflist_pop(cons_subject, value_var)rdflist_push(cons_subject, value)rdflist_append(cons_subject, value, new_cell)rdflist_clear(cons_subject, new_list_var)rdflist_empty(list_var)rdflist_is_empty(cons_subject)rdflist_slice(cons_subject, start, end, result_var)rdflist_insert(cons_subject, position, value)rdflist_drop(cons_subject, position)rdflist_swap(cons_subject, pos_a, pos_b)Variable Scoping
The
localizemethod implements the same pattern as JavaScript:eq()clauses placed outside theselect("")wrapperselect("")with empty variable listVarsUniqueto prevent collisionsThis ensures that:
Files Modified
terminusdb_client/woqlquery/woql_query.py- Added VarsUnique class, localize method, lib method, and WOQLLibrary classterminusdb_client/woqlquery/__init__.py- Updated exportsFiles Added
terminusdb_client/tests/test_woql_localize.py- Unit tests for localize and VarsUnique (13 tests)terminusdb_client/tests/test_woql_rdflist.py- Unit tests for RDFList operations (30 tests)docs/PR_RDFLIST_LOCALIZE_ALIGNMENT.md- This documentationTests
All 43 new unit tests pass:
localizeandVarsUniquefunctionalityTests verify:
Compatibility
The implementation aligns with the JavaScript client's
woqlLibrary.jsandwoqlQuery.js:select("")andeq()bindingsUsage Example