From 8ee3b77bb1c97b5105d8d102ea1bb6331a70b548 Mon Sep 17 00:00:00 2001 From: danielfeismann Date: Wed, 21 Aug 2024 19:28:16 +0200 Subject: [PATCH 1/2] refactor ThermalEnergyDemand --- .../simona/model/thermal/ThermalGrid.scala | 20 +++----- .../model/thermal/ThermalGridSpec.scala | 47 ++++++++++++++----- 2 files changed, 43 insertions(+), 24 deletions(-) diff --git a/src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala b/src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala index 85b41b35ff..c38a72d285 100644 --- a/src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala +++ b/src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala @@ -9,15 +9,10 @@ package edu.ie3.simona.model.thermal import com.typesafe.scalalogging.LazyLogging import edu.ie3.datamodel.models.input.thermal.CylindricalStorageInput import edu.ie3.datamodel.models.result.ResultEntity -import edu.ie3.datamodel.models.result.thermal.{ - CylindricalStorageResult, - ThermalHouseResult, -} +import edu.ie3.datamodel.models.result.thermal.{CylindricalStorageResult, ThermalHouseResult} +import edu.ie3.simona.exceptions.InvalidParameterException import edu.ie3.simona.exceptions.agent.InconsistentStateException -import edu.ie3.simona.model.thermal.ThermalGrid.{ - ThermalEnergyDemand, - ThermalGridState, -} +import edu.ie3.simona.model.thermal.ThermalGrid.{ThermalEnergyDemand, ThermalGridState} import edu.ie3.simona.model.thermal.ThermalHouse.ThermalHouseState import edu.ie3.simona.model.thermal.ThermalStorage.ThermalStorageState import edu.ie3.simona.util.TickUtil.TickLong @@ -483,13 +478,12 @@ object ThermalGrid { def hasRequiredDemand: Boolean = required > zeroMWH - def hasAdditionalDemand: Boolean = possible > required + def hasAdditionalDemand: Boolean = possible > zeroMWH } object ThermalEnergyDemand { /** Builds a new instance of [[ThermalEnergyDemand]]. If the possible energy - * is less than the required energy, this is considered to be a bad state - * and the required energy is curtailed to the possible energy. + * is less than the required energy, this is considered to be a bad state. * @param required * The absolutely required energy to reach target state * @param possible @@ -501,8 +495,8 @@ object ThermalGrid { required: Energy, possible: Energy, ): ThermalEnergyDemand = { - if (possible < required) - new ThermalEnergyDemand(possible, possible) + if (math.abs(possible.toKilowattHours) < math.abs(required.toKilowattHours)) + throw new InvalidParameterException(s"The possible amount of energy {$possible} is smaller than the required amount of energy {$required}. This is not supported.") else new ThermalEnergyDemand(required, possible) } diff --git a/src/test/scala/edu/ie3/simona/model/thermal/ThermalGridSpec.scala b/src/test/scala/edu/ie3/simona/model/thermal/ThermalGridSpec.scala index e4c1c14c70..9244a28bdd 100644 --- a/src/test/scala/edu/ie3/simona/model/thermal/ThermalGridSpec.scala +++ b/src/test/scala/edu/ie3/simona/model/thermal/ThermalGridSpec.scala @@ -6,6 +6,7 @@ package edu.ie3.simona.model.thermal +import edu.ie3.simona.exceptions.InvalidParameterException import edu.ie3.simona.model.thermal.ThermalGrid.ThermalEnergyDemand import edu.ie3.simona.test.common.UnitSpec import squants.energy.{MegawattHours, WattHours, Watts} @@ -20,16 +21,23 @@ class ThermalGridSpec extends UnitSpec { "Testing the thermal energy demand" when { "instantiating it from given values" should { - "correct non-sensible input" in { + "throw exception for non-sensible input (positive)" in { val possible = MegawattHours(40d) val required = MegawattHours(42d) - val energyDemand = ThermalEnergyDemand(required, possible) + intercept[InvalidParameterException] {ThermalEnergyDemand(required, possible) + }.getMessage shouldBe s"The possible amount of energy {$possible} is smaller than the required amount of energy {$required}. This is not supported." + } - energyDemand.required should approximate(possible) - energyDemand.possible should approximate(possible) + "throw exception for non-sensible input (negative)" in { + val possible = MegawattHours(-40d) + val required = MegawattHours(-42d) + + intercept[InvalidParameterException] {ThermalEnergyDemand(required, possible) + }.getMessage shouldBe s"The possible amount of energy {$possible} is smaller than the required amount of energy {$required}. This is not supported." } + "set the correct values, if they are sensible" in { val possible = MegawattHours(45d) val required = MegawattHours(42d) @@ -51,22 +59,29 @@ class ThermalGridSpec extends UnitSpec { } "checking for required and additional demand" should { - "return proper information, if no required but additional demand is apparent" in { + "return proper information, if no required and no additional demand is apparent" in { val required = MegawattHours(0d) - val possible = MegawattHours(45d) + val possible = MegawattHours(0d) val energyDemand = ThermalEnergyDemand(required, possible) energyDemand.hasRequiredDemand shouldBe false - energyDemand.hasAdditionalDemand shouldBe true + energyDemand.hasAdditionalDemand shouldBe false } - "return proper information, if required but no additional demand is apparent" in { - val required = MegawattHours(45d) + "return proper information, if no required but additional demand is apparent" in { + val required = MegawattHours(0d) val possible = MegawattHours(45d) val energyDemand = ThermalEnergyDemand(required, possible) - energyDemand.hasRequiredDemand shouldBe true - energyDemand.hasAdditionalDemand shouldBe false + energyDemand.hasRequiredDemand shouldBe false + energyDemand.hasAdditionalDemand shouldBe true + } + + "throw exception, if required demand is higher than possible demand" in { + val required = MegawattHours(1d) + val possible = MegawattHours(0d) + intercept[InvalidParameterException] {ThermalEnergyDemand(required, possible) + }.getMessage shouldBe s"The possible amount of energy {$possible} is smaller than the required amount of energy {$required}. This is not supported." } "return proper information, if required and additional demand is apparent" in { @@ -77,6 +92,16 @@ class ThermalGridSpec extends UnitSpec { energyDemand.hasRequiredDemand shouldBe true energyDemand.hasAdditionalDemand shouldBe true } + + //FIXME: Think about "negative demand", maybe add more cases as well + "return proper information, if no required but additional demand is apparent (negative)" in { + val required = MegawattHours(-10d) + val possible = MegawattHours(-45d) + + val energyDemand = ThermalEnergyDemand(required, possible) + energyDemand.hasRequiredDemand shouldBe false + energyDemand.hasAdditionalDemand shouldBe true + } } "adding two demands" should { From ced3b555d016db7d358a9bec890c7d65022142c3 Mon Sep 17 00:00:00 2001 From: danielfeismann Date: Wed, 21 Aug 2024 19:29:30 +0200 Subject: [PATCH 2/2] fmt --- .../ie3/simona/model/thermal/ThermalGrid.scala | 18 ++++++++++++++---- .../simona/model/thermal/ThermalGridSpec.scala | 12 +++++++----- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala b/src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala index c38a72d285..4f8f3e0631 100644 --- a/src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala +++ b/src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala @@ -9,10 +9,16 @@ package edu.ie3.simona.model.thermal import com.typesafe.scalalogging.LazyLogging import edu.ie3.datamodel.models.input.thermal.CylindricalStorageInput import edu.ie3.datamodel.models.result.ResultEntity -import edu.ie3.datamodel.models.result.thermal.{CylindricalStorageResult, ThermalHouseResult} +import edu.ie3.datamodel.models.result.thermal.{ + CylindricalStorageResult, + ThermalHouseResult, +} import edu.ie3.simona.exceptions.InvalidParameterException import edu.ie3.simona.exceptions.agent.InconsistentStateException -import edu.ie3.simona.model.thermal.ThermalGrid.{ThermalEnergyDemand, ThermalGridState} +import edu.ie3.simona.model.thermal.ThermalGrid.{ + ThermalEnergyDemand, + ThermalGridState, +} import edu.ie3.simona.model.thermal.ThermalHouse.ThermalHouseState import edu.ie3.simona.model.thermal.ThermalStorage.ThermalStorageState import edu.ie3.simona.util.TickUtil.TickLong @@ -495,8 +501,12 @@ object ThermalGrid { required: Energy, possible: Energy, ): ThermalEnergyDemand = { - if (math.abs(possible.toKilowattHours) < math.abs(required.toKilowattHours)) - throw new InvalidParameterException(s"The possible amount of energy {$possible} is smaller than the required amount of energy {$required}. This is not supported.") + if ( + math.abs(possible.toKilowattHours) < math.abs(required.toKilowattHours) + ) + throw new InvalidParameterException( + s"The possible amount of energy {$possible} is smaller than the required amount of energy {$required}. This is not supported." + ) else new ThermalEnergyDemand(required, possible) } diff --git a/src/test/scala/edu/ie3/simona/model/thermal/ThermalGridSpec.scala b/src/test/scala/edu/ie3/simona/model/thermal/ThermalGridSpec.scala index 9244a28bdd..b59e6000fb 100644 --- a/src/test/scala/edu/ie3/simona/model/thermal/ThermalGridSpec.scala +++ b/src/test/scala/edu/ie3/simona/model/thermal/ThermalGridSpec.scala @@ -25,7 +25,8 @@ class ThermalGridSpec extends UnitSpec { val possible = MegawattHours(40d) val required = MegawattHours(42d) - intercept[InvalidParameterException] {ThermalEnergyDemand(required, possible) + intercept[InvalidParameterException] { + ThermalEnergyDemand(required, possible) }.getMessage shouldBe s"The possible amount of energy {$possible} is smaller than the required amount of energy {$required}. This is not supported." } @@ -33,11 +34,11 @@ class ThermalGridSpec extends UnitSpec { val possible = MegawattHours(-40d) val required = MegawattHours(-42d) - intercept[InvalidParameterException] {ThermalEnergyDemand(required, possible) + intercept[InvalidParameterException] { + ThermalEnergyDemand(required, possible) }.getMessage shouldBe s"The possible amount of energy {$possible} is smaller than the required amount of energy {$required}. This is not supported." } - "set the correct values, if they are sensible" in { val possible = MegawattHours(45d) val required = MegawattHours(42d) @@ -80,7 +81,8 @@ class ThermalGridSpec extends UnitSpec { "throw exception, if required demand is higher than possible demand" in { val required = MegawattHours(1d) val possible = MegawattHours(0d) - intercept[InvalidParameterException] {ThermalEnergyDemand(required, possible) + intercept[InvalidParameterException] { + ThermalEnergyDemand(required, possible) }.getMessage shouldBe s"The possible amount of energy {$possible} is smaller than the required amount of energy {$required}. This is not supported." } @@ -93,7 +95,7 @@ class ThermalGridSpec extends UnitSpec { energyDemand.hasAdditionalDemand shouldBe true } - //FIXME: Think about "negative demand", maybe add more cases as well + // FIXME: Think about "negative demand", maybe add more cases as well "return proper information, if no required but additional demand is apparent (negative)" in { val required = MegawattHours(-10d) val possible = MegawattHours(-45d)