From d73a3b901f62986970456d140c2b22e1455799ad Mon Sep 17 00:00:00 2001
From: Mikael Henriksson <mike.zx@hotmail.com>
Date: Mon, 8 May 2023 13:50:23 +0200
Subject: [PATCH] resources.py: add add_process and remove_process methods to
 ProcessCollection

---
 b_asic/resources.py    | 22 ++++++++++++++++++++--
 test/test_resources.py | 17 +++++++++++++++--
 2 files changed, 35 insertions(+), 4 deletions(-)

diff --git a/b_asic/resources.py b/b_asic/resources.py
index 1d2bb49f..bdf330ce 100644
--- a/b_asic/resources.py
+++ b/b_asic/resources.py
@@ -443,7 +443,7 @@ class ProcessCollection:
         return self._schedule_time
 
     def __len__(self):
-        return len(self._collection)
+        return len(self.collection)
 
     def add_process(self, process: Process):
         """
@@ -454,7 +454,25 @@ class ProcessCollection:
         process : Process
             The process object to be added to the collection.
         """
-        self._collection.add(process)
+        self.collection.add(process)
+
+    def remove_process(self, process: Process):
+        """
+        Remove a processes from this process collection.
+
+        Raises KeyError if the process is not in this collection.
+
+        Parameters
+        ----------
+        process : Process
+            The processes object to remove from this collection
+        """
+        if process not in self.collection:
+            raise KeyError(
+                f"Can't remove process: '{process}', as it is not in collection."
+            )
+        else:
+            self.collection.remove(process)
 
     def plot(
         self,
diff --git a/test/test_resources.py b/test/test_resources.py
index 839347f9..6248c41c 100644
--- a/test/test_resources.py
+++ b/test/test_resources.py
@@ -37,8 +37,8 @@ class TestProcessCollectionPlainMemoryVariable:
         fig, ax = plt.subplots(1, 2)
         assignment = simple_collection.left_edge_cell_assignment()
         for cell in assignment:
-            assignment[cell].plot(ax=ax[1], row=cell)
-        simple_collection.plot(ax[0])
+            assignment[cell].plot(ax=ax[1], row=cell)  # type: ignore
+        simple_collection.plot(ax[0])  # type:ignore
         return fig
 
     def test_cell_assignment_matrix_transposer(self):
@@ -132,3 +132,16 @@ class TestProcessCollectionPlainMemoryVariable:
 
     def test_show(self, simple_collection: ProcessCollection):
         simple_collection.show()
+
+    def test_add_remove_process(self, simple_collection: ProcessCollection):
+        new_proc = PlainMemoryVariable(1, 0, {0: 3})
+        assert len(simple_collection) == 7
+        assert new_proc not in simple_collection
+
+        simple_collection.add_process(new_proc)
+        assert len(simple_collection) == 8
+        assert new_proc in simple_collection
+
+        simple_collection.remove_process(new_proc)
+        assert len(simple_collection) == 7
+        assert new_proc not in simple_collection
-- 
GitLab