-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElement.py
More file actions
357 lines (277 loc) · 8.94 KB
/
Element.py
File metadata and controls
357 lines (277 loc) · 8.94 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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
""" Policies:
element.ID is unique across all data structures
element.typ refers to the typ of an elementgraph with that element as the root
element.name sometimes refers to the predicate name or operator name. otherwise None
num_args is 0 means that num_args is in essence None. If predicate with no args, then this may cause problems
Properties:
Another element is consistent with self iff for each non-None parameter in self, the other's parameter is None or ==
Another element is equivalent with self iff for each non-None parameter in self, other's parameter == and cannot be None
Another element is identical just when their ids match and they're equivalent
Operations:
Merge operation takes all non-None parameters of other, and assumes co-consistency
"""
import copy
from uuid import uuid4
from GlobalContainer import GC
class Element:
"""Element is a token or label with the following attributes"""
def __init__(self, ID=None, typ=None, name=None, arg_name=None):
if ID is None:
ID = uuid4()
self.ID = ID
self.typ = typ
self.name = name
self.arg_name = arg_name
self.replaced_ID = -1
def isConsistent(self, other):
""" Returns True if self and other have same name or unassigned"""
if not self.isConsistentType(other):
return False
if not self.isConsistentName(other):
return False
return True
def isConsistentType(self, other):
if self.typ is not None and other.typ is not None:
if self.typ != other.typ:
return False
return True
def isConsistentName(self, other):
if self.name is not None and other.name is not None:
if self.name != other.name:
return False
return True
def isEquivalent(self, other):
if self.typ is not None:
if self.typ != other.typ:
return False
else:
if other.typ is not None:
return False
return True
def __eq__(self, other):
if other is None:
return False
return self.ID == other.ID
def __ne__(self, other):
return not self.__eq__(other)
def __hash__(self):
return hash(self.ID)
def merge(self, other):
"""merge returns self with non-None properties of other,
and assumes elements are co-consistent"""
if not self.isConsistent(other):
return None
if self.isEquivalent(other):
return self
if self.typ is None and other.typ is not None:
self.typ = other.typ
if self.name is None and other.name is not None:
self.name = other.name
return self
def __repr__(self):
id = str(self.ID)[19:23]
return '({}-{}-{})'.format(id, self.typ, self.name)
class InternalElement(Element):
"""Internal Element is an Element with a possibly unimportant name, and a number of arguments
"""
def __init__(self, ID=None, typ=None, name=None, arg_name=None, num_args=None):
super(InternalElement, self).__init__(ID, typ, name, arg_name)
if num_args == None:
num_args = 0
self.num_args = num_args
def isEquivalent(self, other):
if not super(InternalElement, self).isEquivalent(other):
return False
if self.name is not None:
if self.name != other.name:
return False
else:
if other.name is not None:
return False
if self.num_args != other.num_args:
return False
return True
def isConsistent(self, other):
# If other has a typ, then return False if they are not the same
if not super(InternalElement, self).isConsistent(other):
return False
if self.num_args > 0 and other.num_args > 0:
if self.num_args != other.num_args:
return False
# If other has a predicate name, then return False if they are not the same
if self.name is not None and other.name is not None:
if self.name != other.name:
return False
return True
def merge(self, other):
if super(InternalElement, self).merge(other) is None:
return None
if other.num_args > 0 and self.num_args == 0:
self.num_args = other.num_args
return self
class Operator(InternalElement):
stepnumber = 0
""" An operator element is an internal element with an executed status and orphan status"""
def __init__(self, ID=None, typ=None, name=None, stepnumber=None, num_args=None, executed=None, arg_name=None):
if typ is None:
typ = 'Action'
if num_args is None:
num_args = 0
if stepnumber is None:
stepnumber = Operator.stepnumber
Operator.stepnumber += 1
else:
Operator.stepnumber = stepnumber + 1
super(Operator, self).__init__(ID, typ, name, arg_name, num_args=num_args)
self.stepnumber = stepnumber
self.executed = executed
def __hash__(self):
return hash(self.ID)
def __eq__(self, other):
if other is None:
# this ain't good
raise ValueError('self {} == other {}, other is None'.format(self, other))
# return False
return self.ID == other.ID
# if self.name == other.name:
# if self.stepnumber == other.stepnumber:
# return True
# return False
def isConsistent(self, other):
if not super(Operator, self).isConsistent(other):
return False
if other.executed is not None and self.executed is not None:
if self.executed != other.executed:
return False
return True
def merge(self, other):
if super(Operator, self).merge(other) is None:
return None
if not other.executed is None and self.executed is None:
self.executed = other.executed
return self
def __repr__(self):
if self.executed is None:
exe = ''
else:
exe = '-' + self.executed
uid = str(self.ID)[19:23]
return 'operator{}-{}-{}-{}'.format(exe, self.name, self.stepnumber, uid)
class Literal(InternalElement):
""" A Literal element is an internal element with a truth status
"""
def __init__(self, ID=None, typ=None, name=None, arg_name=None, num_args=None, truth=None):
if num_args is None:
num_args = 0
if typ is None:
typ = 'Condition'
super(Literal, self).__init__(ID, typ, name, arg_name, num_args)
self.truth = truth
def __hash__(self):
return hash(self.name) ^ hash(self.truth)
def isConsistent(self, other):
if not super(Literal, self).isConsistent(other):
return False
if self.truth is not None and other.truth is not None:
if self.truth != other.truth:
return False
return True
def isOpposite(self, other):
opp = copy.deepcopy(self)
if self.truth is True:
opp.truth = False
else:
opp.truth = True
return opp.isConsistent(other)
def isEquivalent(self, other):
if not super(Literal, self).isEquivalent(other):
return False
if self.truth is not None:
if self.truth != other.truth:
return False
else:
if other.truth is not None:
return False
return True
def merge(self, other):
if super(Literal, self).merge(other) is None:
return None
if self.truth is None and other.truth is not None:
self.truth = other.truth
return self
def __repr__(self):
shrt_id = str(self.ID)[19:23]
return 'literal-{}-{}-{}'.format(shrt_id, self.truth, self.name)
class Argument(Element):
def __init__(self, ID=None, typ=None, name=None, arg_name=None):
if typ is None:
typ = 'Arg'
super(Argument, self).__init__(ID, typ, name, arg_name)
def isEquivalent(self, other):
""" 'equivalent' arguments are consistent and have been assigned the same name """
if not super(Argument, self).isEquivalent(other):
if self.typ not in GC.object_types[other.typ] and other.typ not in GC.object_types[self.typ]:
return False
if self.name is not None:
if other.name != self.name:
return False
else:
if other.name is not None:
return False
return True
def isConsistentType(self, other):
if not self.typ == other.typ:
try:
if self.typ not in GC.object_types[other.typ.lower()] and \
other.typ not in GC.object_types[self.typ.lower()]:
return False
except:
raise TypeError('what self {} / other {} typs are these?'.format(self.typ, other.typ))
return True
def isConsistent(self, other):
if not super(Argument, self).isConsistent(other):
return False
return True
def merge(self, other):
if super(Argument, self).merge(other) is None:
return None
if self.typ in GC.object_types[other.typ.lower()]:
self.typ = other.typ
return self
def __repr__(self):
shrt_id = str(self.ID)[19:23]
if self.arg_name is None:
arg_name = ''
else:
arg_name = '-' + self.arg_name
if self.name is None:
name = ''
else:
name = '-' + self.name
return 'arg-{}-{}{}{}'.format(shrt_id, self.typ, name, arg_name)
class Actor(Argument):
""" An actor is an argument """
def __init__(self, ID=None, typ=None, name=None, arg_name=None):
if typ is None:
typ = 'character'
super(Actor, self).__init__(ID, typ, name, arg_name)
def merge(self, other):
if super(Actor, self).merge(other) is None:
return None
return self
def __repr__(self):
shrt_id = str(self.ID)[19:23]
if self.arg_name is None:
arg_name = ''
else:
arg_name = '-' + self.arg_name
if self.name is None:
name = ''
else:
name = '-' + self.name
return 'actor-{}-{}{}{}'.format(shrt_id, self.typ, name, arg_name)
class PlanElement(Element):
def __init__(self, ID=None, typ=None, name=None, arg_name=None):
if typ is None:
typ = 'PlanElementGraph'
super(PlanElement, self).__init__(ID, typ, name, arg_name)