Example #1
0
 def construct(self, data=None):
     """ Apply the rule to construct values in this set """
     if is_debug_set(logger):  #pragma:nocover
         logger.debug("Constructing Check, name=" + self.name)
     #
     if self._constructed:  #pragma:nocover
         return
     timer = ConstructionTimer(self)
     self._constructed = True
     #
     if not self.is_indexed():
         # Scalar component
         res = self._rule(self._parent())
         if not res:
             raise ValueError("BuildCheck %r identified error" % self.name)
     else:
         # Indexed component
         for index in self._index:
             res = apply_indexed_rule(self, self._rule, self._parent(),
                                      index)
             if not res:
                 raise ValueError(
                     "BuildCheck %r identified error with index %r" %
                     (self.name, str(index)))
     timer.report()
Example #2
0
def _process_data_list(param_name, dim, cmd):
    """\
 Called by _process_param() to process a list of data for a Parameter.
 """
    generate_debug_messages = is_debug_set(logger)
    if generate_debug_messages:
        logger.debug("process_data_list %d %s", dim, cmd)

    if len(cmd) % (dim + 1) != 0:
        msg = "Parameter '%s' defined with '%d' dimensions, " \
              "but data has '%d' values: %s."
        msg = msg % (param_name, dim, len(cmd), cmd)
        if len(cmd) % (dim + 1) == dim:
            msg += " Are you missing a value for a %d-dimensional index?" % dim
        elif len(cmd) % dim == 0:
            msg += " Are you missing the values for %d-dimensional indices?" % dim
        else:
            msg += " Data declaration must be given in multiples of %d." % (
                dim + 1)
        raise ValueError(msg)

    ans = {}
    if dim == 0:
        ans[None] = cmd[0]
        return ans
    i = 0
    while i < len(cmd):
        if dim > 1:
            ndx = tuple(cmd[i:i + dim])
        else:
            ndx = cmd[i]
        if cmd[i + dim] != ".":
            ans[ndx] = cmd[i + dim]
        i += dim + 1
    return ans
Example #3
0
    def construct(self, data=None):
        """Construct this component."""
        if is_debug_set(logger):  #pragma:nocover
            try:
                name = str(self.name)
            except:
                name = type(self)
            logger.debug("Constructing Variable, name=%s, from data=%s" %
                         (name, str(data)))

        if self._constructed:
            return
        timer = ConstructionTimer(self)
        self._constructed = True

        #
        # Construct _BooleanVarData objects for all index values
        #
        if not self.is_indexed():
            self._data[None] = self
            self._initialize_members((None, ))
        elif self._dense:
            # This loop is optimized for speed with pypy.
            # Calling dict.update((...) for ...) is roughly
            # 30% slower
            self_weakref = weakref_ref(self)
            for ndx in self._index:
                cdata = self._ComponentDataClass(component=None)
                cdata._component = self_weakref
                self._data[ndx] = cdata
            self._initialize_members(self._index)
        timer.report()
Example #4
0
    def _apply_to(self, instance, **kwds):
        if is_debug_set(logger):
            logger.debug("Calling ArcExpander")

        # need to collect all ports to see every port each
        # is related to so that we can expand empty ports
        port_list, known_port_sets, matched_ports = \
            self._collect_ports(instance)

        self._add_blocks(instance)

        for port in port_list:
            # iterate over ref so that the index set is the same
            # for all occurences of this member in related ports
            # and so we iterate over members deterministically
            ref = known_port_sets[id(matched_ports[port])]
            for k, v in sorted(iteritems(ref)):
                rule, kwds = port._rules[k]
                if v[1] >= 0:
                    index_set = v[0].index_set()
                else:
                    index_set = UnindexedComponent_set
                rule(port, k, index_set, **kwds)

        for arc in instance.component_objects(**obj_iter_kwds):
            arc.deactivate()
Example #5
0
    def construct(self, data=None):
        """Construct this component."""
        if is_debug_set(logger):
            logger.debug("Constructing variable list %s", self.name)

        if self._constructed:
            return
        # Note: do not set _constructed here, or the super() call will
        # not actually construct the component.
        self.index_set().construct()

        # We need to ensure that the indices needed for initialization are
        # added to the underlying implicit set.  We *could* verify that the
        # indices in the initialization dict are all sequential integers,
        # OR we can just add the correct number of sequential integers and
        # then let _validate_index complain when we set the value.
        if self._value_init_value.__class__ is dict:
            for i in range(len(self._value_init_value)):
                self._index.add(i + 1)
        super(VarList, self).construct(data)
        # Note that the current Var initializer silently ignores
        # initialization data that is not in the underlying index set.  To
        # ensure that at least here all initialization data is added to the
        # VarList (so we get potential domain errors), we will re-set
        # everything.
        if self._value_init_value.__class__ is dict:
            for k, v in self._value_init_value.items():
                self[k] = v
