Elffuss BraidHub

kikocis/GraphRAG-SDK public

423ab603a5a9 · 3.8 KiBRawPermalink
1class 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
25class 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 add_attribute(self, name:str, type:type, desc:str|None=None, unique:bool=False, mandatory:bool=False) -> None:
51 """
52 Add attribute to entity
53
54 Parameters:
55 name (str): attribute name
56 type (type): attribute type
57 desc (str): short description for attribute
58 unique (bool): is the attribute value unique
59 mandatory (bool): is the attribute mandatory
60 """
61
62 # Validate arguments
63 if not isinstance(name, str) or name == "":
64 raise Exception("Attribute name must be a none empty string")
65 #if not isinstance(type, type) or type not in [int, str, bool]:
66 if type not in [int, float, str, bool]:
67 raise Exception("Attribute type must be one of the supported types")
68 if not isinstance(unique, bool):
69 raise Exception("primary_key must be of type bool")
70 if name in self.attributes:
71 raise Exception(f"Entity already contains attribute: {name}")
72 if isinstance(desc, str) and desc == "":
73 raise Exception(f"Invalid argument, desc should be a non empty string")
74
75 desc = f"{self.name}'s {name}" if desc is None else desc
76 self.attributes.add(Attribute(name, type, desc, unique, mandatory))
77
78 def remove_attribute(self, name:str) ->None:
79 """
80 Remove attribute from entity
81
82 Parameters:
83 name (str): name of attribute to remove
84 """
85
86 # Validate argument
87 if not isinstance(name, str) or name == "":
88 raise Exception("Attribute name must be a none empty string")
89
90 for attr in self.attributes:
91 if attr.name == name:
92 self.attributes.remove(attr)
93 return
94
95 def get_attribute(self, name:str) -> Attribute|None:
96 """
97 Get attribute
98
99 Parameters:
100 name (str): name of attribute to get
101
102 Returns:
103 attribute
104 """
105
106 # Validate argument
107 if not isinstance(name, str) or name == "":
108 raise Exception("Attribute name must be a none empty string")
109
110 for attr in self.attributes:
111 if attr.name == name:
112 return attr
113
114 return None
115
116 def unique_attributes(self) -> list[Attribute]:
117 """
118 Get a list of Entity's unique attributes
119
120 Returns:
121 list of unique attributes
122 """
123
124 unique_attrs = []
125 for attr in self.attributes:
126 if attr.unique:
127 unique_attrs.append(attr)
128
129 return sorted(unique_attrs)