Skip to content

Commit 97a4bb2

Browse files
authored
Bump component-graph-rs version to 0.5 (#63)
Also revamp the config interface, to follow the rust interface.
2 parents aab0acd + 5d7ab63 commit 97a4bb2

9 files changed

Lines changed: 491 additions & 52 deletions

File tree

‎Cargo.lock‎

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ crate-type = ["cdylib"]
1010

1111
[dependencies]
1212
pyo3 = "0.27.1"
13-
frequenz-microgrid-component-graph = "0.4"
13+
frequenz-microgrid-component-graph = "0.5"

‎RELEASE_NOTES.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@
33
## Upgrading
44

55
- This release updates the `frequenz-microgrid-component-graph` rust crate version to 0.4.
6+
7+
- This release updates the `frequenz-microgrid-component-graph` rust crate version to 0.5. `ComponentGraphConfig` is restructured to match: the six per-category `prefer_X_in_Y_formula` flags are replaced by a global `prefer_meters_in_component_formulas` plus per-formula overrides via the new `FormulaOverrides` class. Also exposes the new `steam_boiler_formula` method, and renames `battery_coalesce_formula` / `pv_coalesce_formula` to `battery_ac_coalesce_formula` / `pv_ac_coalesce_formula`.
8+
9+
- `ComponentGraphConfig.__init__` is now declared as keyword-only in the type stubs (the runtime was already keyword-only, so positional calls were already failing at runtime).
10+
11+
- The per-category preference flags inverted polarity: where the old flags selected the *device*, the new override entries select the *meter*. E.g. `ComponentGraphConfig(prefer_inverters_in_pv_formula=True)` becomes `ComponentGraphConfig(formula_overrides=FormulaOverrides(prefer_meters_in_pv_formula=False))`.

‎python/frequenz/microgrid_component_graph/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
ComponentGraph,
88
ComponentGraphConfig,
99
FormulaGenerationError,
10+
FormulaOverrides,
1011
InvalidGraphError,
1112
)
1213

1314
__all__ = [
1415
"ComponentGraph",
1516
"ComponentGraphConfig",
1617
"FormulaGenerationError",
18+
"FormulaOverrides",
1719
"InvalidGraphError",
1820
]

‎python/frequenz/microgrid_component_graph/__init__.pyi‎

Lines changed: 62 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,14 @@ class ComponentGraphConfig:
1919

2020
def __init__(
2121
self,
22+
*,
2223
allow_component_validation_failures: bool = False,
2324
allow_unconnected_components: bool = False,
2425
allow_unspecified_inverters: bool = False,
2526
disable_fallback_components: bool = False,
2627
include_phantom_loads_in_consumer_formula: bool = False,
27-
prefer_inverters_in_battery_formula: bool = False,
28-
prefer_inverters_in_pv_formula: bool = False,
29-
prefer_chp_in_chp_formula: bool = False,
30-
prefer_ev_chargers_in_ev_formula: bool = False,
31-
prefer_wind_turbines_in_wind_formula: bool = False,
28+
prefer_meters_in_component_formulas: bool = True,
29+
formula_overrides: FormulaOverrides | None = None,
3230
) -> None:
3331
"""Initialize this instance.
3432
@@ -51,26 +49,47 @@ class ComponentGraphConfig:
5149
the measurements of successor meters from the measurements of their
5250
predecessor meters. When `false`, consumer formula is generated by
5351
excluding production and battery components from the grid measurements.
54-
prefer_inverters_in_battery_formula: Whether to prefer battery inverters
55-
when generating Battery formulas. When this is `true`, battery inverters
56-
will be the primary source and battery meters will be secondary. When
57-
`false`, battery meters will be the primary source.
58-
prefer_inverters_in_pv_formula: Whether to prefer PV inverters when
59-
generating PV formulas. When this is `true`, PV inverters will be the
60-
primary source and PV meters will be secondary. When `false`, PV meters
61-
will be the primary source.
62-
prefer_chp_in_chp_formula: Whether to prefer CHP when generating CHP
63-
formulas. When this is `true`, CHPs will be the primary source and CHP
64-
meters will be secondary. When `false`, CHP meters will be the primary
65-
source.
66-
prefer_ev_chargers_in_ev_formula: Whether to prefer EV chargers when
67-
generating EV charger formulas. When this is `true`, EV chargers will be
68-
the primary source and EV meters will be secondary. When `false`, EV
69-
meters will be the primary source.
70-
prefer_wind_turbines_in_wind_formula: Whether to prefer wind turbines when
71-
generating wind turbine formulas. When this is `true`, wind turbines
72-
will be the primary source and wind meters will be secondary. When
73-
`false`, wind meters will be the primary source.
52+
prefer_meters_in_component_formulas: Default policy for the per-category
53+
formulas. When `True` (the default), the meter measurement is the
54+
primary source and the device measurement is the fallback for
55+
`battery_formula`, `chp_formula`, `pv_formula`, `wind_turbine_formula`,
56+
`ev_charger_formula`, and `steam_boiler_formula`. When `False`, the
57+
device is primary and the meter is the fallback. Has no effect on
58+
`grid_formula`, `consumer_formula`, `producer_formula`, or any of the
59+
coalesce formulas.
60+
formula_overrides: Per-formula overrides for the meter/device preference;
61+
see `FormulaOverrides`. Each entry, when set, takes precedence over
62+
`prefer_meters_in_component_formulas` for that formula.
63+
"""
64+
65+
class FormulaOverrides:
66+
"""Per-formula overrides for the meter/device preference.
67+
68+
Each parameter is `None` by default, meaning the corresponding formula
69+
follows the global `prefer_meters_in_component_formulas` setting on
70+
`ComponentGraphConfig`. Setting `True` forces the meter as primary
71+
for that formula; `False` forces the device.
72+
"""
73+
74+
def __init__(
75+
self,
76+
*,
77+
prefer_meters_in_pv_formula: bool | None = None,
78+
prefer_meters_in_battery_formula: bool | None = None,
79+
prefer_meters_in_chp_formula: bool | None = None,
80+
prefer_meters_in_ev_charger_formula: bool | None = None,
81+
prefer_meters_in_wind_turbine_formula: bool | None = None,
82+
prefer_meters_in_steam_boiler_formula: bool | None = None,
83+
) -> None:
84+
"""Initialize this instance.
85+
86+
Args:
87+
prefer_meters_in_pv_formula: Override for `pv_formula`.
88+
prefer_meters_in_battery_formula: Override for `battery_formula`.
89+
prefer_meters_in_chp_formula: Override for `chp_formula`.
90+
prefer_meters_in_ev_charger_formula: Override for `ev_charger_formula`.
91+
prefer_meters_in_wind_turbine_formula: Override for `wind_turbine_formula`.
92+
prefer_meters_in_steam_boiler_formula: Override for `steam_boiler_formula`.
7493
"""
7594

