Exemplo n.º 1
0
    def get_next_event(self):
        """
        Advance the SDK's internal state machine and return an event for
        processing.

        Returns:
            an instance of `Event`

        Example:
            event = self.get_next_event()
            if isinstance(event, EpisodeStartEvent):
                # do something
            elif isinstance(event, SimulateEvent):
                # do something else
            elif isinstance(event, EpisodeFinishEvent):
                # book keeping
            else:
                # do nothing
        """
        try:
            event = None
            event = self._ioloop.run_sync(self._impl.get_next_event)
        except KeyboardInterrupt:
            event = FinishedEvent()
        except BonsaiClientError as e:
            log.error(e)
            raise e.original_exception
        except BonsaiServerError as e:
            log.error(e)
            event = FinishedEvent()
        except SimStateError as e:
            log.error(e)
            raise e

        return event
Exemplo n.º 2
0
    async def get_next_event(self):
        """ Update the internal event machine and return the next
        event for processing"""
        # Grab a web socket connection if needed
        if self._sim_connection.client is None:
            message = await self._sim_connection.connect()
            # If the connection failed, report
            if message is not None:
                await self._handle_disconnect(message)
                return UnknownEvent()

        if self._prev_message_type == ServerToSimulator.PREDICTION:
            if self._prev_step_terminal[0]:
                self._prev_step_terminal[0] = False
                self._prev_step_finish = True
                event = EpisodeFinishEvent()
            else:
                event = self._process_sim_step()
            if event is not None:
                return event

        await self._ws_send_recv()

        pmt = self._prev_message_type
        if pmt == ServerToSimulator.ACKNOWLEDGE_REGISTER:
            if self._sim.predict:
                self._initial_state = self._new_state_message()
                event = EpisodeStartEvent(self._init_properties,
                                          self._initial_state)
                self._prev_step_finish = False
            else:
                event = UnknownEvent()
        elif (pmt == ServerToSimulator.SET_PROPERTIES
              or pmt == ServerToSimulator.RESET):
            event = UnknownEvent()
        elif pmt == ServerToSimulator.STOP:
            if self._prev_step_finish:
                event = UnknownEvent()
                self._prev_step_finish = False
            else:
                event = EpisodeFinishEvent()
        elif pmt == ServerToSimulator.START:
            self._initial_state = self._new_state_message()
            event = EpisodeStartEvent(self._init_properties,
                                      self._initial_state)
            self._prev_step_finish = False
        elif pmt == ServerToSimulator.PREDICTION:
            event = self._process_sim_step()
        elif pmt == ServerToSimulator.FINISHED:
            event = FinishedEvent()
        else:
            event = UnknownEvent()

        return event
Exemplo n.º 3
0
    def get_next_event(self):
        """ Update the internal event machine and return the next
        event for processing"""
        # Grab a web socket connection if needed
        if self._ws is None:
            message = yield self._connect()
            # If the connection failed, report
            if message is not None:
                raise BonsaiServerError(
                    "Error while connecting to websocket: {}".format(message))

        if self._prev_message_type == ServerToSimulator.PREDICTION:
            if self._prev_step_terminal[0]:
                self._prev_step_terminal[0] = False
                self._prev_step_finish = True
                event = EpisodeFinishEvent()
            else:
                event = self._process_sim_step()
            if event is not None:
                raise gen.Return(event)

        yield self._ws_send_recv()

        pmt = self._prev_message_type
        if pmt == ServerToSimulator.ACKNOWLEDGE_REGISTER:
            if self._sim.predict:
                self._initial_state = self._new_state_message()
                event = EpisodeStartEvent(self._init_properties,
                                          self._initial_state)
                self._prev_step_finish = False
            else:
                event = UnknownEvent()
        if pmt == ServerToSimulator.SET_PROPERTIES or \
           pmt == ServerToSimulator.RESET:
            event = UnknownEvent()
        elif pmt == ServerToSimulator.STOP:
            if self._prev_step_finish:
                event = UnknownEvent()
                self._prev_step_finish = False
            else:
                event = EpisodeFinishEvent()
        elif pmt == ServerToSimulator.START:
            self._initial_state = self._new_state_message()
            event = EpisodeStartEvent(self._init_properties,
                                      self._initial_state)
            self._prev_step_finish = False
        elif pmt == ServerToSimulator.PREDICTION:
            event = self._process_sim_step()
        elif pmt == ServerToSimulator.FINISHED:
            event = FinishedEvent()
        else:
            event = UnknownEvent()

        raise gen.Return(event)
Exemplo n.º 4
0
    def get_next_event(self):
        """
        Advance the SDK's internal state machine and return an event for
        processing.

        Returns:
            an instance of `Event`

        Example:
            event = self.get_next_event()
            if isinstance(event, EpisodeStartEvent):
                # do something
            elif isinstance(event, SimulateEvent):
                # do something else
            elif isinstance(event, EpisodeFinishEvent):
                # book keeping
            else:
                # do nothing
        """
        event = None
        try:
            event = self._ioloop.run_until_complete(
                asyncio.ensure_future(self._impl.get_next_event(),
                                      loop=self._ioloop))
        except KeyboardInterrupt:
            event = FinishedEvent()
        except BonsaiClientError as e:
            log.error(e)
            raise e.original_exception
        except BonsaiServerError as e:
            log.error(e)
            event = FinishedEvent()
        except SimStateError as e:
            log.error(e)
            raise e
        finally:
            if event is None or isinstance(event, FinishedEvent):
                self.close()

        return event