Skip to content

Commit b029006

Browse files
Fixing some code smells and simplifying code
Signed-off-by: Sebastian Peter <[email protected]>
1 parent 465bdcf commit b029006

File tree

4 files changed

+103
-62
lines changed

4 files changed

+103
-62
lines changed

src/main/scala/edu/ie3/simona/agent/participant2/ParticipantAgent.scala

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -367,11 +367,7 @@ object ParticipantAgent {
367367
throw new CriticalFailureException(
368368
"Received flex activation while not controlled by EM"
369369
),
370-
_.emAgent ! modelWithFlex.flexOptions.getOrElse(
371-
throw new CriticalFailureException(
372-
"Flex options have not been calculated!"
373-
)
374-
),
370+
_.emAgent ! modelWithFlex.flexOptions,
375371
)
376372

377373
(modelWithFlex, gridAdapter)
@@ -406,7 +402,7 @@ object ParticipantAgent {
406402
"Received issue flex control while not controlled by EM"
407403
),
408404
_.emAgent ! FlexCompletion(
409-
shellWithOP.model.uuid,
405+
shellWithOP.uuid,
410406
changeIndicator.changesAtNextActivation,
411407
changeIndicator.changesAtTick,
412408
),

src/main/scala/edu/ie3/simona/agent/participant2/ParticipantAgentInit.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,7 @@ object ParticipantAgentInit {
176176
simulationEndDate,
177177
)
178178

179-
val requiredServiceTypes =
180-
modelShell.model.getRequiredSecondaryServices.toSet
179+
val requiredServiceTypes = modelShell.requiredServices.toSet
181180

