-
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
Open
shsms
wants to merge
23
commits into
frequenz-floss:v1.x.x
Choose a base branch
from
shsms:new-formulas+new-graph=🎉
base: v1.x.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
The head ref may contain hidden characters: "new-formulas+new-graph=\u{1F389}"
Open
Changes from 1 commit
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,383 @@ | ||
| # License: MIT | ||
| # Copyright © 2025 Frequenz Energy-as-a-Service GmbH | ||
|
|
||
| """A composable formula represented as an AST.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| from collections.abc import Callable | ||
| from typing import Generic | ||
|
|
||
| from frequenz.channels import Broadcast, Receiver | ||
| from typing_extensions import override | ||
|
|
||
| from frequenz.sdk.timeseries.formulas._resampled_stream_fetcher import ( | ||
| ResampledStreamFetcher, | ||
| ) | ||
|
|
||
| from ...actor import BackgroundService | ||
| from .. import ReceiverFetcher, Sample | ||
| from .._base_types import QuantityT | ||
| from . import _ast | ||
| from ._formula_evaluator import FormulaEvaluatingActor | ||
| from ._functions import Coalesce, Max, Min | ||
|
|
||
| _logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class Formula(BackgroundService, ReceiverFetcher[Sample[QuantityT]]): | ||
| """A formula represented as an AST.""" | ||
|
|
||
| def __init__( # pylint: disable=too-many-arguments | ||
| self, | ||
| *, | ||
| name: str, | ||
| root: _ast.Node, | ||
| create_method: Callable[[float], QuantityT], | ||
| streams: list[_ast.TelemetryStream[QuantityT]], | ||
| sub_formulas: list[Formula[QuantityT]] | None = None, | ||
| metric_fetcher: ResampledStreamFetcher | None = None, | ||
| ) -> None: | ||
| """Create a `Formula` instance. | ||
|
|
||
| Args: | ||
| name: The name of the formula. | ||
| root: The root node of the formula AST. | ||
| create_method: A method to generate the output values with. If the | ||
| formula is for generating power values, this would be | ||
| `Power.from_watts`, for example. | ||
| streams: The telemetry streams that the formula depends on. | ||
| sub_formulas: Any sub-formulas that this formula depends on. | ||
| metric_fetcher: An optional metric fetcher that needs to be started | ||
| before the formula can be evaluated. | ||
| """ | ||
| BackgroundService.__init__(self) | ||
| self._name: str = name | ||
| self._root: _ast.Node = root | ||
| self._components: list[_ast.TelemetryStream[QuantityT]] = streams | ||
| self._create_method: Callable[[float], QuantityT] = create_method | ||
| self._sub_formulas: list[Formula[QuantityT]] = sub_formulas or [] | ||
|
|
||
| self._channel: Broadcast[Sample[QuantityT]] = Broadcast( | ||
| name=f"{self}", | ||
| resend_latest=True, | ||
| ) | ||
| self._evaluator: FormulaEvaluatingActor[QuantityT] = FormulaEvaluatingActor( | ||
| root=self._root, | ||
| components=self._components, | ||
| create_method=self._create_method, | ||
| output_channel=self._channel, | ||
| metric_fetcher=metric_fetcher, | ||
| ) | ||
|
|
||
| @override | ||
| def __str__(self) -> str: | ||
| """Return a string representation of the formula.""" | ||
| return f"[{self._name}]({self._root})" | ||
|
|
||
| @override | ||
| def new_receiver(self, *, limit: int = 50) -> Receiver[Sample[QuantityT]]: | ||
| """Subscribe to the formula evaluator to get evaluated samples.""" | ||
| if not self._evaluator.is_running: | ||
| # raise RuntimeError( | ||
| # f"Formula evaluator for '{self._root}' is not running. Please " | ||
| # + "call `start()` on the formula before using it.", | ||
| # ) | ||
| # _logger.warning( | ||
| # "Formula evaluator for '%s' is not running. Starting it. " | ||
| # + "Please call `start()` on the formula before using it." | ||
| # self._root, | ||
| # ) | ||
| self.start() | ||
| return self._channel.new_receiver(limit=limit) | ||
|
|
||
| @override | ||
| def start(self) -> None: | ||
| """Start the formula evaluator.""" | ||
| for sub_formula in self._sub_formulas: | ||
| sub_formula.start() | ||
| self._evaluator.start() | ||
|
|
||
| @override | ||
| async def stop(self, msg: str | None = None) -> None: | ||
| """Stop the formula evaluator.""" | ||
| await BackgroundService.stop(self, msg) | ||
| for sub_formula in self._sub_formulas: | ||
| await sub_formula.stop(msg) | ||
| await self._evaluator.stop(msg) | ||
|
Comment on lines
+102
to
+108
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should probably override |
||
|
|
||
| def __add__( | ||
| self, other: FormulaBuilder[QuantityT] | QuantityT | Formula[QuantityT] | ||
| ) -> FormulaBuilder[QuantityT]: | ||
| """Create an addition operation node.""" | ||
| return FormulaBuilder(self, self._create_method) + other | ||
|
|
||
| def __sub__( | ||
| self, other: FormulaBuilder[QuantityT] | QuantityT | Formula[QuantityT] | ||
| ) -> FormulaBuilder[QuantityT]: | ||
| """Create a subtraction operation node.""" | ||
| return FormulaBuilder(self, self._create_method) - other | ||
|
|
||
| def __mul__(self, other: float) -> FormulaBuilder[QuantityT]: | ||
| """Create a multiplication operation node.""" | ||
| return FormulaBuilder(self, self._create_method) * other | ||
|
|
||
| def __truediv__(self, other: float) -> FormulaBuilder[QuantityT]: | ||
| """Create a division operation node.""" | ||
| return FormulaBuilder(self, self._create_method) / other | ||
|
|
||
| def coalesce( | ||
| self, | ||
| other: list[FormulaBuilder[QuantityT] | QuantityT | Formula[QuantityT]], | ||
| ) -> FormulaBuilder[QuantityT]: | ||
| """Create a coalesce operation node.""" | ||
| return FormulaBuilder(self, self._create_method).coalesce(other) | ||
|
|
||
| def min( | ||
| self, | ||
| other: list[FormulaBuilder[QuantityT] | QuantityT | Formula[QuantityT]], | ||
| ) -> FormulaBuilder[QuantityT]: | ||
| """Create a min operation node.""" | ||
| return FormulaBuilder(self, self._create_method).min(other) | ||
|
|
||
| def max( | ||
| self, | ||
| other: list[FormulaBuilder[QuantityT] | QuantityT | Formula[QuantityT]], | ||
| ) -> FormulaBuilder[QuantityT]: | ||
| """Create a max operation node.""" | ||
| return FormulaBuilder(self, self._create_method).max(other) | ||
|
|
||
|
|
||
| class FormulaBuilder(Generic[QuantityT]): | ||
| """A builder for higher-order formulas represented as ASTs.""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| formula: Formula[QuantityT] | _ast.Node, | ||
| create_method: Callable[[float], QuantityT], | ||
| streams: list[_ast.TelemetryStream[QuantityT]] | None = None, | ||
| sub_formulas: list[Formula[QuantityT]] | None = None, | ||
| ) -> None: | ||
| """Create a `FormulaBuilder` instance. | ||
|
|
||
| Args: | ||
| formula: The initial formula to build upon. | ||
| create_method: A method to generate the output values with. If the | ||
| formula is for generating power values, this would be | ||
| `Power.from_watts`, for example. | ||
| streams: The telemetry streams that the formula depends on. | ||
| sub_formulas: Any sub-formulas that this formula depends on. | ||
| """ | ||
| self._create_method: Callable[[float], QuantityT] = create_method | ||
| self._streams: list[_ast.TelemetryStream[QuantityT]] = streams or [] | ||
| """Input streams that need to be synchronized before evaluation.""" | ||
| self._sub_formulas: list[Formula[QuantityT]] = sub_formulas or [] | ||
| """Sub-formulas whose lifetimes are managed by this formula.""" | ||
|
|
||
| if isinstance(formula, Formula): | ||
| self.root: _ast.Node = _ast.TelemetryStream( | ||
| None, | ||
| str(formula), | ||
| formula.new_receiver(), | ||
| ) | ||
| self._streams.append(self.root) | ||
| self._sub_formulas.append(formula) | ||
| else: | ||
| self.root = formula | ||
|
|
||
| def __add__( | ||
| self, | ||
| other: FormulaBuilder[QuantityT] | QuantityT | Formula[QuantityT], | ||
| ) -> FormulaBuilder[QuantityT]: | ||
| """Create an addition operation node.""" | ||
| if isinstance(other, FormulaBuilder): | ||
| right_node = other.root | ||
| self._streams.extend(other._streams) | ||
| elif isinstance(other, Formula): | ||
| right_node = _ast.TelemetryStream(None, str(other), other.new_receiver()) | ||
| self._streams.append(right_node) | ||
| self._sub_formulas.append(other) | ||
| else: | ||
| right_node = _ast.Constant(None, other.base_value) | ||
|
|
||
| new_root = _ast.Add(None, self.root, right_node) | ||
| return FormulaBuilder( | ||
| new_root, | ||
| self._create_method, | ||
| self._streams, | ||
| self._sub_formulas, | ||
| ) | ||
|
|
||
| def __sub__( | ||
| self, | ||
| other: FormulaBuilder[QuantityT] | QuantityT | Formula[QuantityT], | ||
| ) -> FormulaBuilder[QuantityT]: | ||
| """Create a subtraction operation node.""" | ||
| if isinstance(other, FormulaBuilder): | ||
| right_node = other.root | ||
| self._streams.extend(other._streams) | ||
| elif isinstance(other, Formula): | ||
| right_node = _ast.TelemetryStream(None, str(other), other.new_receiver()) | ||
| self._streams.append(right_node) | ||
| self._sub_formulas.append(other) | ||
| else: | ||
| right_node = _ast.Constant(None, other.base_value) | ||
|
|
||
| new_root = _ast.Sub(None, self.root, right_node) | ||
| return FormulaBuilder( | ||
| new_root, | ||
| self._create_method, | ||
| self._streams, | ||
| self._sub_formulas, | ||
| ) | ||
|
|
||
| def __mul__(self, other: float) -> FormulaBuilder[QuantityT]: | ||
| """Create a multiplication operation node.""" | ||
| right_node = _ast.Constant(None, other) | ||
| new_root = _ast.Mul(None, self.root, right_node) | ||
| return FormulaBuilder( | ||
| new_root, | ||
| self._create_method, | ||
| self._streams, | ||
| self._sub_formulas, | ||
| ) | ||
|
|
||
| def __truediv__( | ||
| self, | ||
| other: float, | ||
| ) -> FormulaBuilder[QuantityT]: | ||
| """Create a division operation node.""" | ||
| right_node = _ast.Constant(None, other) | ||
| new_root = _ast.Div(None, self.root, right_node) | ||
| return FormulaBuilder( | ||
| new_root, | ||
| self._create_method, | ||
| self._streams, | ||
| self._sub_formulas, | ||
| ) | ||
|
|
||
| def coalesce( | ||
| self, | ||
| other: list[FormulaBuilder[QuantityT] | QuantityT | Formula[QuantityT]], | ||
| ) -> FormulaBuilder[QuantityT]: | ||
| """Create a coalesce operation node.""" | ||
| right_nodes: list[_ast.Node] = [] | ||
| for item in other: | ||
| if isinstance(item, FormulaBuilder): | ||
| right_nodes.append(item.root) | ||
| self._streams.extend(item._streams) # pylint: disable=protected-access | ||
| elif isinstance(item, Formula): | ||
| right_node = _ast.TelemetryStream( | ||
| None, | ||
| str(item), | ||
| item.new_receiver(), | ||
| ) | ||
| right_nodes.append(right_node) | ||
| self._streams.append(right_node) | ||
| self._sub_formulas.append(item) | ||
| else: | ||
| right_nodes.append(_ast.Constant(None, item.base_value)) | ||
|
|
||
| new_root = _ast.FunCall( | ||
| None, | ||
| Coalesce(), | ||
| [self.root] + right_nodes, | ||
| ) | ||
|
|
||
| return FormulaBuilder( | ||
| new_root, | ||
| self._create_method, | ||
| self._streams, | ||
| self._sub_formulas, | ||
| ) | ||
|
|
||
| def min( | ||
| self, | ||
| other: list[FormulaBuilder[QuantityT] | QuantityT | Formula[QuantityT]], | ||
| ) -> FormulaBuilder[QuantityT]: | ||
| """Create a min operation node.""" | ||
| right_nodes: list[_ast.Node] = [] | ||
| for item in other: | ||
| if isinstance(item, FormulaBuilder): | ||
| right_nodes.append(item.root) | ||
| self._streams.extend(item._streams) # pylint: disable=protected-access | ||
| elif isinstance(item, Formula): | ||
| right_node = _ast.TelemetryStream( | ||
| None, | ||
| str(item), | ||
| item.new_receiver(), | ||
| ) | ||
| right_nodes.append(right_node) | ||
| self._streams.append(right_node) | ||
| self._sub_formulas.append(item) | ||
| else: | ||
| right_nodes.append(_ast.Constant(None, item.base_value)) | ||
|
|
||
| new_root = _ast.FunCall( | ||
| None, | ||
| Min(), | ||
| [self.root] + right_nodes, | ||
| ) | ||
|
|
||
| return FormulaBuilder( | ||
| new_root, | ||
| self._create_method, | ||
| self._streams, | ||
| self._sub_formulas, | ||
| ) | ||
|
|
||
| def max( | ||
| self, | ||
| other: list[FormulaBuilder[QuantityT] | QuantityT | Formula[QuantityT]], | ||
| ) -> FormulaBuilder[QuantityT]: | ||
| """Create a max operation node.""" | ||
| right_nodes: list[_ast.Node] = [] | ||
| for item in other: | ||
| if isinstance(item, FormulaBuilder): | ||
| right_nodes.append(item.root) | ||
| self._streams.extend(item._streams) # pylint: disable=protected-access | ||
| elif isinstance(item, Formula): | ||
| right_node = _ast.TelemetryStream( | ||
| None, | ||
| str(item), | ||
| item.new_receiver(), | ||
| ) | ||
| right_nodes.append(right_node) | ||
| self._streams.append(right_node) | ||
| self._sub_formulas.append(item) | ||
| else: | ||
| right_nodes.append(_ast.Constant(None, item.base_value)) | ||
|
|
||
| new_root = _ast.FunCall( | ||
| None, | ||
| Max(), | ||
| [self.root] + right_nodes, | ||
| ) | ||
|
|
||
| return FormulaBuilder( | ||
| new_root, | ||
| self._create_method, | ||
| self._streams, | ||
| self._sub_formulas, | ||
| ) | ||
|
|
||
| def build( | ||
| self, | ||
| name: str, | ||
| ) -> Formula[QuantityT]: | ||
| """Build a `Formula` instance. | ||
|
|
||
| Args: | ||
| name: The name of the formula. | ||
|
|
||
| Returns: | ||
| A `Formula` instance. | ||
| """ | ||
| return Formula( | ||
| name=name, | ||
| root=self.root, | ||
| create_method=self._create_method, | ||
| streams=self._streams, | ||
| sub_formulas=self._sub_formulas, | ||
| ) | ||
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.