Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for multigraphs #42

Merged
merged 10 commits into from
May 14, 2024
Merged

Conversation

jackboyla
Copy link
Contributor

@jackboyla jackboyla commented May 9, 2024

hello again👋 adding support for multigraphs this time

from grandcypher import GrandCypher
import networkx as nx

host = nx.MultiDiGraph()
host.add_node("a", name="Alice", age=30)
host.add_node("b", name="Bob", age=40)
host.add_node("c", name="Charlie", age=50)
host.add_edge("a", "b", __labels__={"friend"}, years=3)
host.add_edge("a", "c", __labels__={"colleague"}, years=10)
host.add_edge("a", "c", __labels__={"parent"}, duration='forever')
host.add_edge("b", "c", __labels__={"colleague"}, duration=10)
host.add_edge("b", "c", __labels__={"mentor"}, years=2)

qry = """
MATCH (a)-[r]->(b)
RETURN a.name, b.name, r.__labels__, r.duration
"""
res = GrandCypher(host).run(qry)
print(res)

'''
{'a.name': ['Alice', 'Alice', 'Bob'], 
'b.name': ['Bob', 'Charlie', 'Charlie'], 
'r.__labels__': [{0: {'friend'}}, {0: {'colleague'}, 1: {'parent'}}, {0: {'colleague'}, 1: {'mentor'}}], 
'r.duration': [{0: None}, {0: None, 1: 'forever'}, {0: 10, 1: None}]}
'''

This implementation deviates from standard Cypher in the output, as it provides nested relations + relation attributes e.g
GrandCypher returns:

{'a.name': ['Alice', 'Alice', 'Bob'], 
'b.name': ['Bob', 'Charlie', 'Charlie'], 
'r.__labels__': [{0: {'friend'}}, {0: {'colleague'}, 1: {'parent'}}, {0: {'colleague'}, 1: {'mentor'}}], 
'r.duration': [{0: None}, {0: None, 1: 'forever'}, {0: 10, 1: None}]}

when Neo4j returns the expanded version:

{'a.name': ['Alice', 'Alice', 'Alice ,'Bob', 'Bob'], 
'b.name': ['Bob', 'Charlie', 'Charlie', 'Charlie', 'Charlie'], 
'r.__labels__': [{0: {'friend'}}, {0: {'colleague'}}, {0: {'parent'}}, {0: {'colleague'}}, {0: {'mentor'}}], 
'r.duration': [{0: None}, {0: None}, {0: 'forever'}, {0: 10}, {0: None}]}

@jackboyla
Copy link
Contributor Author

bump 😇 @j6k4m8

@j6k4m8
Copy link
Member

j6k4m8 commented May 13, 2024

sorry @jackboyla -- just getting back from vacation today!

this is SO awesome and totally will be a force-multiplier for this library! doing some testing locally on this right now and will report back!

@jackboyla
Copy link
Contributor Author

no problem! 😁

Copy link
Member

@j6k4m8 j6k4m8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is so great, really thoughtful implementation!

@j6k4m8
Copy link
Member

j6k4m8 commented May 14, 2024

Amazing @jackboyla, LGTM! (I'll add you to the changelog/readme "thanks" if you don't do it yourself! ;) )

@j6k4m8 j6k4m8 merged commit 78173a3 into aplbrain:master May 14, 2024
6 checks passed
@jackboyla jackboyla deleted the support-multigraph branch May 15, 2024 06:56
@jackboyla
Copy link
Contributor Author

Cheers @j6k4m8 ! glad I could help😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants