Skip to content

Commit e01d5c9

Browse files
Add Consumer and Producer formula steps
Signed-off-by: Matthias Wende <[email protected]>
1 parent 862ebe4 commit e01d5c9

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

src/frequenz/sdk/timeseries/formula_engine/_formula_steps.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,64 @@ def apply(self, eval_stack: list[float]) -> None:
177177
eval_stack.append(res)
178178

179179

180+
class Consumption(FormulaStep):
181+
"""A formula step that represents the consumption operator.
182+
183+
The consumption operator is the maximum of the value on top
184+
of the evaluation stack and 0.
185+
"""
186+
187+
def __repr__(self) -> str:
188+
"""Return a string representation of the step.
189+
190+
Returns:
191+
A string representation of the step.
192+
"""
193+
return "consumption"
194+
195+
def apply(self, eval_stack: list[float]) -> None:
196+
"""
197+
Apply the consumption formula.
198+
199+
Replace the top of the eval eval_stack with the same value if the value
200+
is positive or 0.
201+
202+
Args:
203+
eval_stack: An evaluation stack, to apply the formula step on.
204+
"""
205+
val = eval_stack.pop()
206+
eval_stack.append(max(val, 0))
207+
208+
209+
class Production(FormulaStep):
210+
"""A formula step that represents the production operator.
211+
212+
The production operator is the maximum of the value times minus one on top
213+
of the evaluation stack and 0.
214+
"""
215+
216+
def __repr__(self) -> str:
217+
"""Return a string representation of the step.
218+
219+
Returns:
220+
A string representation of the step.
221+
"""
222+
return "production"
223+
224+
def apply(self, eval_stack: list[float]) -> None:
225+
"""
226+
Apply the production formula.
227+
228+
Replace the top of the eval eval_stack with its absolute value if the
229+
value is negative or 0.
230+
231+
Args:
232+
eval_stack: An evaluation stack, to apply the formula step on.
233+
"""
234+
val = eval_stack.pop()
235+
eval_stack.append(max(-val, 0))
236+
237+
180238
class OpenParen(FormulaStep):
181239
"""A no-op formula step used while building a prefix formula engine.
182240

0 commit comments

Comments
 (0)