def create_graph(connection_string: str): gh = SQErzoGraph(connection_string) gh.truncate() # Drop database # Creates 20 relations for n in range(20): u1 = UserNode(name=f"UName-{n}") gh.save(u1) d1 = UserNode(name=f"DName-{n}") gh.save(d1) u1_meet_g1 = MeetEdge(source=u1, destination=d1) gh.save(u1_meet_g1)
def create_graph(connection_string: str, count: int = 500): gh = SQErzoGraph(connection_string) gh.truncate() # Drop database with gh.transaction() as tx: for n in range(count): u1 = UserNode(name=f"UName-{n}") d1 = UserNode(name=f"DName-{n}") tx.add(u1) tx.add(d1) u1_meet_g1 = MeetEdge(source=u1, destination=d1) tx.add(u1_meet_g1)
def create_graph(connection_string: str, nodes_count = 500): gh = SQErzoGraph(connection_string) gh.truncate() # Drop database nodes = [] # # Add some data and relations: User1 -[meet]-> User 2 # with gh.transaction() as tx: for n in range(nodes_count): u1_name = f"uname{n}" d1_name = f"dname{n}" u1 = UserNode(name=u1_name, email=f"{u1_name}@{u1_name}.com") d1 = UserNode(name=d1_name, email=f"{d1_name}@{d1_name}.com") nodes.append(u1) nodes.append(d1) tx.add(u1) tx.add(d1) u2_meet_u1 = MeetEdge( source=u1, destination=d1 ) u1_meet_u2 = MeetEdge( source=d1, destination=u1 ) tx.add(u1_meet_u2) tx.add(u2_meet_u1) # # Add some works relations: User 1 -[WorksWith]-> User 2 # with gh.transaction() as tx: for i in range(0, len(nodes), 4): u1 = nodes[i] u2 = nodes[i + 2] work_1 = WorksWithEdge( source=u1, destination=u2 ) work_2 = WorksWithEdge( source=u2, destination=u1 ) tx.add(work_1) tx.add(work_2) q = gh.Query.raw( "match (u1:User)-[:Meet]->(u2:User) return u1, u2" ).execute(map_to={"u1": UserNode, "u2": UserNode}) print(q)
def create_graph(connection_string: str): gh = SQErzoGraph(connection_string) gh.truncate() # Drop database user_names = [] with gh.transaction() as tx: for n in range(5): u1_name = f"UName-{n}" user_names.append(u1_name) u1 = UserNode(name=u1_name) d1 = UserNode(name=f"DName-{n}") tx.add(u1) tx.add(d1) u1_meet_g1 = MeetEdge(source=u1, destination=d1) tx.add(u1_meet_g1) for name in user_names: node = gh.fetch_one(UserNode, name=name) print(node)
def create_graph(database_path: str, db_type: str, count: int = 50): gh = SQErzoGraph() gh.truncate() # # Clean the graph # print("[*] Cleaning graph") with open(database_path, "rb") as f: mail_database = pickle.load(f) # ------------------------------------------------------------------------- # get_or_create each email # ------------------------------------------------------------------------- # [ (source identity, in_reply) ] in_reply_rel = [] print("[*] Building graph... ") c = 1 total = count or len(mail_database) for mail_id, user_email in mail_database.items(): if c > count: break # ------------------------------------------------------------------------- # Stores mail # ------------------------------------------------------------------------- email = MailNode(subject=user_email["Subject"], date=user_email["Date"], message_id=user_email["Message-Id"]) gh.get_or_create(email) # # Check if mail is a reply of other, link them # if reply_to := user_email.get("In-Reply-To", None): in_reply_rel.append((email, reply_to)) from_person = PersonNode(name=user_email["From"]["name"], email=user_email["From"]["email"]) gh.get_or_create(from_person) # ------------------------------------------------------------------------- # From Person -[Sent]-> Email # ------------------------------------------------------------------------- from_rel_mail = SentEdge( source=from_person, destination=email, ) gh.get_or_create(from_rel_mail) # ------------------------------------------------------------------------- # Stores To # ------------------------------------------------------------------------- for to in user_email["To"]: to_person = PersonNode(name=to["name"], email=to["email"]) gh.get_or_create(to_person) # ------------------------------------------------------------------------- # Email -[To]-> Person # ------------------------------------------------------------------------- to_rel_mail = ToEdge(source=email, destination=to_person) gh.get_or_create(to_rel_mail) # ------------------------------------------------------------------------- # Stores Mail CC # ------------------------------------------------------------------------- for user_to in user_email.get("CC", []): p = PersonNode(email=user_to["email"], name=user_to["name"]) gh.get_or_create(p) # ------------------------------------------------------------------------- # Email -[CC]-> Person # ------------------------------------------------------------------------- to_rel = CCEdge(source=email, destination=p) gh.get_or_create(to_rel) print(f"\r[*] Processed: {c}/{total}", end='', flush=True) c += 1
def create_graph(connection_string: str): gh = SQErzoGraph(connection_string) gh.truncate() # Drop database u1 = UserNode(name=f"UName-11") gh.save(u1) d1 = UserNode(name=f"DName-12") gh.save(d1) u1_meet_g1 = MeetEdge(source=u1, destination=d1) gh.save(u1_meet_g1) g1_meet_u1 = MeetEdge(source=d1, destination=u1) gh.save(g1_meet_u1)
def create_graph(connection_string: str): gh = SQErzoGraph(connection_string) gh.truncate() # Drop database nodes_ids = [] # Insert some nodes / relations for n in range(20): u1 = UserNode(name=f"UName-{n}") u2 = UserNode(name=f"UName-two{n}") gh.save(u1) gh.save(u2) nodes_ids.append(u1.make_identity()) try: gh.save(u2) except SQErzoElementExistException: #When you insert the same node twice an exception will raise pass u1_meet_g1 = MeetEdge( source=u1, destination=u2 ) gh.save(u1_meet_g1) # Update some nodes for node_id in nodes_ids: n: UserNode = gh.get_node_by_id(node_id, UserNode) gh.update(n)