From 330760f206a3701f6a43cf513657277842a61479 Mon Sep 17 00:00:00 2001
From: angloth <angus.lothian@hotmail.com>
Date: Mon, 2 Mar 2020 11:21:00 +0100
Subject: [PATCH] Remove GraphID class and replace it with a string, add
 funcitonality for finding a graph object by id

---
 b_asic/graph_id.py                       | 48 ++----------------
 b_asic/signal_flow_graph.py              | 21 ++++++--
 test/fixtures/graph_id.py                | 23 ---------
 test/graph_id/conftest.py                |  1 -
 test/graph_id/test_graph_id.py           | 64 ------------------------
 test/graph_id/test_graph_id_generator.py | 25 +++++----
 6 files changed, 35 insertions(+), 147 deletions(-)
 delete mode 100644 test/fixtures/graph_id.py
 delete mode 100644 test/graph_id/test_graph_id.py

diff --git a/b_asic/graph_id.py b/b_asic/graph_id.py
index 9eba69f5..c19f6fb0 100644
--- a/b_asic/graph_id.py
+++ b/b_asic/graph_id.py
@@ -6,6 +6,7 @@ TODO: More info
 from collections import defaultdict
 from typing import NewType, Union, DefaultDict
 
+GraphID = NewType("GraphID", str)
 GraphIDType = NewType("GraphIDType", str)
 GraphIDNumber = NewType("GraphIDNumber", int)
 
@@ -19,52 +20,11 @@ class GraphIDGenerator:
     def __init__(self):
         self._next_id_number = defaultdict(lambda: 1)       # Initalises every key element to 1
 
-    def get_next_id(self, graph_id_type: GraphIDType):
+    def get_next_id(self, graph_id_type: GraphIDType) -> GraphID:
         """
-        Retrns the next graph id for a certain graph id type.
+        Returns the next graph id for a certain graph id type.
         """
-        graph_id = GraphID(graph_id_type, self._next_id_number[graph_id_type])
+        graph_id = graph_id_type + str(self._next_id_number[graph_id_type])
         self._next_id_number[graph_id_type] += 1            # Increase the current id number
         return graph_id
 
-
-class GraphID:
-    """
-    Graph ID class that saves the id of for a graph object.
-
-    The ID consists of an id_type that is saved as a string and an 
-    id_number that is saved as an integer. 
-    """
-
-    graph_id_type: GraphIDType
-    graph_id_number: GraphIDNumber
-
-
-    def __init__(self, graph_id_type: GraphIDType, graph_id_number: GraphIDNumber):
-        self.graph_id_type = graph_id_type
-        self.graph_id_number = graph_id_number
-
-
-    def __str__(self) -> str:
-        return self.graph_id_type + str(self.graph_id_number)
-
-
-    def __repr__(self) -> str:
-        return str(self)
-
-
-    def __hash__(self) -> int:
-        return hash(str(self))
-
-
-    def __eq__(self, other: object) -> bool:
-        assert isinstance(other, GraphID), "Other object not an instance of GraphID"
-        return self.graph_id_type == other.graph_id_type and \
-            self.graph_id_number == other.graph_id_number
-
-    
-    def get_next_id(self) -> 'GraphID':
-        """
-        Returns a new GraphID of the same type with an incremented id number.
-        """ 
-        return GraphID(self.graph_id_type, self.graph_id_number + 1)
diff --git a/b_asic/signal_flow_graph.py b/b_asic/signal_flow_graph.py
index cbc29fa2..05e6ad66 100644
--- a/b_asic/signal_flow_graph.py
+++ b/b_asic/signal_flow_graph.py
@@ -11,14 +11,13 @@ from b_asic.graph_id import GraphIDGenerator, GraphID
 
 from typing import List, Dict, Union
 
-
 class SFG(BasicOperation):
 	"""
 	Signal flow graph.
 	TODO: More info.
 	"""
 