182181
if (requiredServiceTypes.isEmpty) {
183182
// Models that do not use secondary data always start at tick 0
@@ -278,7 +277,7 @@ object ParticipantAgentInit {
278277
Some(firstTick),
279278
),
280279
_.emAgent ! FlexCompletion(
281-
modelShell.model.uuid,
280+
modelShell.uuid,
282281
requestAtNextActivation = false,
283282
Some(firstTick),
284283
),

src/main/scala/edu/ie3/simona/agent/participant2/ParticipantGridAdapter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ object ParticipantGridAdapter {
157157
val lastTickBeforeWindowStart =
158158
tickToPower.rangeUntil(windowStart + 1).lastOption
159159

160-
// throw out all entries before or at windowStart
160+
// remove all entries before or at windowStart
161161
val reducedMap = tickToPower.rangeFrom(windowStart + 1)
162162

163163
// combine both

src/main/scala/edu/ie3/simona/model/participant2/ParticipantModelShell.scala

Lines changed: 98 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import edu.ie3.simona.exceptions.CriticalFailureException
1818
import edu.ie3.simona.model.SystemComponent
1919
import edu.ie3.simona.model.em.EmTools
2020
import edu.ie3.simona.model.participant2.ParticipantModel.{
21-
OperationChangeIndicator,
2221
ModelState,
2322
OperatingPoint,
23+
OperationChangeIndicator,
2424
OperationRelevantData,
2525
}
2626
import edu.ie3.simona.model.participant2.ParticipantModelShell.ResultsContainer
@@ -29,6 +29,7 @@ import edu.ie3.simona.ontology.messages.flex.FlexibilityMessage.{
2929
ProvideFlexOptions,
3030
}
3131
import edu.ie3.simona.ontology.messages.flex.MinMaxFlexibilityMessage.ProvideMinMaxFlexOptions
32+
import edu.ie3.simona.service.ServiceType
3233
import edu.ie3.simona.util.TickUtil.TickLong
3334
import edu.ie3.util.scala.OperationInterval
3435
import edu.ie3.util.scala.quantities.DefaultQuantities._
@@ -38,6 +39,7 @@ import squants.Dimensionless
3839
import squants.energy.Power
3940

4041
import java.time.ZonedDateTime
42+
import java.util.UUID
4143
import scala.reflect.ClassTag
4244

4345
/** Takes care of:
@@ -53,18 +55,75 @@ final case class ParticipantModelShell[
5355
S <: ModelState,
5456
OR <: OperationRelevantData,
5557
](
56-
model: ParticipantModel[OP, S, OR] with ParticipantFlexibility[OP, S, OR],
57-
operationInterval: OperationInterval,
58-
simulationStartDate: ZonedDateTime,
59-
state: Option[S] = None,
60-
relevantData: Option[OR] = None,
61-
flexOptions: Option[ProvideFlexOptions] = None,
62-
lastOperatingPoint: Option[OP] = None,
63-
operatingPoint: Option[OP] = None,
64-
private val modelChange: OperationChangeIndicator =
58+
private val model: ParticipantModel[OP, S, OR]
59+
with ParticipantFlexibility[OP, S, OR],
60+
private val operationInterval: OperationInterval,
61+
private val simulationStartDate: ZonedDateTime,
62+
private val _state: Option[S] = None,
63+
private val _relevantData: Option[OR] = None,
64+
private val _flexOptions: Option[ProvideFlexOptions] = None,
65+
private val _lastOperatingPoint: Option[OP] = None,
66+
private val _operatingPoint: Option[OP] = None,
67+
private val _modelChange: OperationChangeIndicator =
6568
OperationChangeIndicator(),
6669
) {
6770

71+
/** Returns the model UUID.
72+
*
73+
* @return
74+
* the UUID of the model
75+
*/
76+
def uuid: UUID = model.uuid
77+
78+
/** Returns the types of required secondary services for the model to
79+
* function.
80+
*
81+
* @return
82+
* the types of secondary services required
83+
*/
84+
def requiredServices: Iterable[ServiceType] =
85+
model.getRequiredSecondaryServices
86+
87+
/** Returns the current relevant data, if present, or throws a
88+
* [[CriticalFailureException]]. Only call this if you are certain the
89+
* operation relevant data has been set.
90+
*
91+
* @return
92+
* the operation relevant data
93+
*/
94+
private def getRelevantData: OR =
95+
_relevantData.getOrElse(
96+
throw new CriticalFailureException("No relevant data available!")
97+
)
98+
99+
/** Returns the current operating point, if present, or throws a
100+
* [[CriticalFailureException]]. Only call this if you are certain the
101+
* operating point has been set.
102+
*
103+
* @return
104+
* the operating point
105+
*/
106+
private def operatingPoint: OP = {
107+
_operatingPoint
108+
.getOrElse(
109+
throw new CriticalFailureException("No operating point available!")
110+
)
111+
}
112+
113+
/** Returns the current flex options, if present, or throws a
114+
* [[CriticalFailureException]]. Only call this if you are certain the flex
115+
* options have been set.
116+
*
117+
* @return
118+
* the flex options
119+
*/
120+
def flexOptions: ProvideFlexOptions =
121+
_flexOptions.getOrElse(
122+
throw new CriticalFailureException(
123+
"Flex options have not been calculated!"
124+
)
125+
)
126+
68127
def updateRelevantData(
69128
receivedData: Seq[Data],
70129
nodalVoltage: Dimensionless,
@@ -79,7 +138,7 @@ final case class ParticipantModelShell[
79138
currentSimulationTime,
80139
)
81140

82-
copy(relevantData = Some(updatedRelevantData))
141+
copy(_relevantData = Some(updatedRelevantData))
83142
}
84143

85144
/** Update operating point when the model is '''not''' em-controlled.
@@ -92,12 +151,10 @@ final case class ParticipantModelShell[
92151
): ParticipantModelShell[OP, S, OR] = {
93152
val currentState = determineCurrentState(currentTick)
94153

95-
def modelOperatingPoint() = {
154+
def modelOperatingPoint(): (OP, OperationChangeIndicator) = {
96155
val (modelOp, modelNextTick) = model.determineOperatingPoint(
97156
currentState,
98-
relevantData.getOrElse(
99-
throw new CriticalFailureException("No relevant data available!")
100-
),
157+
getRelevantData,
101158
)
102159
val modelIndicator =
103160
OperationChangeIndicator(changesAtTick = modelNextTick)
@@ -108,10 +165,10 @@ final case class ParticipantModelShell[
108165
determineOperatingPoint(modelOperatingPoint, currentTick)
109166

110167
copy(
111-
state = Some(currentState),
112-
lastOperatingPoint = operatingPoint,
113-
operatingPoint = Some(newOperatingPoint),
114-
modelChange = newChangeIndicator,
168+
_state = Some(currentState),
169+
_lastOperatingPoint = _operatingPoint,
170+
_operatingPoint = Some(newOperatingPoint),
171+
_modelChange = newChangeIndicator,
115172
)
116173
}
117174

@@ -122,23 +179,16 @@ final case class ParticipantModelShell[
122179
currentTick: Long,
123180
nodalVoltage: Dimensionless,
124181
): ResultsContainer = {
125-
val op = operatingPoint
126-
.getOrElse(
127-
throw new CriticalFailureException("No operating point available!")
128-
)
129-
130-
val activePower = op.activePower
131-
val reactivePower = op.reactivePower.getOrElse(
182+
val activePower = operatingPoint.activePower
183+
val reactivePower = operatingPoint.reactivePower.getOrElse(
132184
activeToReactivePowerFunc(nodalVoltage)(activePower)
133185
)
134186
val complexPower = ComplexPower(activePower, reactivePower)
135187

136188
val participantResults = model.createResults(
137-
state.getOrElse(
138-
throw new CriticalFailureException("No model state available!")
139-
),
140-
lastOperatingPoint,
141-
op,
189+
determineCurrentState(currentTick),
190+
_lastOperatingPoint,
191+
operatingPoint,
142192
complexPower,
143193
currentTick.toDateTime(simulationStartDate),
144194
)
@@ -156,16 +206,14 @@ final case class ParticipantModelShell[
156206
if (operationInterval.includes(currentTick)) {
157207
model.calcFlexOptions(
158208
currentState,
159-
relevantData.getOrElse(
160-
throw new CriticalFailureException("No relevant data available!")
161-
),
209+
getRelevantData,
162210
)
163211
} else {
164212
// Out of operation, there's no way to operate besides 0 kW
165213
ProvideMinMaxFlexOptions.noFlexOption(model.uuid, zeroKW)
166214
}
167215

168-
copy(state = Some(currentState), flexOptions = Some(flexOptions))
216+
copy(_state = Some(currentState), _flexOptions = Some(flexOptions))
169217
}
170218

171219
/** Update operating point on receiving [[IssueFlexControl]], i.e. when the
@@ -181,8 +229,8 @@ final case class ParticipantModelShell[
181229

182230
val currentTick = flexControl.tick
183231

184-
def modelOperatingPoint() = {
185-
val fo = flexOptions.getOrElse(
232+
def modelOperatingPoint(): (OP, OperationChangeIndicator) = {
233+
val fo = _flexOptions.getOrElse(
186234
throw new CriticalFailureException("No flex options available!")
187235
)
188236

@@ -193,9 +241,7 @@ final case class ParticipantModelShell[
193241

194242
model.handlePowerControl(
195243
currentState,
196-
relevantData.getOrElse(
197-
throw new CriticalFailureException("No relevant data available!")
198-
),
244+
getRelevantData,
199245
fo,
200246
setPointActivePower,
201247
)
@@ -205,10 +251,10 @@ final case class ParticipantModelShell[
205251
determineOperatingPoint(modelOperatingPoint, currentTick)
206252

207253
copy(
208-
state = Some(currentState),
209-
lastOperatingPoint = operatingPoint,
210-
operatingPoint = Some(newOperatingPoint),
211-
modelChange = newChangeIndicator,
254+
_state = Some(currentState),
255+
_lastOperatingPoint = _operatingPoint,
256+
_operatingPoint = Some(newOperatingPoint),
257+
_modelChange = newChangeIndicator,
212258
)
213259
}
214260

@@ -238,12 +284,12 @@ final case class ParticipantModelShell[
238284
// the end of the operation interval
239285
val adaptedNextTick =
240286
Seq(
241-
modelChange.changesAtTick,
287+
_modelChange.changesAtTick,
242288
nextDataTick,
243289
Option(operationInterval.end),
244290
).flatten.minOption
245291

246-
modelChange.copy(changesAtTick = adaptedNextTick)
292+
_modelChange.copy(changesAtTick = adaptedNextTick)
247293
} else {
248294
// If the model is not active, all activation ticks are ignored besides
249295
// potentially the operation start
@@ -262,26 +308,26 @@ final case class ParticipantModelShell[
262308
val currentState = determineCurrentState(request.tick)
263309
val updatedState = model.handleRequest(currentState, ctx, request)
264310

265-
copy(state = Some(updatedState))
311+
copy(_state = Some(updatedState))
266312
}
267313

268314
private def determineCurrentState(currentTick: Long): S = {
269315
// new state is only calculated if there's an old state and an operating point
270-
val currentState = state
271-
.zip(operatingPoint)
316+
val state = _state
317+
.zip(_operatingPoint)
272318
.flatMap { case (st, op) =>
273319
Option.when(st.tick < currentTick) {
274320
model.determineState(st, op, currentTick)
275321
}
276322
}
277323
.getOrElse(model.initialState(currentTick))
278324

279-
if (currentState.tick != currentTick)
325+
if (state.tick != currentTick)
280326
throw new CriticalFailureException(
281-
s"New state $currentState is not set to current tick $currentTick"
327+
s"New state $state is not set to current tick $currentTick"
282328
)
283329

284-
currentState
330+
state
285331
}
286332

287333
}

0 commit comments

Comments
 (0)