Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/substitution expression #555

Merged
merged 3 commits into from
Feb 16, 2024
Merged

Conversation

ccamel
Copy link
Member

@ccamel ccamel commented Feb 14, 2024

This PR implements the solution adopted from the discussion okp4/community#14, which is to use an unstructured format for substitutions of the logic module by directly representing them as a Prolog term string (atom, number, or compound).

I also seized the opportunity to refactor the scanning process of the solutions, thereby eliminating the use of go routines and channels in the ichiban/prolog lib (cf interpreter.go#L200).

Notes:
⚠️ This change introduces a breaking change in the logic module contract.

Summary by CodeRabbit

  • New Features
    • Enhanced query capabilities with the introduction of expression to represent values in substitutions, offering more precise and flexible query results.
    • Introduced UserOutput in AskResponse and an Error field in Answer struct for improved user feedback and error handling.
  • Bug Fixes
    • Updated test cases across various modules to use the new testutil.TermResults type, ensuring consistency and reliability in test results.
  • Documentation
    • Updated documentation to reflect changes in entity declarations and type details, aligning with the latest system logic and functionalities.
  • Refactor
    • Refactored query execution logic for enhanced performance and accuracy.
    • Transitioned to using expression instead of term in various contexts, streamlining the handling of substitutions.
  • Tests
    • Added and updated test scenarios to cover new query patterns and functionalities, ensuring robustness.

@ccamel ccamel self-assigned this Feb 14, 2024
Copy link
Contributor

coderabbitai bot commented Feb 14, 2024

Walkthrough

The recent modifications primarily revolve around the transition from using Term to Expression in various logic-related components, enhancing the system's compatibility with Prolog terms. This shift includes updates to file structures, test cases, and error handling mechanisms, aiming to streamline processes and improve clarity in the logic module. The changes reflect a broader effort to align with Prolog conventions and enhance the usability and maintainability of the system.

Changes

File Pattern Change Summary
docs/proto/logic.md
proto/logic/v1beta2/...
Replaced Term with Expression in documentation and types.
x/logic/keeper/...
x/logic/predicate/...
Updated tests and internal logic to use Expression and adjusted error handling.
x/logic/testutil/logic.go
x/logic/wasm/types.go
Introduced new types for handling term results and updated response structures.

Related issues

  • 🧠 Logic: ♻️ Refactor prolog predicates errors #499: The changes in this PR could pave the way for standardizing error messages in the logic module, as it introduces a more structured approach to handling expressions and terms. This aligns with the issue's objective of refactoring error messages to follow Prolog conventions.

Poem

In the realm of logic so vast and deep,
A change was made, not small, not meek.
From Term to Expression, the shift was bold,
A tale of transformation, gracefully told.
🐇 CodeRabbit danced, with joy so bright,
For logic's path now gleamed with light.
In code, in tests, the future's shaped,
A leap forward, elegantly draped.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

@ccamel ccamel force-pushed the refactor/substituion-expression branch from a75b438 to 5a3c040 Compare February 14, 2024 15:25
@ccamel ccamel marked this pull request as ready for review February 14, 2024 15:27
@ccamel ccamel requested a review from amimart February 14, 2024 15:31
@ccamel ccamel changed the title Refactor/substituion expression Refactor/substitution expression Feb 14, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 20a4840 and 5a3c040.
Files ignored due to path filters (1)
  • x/logic/types/types.pb.go is excluded by: !**/*.pb.go
Files selected for processing (16)
  • docs/proto/logic.md (2 hunks)
  • proto/logic/v1beta2/types.proto (1 hunks)
  • x/logic/keeper/grpc_query_ask_test.go (7 hunks)
  • x/logic/keeper/interpreter.go (6 hunks)
  • x/logic/predicate/address_test.go (4 hunks)
  • x/logic/predicate/bank_test.go (26 hunks)
  • x/logic/predicate/builtin_test.go (2 hunks)
  • x/logic/predicate/crypto_test.go (12 hunks)
  • x/logic/predicate/did_test.go (2 hunks)
  • x/logic/predicate/encoding_test.go (2 hunks)
  • x/logic/predicate/file_test.go (11 hunks)
  • x/logic/predicate/json_test.go (17 hunks)
  • x/logic/predicate/string_test.go (13 hunks)
  • x/logic/predicate/uri_test.go (4 hunks)
  • x/logic/testutil/logic.go (1 hunks)
  • x/logic/wasm/types.go (3 hunks)
Additional comments: 69
proto/logic/v1beta2/types.proto (1)
  • 15-16: LGTM!
x/logic/predicate/builtin_test.go (1)
  • 61-61: LGTM!
x/logic/testutil/logic.go (1)
  • 14-15: LGTM!
x/logic/wasm/types.go (3)
  • 16-19: LGTM!
  • 38-38: LGTM!
  • 78-79: LGTM!
x/logic/predicate/encoding_test.go (2)
  • 30-30: LGTM!
  • 110-110: LGTM!
x/logic/predicate/did_test.go (1)
  • 30-30: LGTM!
x/logic/predicate/address_test.go (1)
  • 30-30: LGTM!
x/logic/keeper/grpc_query_ask_test.go (10)
  • 35-43: LGTM!
  • 53-54: LGTM!
  • 66-67: LGTM!
  • 79-80: LGTM!
  • 102-103: LGTM!
  • 115-116: LGTM!
  • 128-129: LGTM!
  • 141-142: LGTM!
  • 157-158: LGTM!
  • 172-172: LGTM!
x/logic/keeper/interpreter.go (6)
  • 6-6: strings import added but not directly used in the provided code changes.
  • 9-9: github.com/ichiban/prolog/engine import added and correctly used in the queryInterpreter function.
  • 44-50: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [47-57]

In execute, correctly refactored to use queryInterpreter. Ensure limits.MaxResultCount is properly checked before dereferencing.

  • 75-109: queryInterpreter function correctly handles query execution and result processing. However, ensure error handling and limit checks are robust and tested.
  • 164-172: checkLimits function correctly checks query size limits against provided limits. Ensure error messages are clear and tested.
  • 198-223: Utility functions parsedVarsToVars and envsToResults correctly process variables and results. Ensure error handling in envsToResults is robust.
x/logic/predicate/uri_test.go (1)
  • 39-144: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [30-160]

Replaced types.TermResults with testutil.TermResults in test cases, aligning with the new substitution mechanism. Ensure all test cases are updated and correctly use testutil.TermResults.

x/logic/predicate/string_test.go (1)
  • 28-42: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [31-360]

Replaced types.TermResults with testutil.TermResults in test cases, aligning with the new substitution mechanism. Ensure all test cases are updated and correctly use testutil.TermResults.

x/logic/predicate/file_test.go (14)
  • 39-39: Ensure the replacement of types.TermResults with testutil.TermResults aligns with the new substitution mechanism and does not alter the intended test outcomes.
  • 51-51: Confirm that the empty testutil.TermResults correctly represents the expected outcome for this test case, given the changes in substitution representation.
  • 57-57: Verify that the empty testutil.TermResults accurately reflects the expected result, considering the updated substitution mechanism.
  • 73-73: Check if the representation of testutil.TermResults with multiple entries correctly matches the expected test outcomes after the substitution mechanism update.
  • 79-79: Ensure the representation of testutil.TermResults with multiple entries is consistent with the expected outcomes, given the changes to the substitution mechanism.
  • 85-85: Confirm that the empty testutil.TermResults and the specified wantError are appropriate for the test case, considering the substitution mechanism changes.
  • 97-97: Verify that the empty testutil.TermResults correctly represents the expected outcome for this test case, in light of the substitution mechanism changes.
  • 113-113: Check if the representation of testutil.TermResults with a single entry accurately reflects the expected test outcomes after the substitution mechanism update.
  • 119-119: Ensure the representation of testutil.TermResults with a single entry is consistent with the expected outcomes, given the changes to the substitution mechanism.
  • 153-155: Review the loop for scanning solutions to ensure it correctly handles the conversion of solutions to testutil.TermResults and properly checks for errors and expected results.
  • 200-200: Confirm the replacement of types.TermResults with testutil.TermResults aligns with the new substitution mechanism and does not alter the intended test outcomes.
  • 210-210: Verify that the testutil.TermResults with a single entry correctly represents the expected outcome for this test case, considering the substitution mechanism changes.
  • 221-221: Check if the representation of testutil.TermResults with a single entry accurately reflects the expected test outcomes after the substitution mechanism update.
  • 346-348: Review the loop for scanning solutions to ensure it correctly handles the conversion of solutions to testutil.TermResults and properly checks for errors and expected results.
x/logic/predicate/crypto_test.go (19)
  • 30-30: Ensure the replacement of types.TermResults with testutil.TermResults aligns with the new substitution mechanism and does not alter the intended test outcomes.
  • 37-37: Confirm that the testutil.TermResults with a single entry correctly represents the expected outcome for this test case, considering the substitution mechanism changes.
  • 45-45: Verify that the empty testutil.TermResults correctly represents the expected outcome for this test case, in light of the substitution mechanism changes.
  • 51-51: Check if the representation of testutil.TermResults with a single entry accurately reflects the expected test outcomes after the substitution mechanism update.
  • 59-59: Ensure the representation of testutil.TermResults with a single entry is consistent with the expected outcomes, given the changes to the substitution mechanism.
  • 67-67: Confirm that the testutil.TermResults with a single entry correctly represents the expected outcome for this test case, considering the substitution mechanism changes.
  • 75-75: Verify that the testutil.TermResults with a single entry accurately reflects the expected test outcomes after the substitution mechanism update.
  • 88-88: Check if the representation of testutil.TermResults with a single entry is appropriate for the expected outcomes, given the changes to the substitution mechanism.
  • 96-96: Ensure the representation of testutil.TermResults with a single entry is consistent with the expected outcomes, considering the substitution mechanism changes.
  • 125-127: Review the loop for scanning solutions to ensure it correctly handles the conversion of solutions to testutil.TermResults and properly checks for errors and expected results.
  • 166-166: Confirm the replacement of types.TermResults with testutil.TermResults aligns with the new substitution mechanism and does not alter the intended test outcomes.
  • 178-178: Verify that the empty testutil.TermResults correctly represents the expected outcome for this test case, in light of the substitution mechanism changes.
  • 187-187: Check if the empty testutil.TermResults accurately reflects the expected test outcomes after the substitution mechanism update.
  • 248-248: Ensure the representation of testutil.TermResults with a single entry is consistent with the expected outcomes, given the changes to the substitution mechanism.
  • 280-280: Verify that the empty testutil.TermResults correctly represents the expected outcome for this test case, considering the substitution mechanism changes.
  • 291-291: Check if the empty testutil.TermResults accurately reflects the expected test outcomes after the substitution mechanism update.
  • 303-303: Confirm that the testutil.TermResults with a single entry correctly represents the expected outcome for this test case, in light of the substitution mechanism changes.
  • 314-314: Ensure the representation of testutil.TermResults with a single entry is consistent with the expected outcomes, considering the substitution mechanism changes.
  • 342-344: Review the loop for scanning solutions to ensure it correctly handles the conversion of solutions to testutil.TermResults and properly checks for errors and expected results.
x/logic/predicate/json_test.go (5)
  • 31-31: Ensure the replacement of types.TermResults with testutil.TermResults is consistent across all test cases. This change aligns with the PR objectives to update the test files for the new substitution representation.
  • 53-53: The test cases correctly use testutil.TermResults for expected results, aligning with the PR's objective to refactor the substitution mechanism. Each test case is well-described, and the expected outcomes are clearly defined.

Also applies to: 61-61, 71-71, 79-79, 87-87, 95-95, 105-105, 129-129, 137-137, 147-147, 157-157, 165-165, 173-173, 181-181, 192-192, 200-200, 208-208, 216-216, 224-224, 234-234, 242-242, 250-250, 258-258, 286-286, 302-302, 310-310, 318-318, 326-326, 342-342, 350-350, 360-360

  • 388-388: The loop for processing solution bindings correctly initializes testutil.TermResults for each iteration. This is consistent with the updated substitution representation.
  • 390-390: The call to sols.Scan(m) correctly passes the testutil.TermResults instance. This ensures that the test cases are aligned with the new substitution mechanism.
  • 492-492: The handling of solution bindings in the more complex struct bidirectional tests is consistent with the changes made in the simpler test cases. The usage of testutil.TermResults and the scanning process align with the PR's objectives.

Also applies to: 494-494, 531-531, 533-533

x/logic/predicate/bank_test.go (2)
  • 40-40: The change from types.TermResults to testutil.TermResults aligns with the PR objectives to update the test files for the new substitution representation.
  • 486-486: The call to sols.Scan(m) directly passes a map (testutil.TermResults), assuming it's correctly handled by the method. Ensure that sols.Scan() is implemented to accept and correctly populate a map structure.

Verify the implementation of sols.Scan() to ensure it supports scanning into a map and correctly populates it with the solution bindings.

docs/proto/logic.md (1)
  • 379-379: The change from Term to expression in the Substitution entity is correctly documented. This aligns with the PR's objective to represent substitutions directly as Prolog term strings, enhancing flexibility and expressiveness.

x/logic/predicate/bank_test.go Show resolved Hide resolved
x/logic/keeper/interpreter.go Outdated Show resolved Hide resolved
Adopt expression for the values substitued represented as a prolog term.
Previous format was an incorrect mix of structured/unstructured.
@ccamel ccamel force-pushed the refactor/substituion-expression branch from 5a3c040 to 6c4cb3d Compare February 16, 2024 10:00
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between cf3a66a and 6c4cb3d.
Files ignored due to path filters (1)
  • x/logic/types/types.pb.go is excluded by: !**/*.pb.go
Files selected for processing (16)
  • docs/proto/logic.md (2 hunks)
  • proto/logic/v1beta2/types.proto (1 hunks)
  • x/logic/keeper/grpc_query_ask_test.go (7 hunks)
  • x/logic/keeper/interpreter.go (6 hunks)
  • x/logic/predicate/address_test.go (4 hunks)
  • x/logic/predicate/bank_test.go (26 hunks)
  • x/logic/predicate/builtin_test.go (2 hunks)
  • x/logic/predicate/crypto_test.go (12 hunks)
  • x/logic/predicate/did_test.go (2 hunks)
  • x/logic/predicate/encoding_test.go (2 hunks)
  • x/logic/predicate/file_test.go (11 hunks)
  • x/logic/predicate/json_test.go (17 hunks)
  • x/logic/predicate/string_test.go (13 hunks)
  • x/logic/predicate/uri_test.go (4 hunks)
  • x/logic/testutil/logic.go (1 hunks)
  • x/logic/wasm/types.go (3 hunks)
Files skipped from review as they are similar to previous changes (15)
  • docs/proto/logic.md
  • proto/logic/v1beta2/types.proto
  • x/logic/keeper/grpc_query_ask_test.go
  • x/logic/predicate/address_test.go
  • x/logic/predicate/bank_test.go
  • x/logic/predicate/builtin_test.go
  • x/logic/predicate/crypto_test.go
  • x/logic/predicate/did_test.go
  • x/logic/predicate/encoding_test.go
  • x/logic/predicate/file_test.go
  • x/logic/predicate/json_test.go
  • x/logic/predicate/string_test.go
  • x/logic/predicate/uri_test.go
  • x/logic/testutil/logic.go
  • x/logic/wasm/types.go
Additional comments: 7
x/logic/keeper/interpreter.go (7)
  • 4-6: The addition of the strings import is justified by its use in the new queryInterpreter function for reading the query string. The update to the context import seems to be a reordering, which is fine as long as it follows the project's import organization conventions.
  • 9-9: The addition of the github.com/ichiban/prolog/engine import is necessary for the new Prolog engine functionalities introduced, such as parsing and executing Prolog queries. This aligns with the PR's objective to refactor the solution scanning process.
  • 53-67: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [43-75]

The refactoring of the execute method to use queryInterpreter for query execution simplifies the execution flow by abstracting the query interpretation logic into a separate function. This change potentially improves maintainability and readability. However, ensure that all error handling and context enhancements are preserved and correctly implemented in the new structure.

  • 75-108: The introduction of queryInterpreter is a significant change that centralizes the logic for executing queries and processing results. It's important to ensure that the logic for handling limits, parsing queries, and converting environments to results is correct and efficient. The use of lo.IfF for conditional error message handling is a neat touch, but ensure that the library github.com/samber/lo is used consistently and effectively throughout the project.
  • 71-117: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [114-163]

The newInterpreter function setup includes configuring predicates and virtual files filters based on parameters, which is a good practice for flexibility and configurability. However, ensure that the logic for handling default values and overrides is thoroughly tested, especially the use of nonNilNorZeroOrDefaultUint64 and the mapping of URLs. The creation of a BoundedBuffer for user output based on limits is a thoughtful addition for managing output size.

  • 163-171: The checkLimits function adds a necessary validation step for query size against configured limits. This is a good practice for enforcing constraints and preventing potential abuse. Ensure that the error message provides clear and actionable information to the caller.
  • 197-222: Utility functions parsedVarsToVars and envsToResults are crucial for converting Prolog engine data structures to the application's domain types. It's important to ensure that the conversion logic correctly handles Prolog terms and environments, especially error handling during the scanning of expressions. The use of string(expression) in line 215 to convert prolog.TermString to a Go string should be verified for correctness in representing Prolog terms.

Copy link
Member

@amimart amimart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! 🙏

@ccamel ccamel merged commit 3701401 into main Feb 16, 2024
19 checks passed
@ccamel ccamel deleted the refactor/substituion-expression branch February 16, 2024 11:05
@bot-anik
Copy link
Member

🎉 This PR is included in version 7.0.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants