generated from okp4/template-oss
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #311 from okp4/feat/source-file-predicate
🧠 Logic: 🗂️ source_file/1 predicate
- Loading branch information
Showing
9 changed files
with
291 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -403,6 +403,7 @@ mock: ## Generate all the mocks (for tests) | |
@echo "${COLOR_CYAN} 🧱 Generating all the mocks${COLOR_RESET}" | ||
@go install github.com/golang/mock/[email protected] | ||
@mockgen -source=x/logic/types/expected_keepers.go -package testutil -destination x/logic/testutil/expected_keepers_mocks.go | ||
@mockgen -source=x/logic/fs/fs.go -package testutil -destination x/logic/testutil/fs_mocks.go | ||
|
||
## Release: | ||
.PHONY: release-assets | ||
|
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package predicate | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"reflect" | ||
"sort" | ||
|
||
"github.com/ichiban/prolog/engine" | ||
) | ||
|
||
// SourceFile is a predicate that unify the given term with the currently loaded source file. The signature is as follows: | ||
// | ||
// source_file(?File). | ||
// | ||
// Where File represents a loaded source file. | ||
// | ||
// Example: | ||
// | ||
// # Query all the loaded source files, in alphanumeric order. | ||
// - source_file(File). | ||
// | ||
// # Query the given source file is loaded. | ||
// - source_file('foo.pl'). | ||
func SourceFile(vm *engine.VM, file engine.Term, cont engine.Cont, env *engine.Env) *engine.Promise { | ||
loaded := getLoadedSources(vm) | ||
|
||
inputFile, err := getFile(env, file) | ||
if err != nil { | ||
return engine.Error(fmt.Errorf("source_file/1: %w", err)) | ||
} | ||
|
||
if inputFile != nil { | ||
if _, ok := loaded[*inputFile]; ok { | ||
return engine.Unify(vm, file, engine.NewAtom(*inputFile), cont, env) | ||
} | ||
return engine.Delay() | ||
} | ||
|
||
promises := make([]func(ctx context.Context) *engine.Promise, 0, len(loaded)) | ||
sortedSource := sortLoadedSources(loaded) | ||
for i := range sortedSource { | ||
term := engine.NewAtom(sortedSource[i]) | ||
promises = append( | ||
promises, | ||
func(ctx context.Context) *engine.Promise { | ||
return engine.Unify( | ||
vm, | ||
file, | ||
term, | ||
cont, | ||
env, | ||
) | ||
}) | ||
} | ||
|
||
return engine.Delay(promises...) | ||
} | ||
|
||
func getLoadedSources(vm *engine.VM) map[string]struct{} { | ||
loadedField := reflect.ValueOf(vm).Elem().FieldByName("loaded").MapKeys() | ||
loaded := make(map[string]struct{}, len(loadedField)) | ||
for _, value := range loadedField { | ||
loaded[value.String()] = struct{}{} | ||
} | ||
|
||
return loaded | ||
} | ||
|
||
func sortLoadedSources(sources map[string]struct{}) []string { | ||
result := make([]string, 0, len(sources)) | ||
for filename := range sources { | ||
result = append(result, filename) | ||
} | ||
sort.SliceStable(result, func(i, j int) bool { | ||
return result[i] < result[j] | ||
}) | ||
|
||
return result | ||
} | ||
|
||
//nolint:nilnil | ||
func getFile(env *engine.Env, term engine.Term) (*string, error) { | ||
switch file := env.Resolve(term).(type) { | ||
case engine.Variable: | ||
case engine.Atom: | ||
strFile := file.String() | ||
return &strFile, nil | ||
default: | ||
return nil, fmt.Errorf("cannot unify file with %T", term) | ||
} | ||
return nil, nil | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
//nolint:gocognit | ||
package predicate | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"testing" | ||
"time" | ||
|
||
"github.com/cosmos/cosmos-sdk/store" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/golang/mock/gomock" | ||
"github.com/ichiban/prolog/engine" | ||
"github.com/okp4/okp4d/x/logic/fs" | ||
"github.com/okp4/okp4d/x/logic/testutil" | ||
"github.com/okp4/okp4d/x/logic/types" | ||
. "github.com/smartystreets/goconvey/convey" | ||
"github.com/tendermint/tendermint/libs/log" | ||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types" | ||
tmdb "github.com/tendermint/tm-db" | ||
) | ||
|
||
func TestSourceFile(t *testing.T) { | ||
Convey("Given test cases", t, func() { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
cases := []struct { | ||
query string | ||
wantResult []types.TermResults | ||
wantError error | ||
wantSuccess bool | ||
}{ | ||
{ | ||
query: "source_file(file).", | ||
wantSuccess: false, | ||
}, | ||
{ | ||
query: "consult(file1), consult(file2), source_file(file1).", | ||
wantResult: []types.TermResults{{}}, | ||
wantSuccess: true, | ||
}, | ||
{ | ||
query: "consult(file1), consult(file2), consult(file3), source_file(file2).", | ||
wantResult: []types.TermResults{{}}, | ||
wantSuccess: true, | ||
}, | ||
{ | ||
query: "consult(file1), consult(file2), source_file(file3).", | ||
wantSuccess: false, | ||
}, | ||
{ | ||
query: "source_file(X).", | ||
wantSuccess: false, | ||
}, | ||
{ | ||
query: "consult(file1), consult(file2), source_file(X).", | ||
wantResult: []types.TermResults{{"X": "file1"}, {"X": "file2"}}, | ||
wantSuccess: true, | ||
}, | ||
{ | ||
query: "consult(file2), consult(file3), consult(file1), source_file(X).", | ||
wantResult: []types.TermResults{{"X": "file1"}, {"X": "file2"}, {"X": "file3"}}, | ||
wantSuccess: true, | ||
}, | ||
{ | ||
query: "source_file(foo(bar)).", | ||
wantResult: []types.TermResults{}, | ||
wantError: fmt.Errorf("source_file/1: cannot unify file with *engine.compound"), | ||
}, | ||
} | ||
|
||
for nc, tc := range cases { | ||
Convey(fmt.Sprintf("Given the query #%d: %s", nc, tc.query), func() { | ||
Convey("and a mocked file system", func() { | ||
uri, _ := url.Parse("file://dump.pl") | ||
mockedFS := testutil.NewMockFS(ctrl) | ||
mockedFS.EXPECT().Open(gomock.Any()).AnyTimes().Return(fs.NewVirtualFile( | ||
[]byte("dumb(dumber)."), | ||
uri, | ||
time.Now(), | ||
), nil) | ||
|
||
Convey("and a context", func() { | ||
db := tmdb.NewMemDB() | ||
stateStore := store.NewCommitMultiStore(db) | ||
ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger()) | ||
|
||
Convey("and a vm", func() { | ||
interpreter := testutil.NewInterpreterMust(ctx) | ||
interpreter.FS = mockedFS | ||
interpreter.Register1(engine.NewAtom("source_file"), SourceFile) | ||
|
||
Convey("When the predicate is called", func() { | ||
sols, err := interpreter.QueryContext(ctx, tc.query) | ||
|
||
Convey("Then the error should be nil", func() { | ||
So(err, ShouldBeNil) | ||
So(sols, ShouldNotBeNil) | ||
|
||
Convey("and the bindings should be as expected", func() { | ||
var got []types.TermResults | ||
for sols.Next() { | ||
m := types.TermResults{} | ||
err := sols.Scan(m) | ||
So(err, ShouldBeNil) | ||
|
||
got = append(got, m) | ||
} | ||
|
||
if tc.wantError != nil { | ||
So(sols.Err(), ShouldNotBeNil) | ||
So(sols.Err().Error(), ShouldEqual, tc.wantError.Error()) | ||
} else { | ||
So(sols.Err(), ShouldBeNil) | ||
|
||
if tc.wantSuccess { | ||
So(len(got), ShouldBeGreaterThan, 0) | ||
So(len(got), ShouldEqual, len(tc.wantResult)) | ||
for iGot, resultGot := range got { | ||
for varGot, termGot := range resultGot { | ||
So(testutil.ReindexUnknownVariables(termGot), ShouldEqual, tc.wantResult[iGot][varGot]) | ||
} | ||
} | ||
} else { | ||
So(len(got), ShouldEqual, 0) | ||
} | ||
} | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
} | ||
}) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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