Beispiel #1
0
def test_state_access():
    """Test whether calling a ``State`` object calls the ``get_state_value`` and
    ``set_state_value`` methods of the provider object"""

    system = System()
    state = State(system)

    provider = Mock()

    # Check read access
    state(provider)
    provider.get_state_value.assert_called_with(state)

    # Check write access
    provider.reset_mock()
    state.set_value(provider, 10)
    provider.set_state_value.assert_called_with(state, 10)
Beispiel #2
0
class ZeroOrderHold(Block):
    """A zero-order-hold block which samples an input signal when the connected
    event occurs.

    The block provides an event port ``event_input`` that should be connected
    to the event source that shall trigger the sampling.
    """

    def __init__(self, owner, shape=1, initial_condition=None):
        """
        Constructor for ``ZeroOrderHold``

        Args:
            owner: The owner of the block (system or block)
            shape: The shape of the input and output signal
            initial_condition: The initial state of the sampling output
                (before the first tick of the block)
        """
        Block.__init__(self, owner)

        self.event_input = EventPort(self)
        self.event_input.register_listener(self.update_state)
        self.input = Port(shape=shape)
        self.output = State(
            self,
            shape=shape,
            initial_condition=initial_condition,
            derivative_function=None,
        )

    def update_state(self, data):
        """Update the state on a clock event

        Args:
          data: The time, states and signals of the system
        """
        self.output.set_value(data, self.input(data))