-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathRuleBoolean.lua
280 lines (237 loc) · 7.39 KB
/
RuleBoolean.lua
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
--[[----------------------------------------------------------------------------
LiteMount/RuleBoolean.lua
Copyright 2011 Mike Battersby
----------------------------------------------------------------------------]]--
local _, LM = ...
local L = LM.Localize
--[[
<conditions> := <condition> |
<condition> <conditions>
<condition> := "[" <expressions> "]"
<expressions> := <expr> |
<expr> "," <expressions>
<expr> := "no" <setting> |
<setting>
<setting> := <tag> |
<tag> ":" <args>
<args> := <arg> |
<arg> / <args>
<arg> := [-a-zA-Z0-9]+
<tag> := See CONDITIONS array in code
]]
local function any(f, cond, context, ...)
local n = select('#', ...)
for i = 1, n do
local v = select(i, ...)
if f(cond, context, v) then return true end
end
return false
end
LM.RuleBoolean = { }
function LM.RuleBoolean:Get()
return CreateFromMixins(LM.RuleBoolean)
end
function LM.RuleBoolean:Leaf(text)
local c = LM.RuleBoolean:Get()
c.op = 'LEAF'
local condition, argstr = strsplit(':', text)
c.condition = condition
if argstr then
c.args = { strsplit('/', argstr) }
else
c.args = { }
end
return c
end
function LM.RuleBoolean:And(...)
local c = LM.RuleBoolean:Get()
c.op = 'AND'
c.conditions = { ... }
return c
end
function LM.RuleBoolean:Or(...)
local c = LM.RuleBoolean:Get()
c.op = 'OR'
c.conditions = { ... }
return c
end
function LM.RuleBoolean:Not(cond)
local c = LM.RuleBoolean:Get()
c.op = 'NOT'
c.conditions = { cond }
return c
end
function LM.RuleBoolean:EvalLeaf(context)
-- Empty condition [] is true
if self.condition == "" then
return true
end
if self.condition:len() > 1 then
if self.condition:sub(1,1) == '@' then
context.rule.unit = self.condition:sub(2)
return true
end
-- This is a mildly awful hack to allow setting binary options on actions
-- using conditions instead of having heterogenous arg types or overloading
-- the expression syntax (even more). Devoid of backwards compatibility it'd
-- probably be better to have used this for Limit as well as Mount.
if self.condition:sub(1,1) == '+' then
context.rule[self.condition:sub(2)] = true
return true
elseif self.condition:sub(1,1) == '-' then
context.rule[self.condition:sub(2)] = false
return true
end
end
local c = LM.Conditions:GetCondition(self.condition)
if not c then
-- Hopefully stopped at compile time and can't happen
return false
end
if c.args then
return c.handler(self, context, unpack(self.args))
elseif #self.args == 0 then
return c.handler(self, context)
else
return any(c.handler, self, context, unpack(self.args))
end
end
function LM.RuleBoolean:EvalNot(context)
return not self.conditions[1]:Eval(context)
end
-- the ANDed sections carry the per-rule context between them
function LM.RuleBoolean:EvalAnd(context)
for _,c in ipairs(self.conditions) do
local v = c:Eval(context)
if not v then return false end
end
return true
end
-- Note: deliberately resets the per-rule context on false
function LM.RuleBoolean:EvalOr(context)
if #self.conditions == 0 then
return true
end
local origRuleContext = CopyTable(context.rule)
for _,c in ipairs(self.conditions) do
local v = c:Eval(context)
if v then return v end
context.rule = origRuleContext
end
return false
end
function LM.RuleBoolean:Eval(context)
if self.op == 'LEAF' then
return self:EvalLeaf(context)
elseif self.op == 'NOT' then
return self:EvalNot(context)
elseif self.op == 'AND' then
return self:EvalAnd(context)
elseif self.op == 'OR' then
return self:EvalOr(context)
end
end
-- 3 or fewer ANDed conditions
function LM.RuleBoolean:IsSimpleCondition()
if #self.conditions ~= 1 then
return false
end
if #self.conditions[1].conditions > 3 then
return false
end
return true
end
function LM.RuleBoolean:GetSimpleConditions()
if self.conditions[1] then
return self.conditions[1].conditions
else
return {}
end
end
local function UnBracket(txt) return txt:sub(2,-2) end
function LM.RuleBoolean:ToString()
if self.op == 'LEAF' then
if #self.args > 0 then
local argstr = table.concat(self.args, '/')
return '[' .. self.condition .. ':' .. argstr .. ']'
else
return '[' .. self.condition .. ']'
end
elseif self.op == 'NOT' then
return '[' .. 'no' .. self.conditions[1]:ToString():sub(2,-2) .. ']'
elseif self.op == 'OR' then
local children = LM.tMap(self.conditions, LM.RuleBoolean.ToString)
return table.concat(children, '')
elseif self.op == 'AND' then
local children = LM.tMap(self.conditions, LM.RuleBoolean.ToString)
children = LM.tMap(children, UnBracket)
return '[' .. table.concat(children, ',') .. ']'
end
end
-- This works exactly just enough for the simple rules that can be created
-- from the UI, and will totally crap itself on a real rule.
function LM.RuleBoolean:ToDisplay()
if self.op == 'OR' then
if #self.conditions == 0 then
return { GREEN_FONT_COLOR:WrapTextInColorCode(ALWAYS:upper()) }
else
return self.conditions[1]:ToDisplay()
end
elseif self.op == 'AND' then
local out = { }
for _,c in ipairs(self.conditions) do
local text = c:ToDisplay()
if c.op == 'NOT' then
table.insert(out, RED_FONT_COLOR:WrapTextInColorCode(text))
else
table.insert(out, GREEN_FONT_COLOR:WrapTextInColorCode(text))
end
end
return out
elseif self.op == 'NOT' then
local text = self.conditions[1]:ToDisplay()
return string.format(L.LM_NOT_FORMAT, text)
elseif self.op == 'LEAF' then
local s = UnBracket(self:ToString())
local c, a = LM.Conditions:ToDisplay(s)
if a then
return string.format('%s : %s', c, a)
elseif c then
return c
else
return s.." |A:gmchat-icon-alert:15:15|a"
end
end
end
function LM.RuleBoolean:ValidateLeaf(badList)
if self.condition == '' then
return true
elseif self.condition:len() > 1 then
if self.condition:sub(1, 1) == '@' then
return true
elseif self.condition:sub(1, 1) == '+' then
return true
elseif self.condition:sub(1, 1) == '-' then
return true
end
end
local c = LM.Conditions:GetCondition(self.condition)
if not c then
table.insert(badList, self.condition)
end
end
function LM.RuleBoolean:Validate(badList)
badList = badList or {}
if self.op == 'LEAF' then
self:ValidateLeaf(badList)
else
for _, c in ipairs(self.conditions) do
c:Validate(badList)
end
end
if #badList == 0 then
return true
else
return false, format(L.LM_ERR_BAD_CONDITION, table.concat(badList, ', '))
end
end