-
Notifications
You must be signed in to change notification settings - Fork 0
/
rule.py
239 lines (222 loc) · 8.67 KB
/
rule.py
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
import base
import typing
import abc
import rucmElement
import defaultRule
from reporter import ErrorInfo, Reporter
class Rule():
def __init__(self):
self.id: int = -1
self.description: str = None
self.status: bool = True
self.rtype: str = None # rtype in ["user","system"]
@abc.abstractmethod
def check(self) -> typing.List[ErrorInfo]:
pass
def __repr__(self):
return str({
'id': self.id,
'description': self.description,
'status': self.status
})
class SimpleRule():
def __init__(self):
self.target: base.RuleSubject = None
self.op : base.SimpleOp = None
self.val: typing.List[str] = []
self.description: str = ""
def check(self, sentence: rucmElement.Sentence) -> bool:
value = self.dynamicFill(sentence.useCaseName)
target = []
if self.target == base.RuleSubject.subject_Val:
wds = sentence.subjects
for word in wds:
target.append(word.val)
elif self.target == base.RuleSubject.object_Val:
wds = sentence.objects
for word in wds:
target.append(word.val)
elif self.target == base.RuleSubject.verb_count:
target.append(len(sentence.verbs))
elif self.target == base.RuleSubject.verb_tense:
target.append(sentence.tense.value)
elif self.target == base.RuleSubject.strs:
# print(sentence.words)
target = [word.val for word in sentence.words]
elif self.target == base.RuleSubject.subject_count:
target.append(len(sentence.subjects))
elif self.target == base.RuleSubject.object_count:
target.append(len(sentence.objects))
elif self.target == base.RuleSubject.participlePhrases_count:
target.append(sentence.participle_count)
elif self.target == base.RuleSubject.adverb_count:
target.append(sentence.adverb_count)
elif self.target == base.RuleSubject.modal_verb_count:
target.append(sentence.modal_verb_count)
elif self.target == base.RuleSubject.pronoun_count:
target.append(sentence.pronoun_count)
elif self.target == base.RuleSubject.sentence_tense:
target.append(sentence.tense.value)
else:
assert False,self.target
# print(sentence.val, target, value, self.target)
if self.op == base.SimpleOp.in_:
if not target:
return False
return all(x in value for x in target)
elif self.op == base.SimpleOp.notin_:
if not target:
return False
return all(x not in value for x in target)
elif self.op == base.SimpleOp.le:
if not target or not value:
return False
cck = []
for tg in target:
res = True
for val in value:
num = float(val)
if float(tg) > num:
res = False
cck.append(res)
return all(cck)
elif self.op == base.SimpleOp.lt:
if not target or not value:
return False
cck = []
for tg in target:
res = True
for val in value:
num = float(val)
if float(tg) >= num:
res = False
cck.append(res)
return all(cck)
elif self.op == base.SimpleOp.ge:
if not target or not value:
return False
cck = []
for tg in target:
res = True
for val in value:
num = float(val)
if float(tg) < num:
res = False
cck.append(res)
return all(cck)
elif self.op == base.SimpleOp.gt:
if not target or not value:
return False
cck = []
for tg in target:
res = True
for val in value:
num = float(val)
if float(tg) <= num:
res = False
cck.append(res)
return all(cck)
elif self.op == base.SimpleOp.eq:
if not target or not value:
return False
cck = []
for tg in target:
res = True
for val in value:
num = float(val)
if float(tg) != num:
res = False
cck.append(res)
return all(cck)
elif self.op == base.SimpleOp.neq:
if not target or not value:
return False
cck = []
for tg in target:
res = True
for val in value:
num = float(val)
if float(tg) == num:
res = False
cck.append(res)
return all(cck)
def dynamicFill(self, useCaseName: str):
# fill list with $actor
# checked right
value = []
if '$actor' in self.val:
value = [x for x in self.val if x != '$actor']
value += rucmElement.RUCMRoot.getActors(useCaseName)
else:
value = self.val
return value
class ComplexRule(Rule):
def __init__(self, ruleDict: dict):
super(ComplexRule, self).__init__()
self.applyScope: base.ApplyScope = None
self.op: typing.List[base.LogicOp] = []
self.simpleRule: typing.List[SimpleRule] = []
def check(self) -> None:
errors = []
if self.applyScope == base.ApplyScope.actionStep:
steps = rucmElement.RUCMRoot.getAllSteps()
for step in steps:
nature = None
for sentence in step.sentences:
# print('to be checked', sentence)
# print(sentence.nature)
if sentence.nature:
nature = sentence.nature
if nature and nature != base.NatureType.mean_while_:
continue
sentences = step.sentences
for sentence in sentences:
if sentence.nature:
continue
checkResult = []
for rule in self.simpleRule:
checkResult.append(rule.check(sentence))
result = checkResult[0]
for i in range(len(checkResult) - 1):
op = self.op[i + 1]
assert (op == base.LogicOp.and_ or op == base.LogicOp.or_)
if op == base.LogicOp.and_:
result = result and checkResult[i+1]
elif op == base.LogicOp.or_:
result = result or checkResult[i+1]
op = self.op[0]
assert (op == base.LogicOp.not_ or op == base.LogicOp.skip_)
if op == base.LogicOp.not_:
result = not result
if not result:
errors.append(ErrorInfo(self.description, \
sentence.useCaseName, sentence))
elif self.applyScope == base.ApplyScope.allSentence:
sentences = rucmElement.RUCMRoot.getAllSentences()
# print(len(sentences))
for sentence in sentences:
checkResult = []
if sentence.nature:
continue
for rule in self.simpleRule:
checkResult.append(rule.check(sentence))
result = checkResult[0]
# print(result)
for i in range(len(checkResult) - 1):
op = self.op[i + 1]
assert (op == base.LogicOp.and_ or op == base.LogicOp.or_)
if op == base.LogicOp.and_:
result = result and checkResult[i+1]
elif op == base.LogicOp.or_:
result = result or checkResult[i+1]
op = self.op[0]
assert (op == base.LogicOp.not_ or op == base.LogicOp.skip_)
if op == base.LogicOp.not_:
result = not result
if not result:
errors.append(ErrorInfo(self.description, \
sentence.useCaseName, sentence))
Reporter.errors += errors
class RuleDB():
defaultRules:typing.List[Rule]=[]
userRules:typing.List[Rule]=[]