role.py 1.93 KB
Newer Older
GromeTT's avatar
GromeTT committed
1
2
"""This module implements the role class."""

Jiahang Chen's avatar
Jiahang Chen committed
3
import sys
GromeTT's avatar
GromeTT committed
4
5
6
from abc import ABC
from ml.identifier import ID

Jiahang Chen's avatar
Jiahang Chen committed
7
8

class Role(ABC):
GromeTT's avatar
GromeTT committed
9
    """Each Role class manages a role which can be assigned to things."""
Jiahang Chen's avatar
Jiahang Chen committed
10
    def __init__(self, name="", identifier=""):
GromeTT's avatar
GromeTT committed
11
12
13
14
15
16
17
        """Constructs a new Role class object with the given name and
        identifier.

        :param name: Name of the feature
        :param identifier: Identifier of the feature
        """

Jiahang Chen's avatar
Jiahang Chen committed
18
19
        self.__identifier = ID(identifier).identifier
        self.__name = name
GromeTT's avatar
GromeTT committed
20
21

        # TODO: Remove code duplication (feature.py).
Jiahang Chen's avatar
Jiahang Chen committed
22
        if 'ml\\fml40' in sys.modules[self.__class__.__module__].__file__:
Jiahang Chen's avatar
Jiahang Chen committed
23
24
25
26
27
28
29
            self.__class_name = "fml40::{}".format(self.__class__.__name__)
        else:
            self.__class_name = "ml40::{}".format(self.__class__.__name__)
        self.__json_out = dict()

    @property
    def class_name(self):
GromeTT's avatar
GromeTT committed
30
31
32
33
34
        """Sets __class_name to value.

        :param value: New class name

        """
Jiahang Chen's avatar
Jiahang Chen committed
35
36
37
38
        return self.__class_name

    @property
    def name(self):
GromeTT's avatar
GromeTT committed
39
40
41
42
43
44
45
        """Returns the object's name.

        :returns: Name of the object
        :rtype: str

        """

Jiahang Chen's avatar
Jiahang Chen committed
46
47
48
49
        return self.__name

    @name.setter
    def name(self, value):
GromeTT's avatar
GromeTT committed
50
51
52
53
54
55
        """Sets __name to value.

        :param value: New name

        """

Jiahang Chen's avatar
Jiahang Chen committed
56
57
58
59
        self.__name = value

    @property
    def identifier(self):
GromeTT's avatar
GromeTT committed
60
61
62
63
64
65
66
        """Returns the object's indentifier.

        :returns: Identifier
        :rtype: str

        """

Jiahang Chen's avatar
Jiahang Chen committed
67
68
69
70
        return self.__identifier

    @identifier.setter
    def identifier(self, value):
GromeTT's avatar
GromeTT committed
71
72
73
74
75
76
        """Builds an ID object from value and assigns the resulting identifier
        to __identifier.

        :param value: Proposal of the identifier
        """

Jiahang Chen's avatar
Jiahang Chen committed
77
78
79
80
81
82
83
84
        self.__identifier = ID(value).identifier

    def to_json(self):
        self.__json_out = {
            "class": self.__class_name,
            "identifier": self.__identifier,
        }
        return self.__json_out