-
Notifications
You must be signed in to change notification settings - Fork 0
/
predicate.go
235 lines (209 loc) · 5.58 KB
/
predicate.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
package dLola
import (
"errors"
"fmt"
)
type BoolExpr interface {
Sprint() string
AcceptBool(BoolExprVisitor)
GetPos() Position
InstantiateBoolExpr(int, int) InstBoolExpr
ConstantSubsBoolExpr(spec *Spec) BoolExpr
}
type BoolExprVisitor interface {
VisitTruePredicate(TruePredicate)
VisitFalsePredicate(FalsePredicate)
VisitNotPredicate(NotPredicate)
VisitAndPredicate(AndPredicate)
VisitOrPredicate(OrPredicate)
VisitIfThenElseExpr(IfThenElseExpr) //same method with same arguments as in ExprVisitor
VisitConstExpr(ConstExpr) //same method with same arguments as in ExprVisitor
VisitStreamOffsetExpr(StreamOffsetExpr) //same method with same arguments as in ExprVisitor
//
VisitNumComparisonPredicate(NumComparisonPredicate)
VisitStrComparisonPredicate(StrComparisonPredicate)
// VisitPathPredicate(PathPredicate)
// VisitStrPredicate(StrPredicate)
// VisitTagPredicate(TagPredicate)
}
/*var (
True TruePredicate
False FalsePredicate
TrueExpr BoolExpr = BoolExpr{True}
FalseExpr BoolExpr = BoolExpr{False}
)*/
type TruePredicate struct{ Pos Position }
type FalsePredicate struct{ Pos Position }
type NotPredicate struct {
Inner BoolExpr
}
type AndPredicate struct {
Left BoolExpr
Right BoolExpr
}
type OrPredicate struct {
Left BoolExpr
Right BoolExpr
}
/*type IfThenElsePredicate struct {
If BoolExpr
Then BoolExpr
Else BoolExpr
}*/
type NumComparisonPredicate struct {
Comp NumComparison
}
type StrComparisonPredicate struct {
Comp StrComparison
}
func BoolExprToExpr(p BoolExpr) Expr {
if s, ok := p.(StreamOffsetExpr); ok {
return s
} else if k, ok := p.(ConstExpr); ok {
return k
} else {
return NewBooleanExpr(p)
}
}
func getBoolExpr(e interface{}) (BoolExpr, error) {
switch v := e.(type) {
case BooleanExpr:
return v.BExpr, nil
case StreamOffsetExpr:
return v, nil
case ConstExpr:
return v, nil
case BoolExpr:
return v, nil
case TruePredicate:
return v, nil
case FalsePredicate:
return v, nil
default:
// fmt.Printf("Is error \n", e)
str := fmt.Sprintf("cannot convert to bool \"%s\"\n", e.(Expr).Sprint()) //here v has type interface{}
return nil, errors.New(str)
}
}
func NewAndPredicate(a, b interface{}) BoolExpr {
preds := ToSlice(b)
first, _ := getBoolExpr(a)
if len(preds) == 0 {
return first
}
right, _ := getBoolExpr(preds[len(preds)-1])
for i := len(preds) - 2; i >= 0; i-- {
left, _ := getBoolExpr(preds[i])
right = AndPredicate{left, right}
}
ret := AndPredicate{first, right}
return ret
}
func NewOrPredicate(a, b interface{}) BoolExpr {
preds := ToSlice(b)
first, _ := getBoolExpr(a)
if len(preds) == 0 {
return first
}
right, _ := getBoolExpr(preds[len(preds)-1])
for i := len(preds) - 2; i >= 0; i-- {
left, _ := getBoolExpr(preds[i])
right = OrPredicate{left, right}
}
return OrPredicate{first, right}
}
func NewNotPredicate(n interface{}) NotPredicate {
return NotPredicate{n.(BoolExpr)}
}
func NewTruePredicate(p interface{}) TruePredicate {
return TruePredicate{NewPosition(p)}
}
func NewFalsePredicate(p interface{}) FalsePredicate {
return FalsePredicate{NewPosition(p)}
}
//
// sprint() functions of the different Predicates
//
func (p AndPredicate) Sprint() string {
return fmt.Sprintf("(%s /\\ %s)", p.Left.Sprint(), p.Right.Sprint())
}
func (p OrPredicate) Sprint() string {
return fmt.Sprintf("(%s \\/ %s)", p.Left.Sprint(), p.Right.Sprint())
}
func (p NotPredicate) Sprint() string {
return fmt.Sprintf("~ %s", p.Inner.Sprint())
}
func (p TruePredicate) Sprint() string {
return fmt.Sprintf("true")
}
func (p FalsePredicate) Sprint() string {
return fmt.Sprintf("false")
}
func (p NumComparisonPredicate) Sprint() string {
return p.Comp.Sprint()
}
func (p StrComparisonPredicate) Sprint() string {
return p.Comp.Sprint()
}
func (this TruePredicate) AcceptBool(v BoolExprVisitor) {
v.VisitTruePredicate(this)
}
func (this FalsePredicate) AcceptBool(v BoolExprVisitor) {
v.VisitFalsePredicate(this)
}
func (this NotPredicate) AcceptBool(v BoolExprVisitor) {
v.VisitNotPredicate(this)
}
func (this AndPredicate) AcceptBool(v BoolExprVisitor) {
v.VisitAndPredicate(this)
}
func (this OrPredicate) AcceptBool(v BoolExprVisitor) {
v.VisitOrPredicate(this)
}
func (this IfThenElseExpr) AcceptBool(v BoolExprVisitor) {
v.VisitIfThenElseExpr(this)
}
// ConstExpr implement AcceptBool so StreamExpr are BoolExpr
func (this ConstExpr) AcceptBool(v BoolExprVisitor) {
v.VisitConstExpr(this)
}
// StreamExpr impleemnts AcceptBool so StreamExpr are Boolexpr
func (this StreamOffsetExpr) AcceptBool(v BoolExprVisitor) {
v.VisitStreamOffsetExpr(this)
}
func (this NumComparisonPredicate) AcceptBool(v BoolExprVisitor) {
v.VisitNumComparisonPredicate(this)
}
func (this StrComparisonPredicate) AcceptBool(v BoolExprVisitor) {
v.VisitStrComparisonPredicate(this)
}
func NewNumComparisonPredicate(a interface{}) NumComparisonPredicate {
return NumComparisonPredicate{a.(NumComparison)}
}
func NewStrComparisonPredicate(a interface{}) StrComparisonPredicate {
return StrComparisonPredicate{a.(StrComparison)}
}
func (this AndPredicate) GetPos() Position {
return this.Left.GetPos()
}
func (this OrPredicate) GetPos() Position {
return this.Left.GetPos()
}
func (this NotPredicate) GetPos() Position {
return this.Inner.GetPos()
}
func (this TruePredicate) GetPos() Position {
return this.Pos
}
func (this FalsePredicate) GetPos() Position {
return this.Pos
}
/*func (this IfThenElsePredicate) GetPos() Position {
return this.If.GetPos()
}*/
func (this NumComparisonPredicate) GetPos() Position {
return this.Comp.GetPos()
}
func (this StrComparisonPredicate) GetPos() Position {
return this.Comp.GetPos()
}