|
| 1 | +from ngcsimlib.compilers.component_compiler import parse as parse_component, compile as compile_component |
| 2 | +from ngcsimlib.compilers.op_compiler import parse as parse_connection |
| 3 | +from ngcsimlib.utils import Get_Compartment_Batch, Set_Compartment_Batch |
| 4 | + |
| 5 | + |
| 6 | +def _compile(compile_key, components): |
| 7 | + assert compile_key is not None |
| 8 | + ## for each component, get compartments, get output compartments |
| 9 | + resolvers = {} |
| 10 | + for c_name, component in components.items(): |
| 11 | + resolvers[c_name] = parse_component(component, compile_key) |
| 12 | + |
| 13 | + needed_args = [] |
| 14 | + needed_comps = [] |
| 15 | + |
| 16 | + for c_name, component in components.items(): |
| 17 | + _, outs, args, params, comps = resolvers[c_name] |
| 18 | + for _, a in args: |
| 19 | + if a not in needed_args: |
| 20 | + needed_args.append(a) |
| 21 | + |
| 22 | + for connection in component.connections: |
| 23 | + inputs, outputs = parse_connection(connection) |
| 24 | + ncs = [str(i) for i in inputs] |
| 25 | + for nc in ncs: |
| 26 | + if nc not in needed_comps: |
| 27 | + needed_comps.append(nc) |
| 28 | + |
| 29 | + for _, comp in comps: |
| 30 | + path = str(component.__dict__[comp].path) |
| 31 | + if path not in needed_comps: |
| 32 | + needed_comps.append(path) |
| 33 | + arg_order = needed_args + needed_comps |
| 34 | + exc_order = [] |
| 35 | + for c_name, component in components.items(): |
| 36 | + exc_order.extend(compile_component(component, resolvers[c_name], arg_order)) |
| 37 | + |
| 38 | + def compiled(compartment_values, *cargs): |
| 39 | + for exc, outs, name in exc_order: |
| 40 | + _comps = [compartment_values[key] for key in needed_comps] |
| 41 | + vals = exc(*cargs, *_comps) |
| 42 | + if len(outs) == 1: |
| 43 | + compartment_values[outs[0]] = vals |
| 44 | + elif len(outs) > 1: |
| 45 | + for v, t in zip(vals, outs): |
| 46 | + compartment_values[t] = v |
| 47 | + return compartment_values |
| 48 | + |
| 49 | + return compiled, needed_args |
| 50 | + |
| 51 | + |
| 52 | +def compile(command): |
| 53 | + return _compile(command.compile_key, command.components) |
| 54 | + |
| 55 | + |
| 56 | +def dynamic_compile(*components, compile_key=None): |
| 57 | + assert compile_key is not None |
| 58 | + return _compile(compile_key, {c.name: c for c in components}) |
| 59 | + |
| 60 | + |
| 61 | +def wrap_command(command): |
| 62 | + def _wrapped(*args): |
| 63 | + vals = command(Get_Compartment_Batch(), *args) |
| 64 | + Set_Compartment_Batch(vals) |
| 65 | + return vals |
| 66 | + |
| 67 | + return _wrapped |
0 commit comments