Skip to content
Snippets Groups Projects
Commit 330760f2 authored by angloth's avatar angloth
Browse files

Remove GraphID class and replace it with a string, add funcitonality for...

Remove GraphID class and replace it with a string, add funcitonality for finding a graph object by id
parent c6d6d8b5
No related branches found
No related tags found
1 merge request!2Integrated ID system, traversing and som signal tests
......@@ -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)
......@@ -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
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()
import pytest
from test.fixtures.graph_id import *
"""
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]
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment