|
| 1 | +import copy |
| 2 | + |
| 3 | + |
| 4 | +from visualiser import Node |
| 5 | +class Edge: |
| 6 | + |
| 7 | + def __init__(self, source_node: Node, destination_node: Node, label: str = '', |
| 8 | + **attrs: str) -> None: |
| 9 | + # TODO: Remove copy after finding a better way to do this. |
| 10 | + self._source_node = copy.deepcopy(source_node) |
| 11 | + self._destination_node = copy.deepcopy(destination_node) |
| 12 | + |
| 13 | + self._label = label |
| 14 | + self._attrs = attrs |
| 15 | + |
| 16 | + @property |
| 17 | + def label(self) -> str: |
| 18 | + """ |
| 19 | + Get label for edge. |
| 20 | + :return: str |
| 21 | + """ |
| 22 | + return self._label |
| 23 | + |
| 24 | + @label.setter |
| 25 | + def label(self, _label: str) -> None: |
| 26 | + """ |
| 27 | + Set label for edge. |
| 28 | + :param _label: str |
| 29 | + """ |
| 30 | + self._label = _label |
| 31 | + |
| 32 | + @property |
| 33 | + def source_node(self) -> Node: |
| 34 | + """ |
| 35 | + Get source node |
| 36 | + :return: Node |
| 37 | + """ |
| 38 | + return self._source_node |
| 39 | + |
| 40 | + @source_node.setter |
| 41 | + def source_node(self, _source_node: Node) -> None: |
| 42 | + """ |
| 43 | + Set source node. |
| 44 | + :param _source_node: Node |
| 45 | + """ |
| 46 | + self._source_node = _source_node |
| 47 | + |
| 48 | + @property |
| 49 | + def destination_node(self) -> Node: |
| 50 | + """ |
| 51 | + Get destination node. |
| 52 | + :return: Node |
| 53 | + """ |
| 54 | + return self._destination_node |
| 55 | + |
| 56 | + @destination_node.setter |
| 57 | + def destination_node(self, _destination_node: Node) -> None: |
| 58 | + """ |
| 59 | + Sets destination node. |
| 60 | + :param _destination_node: Node |
| 61 | + """ |
| 62 | + self._destination_node = _destination_node |
| 63 | + |
| 64 | + def get_attribute(self, key: str) -> str: |
| 65 | + """ |
| 66 | + Get attribute for edge. |
| 67 | + :param key: str |
| 68 | + :return: The value of attribute with key |
| 69 | + """ |
| 70 | + return self._attrs.get(key) |
| 71 | + |
| 72 | + def set_attribute(self, key: str, value: str) -> None: |
| 73 | + """ |
| 74 | + Set attribute for edge |
| 75 | + :param key: str |
| 76 | + :param value: str |
| 77 | + """ |
| 78 | + self._attrs[key] = value |
| 79 | + |
| 80 | + def remove_attribute(self, key: str) -> None: |
| 81 | + """ |
| 82 | + Remove attribute from edge. |
| 83 | + :param key: str |
| 84 | + """ |
| 85 | + del self._attrs[key] |
| 86 | + |
| 87 | + def get_attributes_string(self) -> str: |
| 88 | + """ |
| 89 | + Get attributes string enclosed in [] |
| 90 | + :return: |
| 91 | + """ |
| 92 | + if len(self._label) == 0: |
| 93 | + return '[' + ', '.join([f'{key}="{value}"' for key, value in self._attrs.items()]) + ']' |
| 94 | + |
| 95 | + return '[' + f'label="{self._label}", ' + ', '.join( |
| 96 | + [f'{key}="{value}"' for key, value in self._attrs.items()]) + ']' |
| 97 | + |
| 98 | + def to_string(self) -> str: |
| 99 | + """ |
| 100 | + Converts dot string equivalent of the current edge. |
| 101 | + :return: Str |
| 102 | + """ |
| 103 | + return f'{self._source_node.name} -> {self._destination_node.name} {self.get_attributes_string()}' |
0 commit comments