Exemple #1
0
    def add_objective(self, expr, name=None, scope=None):
        """Adds an objective to the driver.

        expr: string
            String containing the objective expression.

        name: string (optional)
            Name to be used to refer to the objective in place of the expression
            string.

        scope: object (optional)
            The object to be used as the scope when evaluating the expression.

         """
        if self._max_objectives > 0 and \
                        len(self._objectives) >= self._max_objectives:
            self.parent.raise_exception(
                "Can't add objective '%s'. Only %d"
                " objectives are allowed" % (expr, self._max_objectives),
                RuntimeError)
        expr = _remove_spaces(expr)
        if expr in self._objectives:
            self.parent.raise_exception(
                "Trying to add objective '%s' to"
                " driver, but it's already there" % expr, AttributeError)
        if name is not None and name in self._objectives:
            self.parent.raise_exception(
                "Trying to add objective '%s' to"
                " driver using name '%s', but name is"
                " already used" % (expr, name), AttributeError)

        scope = self._get_scope(scope)
        expreval = Objective(expr, scope)
        unresolved_vars = expreval.get_unresolved()
        if unresolved_vars:
            msg = "Can't add objective '{0}' because of invalid variables {1}"
            error = ConnectedExprEvaluator._invalid_expression_error(
                unresolved_vars, expreval.text, msg)
            self.parent.raise_exception(str(error), type(error))

        name = expr if name is None else name

        expreval.activate()

        self._objectives[name] = expreval

        self.parent.config_changed()
    def add_response(self, expr, name=None, scope=None):
        """Adds a response to the driver.

        expr: string
            String containing the response expression.

        name: string (optional)
            Name to be used to refer to the response in place of the expression
            string.

        scope: object (optional)
            The object to be used as the scope when evaluating the expression.

        """
        expr = _remove_spaces(expr)
        if expr in self._responses:
            self.parent.raise_exception(
                "Trying to add response '%s' to"
                " driver, but it's already there" % expr, AttributeError)
        if name is not None and name in self._responses:
            self.parent.raise_exception(
                "Trying to add response '%s' to"
                " driver using name '%s', but name is"
                " already used" % (expr, name), AttributeError)

        scope = self._get_scope(scope)
        try:
            expreval = Response(expr, scope)
            unresolved_vars = expreval.get_unresolved()
        except AttributeError:
            unresolved_vars = [expr]
        if unresolved_vars:
            msg = "Can't add response '{0}' because of invalid variables {1}"
            error = ConnectedExprEvaluator._invalid_expression_error(
                unresolved_vars, expr, msg)
            self.parent.raise_exception(str(error), type(error))

        name = expr if name is None else name

        #expreval.activate(self.parent)

        self._responses[name] = expreval
        self.parent.config_changed()