-
Notifications
You must be signed in to change notification settings - Fork 32
/
solutions.go
315 lines (279 loc) · 7.02 KB
/
solutions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package prolog
import (
"context"
"errors"
"fmt"
"reflect"
"strings"
"github.com/ichiban/prolog/engine"
)
// ErrClosed indicates the Solutions are already closed and unable to perform the operation.
var ErrClosed = errors.New("closed")
var errConversion = errors.New("conversion failed")
// Solutions is the result of a query. Everytime the Next method is called, it searches for the next solution.
// By calling the Scan method, you can retrieve the content of the solution.
type Solutions struct {
vm *engine.VM
env *engine.Env
vars []engine.ParsedVariable
more chan<- bool
next <-chan *engine.Env
err error
closed bool
}
// Close closes the Solutions and terminates the search for other solutions.
func (s *Solutions) Close() error {
if s.closed {
return ErrClosed
}
close(s.more)
s.closed = true
return nil
}
// Next prepares the next solution for reading with the Scan method. It returns true if it finds another solution,
// or false if there's no further solutions or if there's an error.
func (s *Solutions) Next() bool {
if s.closed {
return false
}
s.more <- true
var ok bool
s.env, ok = <-s.next
return ok
}
// Scan copies the variable values of the current solution into the specified struct/map.
func (s *Solutions) Scan(dest interface{}) error {
o := reflect.ValueOf(dest)
for o.Kind() == reflect.Ptr {
o = o.Elem()
}
switch o.Kind() {
case reflect.Struct:
t := o.Type()
fields := make(map[string]interface{}, t.NumField())
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
name := f.Name
if alias, ok := f.Tag.Lookup("prolog"); ok {
name = alias
}
fields[name] = o.Field(i).Addr().Interface()
}
for _, v := range s.vars {
n := v.Name.String()
f, ok := fields[n]
if !ok {
continue
}
if err := convertAssign(f, s.vm, v.Variable, s.env); err != nil {
return err
}
}
return nil
case reflect.Map:
t := o.Type()
if t.Key() != reflect.TypeOf("") {
return errors.New("map key is not string")
}
for _, v := range s.vars {
dest := reflect.New(t.Elem())
if err := convertAssign(dest.Interface(), s.vm, v.Variable, s.env); err != nil {
return err
}
o.SetMapIndex(reflect.ValueOf(v.Name.String()), dest.Elem())
}
return nil
default:
return fmt.Errorf("invalid kind: %s", o.Kind())
}
}
var atomEmptyList = engine.NewAtom("[]")
func convertAssign(dest interface{}, vm *engine.VM, t engine.Term, env *engine.Env) error {
switch d := dest.(type) {
case *interface{}:
return convertAssignAny(d, vm, t, env)
case *string:
return convertAssignString(d, t, env)
case *int:
return convertAssignInt(d, t, env)
case *int8:
return convertAssignInt8(d, t, env)
case *int16:
return convertAssignInt16(d, t, env)
case *int32:
return convertAssignInt32(d, t, env)
case *int64:
return convertAssignInt64(d, t, env)
case *float32:
return convertAssignFloat32(d, t, env)
case *float64:
return convertAssignFloat64(d, t, env)
case Scanner:
return d.Scan(vm, t, env)
default:
return convertAssignSlice(d, vm, t, env)
}
}
func convertAssignAny(d *interface{}, vm *engine.VM, t engine.Term, env *engine.Env) error {
switch t := env.Resolve(t).(type) {
case engine.Variable:
*d = nil
return nil
case engine.Atom:
if t == atomEmptyList {
*d = []interface{}{}
} else {
*d = t.String()
}
return nil
case engine.Integer:
*d = int(t)
return nil
case engine.Float:
*d = float64(t)
return nil
case engine.Compound:
var s []interface{}
iter := engine.ListIterator{List: t, Env: env}
for iter.Next() {
s = append(s, nil)
if err := convertAssign(&s[len(s)-1], vm, iter.Current(), env); err != nil {
return err
}
}
if err := iter.Err(); err != nil {
return errConversion
}
*d = s
return nil
default:
return errConversion
}
}
func convertAssignString(d *string, t engine.Term, env *engine.Env) error {
switch t := env.Resolve(t).(type) {
case fmt.Stringer:
*d = t.String()
return nil
default:
return errConversion
}
}
func convertAssignInt(d *int, t engine.Term, env *engine.Env) error {
switch t := env.Resolve(t).(type) {
case engine.Integer:
*d = int(t)
return nil
default:
return errConversion
}
}
func convertAssignInt8(d *int8, t engine.Term, env *engine.Env) error {
switch t := env.Resolve(t).(type) {
case engine.Integer:
*d = int8(t)
return nil
default:
return errConversion
}
}
func convertAssignInt16(d *int16, t engine.Term, env *engine.Env) error {
switch t := env.Resolve(t).(type) {
case engine.Integer:
*d = int16(t)
return nil
default:
return errConversion
}
}
func convertAssignInt32(d *int32, t engine.Term, env *engine.Env) error {
switch t := env.Resolve(t).(type) {
case engine.Integer:
*d = int32(t)
return nil
default:
return errConversion
}
}
func convertAssignInt64(d *int64, t engine.Term, env *engine.Env) error {
switch t := env.Resolve(t).(type) {
case engine.Integer:
*d = int64(t)
return nil
default:
return errConversion
}
}
func convertAssignFloat32(d *float32, t engine.Term, env *engine.Env) error {
switch t := env.Resolve(t).(type) {
case engine.Float:
*d = float32(t)
return nil
default:
return errConversion
}
}
func convertAssignFloat64(d *float64, t engine.Term, env *engine.Env) error {
switch t := env.Resolve(t).(type) {
case engine.Float:
*d = float64(t)
return nil
default:
return errConversion
}
}
func convertAssignSlice(d interface{}, vm *engine.VM, t engine.Term, env *engine.Env) error {
v := reflect.ValueOf(d).Elem()
if k := v.Kind(); k != reflect.Slice {
return errConversion
}
v.SetLen(0)
orig := v
iter := engine.ListIterator{List: t, Env: env}
for iter.Next() {
v = reflect.Append(v, reflect.Zero(v.Type().Elem()))
dest := v.Index(v.Len() - 1).Addr().Interface()
if err := convertAssign(dest, vm, iter.Current(), env); err != nil {
return err
}
}
if err := iter.Err(); err != nil {
return errConversion
}
orig.Set(v)
return nil
}
// Err returns the error if exists.
func (s *Solutions) Err() error {
return s.err
}
// Solution is the single result of a query.
type Solution struct {
sols *Solutions
err error
}
// Scan copies the variable values of the solution into the specified struct/map.
func (s *Solution) Scan(dest interface{}) error {
if err := s.err; err != nil {
return err
}
return s.sols.Scan(dest)
}
// Err returns an error that occurred while querying for the Solution, if any.
func (s *Solution) Err() error {
return s.err
}
// Scanner is an interface for custom conversion from term to Go value.
type Scanner interface {
Scan(vm *engine.VM, term engine.Term, env *engine.Env) error
}
// TermString is a string representation of term.
type TermString string
// Scan implements Scanner interface.
func (t *TermString) Scan(vm *engine.VM, term engine.Term, env *engine.Env) error {
var sb strings.Builder
s := engine.NewOutputTextStream(&sb)
_, _ = engine.WriteTerm(vm, s, term, engine.List(engine.NewAtom("quoted").Apply(engine.NewAtom("true"))), engine.Success, env).Force(context.Background())
*t = TermString(sb.String())
return nil
}