-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch_utils.py
137 lines (104 loc) · 3.67 KB
/
match_utils.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
class Edge(object):
def __init__(self, u, v):
self.u = int(u) if u is not None else u
self.v = int(v) if v is not None else v
def __eq__(self, other):
if isinstance(other, Edge):
return (self.u == other.u and self.v == other.v) \
or (self.u == other.v and self.v == other.u)
def __hash__(self):
return hash((self.u, self.v))
def __str__(self):
if self.v is None:
v = '?'
else:
v = self.v
return '{},{}'.format(self.u, v)
class Action(object):
def __init__(self, name, content):
self.name = name
self.content = content
def __eq__(self, other):
if isinstance(other, Action):
return self.name == other.name \
and self.content == other.content
def partial_equals(self, other):
assert isinstance(self.content, Edge) and isinstance(
other.content, Edge), 'not implemented'
if self.name != other.name:
return False
u, v = self.content.u, self.content.v
ou, ov = other.content.u, other.content.v
if v is None or ov is None:
return u == ou or u == ov or v == ou or v == ov
else:
return (u == ou or u == ov) and (v == ou or v == ov)
def __str__(self):
return self.__repr__()
def __hash__(self):
return hash((self.name, self.content))
def __repr__(self):
return '{}({})'.format(self.name, self.content)
class AddAction(Action):
def __init__(self, edge):
self.name = 'ADD'
self.edge = edge
super().__init__(self.name, self.edge)
class RemoveAction(Action):
def __init__(self, edge):
self.name = 'REMOVE'
self.edge = edge
super().__init__(self.name, self.edge)
class Act(object):
def __init__(self, name, content, agent='X'):
self.name = name
self.content = content
self.agent = agent
def __str__(self):
return self.__repr__()
def __repr__(self):
return '{}_{}({})'.format(self.name, self.agent, self.content)
def __hash__(self):
return hash((self.name, self.content, self.agent))
def __eq__(self, other):
if isinstance(other, Act):
return self.name == other.name \
and self.content == other.content \
and self.agent == other.agent
class Instruction(Act):
def __init__(self, action, agent='X'):
self.name = 'INSTRUCT'
self.action = action
self.agent = agent
super().__init__('{}'.format(self.name), action, agent)
class Do(Act):
def __init__(self, action, agent='X'):
self.name = 'DO'
self.action = action
self.agent = agent
super().__init__('{}'.format(self.name), action, agent)
class Match(Act):
def __init__(self, instruct_act, agent='X'):
self.name = 'MATCH'
self.instruct_act = instruct_act
super().__init__('{}'.format(self.name), instruct_act, agent)
class Mismatch(Act):
def __init__(self, instruct_act, agent='X'):
self.name = 'MISMATCH'
self.instruct_act = instruct_act
super().__init__('{}'.format(self.name), instruct_act, agent)
class Nonmatch(Act):
def __init__(self, action, agent='X'):
self.name = 'NONMATCH'
self.action = action
self.agent = agent
super().__init__('{}'.format(self.name), action, agent)
def make_edit_action(action_name, action_edge):
action = None
u, v = action_edge
edge = Edge(u, v)
if action_name == 'ADD':
action = AddAction(edge)
elif action_name == 'REMOVE':
action = RemoveAction(edge)
return action