7695
class ComponentIdProtocol(Protocol):
@@ -341,14 +360,30 @@ class ComponentGraph(Generic[ComponentT, ConnectionT, ComponentIdT]):
341360
are not wind turbines.
342361
"""
343362

363+
def steam_boiler_formula(self, steam_boiler_ids: Set[ComponentIdT] | None) -> str:
364+
"""Generate the steam boiler formula for this component graph.
365+
366+
Args:
367+
steam_boiler_ids: The set of steam boiler component IDs to include in
368+
the formula. If `None`, all steam boilers in the graph will be
369+
included.
370+
371+
Returns:
372+
The steam boiler formula as a string.
373+
374+
Raises:
375+
FormulaGenerationError: if the given component IDs don't exist or
376+
are not steam boilers.
377+
"""
378+
344379
def grid_coalesce_formula(self) -> str:
345380
"""Generate the grid coalesce formula for this component graph.
346381
347382
Returns:
348383
The grid coalesced formula as a string.
349384
"""
350385

351-
def battery_coalesce_formula(self, battery_ids: Set[ComponentIdT] | None) -> str:
386+
def battery_ac_coalesce_formula(self, battery_ids: Set[ComponentIdT] | None) -> str:
352387
"""Generate the battery coalesce formula for this component graph.
353388
354389
Args:
@@ -364,7 +399,7 @@ class ComponentGraph(Generic[ComponentT, ConnectionT, ComponentIdT]):
364399
are not batteries.
365400
"""
366401

367-
def pv_coalesce_formula(self, pv_inverter_ids: Set[ComponentIdT] | None) -> str:
402+
def pv_ac_coalesce_formula(self, pv_inverter_ids: Set[ComponentIdT] | None) -> str:
368403
"""Generate the PV coalesce formula for this component graph.
369404
370405
Args:

‎src/graph.rs‎

