Skip to content

Commit 191450c

Browse files
committed
Revert "Return Formulas from formula generators instead of Strings (frequenz-floss#10)"
This reverts commit ab3b998, reversing changes made to 93bb81c.
1 parent 26927cd commit 191450c

File tree

12 files changed

+119
-209
lines changed

12 files changed

+119
-209
lines changed

src/graph.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ mod formulas;
1313
pub mod iterators;
1414

1515
use crate::{ComponentGraphConfig, Edge, Node};
16-
pub use formulas::Formula;
1716
use petgraph::graph::{DiGraph, NodeIndex};
1817
use std::collections::HashMap;
1918

src/graph/formulas.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,61 +12,58 @@ use crate::Node;
1212

1313
mod expr;
1414
mod fallback;
15-
mod formula;
1615
mod generators;
1716
mod traversal;
1817

19-
pub use formula::Formula;
20-
2118
/// Formulas for various microgrid metrics.
2219
impl<N, E> ComponentGraph<N, E>
2320
where
2421
N: Node,
2522
E: Edge,
2623
{
2724
/// Returns a string representing the consumer formula for the graph.
28-
pub fn consumer_formula(&self) -> Result<Formula, Error> {
25+
pub fn consumer_formula(&self) -> Result<String, Error> {
2926
generators::consumer::ConsumerFormulaBuilder::try_new(self)?.build()
3027
}
3128

3229
/// Returns a string representing the grid formula for the graph.
33-
pub fn grid_formula(&self) -> Result<Formula, Error> {
30+
pub fn grid_formula(&self) -> Result<String, Error> {
3431
generators::grid::GridFormulaBuilder::try_new(self)?.build()
3532
}
3633

3734
/// Returns a string representing the producer formula for the graph.
38-
pub fn producer_formula(&self) -> Result<Formula, Error> {
35+
pub fn producer_formula(&self) -> Result<String, Error> {
3936
generators::producer::ProducerFormulaBuilder::try_new(self)?.build()
4037
}
4138

4239
/// Returns a string representing the battery formula for the graph.
43-
pub fn battery_formula(&self, battery_ids: Option<BTreeSet<u64>>) -> Result<Formula, Error> {
40+
pub fn battery_formula(&self, battery_ids: Option<BTreeSet<u64>>) -> Result<String, Error> {
4441
generators::battery::BatteryFormulaBuilder::try_new(self, battery_ids)?.build()
4542
}
4643

4744
/// Returns a string representing the CHP formula for the graph.
48-
pub fn chp_formula(&self, chp_ids: Option<BTreeSet<u64>>) -> Result<Formula, Error> {
45+
pub fn chp_formula(&self, chp_ids: Option<BTreeSet<u64>>) -> Result<String, Error> {
4946
generators::chp::CHPFormulaBuilder::try_new(self, chp_ids)?.build()
5047
}
5148

5249
/// Returns a string representing the PV formula for the graph.
53-
pub fn pv_formula(&self, pv_inverter_ids: Option<BTreeSet<u64>>) -> Result<Formula, Error> {
50+
pub fn pv_formula(&self, pv_inverter_ids: Option<BTreeSet<u64>>) -> Result<String, Error> {
5451
generators::pv::PVFormulaBuilder::try_new(self, pv_inverter_ids)?.build()
5552
}
5653

5754
/// Returns a string with the coalesce formula for the given component IDs.
5855
///
5956
/// This formula uses the `COALESCE` function to return the first non-null
6057
/// value from the components with the provided IDs.
61-
pub fn coalesce(&self, component_ids: BTreeSet<u64>) -> Result<Formula, Error> {
58+
pub fn coalesce(&self, component_ids: BTreeSet<u64>) -> Result<String, Error> {
6259
generators::generic::CoalesceFormulaBuilder::try_new(self, component_ids)?.build()
6360
}
6461

6562
/// Returns a string representing the EV charger formula for the graph.
6663
pub fn ev_charger_formula(
6764
&self,
6865
ev_charger_ids: Option<BTreeSet<u64>>,
69-
) -> Result<Formula, Error> {
66+
) -> Result<String, Error> {
7067
generators::ev_charger::EVChargerFormulaBuilder::try_new(self, ev_charger_ids)?.build()
7168
}
7269
}

src/graph/formulas/formula.rs

Lines changed: 0 additions & 54 deletions
This file was deleted.

src/graph/formulas/generators/battery.rs

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
use std::collections::BTreeSet;
77

88
use crate::component_category::CategoryPredicates;
9-
use crate::graph::formulas::expr::Expr;
10-
use crate::graph::formulas::Formula;
119
use crate::{ComponentGraph, Edge, Error, Node};
1210

1311
pub(crate) struct BatteryFormulaBuilder<'a, N, E>
@@ -49,14 +47,14 @@ where
4947
/// This is the sum of all battery_inverters in the graph. If the
5048
/// battery_ids are provided, only the batteries with the given ids are
5149
/// included in the formula.
52-
pub fn build(self) -> Result<Formula, Error> {
50+
pub fn build(self) -> Result<String, Error> {
5351
if self.inverter_ids.is_empty() {
54-
return Ok(Formula::new(Expr::number(0.0)));
52+
return Ok("0.0".to_string());
5553
}
5654

5755
self.graph
5856
.fallback_expr(self.inverter_ids, false)
59-
.map(Formula::new)
57+
.map(|expr| expr.to_string())
6058
}
6159

6260
fn find_inverter_ids(
@@ -106,7 +104,7 @@ mod tests {
106104
builder.connect(grid, grid_meter);
107105

108106
let graph = builder.build(None)?;
109-
let formula = graph.battery_formula(None)?.to_string();
107+
let formula = graph.battery_formula(None)?;
110108
assert_eq!(formula, "0.0");
111109

112110
// Add a battery meter with one inverter and one battery.
@@ -117,7 +115,7 @@ mod tests {
117115
assert_eq!(meter_bat_chain.component_id(), 2);
118116

119117
let graph = builder.build(None)?;
120-
let formula = graph.battery_formula(None)?.to_string();
118+
let formula = graph.battery_formula(None)?;
121119
assert_eq!(formula, "COALESCE(#3, #2, 0.0)");
122120

123121
// Add a second battery meter with one inverter and two batteries.
@@ -127,22 +125,18 @@ mod tests {
127125
assert_eq!(meter_bat_chain.component_id(), 5);
128126

129127
let graph = builder.build(None)?;
130-
let formula = graph.battery_formula(None)?.to_string();
128+
let formula = graph.battery_formula(None)?;
131129
assert_eq!(formula, "COALESCE(#3, #2, 0.0) + COALESCE(#6, #5, 0.0)");
132130

133-
let formula = graph
134-
.battery_formula(Some(BTreeSet::from([4])))?
135-
.to_string();
131+
let formula = graph.battery_formula(Some(BTreeSet::from([4])))?;
136132
assert_eq!(formula, "COALESCE(#3, #2, 0.0)");
137133

138-
let formula = graph
139-
.battery_formula(Some(BTreeSet::from([7, 8])))?
140-
.to_string();
134+
let formula = graph.battery_formula(Some(BTreeSet::from([7, 8])))?;
141135
assert_eq!(formula, "COALESCE(#6, #5, 0.0)");
142136

143137
let formula = graph
144-
.battery_formula(Some(BTreeSet::from([4, 8, 7])))?
145-
.to_string();
138+
.battery_formula(Some(BTreeSet::from([4, 8, 7])))
139+
.unwrap();
146140
assert_eq!(formula, "COALESCE(#3, #2, 0.0) + COALESCE(#6, #5, 0.0)");
147141

148142
// Add a third battery meter with two inverters with two connected batteries.
@@ -152,7 +146,7 @@ mod tests {
152146
assert_eq!(meter_bat_chain.component_id(), 9);
153147

154148
let graph = builder.build(None)?;
155-
let formula = graph.battery_formula(None)?.to_string();
149+
let formula = graph.battery_formula(None)?;
156150
assert_eq!(
157151
formula,
158152
concat!(
@@ -163,8 +157,8 @@ mod tests {
163157
);
164158

165159
let formula = graph
166-
.battery_formula(Some(BTreeSet::from([12, 13])))?
167-
.to_string();
160+
.battery_formula(Some(BTreeSet::from([12, 13])))
161+
.unwrap();
168162
assert_eq!(
169163
formula,
170164
"COALESCE(#11 + #10, #9, COALESCE(#11, 0.0) + COALESCE(#10, 0.0))"
@@ -177,7 +171,7 @@ mod tests {
177171
assert_eq!(meter_pv_chain.component_id(), 14);
178172

179173
let graph = builder.build(None)?;
180-
let formula = graph.battery_formula(None)?.to_string();
174+
let formula = graph.battery_formula(None)?;
181175
assert_eq!(
182176
formula,
183177
concat!(
@@ -214,7 +208,7 @@ mod tests {
214208
allow_unspecified_inverters: true,
215209
..Default::default()
216210
}))?;
217-
let formula = graph.battery_formula(None)?.to_string();
211+
let formula = graph.battery_formula(None)?;
218212
assert_eq!(
219213
formula,
220214
concat!(
@@ -226,26 +220,22 @@ mod tests {
226220
);
227221

228222
let formula = graph
229-
.battery_formula(Some(BTreeSet::from([19, 21])))?
230-
.to_string();
223+
.battery_formula(Some(BTreeSet::from([19, 21])))
224+
.unwrap();
231225
assert_eq!(
232226
formula,
233227
"COALESCE(#20 + #18, #17, COALESCE(#20, 0.0) + COALESCE(#18, 0.0))"
234228
);
235229

236-
let formula = graph
237-
.battery_formula(Some(BTreeSet::from([19])))?
238-
.to_string();
230+
let formula = graph.battery_formula(Some(BTreeSet::from([19]))).unwrap();
239231
assert_eq!(formula, "COALESCE(#18, 0.0)");
240232

241-
let formula = graph
242-
.battery_formula(Some(BTreeSet::from([21])))?
243-
.to_string();
233+
let formula = graph.battery_formula(Some(BTreeSet::from([21]))).unwrap();
244234
assert_eq!(formula, "COALESCE(#20, 0.0)");
245235

246236
let formula = graph
247-
.battery_formula(Some(BTreeSet::from([4, 12, 13, 19])))?
248-
.to_string();
237+
.battery_formula(Some(BTreeSet::from([4, 12, 13, 19])))
238+
.unwrap();
249239
assert_eq!(
250240
formula,
251241
concat!(

src/graph/formulas/generators/chp.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
use std::collections::BTreeSet;
77

88
use crate::component_category::CategoryPredicates;
9-
use crate::graph::formulas::expr::Expr;
10-
use crate::graph::formulas::Formula;
119
use crate::{ComponentGraph, Edge, Error, Node};
1210

1311
pub(crate) struct CHPFormulaBuilder<'a, N, E>
@@ -45,9 +43,9 @@ where
4543
///
4644
/// This is the sum of all CHPs in the graph. If the chp_ids are provided,
4745
/// only the CHPs with the given ids are included in the formula.
48-
pub fn build(self) -> Result<Formula, Error> {
46+
pub fn build(self) -> Result<String, Error> {
4947
if self.chp_ids.is_empty() {
50-
return Ok(Formula::new(Expr::number(0.0)));
48+
return Ok("0.0".to_string());
5149
}
5250

5351
for id in &self.chp_ids {
@@ -61,7 +59,7 @@ where
6159

6260
self.graph
6361
.fallback_expr(self.chp_ids, false)
64-
.map(Formula::new)
62+
.map(|expr| expr.to_string())
6563
}
6664
}
6765

@@ -80,7 +78,7 @@ mod tests {
8078
builder.connect(grid, grid_meter);
8179

8280
let graph = builder.build(None)?;
83-
let formula = graph.chp_formula(None)?.to_string();
81+
let formula = graph.chp_formula(None)?;
8482
assert_eq!(formula, "0.0");
8583

8684
// Add a chp meter with one chp
@@ -91,7 +89,7 @@ mod tests {
9189
assert_eq!(meter_chp_chain.component_id(), 2);
9290

9391
let graph = builder.build(None)?;
94-
let formula = graph.chp_formula(None)?.to_string();
92+
let formula = graph.chp_formula(None)?;
9593
assert_eq!(formula, "COALESCE(#3, #2, 0.0)");
9694

9795
// Add a battery meter with one inverter and two batteries.
@@ -101,7 +99,7 @@ mod tests {
10199
assert_eq!(meter_bat_chain.component_id(), 4);
102100

103101
let graph = builder.build(None)?;
104-
let formula = graph.chp_formula(None)?.to_string();
102+
let formula = graph.chp_formula(None)?;
105103
assert_eq!(formula, "COALESCE(#3, #2, 0.0)");
106104

107105
// Add a chp meter with two CHPs.
@@ -111,7 +109,7 @@ mod tests {
111109
assert_eq!(meter_chp_chain.component_id(), 8);
112110

113111
let graph = builder.build(None)?;
114-
let formula = graph.chp_formula(None)?.to_string();
112+
let formula = graph.chp_formula(None)?;
115113
assert_eq!(
116114
formula,
117115
concat!(
@@ -120,9 +118,7 @@ mod tests {
120118
)
121119
);
122120

123-
let formula = graph
124-
.chp_formula(Some(BTreeSet::from([10, 3])))?
125-
.to_string();
121+
let formula = graph.chp_formula(Some(BTreeSet::from([10, 3]))).unwrap();
126122
assert_eq!(formula, "COALESCE(#3, #2, 0.0) + COALESCE(#10, 0.0)");
127123

128124
// add a meter direct to the grid with three CHPs
@@ -132,7 +128,7 @@ mod tests {
132128
assert_eq!(meter_chp_chain.component_id(), 11);
133129

134130
let graph = builder.build(None)?;
135-
let formula = graph.chp_formula(None)?.to_string();
131+
let formula = graph.chp_formula(None)?;
136132
assert_eq!(
137133
formula,
138134
concat!(
@@ -147,8 +143,8 @@ mod tests {
147143
);
148144

149145
let formula = graph
150-
.chp_formula(Some(BTreeSet::from([3, 9, 10, 12, 13])))?
151-
.to_string();
146+
.chp_formula(Some(BTreeSet::from([3, 9, 10, 12, 13])))
147+
.unwrap();
152148
assert_eq!(
153149
formula,
154150
concat!(
@@ -159,8 +155,8 @@ mod tests {
159155
);
160156

161157
let formula = graph
162-
.chp_formula(Some(BTreeSet::from([3, 9, 10, 12, 13, 14])))?
163-
.to_string();
158+
.chp_formula(Some(BTreeSet::from([3, 9, 10, 12, 13, 14])))
159+
.unwrap();
164160
assert_eq!(
165161
formula,
166162
concat!(
@@ -174,9 +170,7 @@ mod tests {
174170
),
175171
);
176172

177-
let formula = graph
178-
.chp_formula(Some(BTreeSet::from([10, 14])))?
179-
.to_string();
173+
let formula = graph.chp_formula(Some(BTreeSet::from([10, 14]))).unwrap();
180174
assert_eq!(formula, "COALESCE(#10, 0.0) + COALESCE(#14, 0.0)");
181175

182176
// Failure cases:

0 commit comments

Comments
 (0)