-
Notifications
You must be signed in to change notification settings - Fork 3
/
actions.py
389 lines (303 loc) · 10.8 KB
/
actions.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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
from dataclasses import dataclass
from collections import deque, ChainMap
from types import MethodType
from abc import ABC, abstractmethod
from obj_tables import core
from .attributes import action
### Action prototypes
class SimulatorAction(ABC):
pass
class PrimaryAction(ABC):
# Primary actions record idx of objects as strings
# This makes the primary action as a record that can be used to
# rollback or output, among other things
@classmethod
@abstractmethod
def make(self,*args,**kwargs):
pass
@abstractmethod
def execute(self,sim):
pass
@abstractmethod
def rollback(self,sim):
pass
@abstractmethod
def remap(self,d):
pass
class CompositeAction(ABC):
@abstractmethod
def expand(self):
pass
######## Simulator actions
RollbackAction = type('RollbackAction',(SimulatorAction,),{})
TerminateAction = type('TerminateAction',(SimulatorAction,),{})
############## Primary Actions
# NodeAction -> AddNode, RemoveNode
# SetAttr
# EdgeAction -> AddEdge, RemoveEdge
# make(...) -> primary constructor
# execute(sim) -> executes the action on a simulation state
# rollback(sim) -> rolls back the action on a simulation state
@dataclass
class NodeAction(PrimaryAction):
_class: type
idx: str
attrs: dict
def add_node(self,sim):
x = self._class(id=self.idx)
for attr, value in self.attrs.items():
x.safely_set_attr(attr,value)
sim.add(x)
return self
def remove_node(self,sim, register=True):
x = sim.pop(self.idx)
return self
def remap(self,d):
return self.__class__(
_class = self._class,
idx = d[self.idx],
attrs = self.attrs
)
class AddNode(NodeAction):
@classmethod
def make(cls,_class,idx,attrs={}):
return cls(_class=_class,idx=idx,attrs=attrs)
def execute(self,sim):
return self.add_node(sim)
def rollback(self,sim):
return self.remove_node(sim)
class RemoveNode(NodeAction):
@classmethod
def make(cls,node):
return cls(_class=node.__class__,idx=node.id,attrs=node.get_literal_attrdict())
def execute(self,sim):
return self.remove_node(sim,register=True)
def rollback(self,sim):
return self.add_node(sim)
@dataclass
class SetAttr(PrimaryAction):
idx: str
attr: str
value: 'typing.Any'
old_value: 'typing.Any' = None
@classmethod
def make(cls,node,attr,value):
return cls(idx=node.id,attr=attr,value=value,old_value=node.get(attr))
def execute(self,sim):
node = sim[self.idx]
node.safely_set_attr(self.attr,self.value)
return self
def rollback(self,sim):
node = sim[self.idx]
node.safely_set_attr(self.attr,self.old_value)
return self
def remap(self,d):
return self.__class__(
idx = d[self.idx],
attr = self.attr,
value = self.value,
old_value = self.old_value
)
@dataclass
class EdgeAction(PrimaryAction):
source_idx: str
source_attr: str
target_idx: str
target_attr: str
@classmethod
def make(cls,source,attr,target):
return cls(
source_idx = source.id,
source_attr = attr,
target_idx = target.id,
target_attr = source.get_related_name(attr)
)
def add_edge(self,sim,register=True):
source,target = sim[self.source_idx], sim[self.target_idx]
source.safely_add_edge(self.source_attr,target)
# _class1, idx1, attr1 = source.__class__, source.id, self.source_attr
# _class2, idx2, attr2 = target.__class__, target.id, self.target_attr
# d1 = dict(_class=_class1,idx1=idx1,attr1=attr1,idx2=idx2,attr2=attr2,action='AddEdge')
# d2 = dict(_class=_class2,idx1=idx2,attr1=attr2,idx2=idx1,attr2=attr1,action='AddEdge')
return self
def remove_edge(self,sim):
source,target = sim[self.source_idx], sim[self.target_idx]
source.safely_remove_edge(self.source_attr,target)
# _class1, idx1, attr1 = source.__class__, source.id, self.source_attr
# _class2, idx2, attr2 = target.__class__, target.id, self.target_attr
# d1 = dict(_class=_class1,idx1=idx1,attr1=attr1,idx2=idx2,attr2=attr2,action='RemoveEdge')
# d2 = dict(_class=_class2,idx1=idx2,attr1=attr2,idx2=idx1,attr2=attr1,action='RemoveEdge')
return self
def remap(self,d):
return self.__class__(
source_idx = d[self.source_idx],
source_attr = self.source_attr,
target_idx = d[self.target_idx],
target_attr = self.target_attr
)
class AddEdge(EdgeAction):
def execute(self,sim):
return self.add_edge(sim)
def rollback(self,sim):
return self.remove_edge(sim)
class RemoveEdge(EdgeAction):
def execute(self,sim):
return self.remove_edge(sim)
def rollback(self,sim):
return self.add_edge(sim)
############## Literal Attr Actions ###########
@dataclass
class RemoveLiteralAttr(CompositeAction):
source: 'typing.Any'
attr: str
def expand(self):
return SetAttr.make(self.source,self.attr,None)
@dataclass
class Flip(CompositeAction):
source: 'typing.Any'
attr: str
def expand(self):
return SetAttr.make(self.source,self.attr,not self.source.get(self.attr))
@dataclass
class SetTrue(CompositeAction):
source: 'typing.Any'
attr: str
def expand(self):
return SetAttr.make(self.source,self.attr,True)
@dataclass
class SetFalse(CompositeAction):
source: 'typing.Any'
attr: str
def expand(self):
return SetAttr.make(self.source,self.attr,False)
@dataclass
class Increment(CompositeAction):
source: 'typing.Any'
attr: str
value: 'typing.Any' = 1
def expand(self):
return SetAttr.make(self.source,self.attr,self.source.get(self.attr) + self.value)
@dataclass
class Decrement(CompositeAction):
source: 'typing.Any'
attr: str
value: 'typing.Any' = 1
def expand(self):
return SetAttr.make(self.source,self.attr,self.source.get(self.attr) - self.value)
######## Edge Attr ##########
@dataclass
class RemoveEdgeAttr(CompositeAction):
source: 'typing.Any'
attr: str
def expand(self):
return [RemoveEdge.make(self.source,self.attr,x) for x in self.source.listget(self.attr)]
@dataclass
class RemoveAllEdges(CompositeAction):
source: 'typing.Any'
def expand(self):
attrs = self.source.get_related_attributes(ignore_None=True)
return [RemoveEdgeAttr(self.source,attr) for attr in attrs]
######### Node Remove ###########
@dataclass
class Remove(CompositeAction):
source: 'typing.Any'
def expand(self):
return [RemoveAllEdges(self.source), RemoveNode.make(self.source)]
############### codegeneration for methods
class ActionMixin:
def attach_actions(self):
self.attach_removenode_method()
for attr,value in self.__class__.Meta.local_attributes.items():
if attr == 'id':
continue
methods = {
core.LiteralAttribute: self.attach_literalsetattr_method,
core.NumericAttribute: self.attach_numericattr_methods,
core.BooleanAttribute: self.attach_booleanattr_methods,
core.RelatedAttribute: self.attach_edgeattr_methods,
}
for attrtype,method in methods.items():
if isinstance(value.__dict__['attr'],attrtype):
method(attr)
def attach_method(self,name,fn):
fn.__name__ = name
fn._is_action = True
setattr(self,name,MethodType(fn, self))
return self
def attach_literalsetattr_method(self,attr):
def setterfn(self,value):
return SetAttr.make(self,attr,value)
def removefn(self):
return SetAttr.make(self,attr,None)
self.attach_method('set_{0}'.format(attr),setterfn)
self.attach_method('remove_{0}'.format(attr),removefn)
return self
def attach_numericattr_methods(self,attr):
def incrementfn(self,value=None):
if value is None:
return Increment(self,attr)
return Increment(self,attr,value)
def decrementfn(self,value=None):
if value is None:
return Decrement(self,attr)
return Decrement(self,attr,value)
self.attach_method('increment_{0}'.format(attr),incrementfn)
self.attach_method('decrement_{0}'.format(attr),decrementfn)
return self
def attach_booleanattr_methods(self,attr):
def settruefn(self):
return SetTrue(self,attr)
def setfalsefn(self):
return SetFalse(self,attr)
def flipfn(self):
return Flip(self,attr)
self.attach_method('setTrue_{0}'.format(attr),settruefn)
self.attach_method('setFalse_{0}'.format(attr),setfalsefn)
self.attach_method('flip_{0}'.format(attr),flipfn)
return self
def attach_edgeattr_methods(self,attr):
def add_edge_fn(self,*args):
targets = deque()
for arg in args:
if isinstance(arg,list):
for elem in arg:
targets.append(elem)
else:
targets.append(arg)
return [AddEdge.make(self,attr,x) for x in targets]
def remove_edge_fn(self,*args):
targets = deque()
if len(args)==0:
return RemoveEdgeAttr(self,attr)
for arg in args:
if isinstance(arg,list):
for elem in arg:
targets.append(elem)
else:
targets.append(arg)
return [RemoveEdge.make(self,attr,x) for x in targets]
self.attach_method('add_{0}'.format(attr),add_edge_fn)
self.attach_method('remove_{0}'.format(attr),remove_edge_fn)
return self
def attach_removenode_method(self):
def remove_edges_fn(self):
return RemoveAllEdges(self)
def removefn(self):
return Remove(self)
self.attach_method('remove_all_edges',remove_edges_fn)
self.attach_method('remove',removefn)
return self
@dataclass
class CollectReferences:
data: dict
variable: str = ''
def execute(self,match,cache):
match[self.variable] = {k:cache[v] for k,v in self.data.items()}
return self
# Builtins for actions
def add(graphfactory):
idmap = graphfactory.build_random_idmap()
return list(graphfactory.generate_actions(idmap)) + [CollectReferences(data=idmap)]
def remove(match):
return [Remove(source=elem) for elem in match.values()]
action_builtins = {'add':add,'remove':remove}