-
Notifications
You must be signed in to change notification settings - Fork 2
/
BAG.py
96 lines (72 loc) · 3.24 KB
/
BAG.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
import os
import re
import string
from .Argument import Argument
from .Support import Support
from .Attack import Attack
class BAG:
def __init__(self, path=None):
self.arguments = {}
self.attacks = []
self.supports = []
self.path = path
if (path is None):
pass
else:
with open(os.path.abspath(path), "r") as f:
for line in f.readlines():
k_name = line.split("(")[0]
if k_name in string.whitespace:
pass
else:
k_args = re.findall(rf"{k_name}\((.*?)\)", line)[0].replace(" ", "").split(",")
if k_name == "arg":
argument = Argument(k_args[0], float(k_args[1]), None, [], [])
self.arguments[argument.name] = argument
elif k_name == "att":
attacker = self.arguments[k_args[0]]
attacked = self.arguments[k_args[1]]
self.add_attack(attacker, attacked)
elif k_name == "sup":
supporter = self.arguments[k_args[0]]
supported = self.arguments[k_args[1]]
self.add_support(supporter, supported)
def add_attack(self, attacker, attacked):
if type(attacker) != Argument:
raise TypeError("attacker must be of type Argument")
if type(attacked) != Argument:
raise TypeError("attacked must be of type Argument")
if attacker.name in self.arguments:
attacker = self.arguments[attacker.name]
else:
self.arguments[attacker.name] = attacker
if attacked.name in self.arguments:
attacked = self.arguments[attacked.name]
else:
self.arguments[attacked.name] = attacked
attacked.add_attacker(attacker)
self.attacks.append(Attack(attacker, attacked))
def add_support(self, supporter, supported):
if type(supporter) != Argument:
raise TypeError("supporter must be of type Argument")
if type(supported) != Argument:
raise TypeError("supported must be of type Argument")
if supporter.name in self.arguments:
supporter = self.arguments[supporter.name]
else:
self.arguments[supporter.name] = supporter
if supported.name in self.arguments:
supported = self.arguments[supported.name]
else:
self.arguments[supported.name] = supported
supported.add_supporter(supporter)
self.supports.append(Support(supporter, supported))
def reset_strength_values(self):
for a in list(self.arguments.values()):
a.strength = a.initial_weight
def get_arguments(self):
return list(self.arguments.values())
def __str__(self) -> str:
return f"BAG set to read from {self.path} with arguments: {self.arguments}, attacks: {self.attacks} and supports: {self.supports}"
def __repr__(self) -> str:
return f"BAG({self.path}) Arguments: {self.arguments} Attacks: {self.attacks} Supports: {self.supports}"