Example #1
0
    def __init__(self, vocabulary, attribute_system, mapping, dummy=False):
        """
        Construct a VariableAssignment object.

        :param vocabulary: The Vocabulary object :math:`\Sigma` the \
        VariableAssignment is defined over.
        :type  vocabulary: Vocabulary
        :param attribute_system: The AttributeSystem object \
        :math:`\mathcal{S}` from which the objects \
        :math:`\{s_{1}, \ldots, s_{n}\}` in the VariableAssignment come from.
        :type  attribute_system: AttributeSystem
        :param mapping: The mapping :math:`\chi` from the variables V of \
        the Vocabulary object :math:`\Sigma` in the ``vocabulary`` parameter \
        to the objects :math:`\{s_{1}, \ldots, s_{n}\}` of the \
        AttributeSystem object :math:`\mathcal{A}` in the \
        ``attribute_system`` parameter.
        :type  mapping: ``dict``
        :param dummy: A flag for creating a dummy (i.e., empty) \
        VariableAssignment object :math:`\chi_{dummy}`.
        :type  dummy: ``bool``

        :raises TypeError: ``vocabulary`` parameter must be a Vocabulary \
        object, ``attribute_system`` parameter must be an AttributeSystem \
        object and ``mapping`` parameter must be a ``dict`` with ``str`` keys \
        and values.
        :raises ValueError: All keys in the ``mapping`` parameter must be in \
        the Vocabulary object in the ``vocabulary`` parameter's ``V`` member \
        and all values in the ``mapping`` parameter must be unique and match \
        some object in the ``object`` member of the AttributeSystem object in \
        the ``attribute_system`` parameter.
        """

        if not isinstance(mapping, dict):
            raise TypeError("mapping parameter must be of type dict")

        if not all([isinstance(s, str) for s in mapping.keys()]):
            raise TypeError("mapping must be of form str: str")

        if not all([isinstance(s, str) for s in mapping.values()]):
            raise TypeError("mapping must be of form str: str")

        if dummy:
            Assignment.__init__(self, vocabulary, attribute_system)
            self._mapping = {}
            self._is_VariableAssignment = True
        else:
            source = mapping.keys()
            target = mapping.values()

            if len(source) != len(set(source)) or len(target) != len(
                    set(target)):
                raise ValueError("duplicate values in mapping parameter "
                                 "are not allowed; mapping must be 1-to-1.")

            # total mapping so check for equality
            source_condition = set(source) <= set(vocabulary._V)

            target_condition = set(target) <= set(attribute_system._objects)

            if source_condition and target_condition:
                Assignment.__init__(self, vocabulary, attribute_system)
                self._mapping = mapping
                self._source = mapping.keys()
                self._target = mapping.values()
                self._is_VariableAssignment = True
            else:
                raise ValueError("VariableAssignment must be a function from "
                                 "vocabulary._V to attribute_system._objects")
    def __init__(self, vocabulary, attribute_system, mapping):
        """
        Construct a ConstantAssignment object.

        :param vocabulary: The Vocabulary object :math:`\Sigma` the \
        ConstantAssignment is defined over.
        :type  vocabulary: Vocabulary
        :param attribute_system: The AttributeSystem object \
        :math:`\mathcal{S}` from which the objects \
        :math:`\{s_{1}, \ldots, s_{n}\}` in the ConstantAssignment come from.
        :type  attribute_system: AttributeSystem
        :param mapping: The mapping :math:`\\rho` from the constants C of \
        the Vocabulary object :math:`\Sigma` in the ``vocabulary`` parameter \
        to the objects :math:`\{s_{1}, \ldots, s_{n}\}` of the \
        AttributeSystem object :math:`\mathcal{A}` in the \
        ``attribute_system`` parameter.
        :type  mapping: ``dict``

        :raises TypeError: ``vocabulary`` parameter must be a Vocabulary \
        object, ``attribute_system`` parameter must be an AttributeSystem \
        object and ``mapping`` parameter must be a ``dict`` with ``str`` keys \
        and values.
        :raises ValueError: All keys in the ``mapping`` parameter must be in \
        the Vocabulary object in the ``vocabulary`` parameter's ``C`` member \
        and all values in the ``mapping`` parameter must be unique and match \
        some object in the ``object`` member of the AttributeSystem object in \
        the ``attribute_system`` parameter.
        """

        if not isinstance(mapping, dict):
            raise TypeError(
                "mapping parameter must be of type dict")

        if not all([isinstance(s, str) for s in mapping.keys()]):
            raise TypeError("mapping must be of form str: str")

        if not all([isinstance(s, str) for s in mapping.values()]):
            raise TypeError("mapping must be of form str: str")

        source = mapping.keys()
        target = mapping.values()

        if len(target) != len(set(target)):
            raise ValueError(
                "duplicate values in mapping parameter "
                "are not allowed; mapping must be 1-to-1.")

        # note: Vocabularies prevent duplicates as do dictionary keys
        source_condition = set(source) <= set(vocabulary._C)
        # note: AttributeSystems prevent duplicate objects
        target_condition = set(target) <= set(attribute_system._objects)

        if source_condition and target_condition:
            Assignment.__init__(self, vocabulary, attribute_system)
            self._mapping = mapping
            self._source = mapping.keys()
            self._target = mapping.values()
            self._is_ConstantAssignment = True
        else:
            raise ValueError(
                "ConstantAssignment must be a partial (or total) function "
                "from Vocabulary's C to AttributeSystem's objects")
    def __init__(self, vocabulary, attribute_system, mapping, dummy=False):
        """
        Construct a VariableAssignment object.

        :param vocabulary: The Vocabulary object :math:`\Sigma` the \
        VariableAssignment is defined over.
        :type  vocabulary: Vocabulary
        :param attribute_system: The AttributeSystem object \
        :math:`\mathcal{S}` from which the objects \
        :math:`\{s_{1}, \ldots, s_{n}\}` in the VariableAssignment come from.
        :type  attribute_system: AttributeSystem
        :param mapping: The mapping :math:`\chi` from the variables V of \
        the Vocabulary object :math:`\Sigma` in the ``vocabulary`` parameter \
        to the objects :math:`\{s_{1}, \ldots, s_{n}\}` of the \
        AttributeSystem object :math:`\mathcal{A}` in the \
        ``attribute_system`` parameter.
        :type  mapping: ``dict``
        :param dummy: A flag for creating a dummy (i.e., empty) \
        VariableAssignment object :math:`\chi_{dummy}`.
        :type  dummy: ``bool``

        :raises TypeError: ``vocabulary`` parameter must be a Vocabulary \
        object, ``attribute_system`` parameter must be an AttributeSystem \
        object and ``mapping`` parameter must be a ``dict`` with ``str`` keys \
        and values.
        :raises ValueError: All keys in the ``mapping`` parameter must be in \
        the Vocabulary object in the ``vocabulary`` parameter's ``V`` member \
        and all values in the ``mapping`` parameter must be unique and match \
        some object in the ``object`` member of the AttributeSystem object in \
        the ``attribute_system`` parameter.
        """

        if not isinstance(mapping, dict):
            raise TypeError(
                "mapping parameter must be of type dict")

        if not all([isinstance(s, str) for s in mapping.keys()]):
            raise TypeError("mapping must be of form str: str")

        if not all([isinstance(s, str) for s in mapping.values()]):
            raise TypeError("mapping must be of form str: str")

        if dummy:
            Assignment.__init__(self, vocabulary, attribute_system)
            self._mapping = {}
            self._is_VariableAssignment = True
        else:
            source = mapping.keys()
            target = mapping.values()

            if len(source) != len(set(source)) or len(target) != len(set(target)):
                raise ValueError(
                    "duplicate values in mapping parameter "
                    "are not allowed; mapping must be 1-to-1.")

            # total mapping so check for equality
            source_condition = set(source) <= set(vocabulary._V)

            target_condition = set(target) <= set(attribute_system._objects)

            if source_condition and target_condition:
                Assignment.__init__(self, vocabulary, attribute_system)
                self._mapping = mapping
                self._source = mapping.keys()
                self._target = mapping.values()
                self._is_VariableAssignment = True
            else:
                raise ValueError(
                    "VariableAssignment must be a function from "
                    "vocabulary._V to attribute_system._objects")