Skip to content

Commit 9bcd8bc

Browse files
committed
Refactor resolver
Changed the language to reference transitions not resolvers
1 parent 9d709c5 commit 9bcd8bc

File tree

9 files changed

+93
-92
lines changed

9 files changed

+93
-92
lines changed

ngcsimlib/compilers/command_compiler.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ def _compile(compile_key, components):
6161
| resolve the outputs of the compiled function
6262
6363
Args:
64-
compile_key: The key that is being compiled (mapped to each function
65-
that has the @resolver decorator above it)
64+
compile_key: The key for the transition that is being compiled
6665
6766
components: The list of components to compile for this function
6867
@@ -71,15 +70,15 @@ def _compile(compile_key, components):
7170
"""
7271
assert compile_key is not None
7372
## for each component, get compartments, get output compartments
74-
resolvers = {}
73+
transitions = {}
7574
for c_name, component in components.items():
76-
resolvers[c_name] = parse_component(component, compile_key)
75+
transitions[c_name] = parse_component(component, compile_key)
7776

7877
needed_args = []
7978
needed_comps = []
8079

8180
for c_name, component in components.items():
82-
_, outs, args, params, comps = resolvers[c_name]
81+
_, outs, args, params, comps = transitions[c_name]
8382
for a in args:
8483
if a not in needed_args:
8584
needed_args.append(a)
@@ -98,7 +97,7 @@ def _compile(compile_key, components):
9897

9998
exc_order = []
10099
for c_name, component in components.items():
101-
exc_order.extend(compile_component(component, resolvers[c_name]))
100+
exc_order.extend(compile_component(component, transitions[c_name]))
102101

103102
def compiled(compartment_values=None, **kwargs):
104103
if compartment_values is None:

ngcsimlib/compilers/component_compiler.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
1717
"""
1818
from ngcsimlib.compilers.op_compiler import compile as op_compile
19-
from ngcsimlib.utils import get_resolver
19+
from ngcsimlib.utils import get_transition
2020
from ngcsimlib.compartment import Compartment
2121
from ngcsimlib.logger import critical
2222

