Ejemplo n.º 1
0
    def getname(self,
                fully_qualified=False,
                name_buffer=None,
                relative_to=None):
        """Return a string with the component name and index"""
        #
        # Using the buffer, which is a dictionary:  id -> string
        #
        if name_buffer is not None and id(self) in name_buffer:
            # Return the name if it is in the buffer
            return name_buffer[id(self)]

        c = self.parent_component()
        if c is self:
            #
            # This is a scalar component, so call the
            # Component.getname() method
            #
            return super(ComponentData, self).getname(fully_qualified,
                                                      name_buffer, relative_to)
        elif c is not None:
            #
            # Get the name of the parent component
            #
            base = c.getname(fully_qualified, name_buffer, relative_to)
        else:
            #
            # Defensive: this is a ComponentData without a valid
            # parent_component.  As this usually occurs when handling
            # exceptions during model construction, we need to ensure
            # that this method doesn't itself raise another exception.
            #
            return '[Unattached %s]' % (type(self).__name__, )

        if name_buffer is not None:
            # Iterate through the dictionary and generate all names in
            # the buffer
            for idx, obj in c.items():
                name_buffer[id(obj)] = base + index_repr(idx)
            if id(self) in name_buffer:
                # Return the name if it is in the buffer
                return name_buffer[id(self)]
        else:
            #
            # No buffer, so we iterate through the component _data
            # dictionary until we find this object.  This can be much
            # more expensive than if a buffer is provided.
            #
            for idx, obj in c.items():
                if obj is self:
                    return base + index_repr(idx)
        #
        raise RuntimeError("Fatal error: cannot find the component data in "
                           "the owning component's _data dictionary.")
Ejemplo n.º 2
0
    def root_Var_solution(self):
        """Generator to loop over x-bar

        Yields:
            name, value pair for root node solution values
        """
        root_node = self.scenario_tree.findRootNode()
        for variable_id in sorted(root_node._variable_ids):
            var_name, index = root_node._variable_ids[variable_id]
            name = var_name
            if index is not None:
                name += index_repr(index)
            yield name, root_node._solution[variable_id]
Ejemplo n.º 3
0
def name(component, index=NOTSET, fully_qualified=False, relative_to=None):
    """
    Return a string representation of component for a specific
    index value.
    """
    base = component.getname(fully_qualified=fully_qualified,
                             relative_to=relative_to)
    if index is NOTSET:
        return base
    else:
        if index not in component.index_set():
            raise KeyError("Index %s is not valid for component %s" %
                           (index, component.name))
        return base + index_repr(index)
Ejemplo n.º 4
0
    def getname(self,
                fully_qualified=False,
                name_buffer=None,
                relative_to=None):
        """Return a string with the component name and index"""
        # NOTE: There are bugs with name buffers if a user always gives the same
        # dictionary but switches from fully-qualified to local names or changes
        # the component the name is relative to. We will simply deprecate the
        # buffer in a future PR, but for now we will leave this method so it
        # behaves the same when a buffer is given, and, in the absence of a
        # buffer it will construct the name using the index (woohoo!)

        #
        # Using the buffer, which is a dictionary:  id -> string
        #
        if name_buffer is not None:
            deprecation_warning(
                "The 'name_buffer' argument to getname is deprecated. "
                "The functionality is no longer necessary since getting names "
                "is no longer a quadratic operation. Additionally, note that "
                "use of this argument poses risks if the buffer contains "
                "names relative to different Blocks in the model hierarchy or "
                "a mixture of local and fully_qualified names.",
                version='TODO')
            if id(self) in name_buffer:
                # Return the name if it is in the buffer
                return name_buffer[id(self)]

        c = self.parent_component()
        if c is self:
            #
            # This is a scalar component, so call the
            # Component.getname() method
            #
            return super(ComponentData, self).getname(fully_qualified,
                                                      name_buffer, relative_to)
        elif c is not None:
            #
            # Get the name of the parent component
            #
            base = c.getname(fully_qualified, name_buffer, relative_to)
        else:
            #
            # Defensive: this is a ComponentData without a valid
            # parent_component.  As this usually occurs when handling
            # exceptions during model construction, we need to ensure
            # that this method doesn't itself raise another exception.
            #
            return '[Unattached %s]' % (type(self).__name__, )

        if name_buffer is not None:
            # Iterate through the dictionary and generate all names in
            # the buffer
            for idx, obj in c.items():
                name_buffer[id(obj)] = base + index_repr(idx)
            if id(self) in name_buffer:
                # Return the name if it is in the buffer
                return name_buffer[id(self)]
        else:
            #
            # No buffer, we can do what we are going to do all the time after we
            # deprecate the buffer.
            #
            return base + index_repr(self.index())
        #
        raise RuntimeError("Fatal error: cannot find the component data in "
                           "the owning component's _data dictionary.")