Lines changed: 78 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,37 +32,81 @@ impl ComponentGraphConfig {
3232
allow_unspecified_inverters = false,
3333
disable_fallback_components = false,
3434
include_phantom_loads_in_consumer_formula = false,
35-
prefer_inverters_in_battery_formula = false,
36-
prefer_inverters_in_pv_formula = false,
37-
prefer_chp_in_chp_formula = false,
38-
prefer_ev_chargers_in_ev_formula = false,
39-
prefer_wind_turbines_in_wind_formula = false,
35+
prefer_meters_in_component_formulas = true,
36+
formula_overrides = None,
4037
))]
4138
fn new(
4239
allow_component_validation_failures: bool,
4340
allow_unconnected_components: bool,
4441
allow_unspecified_inverters: bool,
4542
disable_fallback_components: bool,
4643
include_phantom_loads_in_consumer_formula: bool,
47-
prefer_inverters_in_battery_formula: bool,
48-
prefer_inverters_in_pv_formula: bool,
49-
prefer_chp_in_chp_formula: bool,
50-
prefer_ev_chargers_in_ev_formula: bool,
51-
prefer_wind_turbines_in_wind_formula: bool,
44+
prefer_meters_in_component_formulas: bool,
45+
formula_overrides: Option<FormulaOverrides>,
5246
) -> Self {
47+
let mut builder = cg::ComponentGraphConfig::builder()
48+
.allow_component_validation_failures(allow_component_validation_failures)
49+
.allow_unconnected_components(allow_unconnected_components)
50+
.allow_unspecified_inverters(allow_unspecified_inverters)
51+
.disable_fallback_components(disable_fallback_components)
52+
.include_phantom_loads_in_consumer_formula(include_phantom_loads_in_consumer_formula)
53+
.prefer_meters_in_component_formulas(prefer_meters_in_component_formulas);
54+
if let Some(overrides) = formula_overrides {
55+
builder = builder.formula_overrides(overrides.overrides);
56+
}
5357
ComponentGraphConfig {
54-
config: cg::ComponentGraphConfig {
55-
allow_component_validation_failures,
56-
allow_unconnected_components,
57-
allow_unspecified_inverters,
58-
disable_fallback_components,
59-
include_phantom_loads_in_consumer_formula,
60-
prefer_inverters_in_battery_formula,
61-
prefer_inverters_in_pv_formula,
62-
prefer_chp_in_chp_formula,
63-
prefer_ev_chargers_in_ev_formula,
64-
prefer_wind_turbines_in_wind_formula,
65-
},
58+
config: builder.build(),
59+
}
60+
}
61+
}
62+
63+
#[pyclass(subclass)]
64+
#[derive(Clone, Default, Debug)]
65+
pub struct FormulaOverrides {
66+
overrides: cg::FormulaOverrides,
67+
}
68+
69+
#[pymethods]
70+
impl FormulaOverrides {
71+
#[new]
72+
#[pyo3(signature = (
73+
*,
74+
prefer_meters_in_pv_formula = None,
75+
prefer_meters_in_battery_formula = None,
76+
prefer_meters_in_chp_formula = None,
77+
prefer_meters_in_ev_charger_formula = None,
78+
prefer_meters_in_wind_turbine_formula = None,
79+
prefer_meters_in_steam_boiler_formula = None,
80+
))]
81+
fn new(
82+
prefer_meters_in_pv_formula: Option<bool>,
83+
prefer_meters_in_battery_formula: Option<bool>,
84+
prefer_meters_in_chp_formula: Option<bool>,
85+
prefer_meters_in_ev_charger_formula: Option<bool>,
86+
prefer_meters_in_wind_turbine_formula: Option<bool>,
87+
prefer_meters_in_steam_boiler_formula: Option<bool>,
88+
) -> Self {
89+
let mut builder = cg::FormulaOverrides::builder();
90+
if let Some(v) = prefer_meters_in_pv_formula {
91+
builder = builder.prefer_meters_in_pv_formula(v);
92+
}
93+
if let Some(v) = prefer_meters_in_battery_formula {
94+
builder = builder.prefer_meters_in_battery_formula(v);
95+
}
96+
if let Some(v) = prefer_meters_in_chp_formula {
97+
builder = builder.prefer_meters_in_chp_formula(v);
98+
}
99+
if let Some(v) = prefer_meters_in_ev_charger_formula {
100+
builder = builder.prefer_meters_in_ev_charger_formula(v);
101+
}
102+
if let Some(v) = prefer_meters_in_wind_turbine_formula {
103+
builder = builder.prefer_meters_in_wind_turbine_formula(v);
104+
}
105+
if let Some(v) = prefer_meters_in_steam_boiler_formula {
106+
builder = builder.prefer_meters_in_steam_boiler_formula(v);
107+
}
108+
FormulaOverrides {
109+
overrides: builder.build(),
66110
}
67111
}
68112
}
@@ -308,6 +352,18 @@ impl ComponentGraph {
308352
.map_err(|e| PyErr::new::<FormulaGenerationError, _>(e.to_string()))
309353
}
310354

355+
#[pyo3(signature = (steam_boiler_ids=None))]
356+
fn steam_boiler_formula(
357+
&self,
358+
py: Python<'_>,
359+
steam_boiler_ids: Option<Bound<'_, PyAny>>,
360+
) -> PyResult<String> {
361+
self.graph
362+
.steam_boiler_formula(extract_ids(py, steam_boiler_ids)?)
363+
.map(|f| f.to_string())
364+
.map_err(|e| PyErr::new::<FormulaGenerationError, _>(e.to_string()))
365+
}
366+
311367
fn grid_coalesce_formula(&self) -> PyResult<String> {
312368
self.graph
313369
.grid_coalesce_formula()

‎src/lib.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,6 @@ mod _component_graph {
3434
use crate::graph::ComponentGraph;
3535
#[pymodule_export]
3636
use crate::graph::ComponentGraphConfig;
37+
#[pymodule_export]
38+
use crate::graph::FormulaOverrides;
3739
}

0 commit comments

Comments
 (0)