-
Notifications
You must be signed in to change notification settings - Fork 3
/
executable.py
248 lines (211 loc) · 7.96 KB
/
executable.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
from .parse import process_expression_string, serialize
from .dependency import DependencyCollector
from ..utils.collections import subdict
from ..schema.actions import RollbackAction, TerminateAction, PrimaryAction, CompositeAction, SimulatorAction, action_builtins, CollectReferences
from .builtins import ordered_builtins, global_builtins
from .exprgraph import dfs_make
from collections import ChainMap, defaultdict
from sortedcontainers import SortedSet
from frozendict import frozendict
from collections.abc import Sequence
import inspect
def register_builtin(name,fn,ordered_arguments=True):
global ordered_builtins
global global_builtins
global_builtins[name] = fn
if ordered_arguments:
ordered_builtins.append(name)
return
class ExecutableExpression:
# is a fancy lambda function that can be executed
start = None
builtins = global_builtins
def __init__(self,keywords,builtins,fn,code,deps):
self.keywords = keywords
self.builtins = builtins
self.fn = fn
self.code = code
self.deps = deps
def to_string(self):
return pformat(dict(
keywords = self.keywords,
builtins = self.builtins,
code = self.code,
fn = self.fn
))
@classmethod
def initialize(cls,s):
try:
tree, depdict = process_expression_string(s,start=cls.start)
deps, code = DependencyCollector(depdict),serialize(tree)
#if deps.has_subvariables:
# err = 'Code `{s}` is nesting too many variables'
# assert has_subvariables, err.format(s=s)
keywords = list(deps.variables)
# this step figures what builtins to use, picks them from the global_builtins list
builtins = subdict(cls.builtins, ['__builtins__'] + list(deps.builtins))
code2 = 'lambda {vars}: {code}'.format(vars=','.join(keywords),code=code)
try:
fn = eval(code2,builtins)
x = cls(keywords=keywords,builtins=builtins,fn=fn,code=code,deps=deps)
except:
x = None
except:
x = None
# Note checking of valid object is OUTSIDE the control of this factory method
return x
@classmethod
def initialize_from_strings(cls,strings,classes,cmax=0):
d = dict()
allowed_forms = '\n'.join([x for c in classes for x in c.allowed_forms])
for s in strings:
for c in classes:
x = c.initialize(s)
if x is not None:
break
if x is None:
err = "`{s}` does not create a valid instance of {c}. It must have one of the forms:\n {f}"
raise ValueError(err.format(s=s,c=classes,f=allowed_forms))
if x.deps.declared_variable is not None:
d[x.deps.declared_variable] = x
else:
d['_{0}'.format(cmax)] = x
cmax += 1
return d
def exec(self,*dicts):
d = ChainMap(*dicts)
kwargs = {x:d[x] for x in self.keywords}
v = self.fn(**kwargs)
if self.__class__.allowed_returns is not None:
types = self.__class__.allowed_returns
err = 'Value {v} returned by {cls} `{code}` is not one of {types}.'
assert isinstance(v,types), err.format(v=v,code=self.code,cls=self.__class__.__name__,types=types)
return v
def build_exprgraph(self):
tree, deps = process_expression_string(self.code,start=self.__class__.start)
return dfs_make(tree)
def full_string(self):
return self.code
class Constraint(ExecutableExpression):
start = 'boolean_expression'
builtins = global_builtins
allowed_forms = ['<expr> <bool_op> <expr>']
allowed_returns = (bool,)
class Computation(ExecutableExpression):
start = 'assignment'
builtins = global_builtins
allowed_forms = ['<var> = <expr>']
allowed_returns = None
def build_exprgraph(self):
assert self.deps.declared_variable is not None
code = f'{self.deps.declared_variable} = {self.code}'
tree, deps = process_expression_string(code,start=self.__class__.start)
return dfs_make(tree)
def full_string(self):
return f'{self.deps.declared_variable} = {self.code}'
class RateLaw(ExecutableExpression):
start = 'expression'
builtins = global_builtins
allowed_forms = ['<expr>']
allowed_returns = (int,float,)
class ObservableExpression(ExecutableExpression):
start = 'expression'
builtins = global_builtins
allowed_forms = ['<expr>']
allowed_returns = None
######## Simulator methods #########
def rollback(expr):
assert isinstance(expr,bool), "Rollback condition must evaluate to a boolean."
return {True:RollbackAction(),False:[]}[expr]
setattr(rollback,'_is_action',True)
def terminate(expr):
assert isinstance(expr,bool), "Terminate condition must evaluate to a boolean."
return {True:TerminateAction(),False:[]}[expr]
setattr(terminate,'_is_action',True)
########### ActionCaller ##########
# an executable expression object that when called on a match
# is equivalent to an action method call
class ActionCaller(ExecutableExpression):
start = 'function_call'
builtins = ChainMap(global_builtins,dict(rollback=rollback,terminate=terminate),action_builtins)
allowed_forms = ['<actioncall> ( <boolexpr> )', '<pattern>.<var>.<actioncall> (<params>)', '<pattern>.<actioncall> (<params>)']
allowed_returns = None
def exec(self,match,*dicts):
v = super().exec(match,*dicts)
#err = 'An element in the following nested list is not a recognized Action: {0}'
#assert verify_list(v,(SimulatorAction,PrimaryAction,CompositeAction)), err.format(list(v))
# verifying that every element of a nested list is an action is slow AF
# just don't do any verification here
# todo: verification can be done by the simulator when it processes the output of an action caller.
return v
def initialize_from_string(string,classes):
for c in classes:
x = c.initialize(string)
if x is not None:
return x
err = 'Could not create a valid instance of {0} from {1}'
assert False, err.format(classes,string)
class ExecutableExpressionManager:
# assume top-level variable are variables of a pattern
# e.g., a.x => a is a node on a pattern, x is an attribute
# similarly, a.x(), x is a computation
def __init__(self,constraint_execs,namespace):
self.execs = constraint_execs
self.namespace = namespace
def pprint(self):
return '\n'.join([x.code for x in self.execs])
def get_attribute_calls(self):
attrcalls = defaultdict(SortedSet)
for c in self.execs:
for k,v in c.deps.attribute_calls.items():
attrcalls[k].update(v)
for fnametuple in c.deps.function_calls:
if len(fnametuple)==2:
var,fname = fnametuple
if isinstance(self.namespace[var],type):
_class = self.namespace[var]
fn = getattr(_class,fname)
assert fn._is_computation, f'Could not find function {fnametuple}'
kws = sorted(set(fn._kws) & set(_class.get_literal_attrs()))
attrcalls[var].update(kws)
for k,v in attrcalls.items():
for a in v:
yield k,a
def get_helper_calls(self):
helpercalls = defaultdict(SortedSet)
for c in self.execs:
for fnametuple,kwargs in c.deps.function_calls.items():
if len(fnametuple)==2:
var,fname = fnametuple
if self.namespace[var]=='Helper Pattern':
helpercalls[var].add(tuple(kwargs['kwpairs']))
for k,v in helpercalls.items():
for tup in v:
yield k,tup
def exec(self,match,*dicts):
for c in self.execs:
if c.deps.declared_variable is not None:
match[c.deps.declared_variable] = c.exec(match,*dicts)
elif not c.exec(match,*dicts):
return None
return match
class ActionManager:
def __init__(self,action_execs,factories):
self.execs = action_execs
for e in self.execs:
for fnametuple in e.deps.function_calls:
if fnametuple == ('add',):
assert len(e.deps.variables)==1
var = list(e.deps.variables)[0]
assert var in factories
setattr(e,'build_variable',var)
def exec(self,match,*dicts):
for c in self.execs:
actions = c.exec(match,*dicts)
if hasattr(c,'build_variable'):
assert isinstance(actions[-1],CollectReferences)
actions[-1].variable = c.build_variable
if isinstance(actions,Sequence):
yield from actions
else:
yield actions