Example #6
0
    def _apply_to_impl(self, instance, **kwds):
        self._config = self.CONFIG(kwds.pop('options', {}))
        self._config.set_value(kwds)
        self._generate_debug_messages = is_debug_set(logger)

        targets = self._config.targets
        if targets is None:
            targets = (instance, )
        knownBlocks = {}
        for t in targets:
            # check that t is in fact a child of instance
            if not is_child_of(
                    parent=instance, child=t, knownBlocks=knownBlocks):
                raise GDP_Error(
                    "Target '%s' is not a component on instance '%s'!" %
                    (t.name, instance.name))
            elif t.ctype is Disjunction:
                if t.is_indexed():
                    self._transform_disjunction(t)
                else:
                    self._transform_disjunctionData(t, t.index())
            elif t.ctype in (Block, Disjunct):
                if t.is_indexed():
                    self._transform_block(t)
                else:
                    self._transform_blockData(t)
            else:
                raise GDP_Error(
                    "Target '%s' was not a Block, Disjunct, or Disjunction. "
                    "It was of type %s and can't be transformed." %
                    (t.name, type(t)))
Example #7
0
    def construct(self, data=None):
        if is_debug_set(logger):  #pragma:nocover
            logger.debug("Constructing Port, name=%s, from data=%s" %
                         (self.name, data))

        if self._constructed:
            return

        timer = ConstructionTimer(self)
        self._constructed = True

        # Construct _PortData objects for all index values
        if self.is_indexed():
            self._initialize_members(self._index)
        else:
            self._data[None] = self
            self._initialize_members([None])

        # get rid of these references
        self._rule = None
        self._initialize = None
        self._implicit = None
        self._extends = None  # especially important as this is another port

        timer.report()
Example #8
0
 def construct(self, data=None):
     if is_debug_set(logger):
         logger.debug(  #pragma:nocover
             "Constructing ComponentDict object, name=%s, from data=%s" %
             (self.name, str(data)))
     if self._constructed:  #pragma:nocover
         return
     self._constructed = True
Example #9
0
    def construct(self, data=None):
        """
        Construct the expression(s) for this constraint.
        """
        if self._constructed:
            return
        self._constructed=True

        timer = ConstructionTimer(self)
        if is_debug_set(logger):
            logger.debug("Constructing constraint %s"
                         % (self.name))

        try:
            # We do not (currently) accept data for constructing Constraints
            assert data is None

            if self.rule is None:
                # If there is no rule, then we are immediately done.
                return

            if self.rule.constant() and self.is_indexed():
                raise IndexError(
                    "Constraint '%s': Cannot initialize multiple indices "
                    "of a constraint with a single expression" %
                    (self.name,) )

            index = None
            block = self.parent_block()
            if self.rule.contains_indices():
                # The index is coming in externally; we need to validate it
                for index in self.rule.indices():
                    self[index] = self.rule(block, index)
            elif not self.index_set().isfinite():
                # If the index is not finite, then we cannot iterate
                # over it.  Since the rule doesn't provide explicit
                # indices, then there is nothing we can do (the
                # assumption is that the user will trigger specific
                # indices to be created at a later time).
                pass
            else:
                # Bypass the index validation and create the member directly
                for index in self.index_set():
                    self._setitem_when_not_present(
                        index, self.rule(block, index)
                    )
        except Exception:
            err = sys.exc_info()[1]
            logger.error(
                "Rule failed when generating expression for "
                "constraint %s with index %s:\n%s: %s"
                % (self.name,
                   str(index),
                   type(err).__name__,
                   err))
            raise
        finally:
            timer.report()
