diff --git a/CHANGELOG.md b/CHANGELOG.md index cafa9fff6c..8e919c9339 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -64,6 +64,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Speeding up additionalActivationTicks in participant's BaseStateData [#421](https://github.com/ie3-institute/simona/pull/421) - Changed format of example grid `vn_simona` [#216](https://github.com/ie3-institute/simona/issues/216) - Renamed ChpData to ChpRelevantData [#494](https://github.com/ie3-institute/simona/issues/494) +- Changed handleResult function so that results are only updated if changes occur [#487](https://github.com/ie3-institute/simona/issues/487) ### Fixed - Location of `vn_simona` test grid (was partially in Berlin and Dortmund) [#72](https://github.com/ie3-institute/simona/issues/72) diff --git a/build.gradle b/build.gradle index be01a4540e..6c6895e600 100644 --- a/build.gradle +++ b/build.gradle @@ -152,6 +152,7 @@ dependencies { implementation "com.sksamuel.avro4s:avro4s-core_${scalaVersion}:4.1.1" implementation 'org.apache.commons:commons-math3:3.6.1' // apache commons math3 + implementation 'org.apache.commons:commons-lang3:3.12.0' // apache commons lang3 FIXME Temporarily used for cloning implementation 'org.apache.poi:poi-ooxml:5.2.3' // used for FilenameUtils implementation 'javax.measure:unit-api:2.1.3' implementation 'tech.units:indriya:2.1.4' // quantities diff --git a/src/main/scala/edu/ie3/simona/event/listener/ResultEventListener.scala b/src/main/scala/edu/ie3/simona/event/listener/ResultEventListener.scala index b607dc5f50..4700cf17ce 100644 --- a/src/main/scala/edu/ie3/simona/event/listener/ResultEventListener.scala +++ b/src/main/scala/edu/ie3/simona/event/listener/ResultEventListener.scala @@ -36,7 +36,10 @@ import edu.ie3.simona.io.result._ import edu.ie3.simona.logging.SimonaFSMActorLogging import edu.ie3.simona.ontology.messages.StopMessage import edu.ie3.simona.util.ResultFileHierarchy +import org.apache.commons.lang3.SerializationUtils +import sourcecode.Text.generate +import java.util.UUID import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.duration.DurationInt import scala.concurrent.{Await, Future} @@ -64,13 +67,16 @@ object ResultEventListener extends Transformer3wResultSupport { * @param classToSink * a map containing the sink for each class that should be processed by the * listener + * @param previousResults + * a map containing only the previous results */ private final case class BaseData( classToSink: Map[Class[_], ResultEntitySink], threeWindingResults: Map[ Transformer3wKey, AggregatedTransformer3wResult - ] = Map.empty + ] = Map.empty, + previousResults: Map[UUID, ResultEntity] = Map.empty ) extends ResultEventListenerData def props( @@ -190,7 +196,11 @@ class ResultEventListener( self ! Init } - /** Handle the given result and possibly update the state data + /** Handle the given result and possibly update the state data The state data + * is compared to the previous corresponding data. Both maps are only updated + * if the state data has changed. Before comparing both maps, previous + * results are cloned and the time is adapted to the corresponding time in + * the state data. * * @param resultEntity * Result entity to handle @@ -203,8 +213,21 @@ class ResultEventListener( resultEntity: ResultEntity, baseData: BaseData ): BaseData = { - handOverToSink(resultEntity, baseData.classToSink) - baseData + val previousResult = baseData.previousResults.get(resultEntity.getUuid) + val previousResultClone = previousResult.map { previousResult => + val resultEntityClone = SerializationUtils.clone(previousResult) + resultEntityClone.setTime(resultEntity.getTime) + resultEntityClone + } + if (previousResultClone.exists(_.equals(resultEntity))) { + baseData + } else { + val updatedPreviousResults = + baseData.previousResults + (resultEntity.getUuid -> SerializationUtils + .clone(resultEntity)) + handOverToSink(resultEntity, baseData.classToSink) + baseData.copy(previousResults = updatedPreviousResults) + } } /** Handle a partial three winding result properly by adding it to an