generated from okp4/template-oss
-
Notifications
You must be signed in to change notification settings - Fork 122
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 #381 from okp4/feat/read_string
🧠 Logic: ⛓️ Implement `read_string/3` predicate
- Loading branch information
Showing
4 changed files
with
285 additions
and
0 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
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,84 @@ | ||
package predicate | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"strings" | ||
|
||
"github.com/ichiban/prolog/engine" | ||
|
||
"github.com/okp4/okp4d/x/logic/util" | ||
) | ||
|
||
// ReadString is a predicate that will read a given Stream and unify it to String. | ||
// Optionally give a max length of reading, when stream reach the given length, the reading is stop. | ||
// If Length is unbound, Stream is read to the end and Length is unified with the number of characters read. | ||
// | ||
// read_string(+Stream, ?Length, -String) is det | ||
// | ||
// Where | ||
// - `Stream`: represent a stream | ||
// - `Length`: is the max length to read | ||
// - `String`: represent the unified read stream as string | ||
// | ||
// Example: | ||
// | ||
// # Given a file `foo.txt` that contains `Hello World`: | ||
// ``` | ||
// file_to_string(File, String, Length) :- | ||
// | ||
// open(File, read, In), | ||
// read_string(In, Length, String), | ||
// close(Stream). | ||
// | ||
// ``` | ||
// | ||
// Result : | ||
// | ||
// ``` | ||
// | ||
// ?- file_to_string('path/file/foo.txt', String, Length). | ||
// | ||
// String = 'Hello World' | ||
// Length = 11 | ||
// | ||
// ```. | ||
func ReadString(vm *engine.VM, stream, length, result engine.Term, cont engine.Cont, env *engine.Env) *engine.Promise { | ||
return engine.Delay(func(ctx context.Context) *engine.Promise { | ||
var s *engine.Stream | ||
switch st := env.Resolve(stream).(type) { | ||
case engine.Variable: | ||
return engine.Error(fmt.Errorf("read_string/3: stream cannot be a variable")) | ||
case *engine.Stream: | ||
s = st | ||
default: | ||
return engine.Error(fmt.Errorf("read_string/3: invalid domain for given stream")) | ||
} | ||
|
||
var maxLength uint64 | ||
if maxLen, ok := env.Resolve(length).(engine.Integer); ok { | ||
maxLength = uint64(maxLen) | ||
} | ||
|
||
var builder strings.Builder | ||
var totalLen uint64 | ||
for { | ||
r, l, err := s.ReadRune() | ||
if err != nil || (maxLength != 0 && totalLen >= maxLength) { | ||
if errors.Is(err, io.EOF) || totalLen >= maxLength { | ||
break | ||
} | ||
return engine.Error(fmt.Errorf("read_string/3: couldn't read stream: %w", err)) | ||
} | ||
totalLen += uint64(l) | ||
_, err = builder.WriteRune(r) | ||
if err != nil { | ||
return engine.Error(fmt.Errorf("read_string/3: couldn't write string: %w", err)) | ||
} | ||
} | ||
|
||
return engine.Unify(vm, Tuple(result, length), Tuple(util.StringToTerm(builder.String()), engine.Integer(totalLen)), cont, env) | ||
}) | ||
} |
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,199 @@ | ||
//nolint:gocognit | ||
package predicate | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/ichiban/prolog/engine" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
|
||
tmdb "github.com/cometbft/cometbft-db" | ||
"github.com/cometbft/cometbft/libs/log" | ||
tmproto "github.com/cometbft/cometbft/proto/tendermint/types" | ||
|
||
"github.com/cosmos/cosmos-sdk/store" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/okp4/okp4d/x/logic/testutil" | ||
"github.com/okp4/okp4d/x/logic/types" | ||
) | ||
|
||
func TestReadString(t *testing.T) { | ||
Convey("Given a test cases", t, func() { | ||
cases := []struct { | ||
input string | ||
program string | ||
query string | ||
wantResult []types.TermResults | ||
wantError error | ||
wantSuccess bool | ||
}{ | ||
{ | ||
input: "foo", | ||
program: "read_input(String) :- current_input(Stream), read_string(Stream, _, String).", | ||
query: `read_input(String).`, | ||
wantResult: []types.TermResults{{ | ||
"String": "foo", | ||
}}, | ||
wantSuccess: true, | ||
}, | ||
{ | ||
input: "foo bar", | ||
program: "read_input(String) :- current_input(Stream), read_string(Stream, _, String).", | ||
query: `read_input(String).`, | ||
wantResult: []types.TermResults{{ | ||
"String": "'foo bar'", | ||
}}, | ||
wantSuccess: true, | ||
}, | ||
{ | ||
input: "foo bar", | ||
program: "read_input(String, Len) :- current_input(Stream), read_string(Stream, Len, String).", | ||
query: `read_input(String, Len).`, | ||
wantResult: []types.TermResults{{ | ||
"String": "'foo bar'", | ||
"Len": "7", | ||
}}, | ||
wantSuccess: true, | ||
}, | ||
{ | ||
input: "foo bar", | ||
program: "read_input(String, Len) :- current_input(Stream), read_string(Stream, Len, String).", | ||
query: `read_input(String, 3).`, | ||
wantResult: []types.TermResults{{ | ||
"String": "foo", | ||
}}, | ||
wantSuccess: true, | ||
}, | ||
{ | ||
input: "foo bar", | ||
program: "read_input(String, Len) :- current_input(Stream), read_string(Stream, Len, String).", | ||
query: `read_input(String, 7).`, | ||
wantResult: []types.TermResults{{ | ||
"String": "'foo bar'", | ||
}}, | ||
wantSuccess: true, | ||
}, | ||
{ | ||
input: "foo bar 🧙", | ||
program: "read_input(String, Len) :- current_input(Stream), read_string(Stream, Len, String).", | ||
query: `read_input(String, _).`, | ||
wantResult: []types.TermResults{{ | ||
"String": "'foo bar 🧙'", | ||
}}, | ||
wantSuccess: true, | ||
}, | ||
{ | ||
input: "foo bar 🧙", | ||
program: "read_input(String, Len) :- current_input(Stream), read_string(Stream, Len, String).", | ||
query: `read_input(String, Len).`, | ||
wantResult: []types.TermResults{{ | ||
"String": "'foo bar 🧙'", | ||
"Len": "12", | ||
}}, | ||
wantSuccess: true, | ||
}, | ||
{ | ||
input: "🧙", | ||
program: "read_input(String, Len) :- current_input(Stream), read_string(Stream, Len, String).", | ||
query: `read_input(String, Len).`, | ||
wantResult: []types.TermResults{{ | ||
"String": "'🧙'", | ||
"Len": "4", | ||
}}, | ||
wantSuccess: true, | ||
}, | ||
{ | ||
input: "🧙", | ||
program: "read_input(String, Len) :- current_input(Stream), read_string(Stream, Len, String).", | ||
query: `read_input(String, 1).`, | ||
wantResult: []types.TermResults{{ | ||
"String": "'🧙'", | ||
}}, | ||
wantSuccess: false, | ||
}, | ||
{ | ||
input: "Hello World!", | ||
program: "read_input(String, Len) :- current_input(Stream), read_string(Stream, Len, String).", | ||
query: `read_input(String, 15).`, | ||
wantResult: []types.TermResults{{ | ||
"String": "'Hello World!'", | ||
}}, | ||
wantSuccess: false, | ||
}, | ||
{ | ||
input: "Hello World!", | ||
program: "read_input(String, Len) :- current_input(Stream), read_string(foo, Len, String).", | ||
query: `read_input(String, Len).`, | ||
wantError: fmt.Errorf("read_string/3: invalid domain for given stream"), | ||
wantSuccess: false, | ||
}, | ||
{ | ||
input: "Hello World!", | ||
query: `read_string(Stream, Len, data).`, | ||
wantError: fmt.Errorf("read_string/3: stream cannot be a variable"), | ||
wantSuccess: false, | ||
}, | ||
} | ||
for nc, tc := range cases { | ||
Convey(fmt.Sprintf("Given the query #%d: %s", nc, tc.query), func() { | ||
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.NewComprehensiveInterpreterMust(ctx) | ||
interpreter.Register3(engine.NewAtom("read_string"), ReadString) | ||
|
||
interpreter.SetUserInput(engine.NewInputTextStream(strings.NewReader(tc.input))) | ||
|
||
err := interpreter.Compile(ctx, tc.program) | ||
So(err, ShouldBeNil) | ||
|
||
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) | ||
} | ||
} | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
} | ||
}) | ||
} |
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