Example #10
0
    def construct(self, data=None):
        """
        Construct the expression(s) for this logical constraint.
        """
        generate_debug_messages = is_debug_set(logger)
        if generate_debug_messages:
            logger.debug("Constructing logical constraint list %s" % self.name)

        if self._constructed:
            return
        self._constructed = True

        assert self._init_expr is None
        _init_rule = self.rule

        #
        # We no longer need these
        #
        self._init_expr = None
        # Utilities like DAE assume this stays around
        # self.rule = None

        if _init_rule is None:
            return

        _generator = None
        _self_parent = self._parent()
        if inspect.isgeneratorfunction(_init_rule):
            _generator = _init_rule(_self_parent)
        elif inspect.isgenerator(_init_rule):
            _generator = _init_rule
        if _generator is None:
            while True:
                val = len(self._index) + 1
                if generate_debug_messages:
                    logger.debug("   Constructing logical constraint index " +
                                 str(val))
                expr = apply_indexed_rule(self, _init_rule, _self_parent, val)
                if expr is None:
                    raise ValueError(
                        "LogicalConstraintList '%s': rule returned None "
                        "instead of LogicalConstraintList.End" % (self.name, ))
                if (expr.__class__ is tuple) and \
                        (expr == LogicalConstraintList.End):
                    return
                self.add(expr)

        else:

            for expr in _generator:
                if expr is None:
                    raise ValueError(
                        "LogicalConstraintList '%s': generator returned None "
                        "instead of LogicalConstraintList.End" % (self.name, ))
                if (expr.__class__ is tuple) and \
                        (expr == LogicalConstraintList.End):
                    return
                self.add(expr)
Example #11
0
    def construct(self, data=None):
        """
        Construct the expression(s) for this objective.
        """
        generate_debug_messages = is_debug_set(logger)
        if generate_debug_messages:
            logger.debug(
                "Constructing objective %s" % (self.name))

        if self._constructed:
            return
        self._constructed=True
        self.index_set().construct()

        assert self._init_expr is None
        _init_rule = self.rule
        _init_sense = self._init_sense

        #
        # We no longer need these
        #
        self._init_expr = None
        self._init_sense = None
        # Utilities like DAE assume this stays around
        #self.rule = None


        if _init_rule is None:
            return

        _generator = None
        _self_parent = self._parent()
        if inspect.isgeneratorfunction(_init_rule):
            _generator = _init_rule(_self_parent)
        elif inspect.isgenerator(_init_rule):
            _generator = _init_rule
        if _generator is None:
            while True:
                val = len(self._index) + 1
                if generate_debug_messages:
                    logger.debug(
                        "   Constructing objective index "+str(val))
                expr = apply_indexed_rule(self,
                                          _init_rule,
                                          _self_parent,
                                          val)
                if (expr.__class__ is tuple) and \
                   (expr == ObjectiveList.End):
                    return
                self.add(expr, sense=_init_sense)

        else:

            for expr in _generator:
                if (expr.__class__ is tuple) and \
                   (expr == ObjectiveList.End):
                    return
                self.add(expr, sense=_init_sense)
Example #12
0
    def construct(self, data=None):
        """
        Construct the expression(s) for this objective.
        """
        if is_debug_set(logger):
            logger.debug("Constructing objective %s" % (self.name))
        if self._constructed:
            return
        timer = ConstructionTimer(self)
        self._constructed = True

        _init_expr = self._init_expr
        _init_sense = self._init_sense
        _init_rule = self.rule

        if (_init_rule is None) and \
           (_init_expr is None):
            # No construction rule or expression specified.
            return

        _self_parent = self._parent()
        if not self.is_indexed():
            #
            # Scalar component
            #
            if _init_rule is None:
                tmp = _init_expr
            else:
                try:
                    tmp = _init_rule(_self_parent)
                except Exception:
                    err = sys.exc_info()[1]
                    logger.error("Rule failed when generating expression for "
                                 "objective %s:\n%s: %s" %
                                 (self.name, type(err).__name__, err))
                    raise
            if self._setitem_when_not_present(None, tmp) is not None:
                self.set_sense(_init_sense)

        else:
            if _init_expr is not None:
                raise IndexError("Cannot initialize multiple indices of an "
                                 "objective with a single expression")
            for ndx in self._index:
                try:
                    tmp = apply_indexed_rule(self, _init_rule, _self_parent,
                                             ndx)
                except Exception:
                    err = sys.exc_info()[1]
                    logger.error(
                        "Rule failed when generating expression for"
                        " objective %s with index %s:\n%s: %s" %
                        (self.name, str(ndx), type(err).__name__, err))
                    raise
                ans = self._setitem_when_not_present(ndx, tmp)
                if ans is not None:
                    ans.set_sense(_init_sense)
        timer.report()
