Skip to content

Commit 816d558

Browse files
committed
Refactor to not store a graph reference in FallbackExpr
Signed-off-by: Sahas Subramanian <[email protected]>
1 parent c2f1c50 commit 816d558

File tree

1 file changed

+29
-31
lines changed

1 file changed

+29
-31
lines changed

src/graph/formulas/fallback.rs

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,39 +25,35 @@ where
2525
FallbackExpr {
2626
prefer_meters,
2727
meter_fallback_for_meters,
28-
graph: self,
2928
}
30-
.generate(BTreeSet::from_iter(component_ids))
29+
.generate(self, BTreeSet::from_iter(component_ids))
3130
}
3231
}
3332

34-
struct FallbackExpr<'a, N, E>
35-
where
36-
N: Node,
37-
E: Edge,
38-
{
33+
struct FallbackExpr {
3934
pub(crate) prefer_meters: bool,
4035
pub(crate) meter_fallback_for_meters: bool,
41-
pub(crate) graph: &'a ComponentGraph<N, E>,
4236
}
4337

44-
impl<N, E> FallbackExpr<'_, N, E>
45-
where
46-
N: Node,
47-
E: Edge,
48-
{
49-
fn generate(&self, mut component_ids: BTreeSet<u64>) -> Result<Expr, Error> {
38+
impl FallbackExpr {
39+
fn generate<N: Node, E: Edge>(
40+
&self,
41+
graph: &ComponentGraph<N, E>,
42+
mut component_ids: BTreeSet<u64>,
43+
) -> Result<Expr, Error> {
5044
let mut formula = None::<Expr>;
51-
if self.graph.config.disable_fallback_components {
45+
if graph.config.disable_fallback_components {
5246
while let Some(component_id) = component_ids.pop_first() {
5347
formula = Self::add_to_option(formula, Expr::component(component_id));
5448
}
5549
return formula.ok_or(Error::internal("No components to generate formula."));
5650
}
5751
while let Some(component_id) = component_ids.pop_first() {
58-
if let Some(expr) = self.meter_fallback(component_id)? {
52+
if let Some(expr) = self.meter_fallback(graph, component_id)? {
5953
formula = Self::add_to_option(formula, expr);
60-
} else if let Some(expr) = self.component_fallback(&mut component_ids, component_id)? {
54+
} else if let Some(expr) =
55+
self.component_fallback(graph, &mut component_ids, component_id)?
56+
{
6157
formula = Self::add_to_option(formula, expr);
6258
} else {
6359
formula = Self::add_to_option(formula, Expr::component(component_id));
@@ -68,23 +64,26 @@ where
6864
}
6965

7066
/// Returns a fallback expression for a meter component.
71-
fn meter_fallback(&self, component_id: u64) -> Result<Option<Expr>, Error> {
72-
let component = self.graph.component(component_id)?;
67+
fn meter_fallback<N: Node, E: Edge>(
68+
&self,
69+
graph: &ComponentGraph<N, E>,
70+
component_id: u64,
71+
) -> Result<Option<Expr>, Error> {
72+
let component = graph.component(component_id)?;
7373
if !component.is_meter() {
7474
return Ok(None);
7575
}
76-
let has_successor_meters = self.graph.has_meter_successors(component_id)?;
76+
let has_successor_meters = graph.has_meter_successors(component_id)?;
7777

7878
if !self.meter_fallback_for_meters && has_successor_meters {
7979
return Ok(Some(Expr::component(component_id)));
8080
}
8181

82-
if !self.graph.has_successors(component_id)? {
82+
if !graph.has_successors(component_id)? {
8383
return Ok(Some(Expr::component(component_id)));
8484
}
8585

86-
let (sum_of_successors, sum_of_coalesced_successors) = self
87-
.graph
86+
let (sum_of_successors, sum_of_coalesced_successors) = graph
8887
.successors(component_id)?
8988
.map(|node| {
9089
(
@@ -136,13 +135,14 @@ where
136135
/// - Battery Inverter
137136
/// - PV Inverter
138137
/// - EV Charger
139-
fn component_fallback(
138+
fn component_fallback<N: Node, E: Edge>(
140139
&self,
140+
graph: &ComponentGraph<N, E>,
141141
component_ids: &mut BTreeSet<u64>,
142142
component_id: u64,
143143
) -> Result<Option<Expr>, Error> {
144-
let component = self.graph.component(component_id)?;
145-
if !component.is_battery_inverter(&self.graph.config)
144+
let component = graph.component(component_id)?;
145+
if !component.is_battery_inverter(&graph.config)
146146
&& !component.is_chp()
147147
&& !component.is_pv_inverter()
148148
&& !component.is_ev_charger()
@@ -152,8 +152,7 @@ where
152152

153153
// If predecessors have other successors that are not in the list of
154154
// component ids, the predecessors can't be used as fallback.
155-
let siblings = self
156-
.graph
155+
let siblings = graph
157156
.siblings_from_predecessors(component_id)?
158157
.filter(|sibling| sibling.component_id() != component_id)
159158
.collect::<Vec<_>>();
@@ -168,8 +167,7 @@ where
168167
}
169168

170169
// Collect predecessor meter ids.
171-
let predecessor_ids: BTreeSet<u64> = self
172-
.graph
170+
let predecessor_ids: BTreeSet<u64> = graph
173171
.predecessors(component_id)?
174172
.filter(|x| x.is_meter())
175173
.map(|x| x.component_id())
@@ -186,7 +184,7 @@ where
186184
component_ids.remove(&sibling.component_id());
187185
}
188186

189-
Ok(Some(self.generate(predecessor_ids)?))
187+
Ok(Some(self.generate(graph, predecessor_ids)?))
190188
}
191189

192190
fn add_to_option(expr: Option<Expr>, other: Expr) -> Option<Expr> {

0 commit comments

Comments
 (0)