-
Notifications
You must be signed in to change notification settings - Fork 0
/
assemble.go
321 lines (280 loc) · 7.25 KB
/
assemble.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
316
317
318
319
320
321
package styx
import (
"fmt"
"sort"
badger "github.com/dgraph-io/badger/v2"
rdf "github.com/underlay/go-rdfjs"
)
// NewIterator populates, scores, sorts, and connects a new constraint graph
func newIterator(
query []*rdf.Quad,
domain []rdf.Term,
index []rdf.Term,
tag TagScheme,
txn *badger.Txn,
dictionary Dictionary,
) (iter *Iterator, err error) {
if domain == nil {
domain = make([]rdf.Term, 0)
}
iter = &Iterator{
query: query,
domain: domain,
pivot: len(domain),
constants: make([]*constraint, 0),
variables: make([]*variable, len(domain)),
ids: make(map[string]int, len(domain)),
unary: newUnaryCache(),
binary: newBinaryCache(),
tag: tag,
txn: txn,
dictionary: dictionary,
}
var split bool
for i, node := range domain {
if node.TermType() == rdf.VariableType {
if split {
return nil, ErrInvalidDomain
}
} else if node.TermType() == rdf.BlankNodeType {
split = true
} else {
return nil, ErrInvalidDomain
}
value := node.String()
iter.variables[i] = &variable{node: node}
iter.ids[value] = i
}
// Check that the domian is valid
if len(domain) < len(index) {
err = ErrInvalidIndex
return
}
for i, quad := range query {
if quad.Graph().TermType() != rdf.DefaultGraphType {
continue
}
variables := [3]*variable{}
for p := 0; p < 3; p++ {
variables[p] = iter.parseNode(quad[p])
}
degree := 0
terms := [3]ID{}
for p := 0; p < 3; p++ {
if variables[p] == nil {
terms[p], err = dictionary.GetID(quad[p], rdf.Default)
if err != nil {
return
}
} else {
degree++
}
}
if degree == 0 {
iter.constants = append(iter.constants, &constraint{index: i, quad: quad})
} else if degree == 1 {
// Only one of the terms is a blank node, so this is a first-degree constraint.
c := &constraint{
index: i,
quad: quad,
terms: terms,
}
for ; c.place < 3; c.place++ {
if variables[c.place] != nil {
break
}
}
err = iter.insertD1(variables[c.place], c, txn)
if err == ErrEndOfSolutions {
iter.empty = true
return iter, nil
} else if err != nil {
return
}
} else if degree == 2 {
// Two of the terms is are blank nodes.
// If they're the same blank node, then we insert one z-degree constraint.
// If they're different, we insert two second-degree constraints.
var p Permutation
for ; p < 3; p++ {
if variables[p] == nil {
break
}
}
q, r := (p+1)%3, (p+2)%3
if variables[q] == variables[r] {
c := &constraint{
index: i,
place: q,
quad: quad,
terms: terms,
}
err = iter.insertDZ(variables[q], c, txn)
if err == ErrEndOfSolutions {
iter.empty = true
return iter, nil
} else if err != nil {
return
}
} else {
neighbors := make([]*constraint, 3)
a := &constraint{index: i, place: q, quad: quad, terms: terms, neighbors: neighbors}
b := &constraint{index: i, place: r, quad: quad, terms: terms, neighbors: neighbors}
neighbors[r], neighbors[q] = b, a
err = iter.insertD2(variables[q], variables[r], a, txn)
if err == ErrEndOfSolutions {
iter.empty = true
return iter, nil
} else if err != nil {
return
}
err = iter.insertD2(variables[r], variables[q], b, txn)
if err == ErrEndOfSolutions {
iter.empty = true
return iter, nil
} else if err != nil {
return
}
}
} else if degree == 3 {
return nil, fmt.Errorf("Cannot handle all-blank triple: %d", i)
}
}
// Make sure that every node in the domain
// actually occurs in the graph
for _, u := range iter.variables {
if len(u.cs) == 0 {
err = ErrInvalidDomain
return
}
}
// Score the variables
for _, u := range iter.variables {
u.norm = 0
for _, c := range u.cs {
u.norm += uint64(c.count) * uint64(c.count)
}
u.score = float64(u.norm) / float64(u.cs.Len())
u.Sort()
u.root = u.cs.Seek(NIL)
if u.root == NIL {
err = ErrEmptyInterset
return
}
// Set the initial value of each variable.
// This will get overwritten to be NIL if/when
// previous dependencies propagate their assignments.
u.value = u.root
}
// Sorting keeps variables at indices less than iter.pivot in place
if len(domain) < len(iter.domain)+1 {
sort.Stable(iter)
// Now we're in a tricky spot. iter.domain and iter.variables
// have changed, but not iter.ids or the variable constraint maps.
transformation := make([]int, len(iter.domain))
sortedIds := make(map[string]int, len(iter.domain))
for i, p := range iter.domain {
value := p.String()
j := iter.ids[value]
transformation[j] = i
sortedIds[value] = i
}
// set the new id map
iter.ids = sortedIds
// Now we relabel all the variables...
for _, u := range iter.variables {
d2 := make(constraintMap, len(u.edges))
for i, cs := range u.edges {
j := transformation[i]
d2[j] = cs
}
u.edges = d2
}
}
// Reset iter.pivot
iter.pivot = len(iter.domain)
for i, u := range iter.variables {
// Set iter.pivot to be the index of the first blank node
if i < iter.pivot && u.node.TermType() == rdf.BlankNodeType {
iter.pivot = i
}
for j, cs := range u.edges {
if j < i {
// So these are connections that point "backward"
// - i.e. q has already come before p.
// These constraints are the ones that get pushed into,
// and so they get deleted from the D2 map
// (which is just for outgoing connections)
cs.Close()
for _, c := range cs {
p := TernaryPrefixes[(c.place+1)%3]
c.iterator = txn.NewIterator(badger.IteratorOptions{
PrefetchValues: false,
Prefix: []byte{p},
})
}
delete(u.edges, j)
}
}
}
// Assemble the dependency maps
iter.in = make([][]int, len(iter.domain))
iter.out = make([][]int, len(iter.domain))
in := make([]map[int]bool, len(iter.domain))
out := make([]map[int]bool, len(iter.domain))
for i := range iter.domain {
out[i] = map[int]bool{}
for j := range iter.variables[i].edges {
if in[j] == nil {
in[j] = map[int]bool{i: true}
} else {
in[j][i] = true
}
for k := range in[i] {
in[j][k] = true
}
}
}
// Invert the input map to get the output map
for i, deps := range in {
for j := range deps {
out[j][i] = true
}
}
// Sort the dependency maps
for i := range iter.domain {
iter.in[i] = make([]int, 0, len(in[i]))
for j := range in[i] {
iter.in[i] = append(iter.in[i], j)
}
sort.Ints(iter.in[i])
iter.out[i] = make([]int, 0, len(out[i]))
for j := range out[i] {
iter.out[i] = append(iter.out[i], j)
}
sort.Ints(iter.out[i])
}
l := len(iter.domain)
iter.cache = make([]*vcache, l)
iter.blacklist = make([]bool, l)
// Viola! We are returning a newly scored, sorted, and connected constraint graph.
return iter, iter.Seek(index)
}
func (iter *Iterator) parseNode(node rdf.Term) *variable {
if node.TermType() != rdf.VariableType && node.TermType() != rdf.BlankNodeType {
return nil
}
value := node.String()
if i, has := iter.ids[value]; has {
u := iter.variables[i]
return u
}
// if node.TermType() == rdf.VariableType {
// iter.pivot++ // lol
// }
v := &variable{node: node}
iter.ids[value] = len(iter.domain)
iter.domain = append(iter.domain, node)
iter.variables = append(iter.variables, v)
return v
}