Elffuss BraidHub

kikocis/GraphRAG-SDK public

3034d86cfbbe · 1.2 KiBRawPermalink
1from .entity import Entity
2
3# Represents an ontolegy relation
4# A relation is composed of:
5# 1. name
6# 2. src entity
7# 3. destination entity
8class Relation(object):
9 def __init__(self, name:str, src:Entity, dest:Entity):
10 # Validate arguments
11 if not isinstance(name, str) or name == "":
12 raise Exception("Relation name must be a none empty string")
13 if not isinstance(src, Entity):
14 raise Exception("src must be am entity")
15 if not isinstance(dest, Entity):
16 raise Exception("dest must be am entity")
17
18 self.src = src
19 self.dest = dest
20 self.name = name
21
22 def __str__(self) -> str:
23 return f"(:{self.src.name})-[:{self.name}]->(:{self.dest.name})"
24
25 def __eq__(self, other) -> bool:
26 if not isinstance(other, Relation):
27 return False
28
29 return (self.src == other.src and
30 self.dest == other.dest and
31 self.name == other.name)
32
33 def __lt__(self, other):
34 return (self.src, self.name, self.dest) < (other.src, other.name, other.dest)
35
36 def __hash__(self) -> int:
37 return hash((self.src, self.name, self.dest))