Skip to content

Commit 4ddea4b

Browse files
authored
Formula generation support (#2)
This PR adds formula generators for various microgrid metrics that traverse the graph to produce and return string formulas.
2 parents 1ef538f + 75d067e commit 4ddea4b

24 files changed

+2805
-152
lines changed

.github/workflows/ci.yaml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,7 @@ jobs:
1616
- name: Fetch sources
1717
uses: actions/checkout@v4
1818
with:
19-
submodules: true
20-
21-
- name: Check formatting
22-
run: cargo fmt --check
23-
24-
- name: Run linter
25-
run: cargo clippy -- -W clippy::unwrap_used -W clippy::expect_used -W clippy::panic
19+
submodules: recursive
2620

2721
- name: Run tests
28-
run: cargo test
22+
uses: frequenz-floss/gh-action-cargo-test@v1.0.0

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ This is a library for representing the components of a microgrid and the
44
connections between them as a Directed Acyclic Graph (DAG).
55

66
Current features:
7-
- validating the graph
8-
- traversing the graph
7+
- Validating the graph
8+
- Traversing the graph
9+
- Formula generation
910

1011
Upcoming features:
11-
- Formula generation
1212
- Python bindings

src/graph.rs

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod meter_roles;
99
mod retrieval;
1010
mod validation;
1111

12+
mod formulas;
1213
pub mod iterators;
1314

1415
use crate::{Edge, Node};
@@ -42,53 +43,4 @@ where
4243
}
4344

4445
#[cfg(test)]
45-
mod test_types {
46-
//! This module contains the `TestComponent` and `TestConnection` types,
47-
//! which implement the `Node` and `Edge` traits respectively.
48-
//!
49-
//! They are shared by all the test modules in the `graph` module.
50-
51-
use crate::{ComponentCategory, Edge, Node};
52-
53-
#[derive(Clone, Debug, PartialEq)]
54-
pub(super) struct TestComponent(u64, ComponentCategory);
55-
56-
impl TestComponent {
57-
pub(super) fn new(id: u64, category: ComponentCategory) -> Self {
58-
TestComponent(id, category)
59-
}
60-
}
61-
62-
impl Node for TestComponent {
63-
fn component_id(&self) -> u64 {
64-
self.0
65-
}
66-
67-
fn category(&self) -> ComponentCategory {
68-
self.1.clone()
69-
}
70-
71-
fn is_supported(&self) -> bool {
72-
true
73-
}
74-
}
75-
76-
#[derive(Clone, Debug, PartialEq)]
77-
pub(super) struct TestConnection(u64, u64);
78-
79-
impl TestConnection {
80-
pub(super) fn new(source: u64, destination: u64) -> Self {
81-
TestConnection(source, destination)
82-
}
83-
}
84-
85-
impl Edge for TestConnection {
86-
fn source(&self) -> u64 {
87-
self.0
88-
}
89-
90-
fn destination(&self) -> u64 {
91-
self.1
92-
}
93-
}
94-
}
46+
mod test_utils;

src/graph/creation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ where
118118
mod tests {
119119
use super::*;
120120
use crate::component_category::BatteryType;
121-
use crate::graph::test_types::{TestComponent, TestConnection};
121+
use crate::graph::test_utils::{TestComponent, TestConnection};
122122
use crate::ComponentCategory;
123123
use crate::InverterType;
124124

src/graph/formulas.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// License: MIT
2+
// Copyright © 2024 Frequenz Energy-as-a-Service GmbH
3+
4+
//! Methods for building formulas for various microgrid metrics.
5+
6+
use std::collections::BTreeSet;
7+
8+
use crate::ComponentGraph;
9+
use crate::Edge;
10+
use crate::Error;
11+
use crate::Node;
12+
13+
mod expr;
14+
mod fallback;
15+
mod generators;
16+
mod traversal;
17+
18+
/// Formulas for various microgrid metrics.
19+
impl<N, E> ComponentGraph<N, E>
20+
where
21+
N: Node,
22+
E: Edge,
23+
{
24+
/// Returns a string representing the consumer formula for the graph.
25+
pub fn consumer_formula(&self) -> Result<String, Error> {
26+
generators::consumer::ConsumerFormulaBuilder::try_new(self)?.build()
27+
}
28+
29+
/// Returns a string representing the grid formula for the graph.
30+
pub fn grid_formula(&self) -> Result<String, Error> {
31+
generators::grid::GridFormulaBuilder::try_new(self)?.build()
32+
}
33+
34+
/// Returns a string representing the producer formula for the graph.
35+
pub fn producer_formula(&self) -> Result<String, Error> {
36+
generators::producer::ProducerFormulaBuilder::try_new(self)?.build()
37+
}
38+
39+
/// Returns a string representing the battery formula for the graph.
40+
pub fn battery_formula(&self, battery_ids: Option<BTreeSet<u64>>) -> Result<String, Error> {
41+
generators::battery::BatteryFormulaBuilder::try_new(self, battery_ids)?.build()
42+
}
43+
44+
/// Returns a string representing the CHP formula for the graph.
45+
pub fn chp_formula(&self, chp_ids: Option<BTreeSet<u64>>) -> Result<String, Error> {
46+
generators::chp::CHPFormulaBuilder::try_new(self, chp_ids)?.build()
47+
}
48+
49+
/// Returns a string representing the PV formula for the graph.
50+
pub fn pv_formula(&self, pv_inverter_ids: Option<BTreeSet<u64>>) -> Result<String, Error> {
51+
generators::pv::PVFormulaBuilder::try_new(self, pv_inverter_ids)?.build()
52+
}
53+
54+
/// Returns a string representing the EV charger formula for the graph.
55+
pub fn ev_charger_formula(
56+
&self,
57+
ev_charger_ids: Option<BTreeSet<u64>>,
58+
) -> Result<String, Error> {
59+
generators::ev_charger::EVChargerFormulaBuilder::try_new(self, ev_charger_ids)?.build()
60+
}
61+
}

0 commit comments

Comments
 (0)