| 1 | class Attribute(object): |
| 2 | def __init__(self, name:str, type:type, desc=None, unique=False, mandatory=False): |
| 3 | self.name = name |
| 4 | self.type = type |
| 5 | self.desc = desc |
| 6 | self.unique = unique |
| 7 | self.mandatory = mandatory |
| 8 | |
| 9 | def __eq__(self, other) -> bool: |
| 10 | if not isinstance(other, Attribute): |
| 11 | return False |
| 12 | |
| 13 | return (self.name == other.name and |
| 14 | self.type == other.type and |
| 15 | self.desc == other.desc and |
| 16 | self.unique == other.unique and |
| 17 | self.mandatory == other.mandatory) |
| 18 | |
| 19 | def __lt__(self, other): |
| 20 | return self.name < other.name |
| 21 | |
| 22 | def __hash__(self) -> int: |
| 23 | return hash(self.name) |
| 24 | |
| 25 | class Entity(object): |
| 26 | """ |
| 27 | Represents an ontolegy entity |
| 28 | """ |
| 29 | |
| 30 | def __init__(self, name:str): |
| 31 | """ |
| 32 | Initialize Entity |
| 33 | |
| 34 | Parameters: |
| 35 | name (str): name of entity |
| 36 | """ |
| 37 | |
| 38 | if not isinstance(name, str) or name == "": |
| 39 | raise Exception("Entity name must be a none empty string") |
| 40 | |
| 41 | self.name = name |
| 42 | self.attributes = set([]) |
| 43 | |
| 44 | def __eq__(self, other) -> bool: |
| 45 | if not isinstance(other, Entity): |
| 46 | return False |
| 47 | |
| 48 | return (self.name == other.name and self.attributes == other.attributes) |
| 49 | |
| 50 | def __hash__(self): |
| 51 | return hash(self.name) |
| 52 | |
| 53 | def __str__(self): |
| 54 | return f"(:{self.name})" |
| 55 | |
| 56 | def add_attribute(self, name:str, type:type, desc:str|None=None, unique:bool=False, mandatory:bool=False) -> 'Entity': |
| 57 | """ |
| 58 | Add attribute to entity |
| 59 | |
| 60 | Parameters: |
| 61 | name (str): attribute name |
| 62 | type (type): attribute type |
| 63 | desc (str): short description for attribute |
| 64 | unique (bool): is the attribute value unique |
| 65 | mandatory (bool): is the attribute mandatory |
| 66 | |
| 67 | Returns: |
| 68 | Entity |
| 69 | """ |
| 70 | |
| 71 | # Validate arguments |
| 72 | if not isinstance(name, str) or name == "": |
| 73 | raise Exception("Attribute name must be a none empty string") |
| 74 | #if not isinstance(type, type) or type not in [int, str, bool]: |
| 75 | if type not in [int, float, str, bool]: |
| 76 | raise Exception("Attribute type must be one of the supported types") |
| 77 | if not isinstance(unique, bool): |
| 78 | raise Exception("primary_key must be of type bool") |
| 79 | if name in self.attributes: |
| 80 | raise Exception(f"Entity already contains attribute: {name}") |
| 81 | if isinstance(desc, str) and desc == "": |
| 82 | raise Exception(f"Invalid argument, desc should be a non empty string") |
| 83 | |
| 84 | desc = f"{self.name}'s {name}" if desc is None else desc |
| 85 | self.attributes.add(Attribute(name, type, desc, unique, mandatory)) |
| 86 | |
| 87 | return self |
| 88 | |
| 89 | def remove_attribute(self, name:str) -> 'Entity': |
| 90 | """ |
| 91 | Remove attribute from entity |
| 92 | |
| 93 | Parameters: |
| 94 | name (str): name of attribute to remove |
| 95 | |
| 96 | Returns: |
| 97 | Entity |
| 98 | """ |
| 99 | |
| 100 | # Validate argument |
| 101 | if not isinstance(name, str) or name == "": |
| 102 | raise Exception("Attribute name must be a none empty string") |
| 103 | |
| 104 | for attr in self.attributes: |
| 105 | if attr.name == name: |
| 106 | self.attributes.remove(attr) |
| 107 | break |
| 108 | |
| 109 | return self |
| 110 | |
| 111 | def get_attribute(self, name:str) -> Attribute|None: |
| 112 | """ |
| 113 | Get attribute |
| 114 | |
| 115 | Parameters: |
| 116 | name (str): name of attribute to get |
| 117 | |
| 118 | Returns: |
| 119 | attribute |
| 120 | """ |
| 121 | |
| 122 | # Validate argument |
| 123 | if not isinstance(name, str) or name == "": |
| 124 | raise Exception("Attribute name must be a none empty string") |
| 125 | |
| 126 | for attr in self.attributes: |
| 127 | if attr.name == name: |
| 128 | return attr |
| 129 | |
| 130 | return None |
| 131 | |
| 132 | def unique_attributes(self) -> list[Attribute]: |
| 133 | """ |
| 134 | Get a list of Entity's unique attributes |
| 135 | |
| 136 | Returns: |
| 137 | list of unique attributes |
| 138 | """ |
| 139 | |
| 140 | unique_attrs = [] |
| 141 | for attr in self.attributes: |
| 142 | if attr.unique: |
| 143 | unique_attrs.append(attr) |
| 144 | |
| 145 | return sorted(unique_attrs) |