-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
58 lines (50 loc) · 1.67 KB
/
parser.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
"""
Parser get rules method reads the rules files and returns the rules.
"""
# two basic operators in our rules
AND_OPERATOR = 'AND'
OR_OPERATOR = 'OR'
"""
this method trims the IS sentence.
"""
def atomic_trim(input):
return input.replace("(", "").replace(")", "").split(" IS ")
"""
getting our rules by reading the rules.fcl and
parsing our rules into dictionary.
"""
def getRules():
with open('rules.fcl', 'r') as file:
# create a dict
res = []
lines = file.readlines()
for line in lines:
# check for empty line
if line == "\n":
continue
# create a dict
line_info = {}
# remove the before IF
removeIF = line.strip().replace(";", "").split("IF")[1].strip()
# split into two parts by then
parts = removeIF.split(" THEN ")
# the rules check
rules = []
if AND_OPERATOR in parts[0]:
line_info['type'] = 'MIN'
two = parts[0].split(" " + AND_OPERATOR + " ")
rules.append(atomic_trim(two[0]))
rules.append(atomic_trim(two[1]))
elif OR_OPERATOR in parts[0]:
line_info['type'] = 'MAX'
two = parts[0].split(" " + OR_OPERATOR + " ")
rules.append(atomic_trim(two[0]))
rules.append(atomic_trim(two[1]))
else:
line_info['type'] = 'NONE'
rules.append(atomic_trim(parts[0]))
# result
line_info['rules'] = rules
line_info['output'] = parts[1].split(" IS ")[1]
res.append(line_info)
return res