-	_graph_objects: Dict[GraphID, Union[Operation, Signal]]
+	_graph_objects_by_id: Dict[GraphID, Union[Operation, Signal]]
 	_graph_id_generator: GraphIDGenerator
 
 	def __init__(self, input_destinations: List[SignalDestination], output_sources: List[SignalSource]):
@@ -26,7 +25,7 @@ class SFG(BasicOperation):
 		super().__init__()
 		# TODO: Allocate input/output ports with appropriate IDs.
 		
-		self._graph_objects = dict # Map Operation ID to Operation objects
+		self._graph_objects_by_id = dict # Map Operation ID to Operation objects
 		self._graph_id_generator = GraphIDGenerator() 
 
 		# TODO: Traverse the graph between the inputs/outputs and add to self._operations.
@@ -55,9 +54,23 @@ class SFG(BasicOperation):
 		return self._add_graph_obj(signal, 'sig')
 
 
+	def find_by_id(self, graph_id: GraphID) -> Optional[Operation]:
+		"""Finds a graph object based on the entered Graph ID and returns it. If no graph
+		object with the entered ID was found then returns None.
+
+		Keyword arguments:
+		graph_id: Graph ID of the wanted object.
+		"""
+		if graph_id in self._graph_objects_by_id:
+			return self._graph_objects_by_id[graph_id]
+		else:
+			return None
+			
+
+
 	def _add_graph_obj(self, obj: Union[Operation, Signal], operation_id_type: str):
 		graph_id = self._graph_id_generator.get_next_id(operation_id_type)
-		self._graph_objects[graph_id] = obj
+		self._graph_objects_by_id[graph_id] = obj
 		return graph_id 
 
 
