|
| 1 | +from util import * |
| 2 | + |
| 3 | +class Edge(object): |
| 4 | + """ |
| 5 | + An edge connecting two nodes. |
| 6 | + """ |
| 7 | + def __init__(self, src_node, relation, dest_node, edge_id=None, properties=None): |
| 8 | + """ |
| 9 | + Create a new edge. |
| 10 | + """ |
| 11 | + assert src_node is not None and dest_node is not None |
| 12 | + |
| 13 | + self.id = edge_id |
| 14 | + self.relation = '' or relation |
| 15 | + self.properties = {} or properties |
| 16 | + self.src_node = src_node |
| 17 | + self.dest_node = dest_node |
| 18 | + |
| 19 | + def toString(self): |
| 20 | + res = "" |
| 21 | + if self.properties: |
| 22 | + props = ','.join(key+':'+str(quote_string(val)) for key, val in self.properties.items()) |
| 23 | + res += '{' + props + '}' |
| 24 | + |
| 25 | + return res |
| 26 | + |
| 27 | + def __str__(self): |
| 28 | + # Source node. |
| 29 | + res = '(' + self.src_node.alias + ')' |
| 30 | + |
| 31 | + # Edge |
| 32 | + res += "-[" |
| 33 | + if self.relation: |
| 34 | + res += ":" + self.relation |
| 35 | + if self.properties: |
| 36 | + props = ','.join(key+':'+str(quote_string(val)) for key, val in self.properties.items()) |
| 37 | + res += '{' + props + '}' |
| 38 | + res += ']->' |
| 39 | + |
| 40 | + # Dest node. |
| 41 | + res += '(' + self.dest_node.alias + ')' |
| 42 | + |
| 43 | + return res |
| 44 | + |
| 45 | + def __eq__(self, rhs): |
| 46 | + # Quick positive check, if both IDs are set. |
| 47 | + if self.id is not None and rhs.id is not None and self.id == rhs.id: |
| 48 | + return True |
| 49 | + |
| 50 | + # Source and destination nodes should match. |
| 51 | + if self.src_node != rhs.src_node: |
| 52 | + return False |
| 53 | + |
| 54 | + if self.dest_node != rhs.dest_node: |
| 55 | + return False |
| 56 | + |
| 57 | + # Relation should match. |
| 58 | + if self.relation != rhs.relation: |
| 59 | + return False |
| 60 | + |
| 61 | + # Quick check for number of properties. |
| 62 | + if len(self.properties) != len(rhs.properties): |
| 63 | + return False |
| 64 | + |
| 65 | + # Compare properties. |
| 66 | + if self.properties != rhs.properties: |
| 67 | + return False |
| 68 | + |
| 69 | + return True |
0 commit comments