| 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 | # Represents an ontolegy entity |
| 26 | class Entity(object): |
| 27 | def __init__(self, name:str): |
| 28 | if not isinstance(name, str) or name == "": |
| 29 | raise Exception("Entity name must be a none empty string") |
| 30 | |
| 31 | self.name = name |
| 32 | self.attributes = set([]) |
| 33 | |
| 34 | def __eq__(self, other) -> bool: |
| 35 | if not isinstance(other, Entity): |
| 36 | return False |
| 37 | |
| 38 | return (self.name == other.name and self.attributes == other.attributes) |
| 39 | |
| 40 | # Add attribute to entity |
| 41 | def add_attribute(self, name:str, type:type, desc=None, unique=False, mandatory=False): |
| 42 | # Validate arguments |
| 43 | if not isinstance(name, str) or name == "": |
| 44 | raise Exception("Attribute name must be a none empty string") |
| 45 | #if not isinstance(type, type) or type not in [int, str, bool]: |
| 46 | if type not in [int, float, str, bool]: |
| 47 | raise Exception("Attribute type must be one of the supported types") |
| 48 | if not isinstance(unique, bool): |
| 49 | raise Exception("primary_key must be of type bool") |
| 50 | if name in self.attributes: |
| 51 | raise Exception(f"Entity already contains attribute: {name}") |
| 52 | if isinstance(desc, str) and desc == "": |
| 53 | raise Exception(f"Invalid argument, desc should be a non empty string") |
| 54 | |
| 55 | desc = f"{self.name}'s {name}" if desc is None else desc |
| 56 | self.attributes.add(Attribute(name, type, desc, unique, mandatory)) |
| 57 | |
| 58 | # Remove attribute from entity |
| 59 | def remove_attribute(self, name:str): |
| 60 | # Validate argument |
| 61 | if not isinstance(name, str) or name == "": |
| 62 | raise Exception("Attribute name must be a none empty string") |
| 63 | |
| 64 | for attr in self.attributes: |
| 65 | if attr.name == name: |
| 66 | self.attributes.remove(attr) |
| 67 | return |
| 68 | |
| 69 | def get_attribute(self, name:str): |
| 70 | # Validate argument |
| 71 | if not isinstance(name, str) or name == "": |
| 72 | raise Exception("Attribute name must be a none empty string") |
| 73 | |
| 74 | for attr in self.attributes: |
| 75 | if attr.name == name: |
| 76 | return attr |
| 77 | |
| 78 | return None |
| 79 | |
| 80 | # returns a list of Entity's unique attributes |
| 81 | def unique_attributes(self): |
| 82 | unique_attrs = [] |
| 83 | for attr in self.attributes: |
| 84 | if attr.unique: |
| 85 | unique_attrs.append(attr) |
| 86 | |
| 87 | return sorted(unique_attrs) |