Skip to content

Commit

Permalink
feat(logic): expose open/3 predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Mar 18, 2024
1 parent a30c8cf commit b71bf4f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 214 deletions.
3 changes: 0 additions & 3 deletions x/logic/interpreter/bootstrap/bootstrap.pl
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@

% Stream selection and control

open(Filename, Mode, Stream) :-
open(Filename, Mode, Stream, []).

close(Stream) :- close(Stream, []).

flush_output :-
Expand Down
1 change: 1 addition & 0 deletions x/logic/interpreter/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ var registry = map[string]any{
"set_input/1": engine.SetInput,
"set_output/1": engine.SetOutput,
"open/4": predicate.Open,
"open/3": predicate.Open3,
"close/2": engine.Close,
"flush_output/1": engine.FlushOutput,
"stream_property/2": engine.StreamProperty,
Expand Down
27 changes: 12 additions & 15 deletions x/logic/keeper/features/consult_1.feature
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Feature: consult/1
expression: "['W',o,r,l,d,!]"
"""


@great_for_documentation
Scenario: Consult a Prolog program which also consults another Prolog program
This scenario demonstrates the capability of a Prolog program to consult another Prolog program. This is useful for
Expand All @@ -56,8 +57,7 @@ Feature: consult/1
}
response: |
:- consult('cosmwasm:storage:okp412ssv28mzr02jffvy4x39akrpky9ykfafzyjzmvgsqqdw78yjevpqgmqnmk?query=%7B%22object_data%22%3A%7B%22id%22%3A%20%225d3933430d0a12794fae719e0db87b6ec5f549b2%22%7D%7D&base64Decode=false').
program(a).
a.
"""
Given the CosmWasm smart contract "okp412ssv28mzr02jffvy4x39akrpky9ykfafzyjzmvgsqqdw78yjevpqgmqnmk" and the behavior:
""" yaml
Expand All @@ -68,24 +68,20 @@ Feature: consult/1
}
}
response: |
program(b).
b.
"""
Given the query:
""" prolog
consult('cosmwasm:storage:okp415ekvz3qdter33mdnk98v8whv5qdr53yusksnfgc08xd26fpdn3ts8gddht?query=%7B%22object_data%22%3A%7B%22id%22%3A%20%224cbe36399aabfcc7158ee7a66cbfffa525bb0ceab33d1ff2cff08759fe0a9b05%22%7D%7D&base64Decode=false'),
program(Name).
a, b.
"""
When the query is run (limited to 2 solutions)
Then the answer we get is:
""" yaml
has_more: false
variables: ["Name"]
variables:
results:
- substitutions:
- variable: Name
expression: "a"
- variable: Name
expression: "b"
"""

@great_for_documentation
Expand Down Expand Up @@ -123,17 +119,18 @@ Feature: consult/1
"""
Given the query:
""" prolog
program(Name).
source_file(File).
"""
When the query is run (limited to 2 solutions)
Then the answer we get is:
""" yaml
has_more: false
variables: ["Name"]
variables: ["File"]
results:
- substitutions:
- variable: Name
expression: "a"
- variable: Name
expression: "b"
- variable: File
expression: "'cosmwasm:storage:okp412ssv28mzr02jffvy4x39akrpky9ykfafzyjzmvgsqqdw78yjevpqgmqnmk?query=%7B%22object_data%22%3A%7B%22id%22%3A%20%225d3933430d0a12794fae719e0db87b6ec5f549b2%22%7D%7D&base64Decode=false'"
- substitutions:
- variable: File
expression: "'cosmwasm:storage:okp415ekvz3qdter33mdnk98v8whv5qdr53yusksnfgc08xd26fpdn3ts8gddht?query=%7B%22object_data%22%3A%7B%22id%22%3A%20%224cbe36399aabfcc7158ee7a66cbfffa525bb0ceab33d1ff2cff08759fe0a9b05%22%7D%7D&base64Decode=false'"
"""
25 changes: 18 additions & 7 deletions x/logic/keeper/features_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,13 @@ func whenTheQueryIsRun(ctx context.Context) error {
return nil
}

func whenTheQueryIsRunLimitedToNSolutions(ctx context.Context, n uint64) error {
tc := testCaseFromContext(ctx).request
func whenTheQueryIsRunLimitedToNSolutions(ctx context.Context, n int) error {
request := testCaseFromContext(ctx).request

limit := sdkmath.NewUint(n)
tc.Limit = &limit
limit := sdkmath.NewUint(uint64(n))
request.Limit = &limit

testCaseFromContext(ctx).request = request

return whenTheQueryIsRun(ctx)
}
Expand Down Expand Up @@ -249,8 +251,8 @@ func initializeScenario(t *testing.T) func(ctx *godog.ScenarioContext) {
ctx.Given(`the CosmWasm smart contract "([^"]+)" and the behavior:`, givenASmartContractWithAddress)
ctx.Given(`the query:`, givenTheQuery)
ctx.Given(`the program:`, givenTheProgram)
ctx.When(`$the query is run^`, whenTheQueryIsRun)
ctx.When(`the query is run (limited to (\d+) solutions)`, whenTheQueryIsRunLimitedToNSolutions)
ctx.When(`^the query is run$`, whenTheQueryIsRun)
ctx.When(`^the query is run \(limited to (\d+) solutions\)$`, whenTheQueryIsRunLimitedToNSolutions)
ctx.Then(`the answer we get is:`, theAnswerWeGetIs)
}
}
Expand All @@ -272,7 +274,7 @@ func newQueryClient(ctx context.Context) (types.QueryServiceClient, error) {
},
)

if err := logicKeeper.SetParams(tc.ctx.Ctx, types.DefaultParams()); err != nil {
if err := logicKeeper.SetParams(tc.ctx.Ctx, logicKeeperParams()); err != nil {
return nil, err
}

Expand All @@ -282,6 +284,15 @@ func newQueryClient(ctx context.Context) (types.QueryServiceClient, error) {
return queryClient, nil
}

func logicKeeperParams() types.Params {
params := types.DefaultParams()
limits := params.Limits
maxResultCount := sdkmath.NewUint(10)
limits.MaxResultCount = &maxResultCount
params.Limits = limits
return params
}

func atoi64(s string) (int64, error) {
i, err := strconv.ParseInt(s, 10, 64)
if err != nil {
Expand Down
19 changes: 18 additions & 1 deletion x/logic/predicate/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (m ioMode) Term() engine.Term {
// - Stream is the stream to be opened.
// - Options is a list of options. No options are currently defined, so the list should be empty.
//
// open/4 gives True when SourceSink can be opened in Mode with the given Options.
// open/4 gives True when SourceSink can be opened in Mode with the given Options.
//
// # Virtual File System (VFS)
//
Expand Down Expand Up @@ -190,6 +190,23 @@ func Open(vm *engine.VM, sourceSink, mode, stream, options engine.Term, k engine
return engine.Unify(vm, stream, s, k, env)
}

// Open3 is a predicate which opens a stream to a source or sink.
// This predicate is a shorthand for open/4 with an empty list of options.
//
// # Signature
//
// open(+SourceSink, +Mode, -Stream)
//
// where:
// - SourceSink is an atom representing the source or sink of the stream, which is typically a URI.
// - Mode is an atom representing the mode of the stream to be opened. It can be one of "read", "write", or "append".
// - Stream is the stream to be opened.
//
// open/3 gives True when SourceSink can be opened in Mode.
func Open3(vm *engine.VM, sourceSink, mode, stream engine.Term, k engine.Cont, env *engine.Env) *engine.Promise {
return Open(vm, sourceSink, mode, stream, prolog.AtomEmptyList, k, env)
}

func getLoadedSources(vm *engine.VM) map[string]struct{} {
loadedField := reflect.ValueOf(vm).Elem().FieldByName("loaded").MapKeys()
loaded := make(map[string]struct{}, len(loadedField))
Expand Down
188 changes: 0 additions & 188 deletions x/logic/predicate/file_test.go

This file was deleted.

0 comments on commit b71bf4f

Please sign in to comment.