Example #13
0
    def construct(self, data=None):
        if is_debug_set(logger):
            logger.debug("Constructing disjunction %s"
                         % (self.name))
        if self._constructed:
            return
        timer = ConstructionTimer(self)
        self._constructed=True

        _self_parent = self.parent_block()
        if not self.is_indexed():
            if self._init_rule is not None:
                expr = self._init_rule(_self_parent)
            elif self._init_expr is not None:
                expr = self._init_expr
            else:
                timer.report()
                return

            if expr is None:
                raise ValueError( _rule_returned_none_error % (self.name,) )
            if expr is Disjunction.Skip:
                timer.report()
                return
            self._data[None] = self
            self._setitem_when_not_present( None, expr )
        elif self._init_expr is not None:
            raise IndexError(
                "Disjunction '%s': Cannot initialize multiple indices "
                "of a disjunction with a single disjunction list" %
                (self.name,) )
        elif self._init_rule is not None:
            _init_rule = self._init_rule
            for ndx in self._index:
                try:
                    expr = apply_indexed_rule(self,
                                             _init_rule,
                                             _self_parent,
                                             ndx)
                except Exception:
                    err = sys.exc_info()[1]
                    logger.error(
                        "Rule failed when generating expression for "
                        "disjunction %s with index %s:\n%s: %s"
                        % (self.name,
                           str(ndx),
                           type(err).__name__,
                           err))
                    raise
                if expr is None:
                    _name = "%s[%s]" % (self.name, str(ndx))
                    raise ValueError( _rule_returned_none_error % (_name,) )
                if expr is Disjunction.Skip:
                    continue
                self._setitem_when_not_present(ndx, expr)
        timer.report()
Example #14
0
    def construct(self, data=None):
        """Initialize the Arc"""
        if is_debug_set(logger):
            logger.debug("Constructing Arc %s" % self.name)

        if self._constructed:
            return

        timer = ConstructionTimer(self)
        self._constructed = True

        if self._rule is None and self._init_vals is None:
            # No construction rule or values specified
            return
        elif self._rule is not None and self._init_vals is not None:
            raise ValueError(
                "Cannot specify rule along with source/destination/ports "
                "keywords for arc '%s'" % self.name)

        self_parent = self._parent()

        if not self.is_indexed():
            if self._rule is None:
                tmp = self._init_vals
                tmp["directed"] = self._init_directed
            else:
                try:
                    tmp = self._rule(self_parent)
                except Exception:
                    err = sys.exc_info()[1]
                    logger.error("Rule failed when generating values for "
                                 "arc %s:\n%s: %s" %
                                 (self.name, type(err).__name__, err))
                    raise
                tmp = _iterable_to_dict(tmp, self._init_directed, self.name)
            self._setitem_when_not_present(None, tmp)
        else:
            if self._init_vals is not None:
                raise IndexError(
                    "Arc '%s': Cannot initialize multiple indices "
                    "of an arc with single ports" % self.name)
            for idx in self._index:
                try:
                    tmp = apply_indexed_rule(self, self._rule, self_parent,
                                             idx)
                except Exception:
                    err = sys.exc_info()[1]
                    logger.error(
                        "Rule failed when generating values for "
                        "arc %s with index %s:\n%s: %s" %
                        (self.name, str(idx), type(err).__name__, err))
                    raise
                tmp = _iterable_to_dict(tmp, self._init_directed, self.name)
                self._setitem_when_not_present(idx, tmp)
        timer.report()
Example #15
0
    def construct(self, data=None):
        """Construct the expression(s) for this constraint."""
        if is_debug_set(logger):
            logger.debug("Constructing constraint %s" % (self.name))
        if self._constructed:
            return
        self._constructed = True

        ref = weakref.ref(self)
        with PauseGC():
            self._data = tuple(
                _MatrixConstraintData(i, ref) for i in range(len(self._lower)))
