File size: 1,625 Bytes
079c32c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from abc import ABC, abstractmethod
from collections import namedtuple
from typing import Any

EnvElementInfo = namedtuple('EnvElementInfo', ['shape', 'value'])


class IEnvElement(ABC):

    @abstractmethod
    def __repr__(self) -> str:
        raise NotImplementedError

    @property
    @abstractmethod
    def info(self) -> Any:
        raise NotImplementedError


class EnvElement(IEnvElement):
    _instance = None
    _name = 'EnvElement'

    def __init__(self, *args, **kwargs) -> None:
        # placeholder
        # self._shape = None
        # self._value = None
        # self._to_agent_processor = None
        # self._from_agent_processor = None
        self._init(*args, **kwargs)
        self._check()

    @abstractmethod
    def _init(*args, **kwargs) -> None:
        raise NotImplementedError

    def __repr__(self) -> str:
        return '{}: {}'.format(self._name, self._details())

    @abstractmethod
    def _details(self) -> str:
        raise NotImplementedError

    def _check(self) -> None:
        flag = [
            hasattr(self, '_shape'),
            hasattr(self, '_value'),
            # hasattr(self, '_to_agent_processor'),
            # hasattr(self, '_from_agent_processor'),
        ]
        assert all(flag), 'this class {} is not a legal subclass of EnvElement({})'.format(self.__class__, flag)

    @property
    def info(self) -> 'EnvElementInfo':
        return EnvElementInfo(
            shape=self._shape,
            value=self._value,
            # to_agent_processor=self._to_agent_processor,
            # from_agent_processor=self._from_agent_processor
        )