@@ -40,11 +40,11 @@ def parse(component, compile_key):
4040
if component.__class__.__dict__.get("auto_resolve", True):
4141
(pure_fn, output_compartments), (
4242
args, parameters, compartments, parse_varnames) = \
43-
get_resolver(component.__class__, compile_key)
43+
get_transition(component.__class__, compile_key)
4444
else:
4545
build_method = component.__class__.__dict__.get(f"build_{compile_key}", None)
4646
if build_method is None:
47-
critical(f"Component {component.name} if flagged to not use resolvers but "
47+
critical(f"Component {component.name} if flagged to not use a stored transition but "
4848
f"does not have a build_{compile_key} method")
4949
return build_method(component)
5050

@@ -82,20 +82,20 @@ def parse(component, compile_key):
8282
return (pure_fn, output_compartments, args, parameters, compartments)
8383

8484

85-
def compile(component, resolver):
85+
def compile(component, transition):
8686
"""
8787
compiles down the component to a single pure method
8888
8989
Args:
9090
component: the component to compile
9191
92-
resolver: the parsed output of the component
92+
transition: the parsed output of the component
9393
9494
Returns:
9595
the compiled method
9696
"""
9797
exc_order = []
98-
pure_fn, outs, _args, params, comps = resolver
98+
pure_fn, outs, _args, params, comps = transition
9999

100100
### Op resolve
101101
for connection in component.connections:

ngcsimlib/compilers/op_compiler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
The second one is the compile method which returns the execution order for
1111
the compile operation. It is important to know that all operation should have
1212
an `is_compilable` flag set to true if they are compilable. Some operations
13-
such as the `add` operation are not compilable as their resolve method
13+
such as the `add` operation are not compilable as their transition method
1414
contains execution logic that will not be captured by the compiled command.
1515
"""
1616
from ngcsimlib.operations.baseOp import BaseOp

ngcsimlib/operations/baseOp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class BaseOp(ABC):
1313
1414
For commands that can be compiled using ngcsimlib's compiler, all their
1515
operational logic must be contained inside the subclass's operation
16-
method. This also means that the resolve method that is defined on the
16+
method. This also means that the transition method that is defined on the
1717
base class should not be overwritten.
1818
1919
For commands that do not need to be compiled using ngcsimlib's compiler

ngcsimlib/resolver.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""
2+
Todo: rewrite to have this be the less favorable option
3+
24
The resolver is an important part of the compiling of components and commands
35
in the ngcsimlib compilers.
46
@@ -38,7 +40,7 @@
3840
"""
3941

4042
from ngcsimlib.compartment import Compartment
41-
from ngcsimlib.utils import add_component_resolver, add_resolver_meta
43+
from ngcsimlib.utils import add_component_transition, add_transition_meta
4244

4345

4446
def resolver(pure_fn,
@@ -99,10 +101,10 @@ def _resolver(fn):
99101
class_name = ".".join(fn.__qualname__.split('.')[:-1])
100102
resolver_key = fn.__qualname__.split('.')[-1]
101103

102-
add_component_resolver(class_name, resolver_key,
104+
add_component_transition(class_name, resolver_key,
103105
(pure_fn, output_compartments))
104106

105-
add_resolver_meta(class_name, resolver_key,
107+
add_transition_meta(class_name, resolver_key,
106108
(args, parameters, compartments, parse_varnames))
107109

108110
def _wrapped(self=None, *_args, **_kwargs):

ngcsimlib/transition.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from ngcsimlib.logger import warn
2-
from ngcsimlib.utils import add_component_resolver, add_resolver_meta
2+
from ngcsimlib.utils import add_component_transition, add_transition_meta
33

44
def add_transition(pure_fn, cls, output_compartments=None, transition_key=None):
55
if output_compartments is None:
@@ -8,9 +8,9 @@ def add_transition(pure_fn, cls, output_compartments=None, transition_key=None):
88
key = pure_fn.__qualname__.split('.')[-1] if transition_key is None else transition_key
99

1010
class_name = cls.__qualname__
11-
add_component_resolver(class_name, key,
11+
add_component_transition(class_name, key,
1212
(pure_fn, output_compartments))
13-
add_resolver_meta(class_name, key,
13+
add_transition_meta(class_name, key,
1414
(None, None, None, True))
1515

1616

ngcsimlib/utils/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
from .io import *
44
from .misc import *
55
from .modules import *
6-
from .resolvers import *
6+
from .transitions import *
77
from .help import *

ngcsimlib/utils/resolvers.py

Lines changed: 0 additions & 71 deletions
This file was deleted.

ngcsimlib/utils/transitions.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
from ngcsimlib.logger import critical, debug
2+
3+
__component_transitions = {}
4+
__transition_meta_data = {}
5+
6+
7+
def get_transition(klass, transition_key, root=None):
8+
"""
9+
A helper method for searching through the transition list
10+
"""
11+
class_name = klass.__name__
12+
13+
if class_name + "/" + transition_key not in __component_transitions.keys():
14+
parent_classes = klass.__bases__
15+
if len(parent_classes) == 0:
16+
return None, None
17+
18+
transition = None
19+
for parent in parent_classes:
20+
transition, meta = get_transition(parent, transition_key,
21+
root=klass if root is None else root)
22+
if transition is not None:
23+
return transition, meta
24+
25+
if transition is None and root is None:
26+
critical(class_name, "has no transition for", transition_key)
27+
if transition is None:
28+
return None, None
29+
30+
if root is not None:
31+
debug(
32+
f"{root.__name__} is using the transition from {class_name} for "
33+
f"resolving key \"{transition_key}\"")
34+
return __component_transitions[class_name + "/" + transition_key], \
35+
__transition_meta_data[class_name + "/" + transition_key]
36+
37+
38+
def add_component_transition(class_name, transition_key, data):
39+
"""
40+
A helper function for adding component transitions
41+
"""
42+
__component_transitions[class_name + "/" + transition_key] = data
43+
44+
45+
def add_transition_meta(class_name, transition_key, data):
46+
"""
47+
A helper function for adding component transition metadata
48+
"""
49+
__transition_meta_data[class_name + "/" + transition_key] = data
50+
51+
52+
def using_transition(**kwargs):
53+
"""
54+
A decorator for linking transitions defined in other classes to this class.
55+
the keyword arguments are compile_key=class_to_inherit_transition_from. This
56+
will add the transition directly to the class and thus will get used before
57+
any transitions in parent classes.
58+
59+
Args:
60+
**kwargs: any number or compile_key=class_to_inherit_transition_from
61+
"""
62+
63+
def _klass_wrapper(cls):
64+
klass_name = cls.__name__
65+
for key, value in kwargs.items():
66+
transition, data = get_transition(value, key)
67+
add_component_transition(klass_name, key, transition)
68+
add_transition_meta(klass_name, key, data)
69+
return cls
70+
71+
return _klass_wrapper

0 commit comments

Comments
 (0)