-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.py
More file actions
55 lines (43 loc) · 2.06 KB
/
Copy pathParser.py
File metadata and controls
55 lines (43 loc) · 2.06 KB
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
from Literal import Literal
from Negation import Negation
from Rule import Rule
from Preference import Preference
class Parser:
def __init__(self, filename):
self.L = set()
self.R = set()
self.A = set()
self.C = set()
self.P = set()
self.parse_file(filename)
def parse_literrals(self, literal_str):
return {Literal(lit.strip()) for lit in literal_str.strip('[]').split(',')}
def parse_rules(self, rule_str):
rule_part = rule_str.split('<-')
conclusion = Literal(rule_part[0].strip())
premises = {Literal(premise.strip()) for premise in rule_part[1].split(',')} if rule_part[1].strip() else []
return premises, conclusion
def parse_file(self, filename):
with open(filename, 'r') as f:
lines = f.readlines()
for line in lines:
line.strip()
if line.startswith('L:'):
self.L = self.parse_literrals(line[2:].strip())
elif line.startswith('A:'):
self.A = self.parse_literrals(line[2:].strip())
elif line.startswith('C('):
parts = line[2:].split('):')
attacker = Literal(parts[1].strip())
attacked = Literal(parts[0].strip())
self.C.add(Negation(attacker, attacked))
elif line.startswith('['):
rule_id= Literal(line.split(']:')[0].strip('[]'))
premises, conclusion = self.parse_rules(line.split(']:')[1].strip())
weight = 1
self.R.add(Rule(premises, conclusion, weight, rule_id))
elif line.startswith('PREF:'):
parts = line[5:].split('>')
self.P.add(Preference(Literal(parts[0].strip()), Literal(parts[1].strip())))
else:
raise Exception(f"Invalid line: {line}")