Skip to content

Commit 1fe14db

Browse files
committed
Update Connection to ComponentConnection
To match the new component graph filtering arguments for `components()`, the `connections()` method now also have its argument names updated: * `start` -> `matching_sources` * `end` -> `matching_destinations` Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 055da47 commit 1fe14db

File tree

8 files changed

+442
-293
lines changed

8 files changed

+442
-293
lines changed

src/frequenz/sdk/microgrid/component_graph.py

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
Chp,
3636
Component,
3737
ComponentCategory,
38-
Connection,
38+
ComponentConnection,
3939
EvCharger,
4040
GridConnectionPoint,
4141
Inverter,
@@ -83,18 +83,18 @@ def components(
8383
@abstractmethod
8484
def connections(
8585
self,
86-
start: set[ComponentId] | None = None,
87-
end: set[ComponentId] | None = None,
88-
) -> set[Connection]:
86+
matching_sources: set[ComponentId] | None = None,
87+
matching_destinations: set[ComponentId] | None = None,
88+
) -> set[ComponentConnection]:
8989
"""Fetch the connections between microgrid components.
9090
9191
Args:
92-
start: The component IDs that the connections' start must match.
93-
end: The component IDs that the connections' end must match.
92+
matching_sources: The component IDs the connections' source must match.
93+
matching_destinations: The component IDs the connections' destination must match.
9494
9595
Returns:
9696
The set of connections between components in the microgrid, filtered by
97-
the provided `start`/`end` choices.
97+
the provided `matching_sources` and `matching_destinations` choices.
9898
"""
9999

100100
@abstractmethod
@@ -355,7 +355,7 @@ class _MicrogridComponentGraph(
355355
def __init__(
356356
self,
357357
components: set[Component] | None = None,
358-
connections: set[Connection] | None = None,
358+
connections: set[ComponentConnection] | None = None,
359359
) -> None:
360360
"""Initialize the component graph.
361361
@@ -433,32 +433,33 @@ def components(
433433
@override
434434
def connections(
435435
self,
436-
start: set[ComponentId] | None = None,
437-
end: set[ComponentId] | None = None,
438-
) -> set[Connection]:
436+
matching_sources: set[ComponentId] | None = None,
437+
matching_destinations: set[ComponentId] | None = None,
438+
) -> set[ComponentConnection]:
439439
"""Fetch the connections between microgrid components.
440440
441441
Args:
442-
start: The component IDs that the connections' start must match.
443-
end: The component IDs that the connections' end must match.
442+
matching_sources: The component IDs that the connections' source must match.
443+
matching_destinations: The component IDs that the connections' destination
444+
must match.
444445
445446
Returns:
446447
The set of connections between components in the microgrid, filtered by
447-
the provided `start`/`end` choices.
448+
the provided `matching_sources` and `matching_destinations` choices.
448449
"""
449-
match (start, end):
450+
match (matching_sources, matching_destinations):
450451
case (None, None):
451-
selection_ids = self._graph.edges
452+
selection = self._graph.edges
452453
case (None, _):
453-
selection_ids = self._graph.in_edges(end)
454+
selection = self._graph.in_edges(matching_destinations)
454455
case (_, None):
455-
selection_ids = self._graph.out_edges(start)
456+
selection = self._graph.out_edges(matching_sources)
456457
case (_, _):
457-
start_edges = self._graph.out_edges(start)
458-
end_edges = self._graph.in_edges(end)
459-
selection_ids = set(start_edges).intersection(end_edges)
458+
source_edges = self._graph.out_edges(matching_sources)
459+
destination_edges = self._graph.in_edges(matching_destinations)
460+
selection = set(source_edges).intersection(destination_edges)
460461

461-
return set(self._graph.edges[i][_DATA_KEY] for i in selection_ids)
462+
return set(self._graph.edges[i][_DATA_KEY] for i in selection)
462463

463464
@override
464465
def predecessors(self, component_id: ComponentId) -> set[Component]:
@@ -512,7 +513,7 @@ def successors(self, component_id: ComponentId) -> set[Component]:
512513
def refresh_from(
513514
self,
514515
components: set[Component],
515-
connections: set[Connection],
516+
connections: set[ComponentConnection],
516517
correct_errors: Callable[["_MicrogridComponentGraph"], None] | None = None,
517518
) -> None:
518519
"""Refresh the graph from the provided list of components and connections.
@@ -523,7 +524,8 @@ def refresh_from(
523524
Args:
524525
components: The components to initialize the graph with. If set, must
525526
provide `connections` as well.
526-
connections: The connections to include in the graph.
527+
connections: The connections to initialize the graph with. If set, must
528+
provide `components` as well.
527529
correct_errors: The callback that, if set, will be invoked if the
528530
provided graph data is in any way invalid (it will attempt to
529531
correct the errors by inferring what the correct data should be).
@@ -534,9 +536,9 @@ def refresh_from(
534536
not fix it.
535537
"""
536538
issues: list[str] = []
537-
if not all(connection.is_valid() for connection in connections):
538-
raise InvalidGraphError(f"Invalid connections in input: {connections}")
539539

540+
for connection in connections:
541+
issues.extend((self._validate_connection(connection)))
540542
for component in components:
541543
issues.extend((self._validate_component(component)))
542544

@@ -550,10 +552,10 @@ def refresh_from(
550552

551553
# Store the original connection object in the edge data (third item in the
552554
# tuple) so that we can retrieve it later.
553-
for connection in connections:
554-
new_graph.add_edge(
555-
connection.start, connection.end, **{_DATA_KEY: connection}
556-
)
555+
new_graph.add_edges_from(
556+
(connection.source, connection.destination, {_DATA_KEY: connection})
557+
for connection in connections
558+
)
557559

558560
# check if we can construct a valid ComponentGraph
559561
# from the new NetworkX graph data
@@ -578,6 +580,20 @@ def refresh_from(
578580
self._graph = new_graph
579581
old_graph.clear() # just in case any references remain, but should not
580582

583+
def _validate_connection(self, connection: ComponentConnection) -> list[str]:
584+
"""Check that the connection is valid.
585+
586+
Args:
587+
connection: connection to validate.
588+
589+
Returns:
590+
List of issues found with the connection.
591+
"""
592+
issues: list[str] = []
593+
if connection.source == connection.destination:
594+
issues.append(f"Connection {connection} has same source and destination!")
595+
return issues
596+
581597
def _validate_component(self, component: Component) -> list[str]:
582598
"""Check that the component is valid.
583599

tests/microgrid/test_datapipeline.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
from frequenz.client.microgrid import (
1414
Component,
1515
ComponentCategory,
16-
Connection,
1716
InverterType,
1817
)
18+
from frequenz.client.microgrid.component import (
19+
ComponentConnection,
20+
)
1921
from pytest_mock import MockerFixture
2022

2123
from frequenz.sdk.microgrid._data_pipeline import _DataPipeline
@@ -66,8 +68,8 @@ async def test_actors_started(
6668
Component(ComponentId(15), ComponentCategory.BATTERY),
6769
},
6870
connections={
69-
Connection(ComponentId(1), ComponentId(4)),
70-
Connection(ComponentId(4), ComponentId(15)),
71+
ComponentConnection(source=ComponentId(1), destination=ComponentId(4)),
72+
ComponentConnection(source=ComponentId(4), destination=ComponentId(15)),
7173
},
7274
)
7375
mock_client.initialize(mocker)

0 commit comments

Comments
 (0)