/
simplify.go
402 lines (372 loc) · 10 KB
/
simplify.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// Copyright 2015 Walter Schulze
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package interp
import (
"fmt"
"github.com/katydid/katydid/relapse/ast"
"github.com/katydid/katydid/relapse/compose"
"github.com/katydid/katydid/relapse/funcs"
nameexpr "github.com/katydid/katydid/relapse/name"
"github.com/katydid/katydid/relapse/parser"
)
//Simplifier simplifies the patterns of a given grammar.
type Simplifier interface {
//Simplify returns a pattern that has been simplified, transformed to an equivalent, but smaller or equal pattern.
Simplify(p *ast.Pattern) *ast.Pattern
//Grammar returns a grammar that has been simplified, transformed to an equivalent, but smaller or equal grammar.
Grammar() *ast.Grammar
//OptimizeForRecord optimizes the simplifier to also takes into account the fact the structure being validated is a record.
//A record can be json, protobuf, reflected go structures or any structure that have unique field names for each structure.
//XML would be an example of a structure for which this simplification is NOT appropriate.
OptimizeForRecord() Simplifier
}
type simplifier struct {
refs ast.RefLookup
cache map[*ast.Pattern]struct{}
record bool
}
//NewSimplifier returns a new Simplifier that is used to simplify a grammar and its patterns.
func NewSimplifier(g *ast.Grammar) Simplifier {
return &simplifier{
refs: ast.NewRefLookup(g),
cache: make(map[*ast.Pattern]struct{}),
}
}
func (this *simplifier) Simplify(p *ast.Pattern) *ast.Pattern {
if _, ok := this.cache[p]; ok {
return p
}
s := this.simplify(p, true)
this.cache[s] = struct{}{}
return s
}
func (this *simplifier) Grammar() *ast.Grammar {
for name, p := range this.refs {
this.refs[name] = this.Simplify(p)
}
return ast.NewGrammar(this.refs)
}
func (this *simplifier) OptimizeForRecord() Simplifier {
this.record = true
return this
}
func checkRef(refs ast.RefLookup, p *ast.Pattern) *ast.Pattern {
for name, rpat := range refs {
if rpat.Equal(p) {
return ast.NewReference(name)
}
}
return p
}
var emptyset = ast.NewNot(ast.NewZAny())
func (this *simplifier) simplify(p *ast.Pattern, top bool) *ast.Pattern {
cRef := func(cp *ast.Pattern) *ast.Pattern {
if top {
return cp
}
return checkRef(this.refs, cp)
}
cachesimp := func(sp *ast.Pattern) *ast.Pattern {
if _, ok := this.cache[sp]; ok {
return sp
}
s := this.simplify(sp, false)
this.cache[s] = struct{}{}
return s
}
simp := func(sp *ast.Pattern) *ast.Pattern {
return this.simplify(sp, false)
}
typ := p.GetValue()
switch v := typ.(type) {
case *ast.Empty:
return p
case *ast.TreeNode:
child := cachesimp(v.GetPattern())
if isNotZany(child) {
return emptyset
}
name := v.GetName()
b := nameexpr.NameToFunc(v.GetName())
if funcs.IsFalse(b) {
return emptyset
}
if funcs.IsTrue(b) {
name = ast.NewAnyName()
}
return cRef(ast.NewTreeNode(name, child))
case *ast.LeafNode:
b, err := compose.NewBool(v.GetExpr())
if err != nil {
//Don't simplify if there is an error to keep this function signature simple.
return p
}
if funcs.IsFalse(b) {
return emptyset
}
e, err := parser.ParseExpr(funcs.Sprint(b))
if err != nil {
//Don't simplify if there is an error to keep this function signature simple.
return p
}
return ast.NewLeafNode(e)
case *ast.Concat:
return cRef(simplifyConcat(
simp(v.GetLeftPattern()),
simp(v.GetRightPattern()),
))
case *ast.Or:
return cRef(simplifyOr(this.refs,
simp(v.GetLeftPattern()),
simp(v.GetRightPattern()),
this.record,
))
case *ast.And:
return cRef(simplifyAnd(this.refs,
simp(v.GetLeftPattern()),
simp(v.GetRightPattern()),
this.record,
))
case *ast.ZeroOrMore:
return cRef(simplifyZeroOrMore(simp(v.GetPattern())))
case *ast.Reference:
return p
case *ast.Not:
return cRef(simplifyNot(simp(v.GetPattern())))
case *ast.ZAny:
return p
case *ast.Contains:
return cRef(simplifyContains(simp(v.GetPattern())))
case *ast.Optional:
return simplifyOptional(simp(v.GetPattern()))
case *ast.Interleave:
return cRef(simplifyInterleave(this.refs,
cachesimp(v.GetLeftPattern()),
cachesimp(v.GetRightPattern()),
))
}
panic(fmt.Sprintf("unknown pattern typ %T", typ))
}
func isNotZany(p *ast.Pattern) bool {
return p.Not != nil && p.GetNot().GetPattern().ZAny != nil
}
func isEmpty(p *ast.Pattern) bool {
return p.Empty != nil
}
func isZany(p *ast.Pattern) bool {
return p.ZAny != nil
}
func simplifyConcat(p1, p2 *ast.Pattern) *ast.Pattern {
if isNotZany(p1) || isNotZany(p2) {
return emptyset
}
if p1.Concat != nil {
return simplifyConcat(
p1.Concat.GetLeftPattern(),
ast.NewConcat(p1.Concat.GetRightPattern(), p2),
)
}
if isEmpty(p1) {
return p2
}
if isEmpty(p2) {
return p1
}
if isZany(p1) && p2.Concat != nil {
if l := p2.Concat.GetLeftPattern(); isZany(p2.Concat.GetRightPattern()) {
return ast.NewContains(l)
}
}
return ast.NewConcat(p1, p2)
}
func getOrs(p *ast.Pattern) []*ast.Pattern {
if p.Or != nil {
return append(getOrs(p.Or.GetLeftPattern()), getOrs(p.Or.GetRightPattern())...)
}
return []*ast.Pattern{p}
}
func simplifyOr(refs ast.RefLookup, p1, p2 *ast.Pattern, record bool) *ast.Pattern {
if isNotZany(p1) {
return p2
}
if isNotZany(p2) {
return p1
}
if isZany(p1) || isZany(p2) {
return ast.NewZAny()
}
if isEmpty(p1) && Nullable(refs, p2) {
return p2
}
if isEmpty(p2) && Nullable(refs, p1) {
return p1
}
if p1.GetLeafNode() != nil && p2.GetLeafNode() != nil {
expr1, err1 := compose.ConvertBuiltInIntoFunction(p1.GetLeafNode().GetExpr())
expr2, err2 := compose.ConvertBuiltInIntoFunction(p2.GetLeafNode().GetExpr())
if err1 == nil && err2 == nil {
return ast.NewLeafNode(ast.NewFunction("or", expr1, expr2))
}
}
left := getOrs(p1)
right := getOrs(p2)
list := orderedSet(append(left, right...))
list = simplifyChildren(list, func(left, right *ast.Pattern) *ast.Pattern {
return simplifyOr(refs, left, right, record)
}, record)
var p *ast.Pattern = list[0]
for i := range list {
if i == 0 {
continue
}
p = ast.NewOr(p, list[i])
}
return p
}
func getAnds(p *ast.Pattern) []*ast.Pattern {
if p.And != nil {
return append(getAnds(p.And.GetLeftPattern()), getAnds(p.And.GetRightPattern())...)
}
return []*ast.Pattern{p}
}
func simplifyChildren(children []*ast.Pattern, op func(left, right *ast.Pattern) *ast.Pattern, record bool) []*ast.Pattern {
if len(children) == 0 || len(children) == 1 {
return children
}
if record {
c0 := children[0].GetContains().GetPattern().GetTreeNode()
c1 := children[1].GetContains().GetPattern().GetTreeNode()
if c0 != nil && c1 != nil {
if c0.GetName().GetName() != nil && c1.GetName().GetName() != nil && c0.GetName().Equal(c1.GetName()) {
newchild := ast.NewContains(ast.NewTreeNode(c0.GetName(), op(c0.GetPattern(), c1.GetPattern())))
children[1] = newchild
return simplifyChildren(children[1:], op, record)
}
}
}
t0 := children[0].TreeNode
t1 := children[1].TreeNode
if t0 != nil && t1 != nil {
if t0.GetName().Equal(t1.GetName()) {
newchild := ast.NewTreeNode(t0.GetName(), op(t0.GetPattern(), t1.GetPattern()))
children[1] = newchild
return simplifyChildren(children[1:], op, record)
}
}
return append([]*ast.Pattern{children[0]}, simplifyChildren(children[1:], op, record)...)
}
func simplifyAnd(refs ast.RefLookup, p1, p2 *ast.Pattern, record bool) *ast.Pattern {
if isNotZany(p1) || isNotZany(p2) {
return emptyset
}
if isZany(p1) {
return p2
}
if isZany(p2) {
return p1
}
if isEmpty(p1) {
if Nullable(refs, p2) {
return ast.NewEmpty()
} else {
return emptyset
}
}
if isEmpty(p2) {
if Nullable(refs, p1) {
return ast.NewEmpty()
} else {
return emptyset
}
}
if p1.GetLeafNode() != nil && p2.GetLeafNode() != nil {
expr1, err1 := compose.ConvertBuiltInIntoFunction(p1.GetLeafNode().GetExpr())
expr2, err2 := compose.ConvertBuiltInIntoFunction(p2.GetLeafNode().GetExpr())
if err1 == nil && err2 == nil {
return ast.NewLeafNode(ast.NewFunction("and", expr1, expr2))
}
}
left := getAnds(p1)
right := getAnds(p2)
list := orderedSet(append(left, right...))
list = simplifyChildren(list, func(left, right *ast.Pattern) *ast.Pattern {
return simplifyAnd(refs, left, right, record)
}, record)
var p *ast.Pattern = list[0]
for i := range list {
if i == 0 {
continue
}
p = ast.NewAnd(p, list[i])
}
return p
}
func simplifyZeroOrMore(p *ast.Pattern) *ast.Pattern {
if p.ZeroOrMore != nil {
return p
}
return ast.NewZeroOrMore(p)
}
func simplifyNot(p *ast.Pattern) *ast.Pattern {
if p.Not != nil {
return p.Not.GetPattern()
}
return ast.NewNot(p)
}
func simplifyOptional(p *ast.Pattern) *ast.Pattern {
if isEmpty(p) {
return ast.NewEmpty()
}
return ast.NewOptional(p)
}
func getInterleaves(p *ast.Pattern) []*ast.Pattern {
if p.Interleave != nil {
return append(getInterleaves(p.Interleave.GetLeftPattern()), getInterleaves(p.Interleave.GetRightPattern())...)
}
return []*ast.Pattern{p}
}
func simplifyInterleave(refs ast.RefLookup, p1, p2 *ast.Pattern) *ast.Pattern {
if isNotZany(p1) || isNotZany(p2) {
return emptyset
}
if isEmpty(p1) {
return p2
}
if isEmpty(p2) {
return p1
}
if isZany(p1) && isZany(p2) {
return p1
}
left := getInterleaves(p1)
right := getInterleaves(p2)
list := orderedSet(append(left, right...))
var p *ast.Pattern = list[0]
for i := range list {
if i == 0 {
continue
}
p = ast.NewInterleave(p, list[i])
}
return p
}
func simplifyContains(p *ast.Pattern) *ast.Pattern {
if isEmpty(p) || isZany(p) {
return ast.NewZAny()
}
if isNotZany(p) {
return p
}
return ast.NewContains(p)
}