Skip to content

Commit

Permalink
Merge pull request #555 from okp4/refactor/substituion-expression
Browse files Browse the repository at this point in the history
Refactor/substitution expression
  • Loading branch information
ccamel authored Feb 16, 2024
2 parents cf3a66a + 6c4cb3d commit 3701401
Show file tree
Hide file tree
Showing 19 changed files with 385 additions and 742 deletions.
14 changes: 1 addition & 13 deletions docs/proto/logic.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ utilized within a query, or limiting the depth of the backtracking algorithm.
- [Answer](#logic.v1beta2.Answer)
- [Result](#logic.v1beta2.Result)
- [Substitution](#logic.v1beta2.Substitution)
- [Term](#logic.v1beta2.Term)

- [logic/v1beta2/query.proto](#logic/v1beta2/query.proto)
- [QueryServiceAskRequest](#logic.v1beta2.QueryServiceAskRequest)
Expand Down Expand Up @@ -377,18 +376,7 @@ Substitution represents a substitution made to the variables in the query to obt
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `variable` | [string](#string) | | variable is the name of the variable. |
| `term` | [Term](#logic.v1beta2.Term) | | term is the term that the variable is substituted with. |

<a name="logic.v1beta2.Term"></a>

### Term

Term is the representation of a piece of data and can be a constant, a variable, or an atom.

| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `name` | [string](#string) | | name is the name of the term. |
| `arguments` | [Term](#logic.v1beta2.Term) | repeated | arguments are the arguments of the term, which can be constants, variables, or atoms. |
| `expression` | [string](#string) | | expression is the value substituted for the variable, represented directly as a Prolog term (e.g., atom, number, compound). |

[//]: # (end messages)

Expand Down
20 changes: 2 additions & 18 deletions proto/logic/v1beta2/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,14 @@ import "gogoproto/gogo.proto";

option go_package = "github.com/okp4/okp4d/x/logic/types";

// Term is the representation of a piece of data and can be a constant, a variable, or an atom.
message Term {
option (gogoproto.goproto_stringer) = true;

// name is the name of the term.
string name = 1 [(gogoproto.moretags) = "yaml:\"name\",omitempty"];
// arguments are the arguments of the term, which can be constants, variables, or atoms.
repeated Term arguments = 2 [
(gogoproto.nullable) = false,
(gogoproto.moretags) = "yaml:\"arguments\",omitempty"
];
}

// Substitution represents a substitution made to the variables in the query to obtain the answer.
message Substitution {
option (gogoproto.goproto_stringer) = true;

// variable is the name of the variable.
string variable = 1 [(gogoproto.moretags) = "yaml:\"variable\",omitempty"];
// term is the term that the variable is substituted with.
Term term = 2 [
(gogoproto.nullable) = false,
(gogoproto.moretags) = "yaml:\"term\",omitempty"
];
// expression is the value substituted for the variable, represented directly as a Prolog term (e.g., atom, number, compound).
string expression = 2 [(gogoproto.moretags) = "yaml:\"expression\",omitempty"];
}

// Result represents the result of a query.
Expand Down
92 changes: 66 additions & 26 deletions x/logic/keeper/grpc_query_ask_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ func TestGRPCAsk(t *testing.T) {
expectedAnswer *types.Answer
expectedError string
}{
{
program: "foo.",
query: "foo.",
expectedAnswer: &types.Answer{
Success: true,
HasMore: false,
Variables: nil,
Results: []types.Result{{Substitutions: nil}},
},
},
{
program: "father(bob, alice).",
query: "father(bob, X).",
Expand All @@ -40,11 +50,8 @@ func TestGRPCAsk(t *testing.T) {
HasMore: false,
Variables: []string{"X"},
Results: []types.Result{{Substitutions: []types.Substitution{{
Variable: "X",
Term: types.Term{
Name: "alice",
Arguments: nil,
},
Variable: "X",
Expression: "alice",
}}}},
},
},
Expand All @@ -56,11 +63,8 @@ func TestGRPCAsk(t *testing.T) {
HasMore: false,
Variables: []string{"X"},
Results: []types.Result{{Substitutions: []types.Substitution{{
Variable: "X",
Term: types.Term{
Name: "[a,l,i,c,e]",
Arguments: nil,
},
Variable: "X",
Expression: "[a,l,i,c,e]",
}}}},
},
},
Expand All @@ -72,11 +76,8 @@ func TestGRPCAsk(t *testing.T) {
HasMore: true,
Variables: []string{"X"},
Results: []types.Result{{Substitutions: []types.Substitution{{
Variable: "X",
Term: types.Term{
Name: "alice",
Arguments: nil,
},
Variable: "X",
Expression: "alice",
}}}},
},
},
Expand All @@ -98,11 +99,8 @@ func TestGRPCAsk(t *testing.T) {
HasMore: false,
Variables: []string{"X"},
Results: []types.Result{{Substitutions: []types.Substitution{{
Variable: "X",
Term: types.Term{
Name: "0",
Arguments: nil,
},
Variable: "X",
Expression: "0",
}}}},
},
},
Expand All @@ -114,11 +112,53 @@ func TestGRPCAsk(t *testing.T) {
HasMore: false,
Variables: []string{"X"},
Results: []types.Result{{Substitutions: []types.Substitution{{
Variable: "X",
Term: types.Term{
Name: "élodie",
Arguments: nil,
},
Variable: "X",
Expression: "élodie",
}}}},
},
},
{
program: "foo(foo(bar)).",
query: "foo(X).",
expectedAnswer: &types.Answer{
Success: true,
HasMore: false,
Variables: []string{"X"},
Results: []types.Result{{Substitutions: []types.Substitution{{
Variable: "X",
Expression: "foo(bar)",
}}}},
},
},
{
program: "father(bob, alice).",
query: "father(A, B).",
expectedAnswer: &types.Answer{
Success: true,
HasMore: false,
Variables: []string{"A", "B"},
Results: []types.Result{{Substitutions: []types.Substitution{{
Variable: "A",
Expression: "bob",
}, {
Variable: "B",
Expression: "alice",
}}}},
},
},
{
program: "father(bob, alice).",
query: "father(B, A).",
expectedAnswer: &types.Answer{
Success: true,
HasMore: false,
Variables: []string{"B", "A"},
Results: []types.Result{{Substitutions: []types.Substitution{{
Variable: "B",
Expression: "bob",
}, {
Variable: "A",
Expression: "alice",
}}}},
},
},
Expand All @@ -129,7 +169,7 @@ func TestGRPCAsk(t *testing.T) {
Success: false,
Error: "error(existence_error(procedure,father/3),root)",
HasMore: false,
Variables: nil,
Variables: []string{"X", "O"},
Results: nil,
},
},
Expand Down
Loading

0 comments on commit 3701401

Please sign in to comment.