Example #16
0
def _process_set(cmd, _model, _data):
    """
    Called by _process_data() to process a set declaration.
    """
    #print("SET %s" % cmd)
    generate_debug_messages = is_debug_set(logger)
    if generate_debug_messages:
        logger.debug("DEBUG: _process_set(start) %s", cmd)
    #
    # Process a set
    #
    if type(cmd[2]) is tuple:
        #
        # An indexed set
        #
        ndx = cmd[2]
        if len(ndx) == 0:
            # At this point, if the index is an empty tuple, then there is an
            # issue with the specification of this indexed set.
            raise ValueError(
                "Illegal indexed set specification encountered: " +
                str(cmd[1]))
        elif len(ndx) == 1:
            ndx = ndx[0]
        if cmd[1] not in _data:
            _data[cmd[1]] = {}
        _data[cmd[1]][ndx] = _process_set_data(cmd[4:], cmd[1], _model)

    elif cmd[2] == ":":
        #
        # A tabular set
        #
        _data[cmd[1]] = {}
        _data[cmd[1]][None] = []
        i = 3
        while cmd[i] != ":=":
            i += 1
        ndx1 = cmd[3:i]
        i += 1
        while i < len(cmd):
            ndx = cmd[i]
            for j in xrange(0, len(ndx1)):
                if cmd[i + j + 1] == "+":
                    #print("DATA %s %s" % (ndx1[j], cmd[i]))
                    _data[cmd[1]][None].append((ndx1[j], cmd[i]))
            i += len(ndx1) + 1
    else:
        #
        # Processing a general set
        #
        _data[cmd[1]] = {}
        _data[cmd[1]][None] = _process_set_data(cmd[3:], cmd[1], _model)
Example #17
0
 def construct(self, data=None):
     if is_debug_set(logger):  #pragma:nocover
         try:
             name = str(self.name)
         except:
             name = type(self)
         logger.debug("Constructing Alias, name=%s, "
                      "from data=%s", name, str(data))
     if self._constructed:
         return
     timer = ConstructionTimer(self)
     self._constructed = True
     timer.report()
Example #18
0
    def construct(self, data=None):
        """
        Construct the expression(s) for this constraint.
        """
        if is_debug_set(logger):
            logger.debug("Constructing constraint %s" % (self.name))
        if self._constructed:
            return
        self._constructed = True

        _init = _LinearMatrixConstraintData
        self._data = tuple(
            _init(i, component=self) for i in range(len(self._range_types)))
Example #19
0
 def _apply_to(self, instance, **kwds):
     self._generate_debug_messages = is_debug_set(logger)
     self.used_args = ComponentMap()  # If everything was sure to go well,
     # this could be a dictionary. But if
     # someone messes up and gives us a Var
     # as a key in bigMargs, I need the error
     # not to be when I try to put it into
     # this map!
     try:
         self._apply_to_impl(instance, **kwds)
     finally:
         # same for our bookkeeping about what we used from bigM arg dict
         self.used_args.clear()
Example #20
0
    def _initialize_component(self, modeldata, namespaces, component_name,
                              profile_memory):
        declaration = self.component(component_name)

        if component_name in modeldata._default:
            if declaration.ctype is not Set:
                declaration.set_default(modeldata._default[component_name])
        data = None

        for namespace in namespaces:
            if component_name in modeldata._data.get(namespace, {}):
                data = modeldata._data[namespace][component_name]
            if data is not None:
                break

        generate_debug_messages = is_debug_set(logger)
        if generate_debug_messages:
            _blockName = "Model" if self.parent_block() is None \
                else "Block '%s'" % self.name
            logger.debug("Constructing %s '%s' on %s from data=%s",
                         declaration.__class__.__name__, declaration.name,
                         _blockName, str(data))
        try:
            declaration.construct(data)
        except:
            err = sys.exc_info()[1]
            logger.error(
                "Constructing component '%s' from data=%s failed:\n    %s: %s",
                str(declaration.name),
                str(data).strip(),
                type(err).__name__, err)
            raise

        if generate_debug_messages:
            _out = StringIO()
            declaration.pprint(ostream=_out)
            logger.debug("Constructed component '%s':\n    %s" %
                         (declaration.name, _out.getvalue()))

        if profile_memory >= 2 and pympler_available:
            mem_used = pympler.muppy.get_size(pympler.muppy.get_objects())
            print(
                "      Total memory = %d bytes following construction of component=%s"
                % (mem_used, component_name))

            if profile_memory >= 3:
                gc.collect()
                mem_used = pympler.muppy.get_size(pympler.muppy.get_objects())
                print(
                    "      Total memory = %d bytes following construction of component=%s (after garbage collection)"
                    % (mem_used, component_name))
