-
Notifications
You must be signed in to change notification settings - Fork 19
Switch to the rust component graph #1295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
The head ref may contain hidden characters: "new-formulas+new-graph=\u{1F389}"
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
23e0cd3
Add AST nodes for the new tree walking Formula implementation
shsms a67e0b3
Introduce a `Peekable` wrapper around `Iterator`
shsms 25dfc0b
Implement a lexer for the new component graph formulas
shsms 9366cfc
Implement a `ResampledStreamFetcher`
shsms c32c852
Implement a `FormulaEvaluatingActor`
shsms 7e2ad89
Implement the `Formula` type
shsms 56a4377
Implement a parser for string formulas
shsms 194b742
Add tests for formulas
shsms 0454451
Add a 3-phase formula type that wraps 3 1-phase formulas
shsms e23af0a
Add a formula pool for storing and reusing formulas
shsms 465f914
Add `frequenz-microgrid-component-graph` as a dependency
shsms 7b0fedf
Remove test for island-mode
shsms b37b5f5
Switch to use the external component graph
shsms edd8617
Delete the old component graph
shsms 678c78a
Replace FormulaEngine with the new Formula
shsms a69ba9e
Remove tests for the old fallback mechanism
shsms 0a843ea
Send test data from secondary components
shsms d70a647
Test priority of component powers in formulas over meter powers
shsms 898c976
Increase number of active namespaces for formula test
shsms c9719e5
Drop old formula engine
shsms b183251
Document the new Formula implementation
shsms 2470c10
Remove all remaining references to FormulaEngines
shsms 5f644c8
Update release notes
shsms File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
docs/user-guide/formula-engine.md → docs/user-guide/formulas.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
llucax marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,241 @@ | ||
| # License: MIT | ||
| # Copyright © 2025 Frequenz Energy-as-a-Service GmbH | ||
|
|
||
| """Graph traversal helpers.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Iterable | ||
| from typing import Callable | ||
|
|
||
| from frequenz.client.common.microgrid.components import ComponentId | ||
| from frequenz.client.microgrid.component import ( | ||
| BatteryInverter, | ||
| Chp, | ||
| Component, | ||
| ComponentConnection, | ||
| EvCharger, | ||
| GridConnectionPoint, | ||
| SolarInverter, | ||
| ) | ||
| from frequenz.microgrid_component_graph import ComponentGraph, InvalidGraphError | ||
|
|
||
|
|
||
| def is_pv_inverter(component: Component) -> bool: | ||
| """Check if the component is a PV inverter. | ||
| Args: | ||
| component: The component to check. | ||
| Returns: | ||
| `True` if the component is a PV inverter, `False` otherwise. | ||
| """ | ||
| return isinstance(component, SolarInverter) | ||
|
|
||
|
|
||
| def is_battery_inverter(component: Component) -> bool: | ||
| """Check if the component is a battery inverter. | ||
| Args: | ||
| component: The component to check. | ||
| Returns: | ||
| `True` if the component is a battery inverter, `False` otherwise. | ||
| """ | ||
| return isinstance(component, BatteryInverter) | ||
|
|
||
|
|
||
| def is_chp(component: Component) -> bool: | ||
| """Check if the component is a CHP. | ||
| Args: | ||
| component: The component to check. | ||
| Returns: | ||
| `True` if the component is a CHP, `False` otherwise. | ||
| """ | ||
| return isinstance(component, Chp) | ||
|
|
||
|
|
||
| def is_ev_charger(component: Component) -> bool: | ||
| """Check if the component is an EV charger. | ||
| Args: | ||
| component: The component to check. | ||
| Returns: | ||
| `True` if the component is an EV charger, `False` otherwise. | ||
| """ | ||
llucax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return isinstance(component, EvCharger) | ||
|
|
||
|
|
||
| def is_battery_chain( | ||
shsms marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| graph: ComponentGraph[Component, ComponentConnection, ComponentId], | ||
| component: Component, | ||
| ) -> bool: | ||
| """Check if the specified component is part of a battery chain. | ||
| A component is part of a battery chain if it is either a battery inverter or a | ||
| battery meter. | ||
| Args: | ||
| graph: The component graph. | ||
| component: component to check. | ||
| Returns: | ||
| Whether the specified component is part of a battery chain. | ||
| """ | ||
| return is_battery_inverter(component) or graph.is_battery_meter(component.id) | ||
|
|
||
|
|
||
| def is_pv_chain( | ||
| graph: ComponentGraph[Component, ComponentConnection, ComponentId], | ||
| component: Component, | ||
| ) -> bool: | ||
| """Check if the specified component is part of a PV chain. | ||
| A component is part of a PV chain if it is either a PV inverter or a PV | ||
| meter. | ||
| Args: | ||
| graph: The component graph. | ||
| component: component to check. | ||
| Returns: | ||
| Whether the specified component is part of a PV chain. | ||
| """ | ||
| return is_pv_inverter(component) or graph.is_pv_meter(component.id) | ||
|
|
||
|
|
||
| def is_ev_charger_chain( | ||
| graph: ComponentGraph[Component, ComponentConnection, ComponentId], | ||
| component: Component, | ||
| ) -> bool: | ||
| """Check if the specified component is part of an EV charger chain. | ||
| A component is part of an EV charger chain if it is either an EV charger or an | ||
| EV charger meter. | ||
| Args: | ||
| graph: The component graph. | ||
| component: component to check. | ||
| Returns: | ||
| Whether the specified component is part of an EV charger chain. | ||
| """ | ||
| return is_ev_charger(component) or graph.is_ev_charger_meter(component.id) | ||
|
|
||
|
|
||
| def is_chp_chain( | ||
| graph: ComponentGraph[Component, ComponentConnection, ComponentId], | ||
| component: Component, | ||
| ) -> bool: | ||
| """Check if the specified component is part of a CHP chain. | ||
| A component is part of a CHP chain if it is either a CHP or a CHP meter. | ||
| Args: | ||
| graph: The component graph. | ||
| component: component to check. | ||
| Returns: | ||
| Whether the specified component is part of a CHP chain. | ||
| """ | ||
| return is_chp(component) or graph.is_chp_meter(component.id) | ||
|
|
||
|
|
||
| def dfs( | ||
| graph: ComponentGraph[Component, ComponentConnection, ComponentId], | ||
| current_node: Component, | ||
| visited: set[Component], | ||
| condition: Callable[[Component], bool], | ||
| ) -> set[Component]: | ||
| """ | ||
| Search for components that fulfill the condition in the Graph. | ||
| DFS is used for searching the graph. The graph traversal is stopped | ||
| once a component fulfills the condition. | ||
| Args: | ||
| graph: The component graph. | ||
| current_node: The current node to search from. | ||
| visited: The set of visited nodes. | ||
| condition: The condition function to check for. | ||
| Returns: | ||
| A set of component ids where the corresponding components fulfill | ||
| the condition function. | ||
| """ | ||
| if current_node in visited: | ||
| return set() | ||
|
|
||
| visited.add(current_node) | ||
|
|
||
| if condition(current_node): | ||
| return {current_node} | ||
|
|
||
| component: set[Component] = set() | ||
|
|
||
| for successor in graph.successors(current_node.id): | ||
| component.update(dfs(graph, successor, visited, condition)) | ||
|
|
||
| return component | ||
|
|
||
|
|
||
| def find_first_descendant_component( | ||
| graph: ComponentGraph[Component, ComponentConnection, ComponentId], | ||
| *, | ||
| descendants: Iterable[type[Component]], | ||
| ) -> Component: | ||
| """Find the first descendant component given root and descendant categories. | ||
| This method looks for the first descendant component from the GRID | ||
| component, considering only the immediate descendants. | ||
| The priority of the component to search for is determined by the order | ||
| of the descendant categories, with the first category having the | ||
| highest priority. | ||
| Args: | ||
| graph: The component graph to search. | ||
| descendants: The descendant classes to search for the first | ||
| descendant component in. | ||
| Returns: | ||
| The first descendant component found in the component graph, | ||
| considering the specified `descendants` categories. | ||
| Raises: | ||
| InvalidGraphError: When no GRID component is found in the graph. | ||
| ValueError: When no component is found in the given categories. | ||
| """ | ||
| # We always sort by component ID to ensure consistent results | ||
|
|
||
| def sorted_by_id(components: Iterable[Component]) -> Iterable[Component]: | ||
| return sorted(components, key=lambda c: c.id) | ||
|
|
||
| root_component = next( | ||
| iter(sorted_by_id(graph.components(matching_types={GridConnectionPoint}))), | ||
| None, | ||
| ) | ||
| if root_component is None: | ||
| raise InvalidGraphError( | ||
| "No GridConnectionPoint component found in the component graph!" | ||
| ) | ||
|
|
||
| successors = sorted_by_id(graph.successors(root_component.id)) | ||
|
|
||
| def find_component(component_class: type[Component]) -> Component | None: | ||
| return next( | ||
| (comp for comp in successors if isinstance(comp, component_class)), | ||
| None, | ||
| ) | ||
|
|
||
| # Find the first component that matches the given descendant categories | ||
| # in the order of the categories list. | ||
| component = next(filter(None, map(find_component, descendants)), None) | ||
|
|
||
| if component is None: | ||
| raise ValueError("Component not found in any of the descendant categories.") | ||
|
|
||
| return component | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.