Skip to content

Commit 256584f

Browse files
committed
V3.2
1 parent 62e1bc9 commit 256584f

File tree

8 files changed

+302
-173
lines changed

8 files changed

+302
-173
lines changed

ngcsimlib/__init__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from ngcsimlib._src.component import Component as Component
2-
from ngcsimlib._src.process import Process
3-
4-
import ngcsimlib.operations
5-
2+
from ngcsimlib._src.process.methodProcess import MethodProcess
3+
from ngcsimlib._src.process.jointProcess import JointProcess
4+
from ngcsimlib._src.deprecators import deprecated, deprecate_args

ngcsimlib/_src/compartment/compartment.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,18 @@ class Compartment(metaclass=CompartmentMeta):
2727
2828
fixed (default=False): sets the flag for if this compartment is fixed.
2929
"""
30-
def __init__(self, initial_value: T, fixed: bool = False):
30+
def __init__(self, initial_value: T, fixed: bool = False,
31+
display_name=None, units=None):
3132
self._initial_value = initial_value
3233

3334
self.name = None
3435
self._root_target = None
3536
self._target = self._root_target
3637
self._fixed = fixed
3738

39+
self.display_name = display_name
40+
self.units = units
41+
3842
@property
3943
def root(self):
4044
return self._root_target
@@ -67,7 +71,11 @@ def set(self, value: T) -> None:
6771
Args:
6872
value: The value to set in the global state.
6973
"""
70-
if self._fixed:
74+
if self.target is None:
75+
self._initial_value = value
76+
return
77+
78+
if self._fixed and gState.check_key(self.target):
7179
warn(f"Attempting to set {self._root_target} which is a fixed compartment. Aborting!")
7280
return
7381
if self.target != self._root_target:
@@ -87,6 +95,9 @@ def get_needed_keys(self):
8795
return set(self.target)
8896

8997
def _get_value(self):
98+
if self.target is None:
99+
return self._initial_value
100+
90101
if isinstance(self.target, BaseOp):
91102
return self.target.get()
92103

ngcsimlib/_src/parser/contextTransformer.py

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,49 @@ def visit_Expr(self, node):
113113

114114
return node
115115

116+
@staticmethod
117+
def _resolve_self_attr_chain_and_path(attr_node: ast.Attribute):
118+
"""
119+
Returns (base_is_self, attr_chain: List[str])
120+
e.g., self.foo.targeted -> (True, ['foo', 'targeted'])
121+
"""
122+
chain = []
123+
node = attr_node
124+
while isinstance(node, ast.Attribute):
125+
chain.insert(0, node.attr)
126+
node = node.value
127+
128+
is_self = isinstance(node, ast.Name) and node.id == "self"
129+
return is_self, chain if is_self else None
130+
116131
def visit_If(self, node):
132+
parent_map = {}
133+
for parent in ast.walk(node.test):
134+
for child in ast.iter_child_nodes(parent):
135+
parent_map[child] = parent
136+
117137
for n in ast.walk(node.test):
118138
if isinstance(n, ast.Attribute):
119-
if isinstance(n.value, ast.Name) and n.value.id == "self":
120-
attr = getattr(self.obj, n.attr, None)
121-
if isinstance(attr, Compartment):
122-
raise RuntimeError("Conditionals can not be dependant on model state")
139+
if isinstance(parent_map.get(n), ast.Attribute):
140+
continue
141+
142+
is_self, chain = self._resolve_self_attr_chain_and_path(n)
143+
if not is_self or not chain:
144+
continue
145+
146+
if chain[-1] == "targeted":
147+
continue
148+
149+
target = self.obj
150+
try:
151+
for attr in chain:
152+
target = getattr(target, attr)
153+
except AttributeError:
154+
continue
155+
156+
if isinstance(target, Compartment) and not target.fixed:
157+
raise RuntimeError(f"{self.obj.name}:{self.method.__name__}:[{target.root}], Conditionals can not be dependant on model state")
158+
123159

124160
condition_expr = ast.Expression(node.test)
125161
compiled = compile(ast.fix_missing_locations(condition_expr), "<ast>", "eval")

ngcsimlib/_src/parser/utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ def __call__(self, *args, **kwargs):
4444
return self._fn(*args, **kwargs)
4545

4646
def _bind(obj, method, ast_obj, namespace=None, auxiliary_ast=None):
47-
code = compile(ast_obj, filename=f"{method.__name__}_compiled", mode='exec')
47+
try:
48+
code = compile(ast_obj, filename=f"{method.__name__}_compiled", mode='exec')
49+
except Exception as e:
50+
print(obj)
51+
print(method)
52+
raise e
4853
namespace = method.__globals__.copy() if namespace is None else namespace
4954
exec(code, namespace)
5055

ngcsimlib/_src/process/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)