Changeset 2911


Ignore:
Timestamp:
Aug 6, 2010 6:51:12 PM (9 years ago)
Author:
prsteel
Message:

Changes to allow Block objects to contain other Block objects.

Added self.domain = None in Block.init as a temporary workaround
to ticket #4108.

Changed logic in _setattr_exec to avoid infinite recursion when
assigning Block objects to Block objects.

Clarified logic in setattr.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • coopr.pyomo/trunk/coopr/pyomo/base/block.py

    r2881 r2911  
    7676        #
    7777        self._declarations=OrderedDict()
     78
     79        # Hack. Right now Block objects assume all attributes derived from
     80        # Component eventually inherit from NumValue, and so have a domain
     81        # attribute.
     82        self.domain = None
    7883
    7984    def concrete_mode(self):
     
    169174        self._declarations[name] = val
    170175        self.__dict__[name]=val
    171         if val.type() is Block:
    172             val._parent_block = self
    173         curr = self
    174         while not curr is None:
    175            val.model=curr
    176            curr = self._parent_block
     176
     177        # Presumably self.model refers to the 'root' Block that will
     178        # eventually be solved, whereas '_parent_block' refers to the
     179        # immediate parent.
     180
     181        # Don't let '_parent_block' attributes trigger recursion
     182        if isinstance(val, Block):
     183            self.__dict__["_parent_block"] = self
     184        else:
     185            curr = self
     186            while curr is not None:
     187                val.model = curr
     188                curr = self._parent_block
     189        # self is the top level model if self.model is None; otherwise,
     190        # self.model is the top level model
     191        if self.model is not None:
     192            self.__dict__["model"] = self.model
     193        else:
     194            self.__dict__["model"] = self
     195
     196#         # Need to break infinite loop of attribute setting
     197#         if isinstance(val, Block) and name != "_parent_block":
     198#             val._parent_block = self
     199#         else:
     200#             curr = self
     201#             while not curr is None:
     202#                val.model=curr
     203#                curr = self._parent_block
     204
    177205        if not self._defer_construction:
    178206            val.construct(None)
     
    183211        # Set Model Declaration
    184212        #
    185         if name != "_component":
     213        if name != "_component" and isinstance(val, Component):
    186214            #
    187215            # If this is a component type, then simply set it
    188216            #
    189             if isinstance(val,Component):
    190                 self._setattr_exec(name,val)
    191                 return
    192         #
    193         # Try to set the value. This may fail if the attribute does not already
    194         # exist in this model, if the set_value function is not defined, or if
    195         # a bad value is provided. In the latter case, a ValueError will be
    196         # thrown, which # we raise. Otherwise, this is an object that we need
    197         # to set directly.
    198         #
    199         try:
    200             self.__dict__[name].set_value(val)
    201         except ValueError, e:
    202             raise
    203         except Exception, e:
    204             self.__dict__[name]=val
     217            self._setattr_exec(name,val)
     218        else:
     219            #
     220            # Try to set the value. This may fail if the attribute
     221            # does not already exist in this model, if the set_value
     222            # function is not defined, or if a bad value is
     223            # provided. In the latter case, a ValueError will be
     224            # thrown, which # we raise. Otherwise, this is an object
     225            # that we need to set directly.
     226            #
     227            try:
     228                self.__dict__[name].set_value(val)
     229            except ValueError, e:
     230                raise
     231            except Exception, e:
     232                self.__dict__[name]=val
    205233
    206234    def pprint(self, filename=None, ostream=None):
Note: See TracChangeset for help on using the changeset viewer.