Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow construction of CUID from another CUID #3464

Merged
merged 9 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions pyomo/contrib/mpc/data/get_cuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,8 @@ def get_indexed_cuid(var, sets=None, dereference=None, context=None):
ComponentUID corresponding to the provided ``var`` and sets

"""
# TODO: Does this function have a good name?
# Should this function be generalized beyond a single indexing set?
if isinstance(var, ComponentUID):
return var
elif isinstance(var, (str, IndexedComponent_slice)):
if isinstance(var, (str, IndexedComponent_slice, ComponentUID)):
# TODO: Raise error if string and context is None
return ComponentUID(var, context=context)
# At this point we are assuming var is a Pyomo Var or VarData object.
Expand Down
6 changes: 1 addition & 5 deletions pyomo/core/base/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -921,11 +921,7 @@ def find_component(self, label_or_component):
a matching component is not found, None is returned.

"""
if type(label_or_component) is ComponentUID:
cuid = label_or_component
else:
cuid = ComponentUID(label_or_component)
return cuid.find_component_on(self)
return ComponentUID(label_or_component).find_component_on(self)

@contextmanager
def _declare_reserved_components(self):
Expand Down
15 changes: 10 additions & 5 deletions pyomo/core/base/componentuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import codecs
import re
import ply.lex
import copy

from pyomo.common.collections import ComponentMap
from pyomo.common.dependencies import pickle
Expand Down Expand Up @@ -76,17 +77,21 @@ class ComponentUID(object):
def __init__(self, component, cuid_buffer=None, context=None):
# A CUID can be initialized from either a reference component or
# the string representation.
def _context_err(_type):
raise ValueError(
f"Context is not allowed when initializing a ComponentUID from {_type}."
)
if isinstance(component, str):
if context is not None:
raise ValueError(
"Context is not allowed when initializing a "
"ComponentUID object from a string type"
)
_context_err(str)
try:
self._cids = tuple(self._parse_cuid_v2(component))
except (OSError, IOError):
self._cids = tuple(self._parse_cuid_v1(component))

elif isinstance(component, ComponentUID):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In find_component, we used type is ComponentUID instead of isinstance, presumably for performance. Should we do the same here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably, yes. (If for no other reason than consistency - a derived class would work here, but not in find_component)

if context is not None:
_context_err(ComponentUID)
self._cids = copy.deepcopy(component._cids)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC, _cids shouldn't contain anything mutable, so we may not need the copy here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that is correct, and would be in favor of the change.

elif type(component) is IndexedComponent_slice:
self._cids = tuple(
self._generate_cuid_from_slice(component, context=context)
Expand Down
23 changes: 21 additions & 2 deletions pyomo/core/tests/unit/test_componentuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ def test_genFromComponent_context(self):
ComponentUID(self.m.s, context=self.m.b[1, '2'])
with self.assertRaisesRegex(
ValueError,
"Context is not allowed when initializing a ComponentUID "
"object from a string type",
"Context is not allowed when initializing a ComponentUID from"
):
ComponentUID("b[1,2].c.a[2]", context=self.m.b[1, '2'])

Expand Down Expand Up @@ -1248,6 +1247,26 @@ def test_cuid_from_slice_errors(self):
):
cuid = ComponentUID(_slice)

def test_cuid_from_cuid(self):
def assert_equal(cuid1, cuid2):
self.assertEqual(cuid1, cuid2)
self.assertFalse(cuid1 is cuid2)

cuid_str = ComponentUID("b.var[1]")
cuid_str_2 = ComponentUID(cuid_str)
assert_equal(cuid_str, cuid_str_2)

cuid_comp = ComponentUID(self.m.b[1, 1].c)
cuid_comp_2 = ComponentUID(cuid_comp)
assert_equal(cuid_str, cuid_str_2)

cuid_slice = ComponentUID(self.m.b[1, :].c)
cuid_slice_2 = ComponentUID(cuid_slice)
assert_equal(cuid_slice, cuid_slice_2)

with self.assertRaisesRegex(ValueError, "Context is not allowed"):
ComponentUID(cuid_comp, context=self.m.b[1, 1])


if __name__ == "__main__":
unittest.main()
Loading