Example #21
0
    def construct(self, data=None):
        """ Apply the rule to construct values in this set """

        if is_debug_set(logger):
            logger.debug(
                "Constructing Expression, name=%s, from data=%s"
                % (self.name, str(data)))

        if self._constructed:
            return
        timer = ConstructionTimer(self)
        self._constructed = True

        _init_expr = self._init_expr
        _init_rule = self._init_rule
        #
        # We no longer need these
        #
        self._init_expr = None
        # Utilities like DAE assume this stays around
        #self._init_rule = None

        if not self.is_indexed():
            self._data[None] = self

        #
        # Construct and initialize members
        #
        if _init_rule is not None:
            # construct and initialize with a rule
            if self.is_indexed():
                for key in self._index:
                    self.add(key,
                             apply_indexed_rule(
                                 self,
                                 _init_rule,
                                 self._parent(),
                                 key))
            else:
                self.add(None, _init_rule(self._parent()))
        else:
            # construct and initialize with a value
            if _init_expr.__class__ is dict:
                for key in self._index:
                    if key not in _init_expr:
                        continue
                    self.add(key, _init_expr[key])
            else:
                for key in self._index:
                    self.add(key, _init_expr)
        timer.report()
Example #22
0
    def pyomo_excepthook(etype, value, tb):
        """
        This exception hook gets called when debugging is on. Otherwise,
        run_command in this module is called.
        """
        global filter_excepthook
        if len(data.options.model.filename) > 0:
            name = "model " + data.options.model.filename
        else:
            name = "model"

        if filter_excepthook:
            action = "loading"
        else:
            action = "running"

        msg = "Unexpected exception (%s) while %s %s:\n    " \
              % (etype.__name__, action, name)

        #
        # This handles the case where the error is propagated by a KeyError.
        # KeyError likes to pass raw strings that don't handle newlines
        # (they translate "\n" to "\\n"), as well as tacking on single
        # quotes at either end of the error message. This undoes all that.
        #
        valueStr = str(value)
        if etype == KeyError:
            valueStr = valueStr.replace(r"\n", "\n")
            if valueStr[0] == valueStr[-1] and valueStr[0] in "\"'":
                valueStr = valueStr[1:-1]

        logger.error(msg + valueStr)

        tb_list = traceback.extract_tb(tb, None)
        i = 0
        if not is_debug_set(logger) and filter_excepthook:
            while i < len(tb_list):
                if data.options.model.filename in tb_list[i][0]:
                    break
                i += 1
            if i == len(tb_list):
                i = 0
        print("\nTraceback (most recent call last):")
        for item in tb_list[i:]:
            print("  File \"" + item[0] + "\", line " + str(item[1]) +
                  ", in " + item[2])
            if item[3] is not None:
                print("    " + item[3])
        sys.exit(1)
Example #23
0
    def construct(self, data=None):
        """
        Constructs this component, applying rule if it exists.
        """
        if is_debug_set(logger):
            logger.debug("Constructing suffix %s", self.name)

        if self._constructed is True:
            return

        timer = ConstructionTimer(self)
        self._constructed = True

        if self._rule is not None:
            self.update_values(self._rule(self._parent()))
        timer.report()
Example #24
0
    def construct(self, data=None):
        """ Apply the rule to construct values in this set """
        if self._constructed:
            return
        self._constructed = True

        timer = ConstructionTimer(self)
        if is_debug_set(logger):
            logger.debug("Constructing Expression, name=%s, from data=%s" %
                         (self.name, str(data)))

        rule = self.rule
        try:
            # We do not (currently) accept data for constructing Constraints
            index = None
            assert data is None

            if rule is None:
                # Historically, Expression objects were dense (but None):
                rule = Initializer(lambda *args: None)
                # If there is no rule, then we are immediately done.
                #return

            block = self.parent_block()
            if rule.contains_indices():
                # The index is coming in externally; we need to validate it
                for index in rule.indices():
                    self[index] = rule(block, index)
            elif not self.index_set().isfinite():
                # If the index is not finite, then we cannot iterate
                # over it.  Since the rule doesn't provide explicit
                # indices, then there is nothing we can do (the
                # assumption is that the user will trigger specific
                # indices to be created at a later time).
                pass
            else:
                # Bypass the index validation and create the member directly
                for index in self.index_set():
                    self._setitem_when_not_present(index, rule(block, index))
        except Exception:
            err = sys.exc_info()[1]
            logger.error("Rule failed when generating expression for "
                         "Expression %s with index %s:\n%s: %s" %
                         (self.name, str(index), type(err).__name__, err))
            raise
        finally:
            timer.report()