diff --git a/test/fixtures/graph_id.py b/test/fixtures/graph_id.py
deleted file mode 100644
index 8cc438a0..00000000
--- a/test/fixtures/graph_id.py
+++ /dev/null
@@ -1,23 +0,0 @@
-import pytest
-from b_asic import GraphID, GraphIDGenerator
-
-"""
-Use a fixture for initializing objects and pass them as argument to a test function
-"""
-@pytest.fixture
-def graph_id():
-    return GraphID("add", 1)
-
-
-@pytest.fixture
-def graph_ids():
-    return [GraphID("add", 1), GraphID("add", 2), \
-        GraphID("sig", 1), GraphID("sig", 1), GraphID("sub", 3), \
-        GraphID("ÅÄÖ", 2), GraphID("", 3), \
-        GraphID("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 4)]
-    
-
-@pytest.fixture
-def graph_id_generator():
-    return GraphIDGenerator()
-    
diff --git a/test/graph_id/conftest.py b/test/graph_id/conftest.py
index ba28d09a..5871ed8e 100644
--- a/test/graph_id/conftest.py
+++ b/test/graph_id/conftest.py
@@ -1,2 +1 @@
 import pytest
-from test.fixtures.graph_id import *
diff --git a/test/graph_id/test_graph_id.py b/test/graph_id/test_graph_id.py
deleted file mode 100644
index 9554c04c..00000000
--- a/test/graph_id/test_graph_id.py
+++ /dev/null
@@ -1,64 +0,0 @@
-"""
-B-ASIC test suite for GraphID.
-
-TODO:
-- Split test cases into more functions.
-- Add docstrings to test cases.
-"""
-
-from b_asic.graph_id import GraphID
-
-import pytest
-
-def test_create_graph_id():
-    """Tests creation of graph ID objects."""
-    graph_id = GraphID("add", 1)
-
-    assert graph_id.graph_id_type == "add"
-    assert graph_id.graph_id_number == 1
-
-def test_create_graph_id_empty_string():
-    graph_id2 = GraphID("", 2)
-
-    assert graph_id2.graph_id_type == ""
-    assert graph_id2.graph_id_number == 2
-
-def test_create_graph_id_long_string():
-    graph_id3 = GraphID("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 3)
-
-    assert graph_id3.graph_id_type == "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
-    assert graph_id3.graph_id_number == 3
-
-def test_graph_id_str(graph_ids):
-    """Test the str() function of graph ids."""
-    assert str(graph_ids[0]) == "add1"
-    assert str(graph_ids[1]) == "add2"
-    assert str(graph_ids[2]) == "sig1"
-    assert str(graph_ids[3]) == "sig1"
-    assert str(graph_ids[4]) == "sub3"
-    assert str(graph_ids[5]) == "ÅÄÖ2"
-    assert str(graph_ids[6]) == "3"
-    assert str(graph_ids[7]) == "ABCDEFGHIJKLMNOPQRSTUVWXYZ4"
-
-def test_graph_id_repr(graph_ids):
-    """Test the repr() function of graph ids."""
-    assert repr(graph_ids[0]) == "add1"
-    assert repr(graph_ids[1]) == "add2"
-    assert repr(graph_ids[2]) == "sig1"
-    assert repr(graph_ids[3]) == "sig1"
-    assert repr(graph_ids[4]) == "sub3"
-    assert repr(graph_ids[5]) == "ÅÄÖ2"
-    assert repr(graph_ids[6]) == "3"
-    assert repr(graph_ids[7]) == "ABCDEFGHIJKLMNOPQRSTUVWXYZ4"
-
-def test_graph_id_eq(graph_ids):
-    """Test the equality of graph ids.""" 
-    assert graph_ids[0] == graph_ids[0]
-    assert graph_ids[0] != graph_ids[1]
-    assert graph_ids[2] == graph_ids[3]
-    assert graph_ids[0] != graph_ids[2]
-    assert graph_ids[4] != graph_ids[6] 
-
-
-
-
diff --git a/test/graph_id/test_graph_id_generator.py b/test/graph_id/test_graph_id_generator.py
index 71eb02b0..7aeb6cad 100644
--- a/test/graph_id/test_graph_id_generator.py
+++ b/test/graph_id/test_graph_id_generator.py
@@ -6,21 +6,24 @@ from b_asic.graph_id import GraphIDGenerator, GraphID
 
 import pytest
 
-def test_empty_string_generator(graph_id_generator):
+def test_empty_string_generator():
     """Test the graph id generator for an empty string type."""
-    assert graph_id_generator.get_next_id("") == GraphID("", 1)
-    assert graph_id_generator.get_next_id("") == GraphID("", 2)
+    graph_id_generator = GraphIDGenerator()
+    assert graph_id_generator.get_next_id("") == "1"
+    assert graph_id_generator.get_next_id("") == "2"
 
 
-def test_normal_string_generator(graph_id_generator):
+def test_normal_string_generator():
     """"Test the graph id generator for a normal string type."""
-    assert graph_id_generator.get_next_id("add") == GraphID("add", 1)
-    assert graph_id_generator.get_next_id("add") == GraphID("add", 2)
+    graph_id_generator = GraphIDGenerator()
+    assert graph_id_generator.get_next_id("add") == "add1"
+    assert graph_id_generator.get_next_id("add") == "add2"
 
-def test_different_strings_generator(graph_id_generator):
+def test_different_strings_generator():
     """Test the graph id generator for different strings."""
-    assert graph_id_generator.get_next_id("sub") == GraphID("sub", 1)
-    assert graph_id_generator.get_next_id("mul") == GraphID("mul", 1)
-    assert graph_id_generator.get_next_id("sub") == GraphID("sub", 2)
-    assert graph_id_generator.get_next_id("mul") == GraphID("mul", 2)
+    graph_id_generator = GraphIDGenerator()
+    assert graph_id_generator.get_next_id("sub") == "sub1"
+    assert graph_id_generator.get_next_id("mul") == "mul1"
+    assert graph_id_generator.get_next_id("sub") == "sub2"
+    assert graph_id_generator.get_next_id("mul") == "mul2"
     
\ No newline at end of file
-- 
GitLab