Skip to content

Commit 272ba63

Browse files
authored
Merge pull request #3464 from Robbybp/cuid-from-cuid
Allow construction of CUID from another CUID
2 parents 95fa211 + ddbe61f commit 272ba63

File tree

4 files changed

+34
-19
lines changed

4 files changed

+34
-19
lines changed

pyomo/contrib/mpc/data/get_cuid.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,8 @@ def get_indexed_cuid(var, sets=None, dereference=None, context=None):
3737
ComponentUID corresponding to the provided ``var`` and sets
3838
3939
"""
40-
# TODO: Does this function have a good name?
4140
# Should this function be generalized beyond a single indexing set?
42-
if isinstance(var, ComponentUID):
43-
return var
44-
elif isinstance(var, (str, IndexedComponent_slice)):
41+
if isinstance(var, (str, IndexedComponent_slice, ComponentUID)):
4542
# TODO: Raise error if string and context is None
4643
return ComponentUID(var, context=context)
4744
# At this point we are assuming var is a Pyomo Var or VarData object.

pyomo/core/base/block.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -921,11 +921,7 @@ def find_component(self, label_or_component):
921921
a matching component is not found, None is returned.
922922
923923
"""
924-
if type(label_or_component) is ComponentUID:
925-
cuid = label_or_component
926-
else:
927-
cuid = ComponentUID(label_or_component)
928-
return cuid.find_component_on(self)
924+
return ComponentUID(label_or_component).find_component_on(self)
929925

930926
@contextmanager
931927
def _declare_reserved_components(self):

pyomo/core/base/componentuid.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ def _index_repr(x):
4343
return __index_repr(x, _pickle)
4444

4545

46+
def _context_err(_type):
47+
raise ValueError(
48+
f"Context is not allowed when initializing a ComponentUID from {_type}."
49+
)
50+
51+
4652
class ComponentUID(object):
4753
"""
4854
A Component unique identifier
@@ -78,15 +84,15 @@ def __init__(self, component, cuid_buffer=None, context=None):
7884
# the string representation.
7985
if isinstance(component, str):
8086
if context is not None:
81-
raise ValueError(
82-
"Context is not allowed when initializing a "
83-
"ComponentUID object from a string type"
84-
)
87+
_context_err(str)
8588
try:
8689
self._cids = tuple(self._parse_cuid_v2(component))
8790
except (OSError, IOError):
8891
self._cids = tuple(self._parse_cuid_v1(component))
89-
92+
elif type(component) is ComponentUID:
93+
if context is not None:
94+
_context_err(ComponentUID)
95+
self._cids = component._cids
9096
elif type(component) is IndexedComponent_slice:
9197
self._cids = tuple(
9298
self._generate_cuid_from_slice(component, context=context)

pyomo/core/tests/unit/test_componentuid.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,7 @@ def test_genFromComponent_context(self):
8181
ValueError, r"Context 'b\[1,'2'\]' does not apply to component 's'"
8282
):
8383
ComponentUID(self.m.s, context=self.m.b[1, '2'])
84-
with self.assertRaisesRegex(
85-
ValueError,
86-
"Context is not allowed when initializing a ComponentUID "
87-
"object from a string type",
88-
):
84+
with self.assertRaisesRegex(ValueError, "Context is not allowed"):
8985
ComponentUID("b[1,2].c.a[2]", context=self.m.b[1, '2'])
9086

9187
def test_parseFromString(self):
@@ -1248,6 +1244,26 @@ def test_cuid_from_slice_errors(self):
12481244
):
12491245
cuid = ComponentUID(_slice)
12501246

1247+
def test_cuid_from_cuid(self):
1248+
def assert_equal(cuid1, cuid2):
1249+
self.assertEqual(cuid1, cuid2)
1250+
self.assertFalse(cuid1 is cuid2)
1251+
1252+
cuid_str = ComponentUID("b.var[1]")
1253+
cuid_str_2 = ComponentUID(cuid_str)
1254+
assert_equal(cuid_str, cuid_str_2)
1255+
1256+
cuid_comp = ComponentUID(self.m.b[1, 1].c)
1257+
cuid_comp_2 = ComponentUID(cuid_comp)
1258+
assert_equal(cuid_str, cuid_str_2)
1259+
1260+
cuid_slice = ComponentUID(self.m.b[1, :].c)
1261+
cuid_slice_2 = ComponentUID(cuid_slice)
1262+
assert_equal(cuid_slice, cuid_slice_2)
1263+
1264+
with self.assertRaisesRegex(ValueError, "Context is not allowed"):
1265+
ComponentUID(cuid_comp, context=self.m.b[1, 1])
1266+
12511267

12521268
if __name__ == "__main__":
12531269
unittest.main()

0 commit comments

Comments
 (0)