Example #25
0
def _process_set_data(cmd, sname, _model):
    """
    Called by _process_set() to process set data.
    """
    generate_debug_messages = is_debug_set(logger)
    if generate_debug_messages:
        logger.debug("DEBUG: _process_set_data(start) %s", cmd)
    if len(cmd) == 0:
        return []
    ans = []
    i = 0
    template = None
    ndx = []
    template = []
    while i < len(cmd):
        if type(cmd[i]) is not tuple:
            if len(ndx) == 0:
                ans.append(cmd[i])
            else:
                #
                # Use the ndx to create a tuple.  This list
                # contains the indices of the values that need
                # to be filled-in
                #
                tmpval = template
                for kk in range(len(ndx)):
                    if i == len(cmd):
                        raise IOError(
                            "Expected another set value to flush out a tuple pattern!"
                        )
                    tmpval[ndx[kk]] = cmd[i]
                    i += 1
                ans.append(tuple(tmpval))
                continue
        elif "*" not in cmd[i]:
            ans.append(cmd[i])
        else:
            template = list(cmd[i])
            ndx = []
            for kk in range(len(template)):
                if template[kk] == '*':
                    ndx.append(kk)
        i += 1
    if generate_debug_messages:
        logger.debug("DEBUG: _process_set_data(end) %s", ans)
    return ans
Example #26
0
    def construct(self, data=None):
        """ Apply the rule to construct values in this set """
        if self._constructed:
            return
        self._constructed = True

        timer = ConstructionTimer(self)
        if is_debug_set(logger):
            logger.debug("Constructing Expression, name=%s, from data=%s" %
                         (self.name, str(data)))

        try:
            # We do not (currently) accept data for constructing Constraints
            assert data is None
            self._construct_from_rule_using_setitem()
        finally:
            timer.report()
Example #27
0
 def construct(self, data=None):
     if is_debug_set(logger):  #pragma:nocover
         logger.debug("Constructing Connector, name=%s, from data=%s" %
                      (self.name, data))
     if self._constructed:
         return
     timer = ConstructionTimer(self)
     self._constructed = True
     #
     # Construct _ConnectorData objects for all index values
     #
     if self.is_indexed():
         self._initialize_members(self._index)
     else:
         self._data[None] = self
         self._initialize_members([None])
     timer.report()
Example #28
0
 def construct(self, data=None):
     """ Apply the rule to construct values in this set """
     if is_debug_set(logger):  #pragma:nocover
         logger.debug("Constructing Action, name=" + self.name)
     #
     if self._constructed:  #pragma:nocover
         return
     timer = ConstructionTimer(self)
     self._constructed = True
     #
     if not self.is_indexed():
         # Scalar component
         self._rule(self._parent())
     else:
         # Indexed component
         for index in self._index_set:
             apply_indexed_rule(self, self._rule, self._parent(), index)
     timer.report()
Example #29
0
    def construct(self, data=None):
        """
        Construct the ExternalGreyBoxBlockDatas
        """
        if self._constructed:
            return
        # Do not set the constructed flag - Block.construct() will do that

        timer = ConstructionTimer(self)
        if is_debug_set(logger):
            logger.debug("Constructing external grey box model %s" %
                         (self.name))

        super(ExternalGreyBoxBlock, self).construct(data)

        if self._init_model is not None:
            block = self.parent_block()
            for index, data in iteritems(self):
                data.set_external_model(self._init_model(block, index))
Example #30
0
    def construct(self, data=None):
        """Construct this component."""
        if self._constructed:
            return
        # Note: do not set _constructed here, or the super() call will
        # not actually construct the component.

        if is_debug_set(logger):
            logger.debug("Constructing variable list %s", self.name)

        self.index_set().construct()

        # We need to ensure that the indices needed for initialization are
        # added to the underlying implicit set.  We *could* verify that the
        # indices in the initialization dict are all sequential integers,
        # OR we can just add the correct number of sequential integers and
        # then let _validate_index complain when we set the value.
        if self._rule_init is not None and self._rule_init.contains_indices():
            for i, idx in enumerate(self._rule_init.indices()):
                self._index.add(i + self._starting_index)
        super(VarList,self).construct(data)