44"""Formula generator from component graph."""
55
66from frequenz .client .microgrid import ComponentCategory , ComponentMetricId
7- from frequenz .quantities import Power
7+ from frequenz .quantities import Power , ReactivePower
88
99from ....microgrid import connection_manager
10+ from ..._base_types import QuantityT
1011from .._formula_engine import FormulaEngine
12+ from .._resampled_formula_builder import ResampledFormulaBuilder
1113from ._formula_generator import FormulaGenerator
1214
1315
14- class SimplePowerFormula (FormulaGenerator [Power ]):
15- """Formula generator from component graph for calculating sum of Power.
16+ class SimpleFormulaBase (FormulaGenerator [QuantityT ]):
17+ """Base class for simple formula generators."""
1618
17- Raises:
18- RuntimeError: If no components are defined in the config or if any
19- component is not found in the component graph.
20- """
19+ def _generate (
20+ self , builder : ResampledFormulaBuilder [ QuantityT ]
21+ ) -> FormulaEngine [ QuantityT ]:
22+ """Generate formula for calculating quantity from the component graph.
2123
22- def generate ( # noqa: DOC502
23- # * ComponentNotFound is raised indirectly by _get_grid_component()
24- # * RuntimeError is raised indirectly by connection_manager.get()
25- self ,
26- ) -> FormulaEngine [Power ]:
27- """Generate formula for calculating producer power from the component graph.
24+ Args:
25+ builder: The builder to use for generating the formula.
2826
2927 Returns:
30- A formula engine that will calculate the producer power .
28+ A formula engine that will calculate the quantity .
3129
3230 Raises:
33- ComponentNotFound: If the component graph does not contain a producer power
34- component.
35- RuntimeError: If the grid component has a single successor that is not a
36- meter.
31+ RuntimeError: If components ids in config are not specified
32+ or component graph does not contain all specified components.
3733 """
38- builder = self ._get_builder (
39- "simple_power_formula" , ComponentMetricId .ACTIVE_POWER , Power .from_watts
40- )
41-
4234 component_graph = connection_manager .get ().component_graph
4335 if self ._config .component_ids is None :
4436 raise RuntimeError ("Power formula without component ids is not supported." )
@@ -65,3 +57,53 @@ def generate( # noqa: DOC502
6557 )
6658
6759 return builder .build ()
60+
61+
62+ class SimplePowerFormula (SimpleFormulaBase [Power ]):
63+ """Formula generator from component graph for calculating sum of Power."""
64+
65+ def generate ( # noqa: DOC502
66+ # * ComponentNotFound is raised indirectly by _get_grid_component()
67+ # * RuntimeError is raised indirectly by connection_manager.get()
68+ self ,
69+ ) -> FormulaEngine [Power ]:
70+ """Generate formula for calculating sum of power from the component graph.
71+
72+ Returns:
73+ A formula engine that will calculate the power.
74+
75+ Raises:
76+ RuntimeError: If components ids in config are not specified
77+ or component graph does not contain all specified components.
78+ """
79+ builder = self ._get_builder (
80+ "simple_power_formula" ,
81+ ComponentMetricId .ACTIVE_POWER ,
82+ Power .from_watts ,
83+ )
84+ return self ._generate (builder )
85+
86+
87+ class SimpleReactivePowerFormula (SimpleFormulaBase [ReactivePower ]):
88+ """Formula generator from component graph for calculating sum of reactive power."""
89+
90+ def generate ( # noqa: DOC502
91+ # * ComponentNotFound is raised indirectly by _get_grid_component()
92+ # * RuntimeError is raised indirectly by connection_manager.get()
93+ self ,
94+ ) -> FormulaEngine [ReactivePower ]:
95+ """Generate formula for calculating sum of reactive power from the component graph.
96+
97+ Returns:
98+ A formula engine that will calculate the power.
99+
100+ Raises:
101+ RuntimeError: If components ids in config are not specified
102+ or component graph does not contain all specified components.
103+ """
104+ builder = self ._get_builder (
105+ "simple_reactive_power_formula" ,
106+ ComponentMetricId .REACTIVE_POWER ,
107+ ReactivePower .from_volt_amperes_reactive ,
108+ )
109+ return self ._generate (builder )
0 commit comments