Skip to content

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

Merged
merged 7 commits into from
Nov 3, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions redisgraph/__init__.py
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
53 changes: 53 additions & 0 deletions redisgraph/path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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([], [])

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
Copy link
Contributor

Choose a reason for hiding this comment

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

self.append_type = type(Edge) seems more aligned with the naming.
but then assert(isinstance(node, self.append_type)) should be updated to
assert(type(node) == self.append_type)

return self

def add_edge(self, edge):
assert(isinstance(edge, self.append_type))
Copy link
Contributor

Choose a reason for hiding this comment

The 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
10 changes: 10 additions & 0 deletions redisgraph/query_result.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .node import Node
from .edge import Edge
from .path import Path
from prettytable import PrettyTable
from redis import ResponseError

Expand All @@ -21,6 +22,7 @@ class ResultSetScalarTypes(object):
VALUE_ARRAY = 6
VALUE_EDGE = 7
VALUE_NODE = 8
VALUE_PATH = 9

class QueryResult(object):
LABELS_ADDED = 'Labels added'
Expand Down Expand Up @@ -124,6 +126,11 @@ def parse_edge(self, cell):
properties = self.parse_entity_properties(cell[4])
return Edge(src_node_id, relation, dest_node_id, edge_id=edge_id, properties=properties)

def parse_path(self, cell):
nodes = self.parse_scalar(cell[0])
edges = self.parse_scalar(cell[1])
return Path(nodes, edges)

def parse_scalar(self, cell):
scalar_type = int(cell[0])
value = cell[1]
Expand Down Expand Up @@ -162,6 +169,9 @@ def parse_scalar(self, cell):
elif scalar_type == ResultSetScalarTypes.VALUE_EDGE:
scalar = self.parse_edge(value)

elif scalar_type == ResultSetScalarTypes.VALUE_PATH:
scalar = self.parse_path(value)

elif scalar_type == ResultSetScalarTypes.VALUE_UNKNOWN:
print("Unknown scalar type\n")

Expand Down
30 changes: 30 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.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()