-
Notifications
You must be signed in to change notification settings - Fork 49
add support for path type #53
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
Changes from 2 commits
529502e
24abf0a
229b3d6
8fa3bdf
7db8259
4a9bf18
a01df09
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .node import Node | ||
from .edge import Edge | ||
from .graph import Graph | ||
from .path import Path |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from .node import Node | ||
from .edge import Edge | ||
|
||
class Path(object): | ||
|
||
def __init__(self, nodes, edges): | ||
assert(isinstance(nodes, list) and isinstance(edges, list)) | ||
self.nodes = nodes | ||
self.edges = edges | ||
self.append_type = Node | ||
|
||
@classmethod | ||
def new_empty_path(cls): | ||
return cls([], []) | ||
|
||
@classmethod | ||
def new_path(cls, nodes, edges): | ||
return cls(nodes, edges) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this function, use constructor. |
||
def nodes(self): | ||
return self.nodes | ||
|
||
def edges(self): | ||
return self.edges | ||
|
||
def get_node(self, index): | ||
return self.nodes[index] | ||
|
||
def get_relationship(self, index): | ||
return self.edges[index] | ||
|
||
def first_node(self): | ||
return self.nodes[0] | ||
|
||
def last_node(self): | ||
return self.nodes[-1] | ||
|
||
def edge_count(self): | ||
return len(self.edges) | ||
|
||
def nodes_count(self): | ||
return len(self.nodes) | ||
|
||
def add_node(self, node): | ||
assert(isinstance(node, self.append_type)) | ||
self.nodes.append(node) | ||
self.append_type = Edge | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return self | ||
|
||
def add_edge(self, edge): | ||
assert(isinstance(edge, self.append_type)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above comment. |
||
self.edges.append(edge) | ||
self.append_type = Node | ||
return self | ||
|
||
def __eq__(self, other): | ||
return self.nodes == other.nodes and self.edges == other.edges |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,5 +65,35 @@ def test_array_functions(self): | |
# All done, remove graph. | ||
redis_graph.delete() | ||
|
||
|
||
def test_path(self): | ||
redis_graph = Graph('social', self.r) | ||
|
||
node0 = Node(node_id=0, label="L1") | ||
node1 = Node(node_id=1, label="L1") | ||
node2 = Node(node_id=2, label="L1") | ||
edge01 = Edge(node0, "R1", node1, edge_id=0, properties={'value': 1}) | ||
edge12 = Edge(node1, "R1", node2, edge_id=1, properties={'value': 2}) | ||
|
||
redis_graph.add_node(node0) | ||
redis_graph.add_node(node1) | ||
redis_graph.add_node(node2) | ||
redis_graph.add_edge(edge01) | ||
redis_graph.add_edge(edge12) | ||
|
||
redis_graph.commit() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. .flush() |
||
|
||
path01 = Path.new_empty_path().add_node(node0).add_edge(edge01).add_node(node1) | ||
path12 = Path.new_empty_path().add_node(node1).add_edge(edge12).add_node(node2) | ||
expected_results = [[path01], [path12]] | ||
|
||
query = "MATCH p=(:L1)-[:R1]->(:L1) RETURN p" | ||
result = redis_graph.query(query) | ||
self.assertEqual(expected_results, result.result_set) | ||
|
||
# All done, remove graph. | ||
redis_graph.delete() | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Uh oh!
There was an error while loading. Please reload this page.