summaryrefslogtreecommitdiff
path: root/opendc-simulator/opendc-simulator-power/src
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2021-09-29 23:56:16 +0200
committerFabian Mastenbroek <mail.fabianm@gmail.com>2021-10-03 17:17:39 +0200
commit4cc1d40d421c8736f8b21b360b61d6b065158b7a (patch)
treecb2de79a72881eb0b2dee6a82dd498faba5dd26d /opendc-simulator/opendc-simulator-power/src
parentdd605ab1f70fef1fbbed848e8ebbd6b231622273 (diff)
refactor(simulator): Migrate to flow-based simulation
This change renames the `opendc-simulator-resources` module into the `opendc-simulator-flow` module to indicate that the core simulation model of OpenDC is based around modelling and simulating flows. Previously, the distinction between resource consumer and provider, and input and output caused some confusion. By switching to a flow-based model, this distinction is now clear (as in, the water flows from source to consumer/sink).
Diffstat (limited to 'opendc-simulator/opendc-simulator-power/src')
-rw-r--r--opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPdu.kt36
-rw-r--r--opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerInlet.kt6
-rw-r--r--opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerSource.kt12
-rw-r--r--opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimUps.kt33
-rw-r--r--opendc-simulator/opendc-simulator-power/src/test/kotlin/org/opendc/simulator/power/SimPduTest.kt52
-rw-r--r--opendc-simulator/opendc-simulator-power/src/test/kotlin/org/opendc/simulator/power/SimPowerSourceTest.kt46
-rw-r--r--opendc-simulator/opendc-simulator-power/src/test/kotlin/org/opendc/simulator/power/SimUpsTest.kt44
7 files changed, 116 insertions, 113 deletions
diff --git a/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPdu.kt b/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPdu.kt
index 1a12a52a..c33f5186 100644
--- a/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPdu.kt
+++ b/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPdu.kt
@@ -22,46 +22,48 @@
package org.opendc.simulator.power
-import org.opendc.simulator.resources.*
+import org.opendc.simulator.flow.*
+import org.opendc.simulator.flow.mux.FlowMultiplexer
+import org.opendc.simulator.flow.mux.MaxMinFlowMultiplexer
/**
* A model of a Power Distribution Unit (PDU).
*
- * @param interpreter The underlying [SimResourceInterpreter] to drive the simulation under the hood.
+ * @param engine The underlying [FlowEngine] to drive the simulation under the hood.
* @param idlePower The idle power consumption of the PDU independent of the load on the PDU.
* @param lossCoefficient The coefficient for the power loss of the PDU proportional to the square load.
*/
public class SimPdu(
- interpreter: SimResourceInterpreter,
+ engine: FlowEngine,
private val idlePower: Double = 0.0,
private val lossCoefficient: Double = 0.0,
) : SimPowerInlet() {
/**
- * The [SimResourceSwitch] that distributes the electricity over the PDU outlets.
+ * The [FlowMultiplexer] that distributes the electricity over the PDU outlets.
*/
- private val switch = SimResourceSwitchMaxMin(interpreter)
+ private val mux = MaxMinFlowMultiplexer(engine)
/**
- * The [SimResourceForwarder] that represents the input of the PDU.
+ * The [FlowForwarder] that represents the input of the PDU.
*/
- private val forwarder = SimResourceForwarder()
+ private val forwarder = FlowForwarder(engine)
/**
* Create a new PDU outlet.
*/
- public fun newOutlet(): Outlet = Outlet(switch, switch.newOutput())
+ public fun newOutlet(): Outlet = Outlet(mux, mux.newInput())
init {
- switch.addInput(forwarder)
+ mux.addOutput(forwarder)
}
- override fun createConsumer(): SimResourceConsumer = object : SimResourceConsumer by forwarder {
- override fun onNext(ctx: SimResourceContext, now: Long, delta: Long): Long {
- val duration = forwarder.onNext(ctx, now, delta)
- val loss = computePowerLoss(ctx.demand)
- val newLimit = ctx.demand + loss
+ override fun createConsumer(): FlowSource = object : FlowSource by forwarder {
+ override fun onPull(conn: FlowConnection, now: Long, delta: Long): Long {
+ val duration = forwarder.onPull(conn, now, delta)
+ val loss = computePowerLoss(conn.demand)
+ val newLimit = conn.demand + loss
- ctx.push(newLimit)
+ conn.push(newLimit)
return duration
}
@@ -81,7 +83,7 @@ public class SimPdu(
/**
* A PDU outlet.
*/
- public class Outlet(private val switch: SimResourceSwitch, private val provider: SimResourceProvider) : SimPowerOutlet(), AutoCloseable {
+ public class Outlet(private val switch: FlowMultiplexer, private val provider: FlowConsumer) : SimPowerOutlet(), AutoCloseable {
override fun onConnect(inlet: SimPowerInlet) {
provider.startConsumer(inlet.createConsumer())
}
@@ -94,7 +96,7 @@ public class SimPdu(
* Remove the outlet from the PDU.
*/
override fun close() {
- switch.removeOutput(provider)
+ switch.removeInput(provider)
}
override fun toString(): String = "SimPdu.Outlet"
diff --git a/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerInlet.kt b/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerInlet.kt
index 0ac1f199..851b28a5 100644
--- a/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerInlet.kt
+++ b/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerInlet.kt
@@ -22,7 +22,7 @@
package org.opendc.simulator.power
-import org.opendc.simulator.resources.SimResourceConsumer
+import org.opendc.simulator.flow.FlowSource
/**
* An abstract inlet that consumes electricity from a power outlet.
@@ -42,7 +42,7 @@ public abstract class SimPowerInlet {
internal var _outlet: SimPowerOutlet? = null
/**
- * Create a [SimResourceConsumer] which represents the consumption of electricity from the power outlet.
+ * Create a [FlowSource] which represents the consumption of electricity from the power outlet.
*/
- public abstract fun createConsumer(): SimResourceConsumer
+ public abstract fun createConsumer(): FlowSource
}
diff --git a/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerSource.kt b/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerSource.kt
index 3ef8ccc6..7faebd75 100644
--- a/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerSource.kt
+++ b/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimPowerSource.kt
@@ -22,25 +22,25 @@
package org.opendc.simulator.power
-import org.opendc.simulator.resources.SimResourceInterpreter
-import org.opendc.simulator.resources.SimResourceSource
+import org.opendc.simulator.flow.FlowEngine
+import org.opendc.simulator.flow.FlowSink
/**
* A [SimPowerOutlet] that represents a source of electricity.
*
- * @param interpreter The underlying [SimResourceInterpreter] to drive the simulation under the hood.
+ * @param engine The underlying [FlowEngine] to drive the simulation under the hood.
*/
-public class SimPowerSource(interpreter: SimResourceInterpreter, public val capacity: Double) : SimPowerOutlet() {
+public class SimPowerSource(engine: FlowEngine, public val capacity: Double) : SimPowerOutlet() {
/**
* The resource source that drives this power source.
*/
- private val source = SimResourceSource(capacity, interpreter)
+ private val source = FlowSink(engine, capacity)
/**
* The power draw at this instant.
*/
public val powerDraw: Double
- get() = source.speed
+ get() = source.rate
override fun onConnect(inlet: SimPowerInlet) {
source.startConsumer(inlet.createConsumer())
diff --git a/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimUps.kt b/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimUps.kt
index 9c7400ed..5eaa91af 100644
--- a/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimUps.kt
+++ b/opendc-simulator/opendc-simulator-power/src/main/kotlin/org/opendc/simulator/power/SimUps.kt
@@ -22,50 +22,51 @@
package org.opendc.simulator.power
-import org.opendc.simulator.resources.*
+import org.opendc.simulator.flow.*
+import org.opendc.simulator.flow.mux.MaxMinFlowMultiplexer
/**
* A model of an Uninterruptible Power Supply (UPS).
*
* This model aggregates multiple power sources into a single source in order to ensure that power is always available.
*
- * @param interpreter The underlying [SimResourceInterpreter] to drive the simulation under the hood.
+ * @param engine The underlying [FlowEngine] to drive the simulation under the hood.
* @param idlePower The idle power consumption of the UPS independent of the load.
* @param lossCoefficient The coefficient for the power loss of the UPS proportional to the load.
*/
public class SimUps(
- interpreter: SimResourceInterpreter,
+ private val engine: FlowEngine,
private val idlePower: Double = 0.0,
private val lossCoefficient: Double = 0.0,
) : SimPowerOutlet() {
/**
* The resource aggregator used to combine the input sources.
*/
- private val switch = SimResourceSwitchMaxMin(interpreter)
+ private val switch = MaxMinFlowMultiplexer(engine)
/**
- * The [SimResourceProvider] that represents the output of the UPS.
+ * The [FlowConsumer] that represents the output of the UPS.
*/
- private val provider = switch.newOutput()
+ private val provider = switch.newInput()
/**
* Create a new UPS outlet.
*/
public fun newInlet(): SimPowerInlet {
- val forward = SimResourceForwarder(isCoupled = true)
- switch.addInput(forward)
+ val forward = FlowForwarder(engine, isCoupled = true)
+ switch.addOutput(forward)
return Inlet(forward)
}
override fun onConnect(inlet: SimPowerInlet) {
val consumer = inlet.createConsumer()
- provider.startConsumer(object : SimResourceConsumer by consumer {
- override fun onNext(ctx: SimResourceContext, now: Long, delta: Long): Long {
- val duration = consumer.onNext(ctx, now, delta)
- val loss = computePowerLoss(ctx.demand)
- val newLimit = ctx.demand + loss
+ provider.startConsumer(object : FlowSource by consumer {
+ override fun onPull(conn: FlowConnection, now: Long, delta: Long): Long {
+ val duration = consumer.onPull(conn, now, delta)
+ val loss = computePowerLoss(conn.demand)
+ val newLimit = conn.demand + loss
- ctx.push(newLimit)
+ conn.push(newLimit)
return duration
}
})
@@ -86,8 +87,8 @@ public class SimUps(
/**
* A UPS inlet.
*/
- public inner class Inlet(private val forwarder: SimResourceForwarder) : SimPowerInlet(), AutoCloseable {
- override fun createConsumer(): SimResourceConsumer = forwarder
+ public inner class Inlet(private val forwarder: FlowForwarder) : SimPowerInlet(), AutoCloseable {
+ override fun createConsumer(): FlowSource = forwarder
/**
* Remove the inlet from the PSU.
diff --git a/opendc-simulator/opendc-simulator-power/src/test/kotlin/org/opendc/simulator/power/SimPduTest.kt b/opendc-simulator/opendc-simulator-power/src/test/kotlin/org/opendc/simulator/power/SimPduTest.kt
index 17a174b7..568a1e8c 100644
--- a/opendc-simulator/opendc-simulator-power/src/test/kotlin/org/opendc/simulator/power/SimPduTest.kt
+++ b/opendc-simulator/opendc-simulator-power/src/test/kotlin/org/opendc/simulator/power/SimPduTest.kt
@@ -28,10 +28,10 @@ import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.opendc.simulator.core.runBlockingSimulation
-import org.opendc.simulator.resources.SimResourceConsumer
-import org.opendc.simulator.resources.SimResourceEvent
-import org.opendc.simulator.resources.SimResourceInterpreter
-import org.opendc.simulator.resources.consumer.SimWorkConsumer
+import org.opendc.simulator.flow.FlowEngine
+import org.opendc.simulator.flow.FlowEvent
+import org.opendc.simulator.flow.FlowSource
+import org.opendc.simulator.flow.source.FixedFlowSource
/**
* Test suite for the [SimPdu] class.
@@ -39,9 +39,9 @@ import org.opendc.simulator.resources.consumer.SimWorkConsumer
internal class SimPduTest {
@Test
fun testZeroOutlets() = runBlockingSimulation {
- val interpreter = SimResourceInterpreter(coroutineContext, clock)
- val source = SimPowerSource(interpreter, capacity = 100.0)
- val pdu = SimPdu(interpreter)
+ val engine = FlowEngine(coroutineContext, clock)
+ val source = SimPowerSource(engine, capacity = 100.0)
+ val pdu = SimPdu(engine)
source.connect(pdu)
assertEquals(0.0, source.powerDraw)
@@ -49,9 +49,9 @@ internal class SimPduTest {
@Test
fun testSingleOutlet() = runBlockingSimulation {
- val interpreter = SimResourceInterpreter(coroutineContext, clock)
- val source = SimPowerSource(interpreter, capacity = 100.0)
- val pdu = SimPdu(interpreter)
+ val engine = FlowEngine(coroutineContext, clock)
+ val source = SimPowerSource(engine, capacity = 100.0)
+ val pdu = SimPdu(engine)
source.connect(pdu)
pdu.newOutlet().connect(SimpleInlet())
@@ -60,9 +60,9 @@ internal class SimPduTest {
@Test
fun testDoubleOutlet() = runBlockingSimulation {
- val interpreter = SimResourceInterpreter(coroutineContext, clock)
- val source = SimPowerSource(interpreter, capacity = 100.0)
- val pdu = SimPdu(interpreter)
+ val engine = FlowEngine(coroutineContext, clock)
+ val source = SimPowerSource(engine, capacity = 100.0)
+ val pdu = SimPdu(engine)
source.connect(pdu)
pdu.newOutlet().connect(SimpleInlet())
@@ -73,28 +73,28 @@ internal class SimPduTest {
@Test
fun testDisconnect() = runBlockingSimulation {
- val interpreter = SimResourceInterpreter(coroutineContext, clock)
- val source = SimPowerSource(interpreter, capacity = 100.0)
- val pdu = SimPdu(interpreter)
+ val engine = FlowEngine(coroutineContext, clock)
+ val source = SimPowerSource(engine, capacity = 100.0)
+ val pdu = SimPdu(engine)
source.connect(pdu)
- val consumer = spyk(SimWorkConsumer(100.0, utilization = 1.0))
+ val consumer = spyk(FixedFlowSource(100.0, utilization = 1.0))
val inlet = object : SimPowerInlet() {
- override fun createConsumer(): SimResourceConsumer = consumer
+ override fun createConsumer(): FlowSource = consumer
}
val outlet = pdu.newOutlet()
outlet.connect(inlet)
outlet.disconnect()
- verify { consumer.onEvent(any(), SimResourceEvent.Exit) }
+ verify { consumer.onEvent(any(), any(), FlowEvent.Exit) }
}
@Test
fun testLoss() = runBlockingSimulation {
- val interpreter = SimResourceInterpreter(coroutineContext, clock)
- val source = SimPowerSource(interpreter, capacity = 100.0)
+ val engine = FlowEngine(coroutineContext, clock)
+ val source = SimPowerSource(engine, capacity = 100.0)
// https://download.schneider-electric.com/files?p_Doc_Ref=SPD_NRAN-66CK3D_EN
- val pdu = SimPdu(interpreter, idlePower = 1.5, lossCoefficient = 0.015)
+ val pdu = SimPdu(engine, idlePower = 1.5, lossCoefficient = 0.015)
source.connect(pdu)
pdu.newOutlet().connect(SimpleInlet())
assertEquals(89.0, source.powerDraw, 0.01)
@@ -102,9 +102,9 @@ internal class SimPduTest {
@Test
fun testOutletClose() = runBlockingSimulation {
- val interpreter = SimResourceInterpreter(coroutineContext, clock)
- val source = SimPowerSource(interpreter, capacity = 100.0)
- val pdu = SimPdu(interpreter)
+ val engine = FlowEngine(coroutineContext, clock)
+ val source = SimPowerSource(engine, capacity = 100.0)
+ val pdu = SimPdu(engine)
source.connect(pdu)
val outlet = pdu.newOutlet()
outlet.close()
@@ -115,6 +115,6 @@ internal class SimPduTest {
}
class SimpleInlet : SimPowerInlet() {
- override fun createConsumer(): SimResourceConsumer = SimWorkConsumer(100.0, utilization = 0.5)
+ override fun createConsumer(): FlowSource = FixedFlowSource(100.0, utilization = 0.5)
}
}
diff --git a/opendc-simulator/opendc-simulator-power/src/test/kotlin/org/opendc/simulator/power/SimPowerSourceTest.kt b/opendc-simulator/opendc-simulator-power/src/test/kotlin/org/opendc/simulator/power/SimPowerSourceTest.kt
index f3829ba1..b411e292 100644
--- a/opendc-simulator/opendc-simulator-power/src/test/kotlin/org/opendc/simulator/power/SimPowerSourceTest.kt
+++ b/opendc-simulator/opendc-simulator-power/src/test/kotlin/org/opendc/simulator/power/SimPowerSourceTest.kt
@@ -31,10 +31,10 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertDoesNotThrow
import org.junit.jupiter.api.assertThrows
import org.opendc.simulator.core.runBlockingSimulation
-import org.opendc.simulator.resources.SimResourceConsumer
-import org.opendc.simulator.resources.SimResourceEvent
-import org.opendc.simulator.resources.SimResourceInterpreter
-import org.opendc.simulator.resources.consumer.SimWorkConsumer
+import org.opendc.simulator.flow.FlowEngine
+import org.opendc.simulator.flow.FlowEvent
+import org.opendc.simulator.flow.FlowSource
+import org.opendc.simulator.flow.source.FixedFlowSource
/**
* Test suite for the [SimPowerSource]
@@ -42,8 +42,8 @@ import org.opendc.simulator.resources.consumer.SimWorkConsumer
internal class SimPowerSourceTest {
@Test
fun testInitialState() = runBlockingSimulation {
- val interpreter = SimResourceInterpreter(coroutineContext, clock)
- val source = SimPowerSource(interpreter, capacity = 100.0)
+ val engine = FlowEngine(coroutineContext, clock)
+ val source = SimPowerSource(engine, capacity = 100.0)
assertFalse(source.isConnected)
assertNull(source.inlet)
@@ -52,8 +52,8 @@ internal class SimPowerSourceTest {
@Test
fun testDisconnectIdempotent() = runBlockingSimulation {
- val interpreter = SimResourceInterpreter(coroutineContext, clock)
- val source = SimPowerSource(interpreter, capacity = 100.0)
+ val engine = FlowEngine(coroutineContext, clock)
+ val source = SimPowerSource(engine, capacity = 100.0)
assertDoesNotThrow { source.disconnect() }
assertFalse(source.isConnected)
@@ -61,8 +61,8 @@ internal class SimPowerSourceTest {
@Test
fun testConnect() = runBlockingSimulation {
- val interpreter = SimResourceInterpreter(coroutineContext, clock)
- val source = SimPowerSource(interpreter, capacity = 100.0)
+ val engine = FlowEngine(coroutineContext, clock)
+ val source = SimPowerSource(engine, capacity = 100.0)
val inlet = SimpleInlet()
source.connect(inlet)
@@ -76,27 +76,27 @@ internal class SimPowerSourceTest {
@Test
fun testDisconnect() = runBlockingSimulation {
- val interpreter = SimResourceInterpreter(coroutineContext, clock)
- val source = SimPowerSource(interpreter, capacity = 100.0)
- val consumer = spyk(SimWorkConsumer(100.0, utilization = 1.0))
+ val engine = FlowEngine(coroutineContext, clock)
+ val source = SimPowerSource(engine, capacity = 100.0)
+ val consumer = spyk(FixedFlowSource(100.0, utilization = 1.0))
val inlet = object : SimPowerInlet() {
- override fun createConsumer(): SimResourceConsumer = consumer
+ override fun createConsumer(): FlowSource = consumer
}
source.connect(inlet)
source.disconnect()
- verify { consumer.onEvent(any(), SimResourceEvent.Exit) }
+ verify { consumer.onEvent(any(), any(), FlowEvent.Exit) }
}
@Test
fun testDisconnectAssertion() = runBlockingSimulation {
- val interpreter = SimResourceInterpreter(coroutineContext, clock)
- val source = SimPowerSource(interpreter, capacity = 100.0)
+ val engine = FlowEngine(coroutineContext, clock)
+ val source = SimPowerSource(engine, capacity = 100.0)
val inlet = mockk<SimPowerInlet>(relaxUnitFun = true)
every { inlet.isConnected } returns false
every { inlet._outlet } returns null
- every { inlet.createConsumer() } returns SimWorkConsumer(100.0, utilization = 1.0)
+ every { inlet.createConsumer() } returns FixedFlowSource(100.0, utilization = 1.0)
source.connect(inlet)
@@ -107,8 +107,8 @@ internal class SimPowerSourceTest {
@Test
fun testOutletAlreadyConnected() = runBlockingSimulation {
- val interpreter = SimResourceInterpreter(coroutineContext, clock)
- val source = SimPowerSource(interpreter, capacity = 100.0)
+ val engine = FlowEngine(coroutineContext, clock)
+ val source = SimPowerSource(engine, capacity = 100.0)
val inlet = SimpleInlet()
source.connect(inlet)
@@ -121,8 +121,8 @@ internal class SimPowerSourceTest {
@Test
fun testInletAlreadyConnected() = runBlockingSimulation {
- val interpreter = SimResourceInterpreter(coroutineContext, clock)
- val source = SimPowerSource(interpreter, capacity = 100.0)
+ val engine = FlowEngine(coroutineContext, clock)
+ val source = SimPowerSource(engine, capacity = 100.0)
val inlet = mockk<SimPowerInlet>(relaxUnitFun = true)
every { inlet.isConnected } returns true
@@ -132,6 +132,6 @@ internal class SimPowerSourceTest {
}
class SimpleInlet : SimPowerInlet() {
- override fun createConsumer(): SimResourceConsumer = SimWorkConsumer(100.0, utilization = 1.0)
+ override fun createConsumer(): FlowSource = FixedFlowSource(100.0, utilization = 1.0)
}
}
diff --git a/opendc-simulator/opendc-simulator-power/src/test/kotlin/org/opendc/simulator/power/SimUpsTest.kt b/opendc-simulator/opendc-simulator-power/src/test/kotlin/org/opendc/simulator/power/SimUpsTest.kt
index 8d5fa857..31ac0b39 100644
--- a/opendc-simulator/opendc-simulator-power/src/test/kotlin/org/opendc/simulator/power/SimUpsTest.kt
+++ b/opendc-simulator/opendc-simulator-power/src/test/kotlin/org/opendc/simulator/power/SimUpsTest.kt
@@ -28,10 +28,10 @@ import org.junit.jupiter.api.Assertions.assertAll
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.opendc.simulator.core.runBlockingSimulation
-import org.opendc.simulator.resources.SimResourceConsumer
-import org.opendc.simulator.resources.SimResourceEvent
-import org.opendc.simulator.resources.SimResourceInterpreter
-import org.opendc.simulator.resources.consumer.SimWorkConsumer
+import org.opendc.simulator.flow.FlowEngine
+import org.opendc.simulator.flow.FlowEvent
+import org.opendc.simulator.flow.FlowSource
+import org.opendc.simulator.flow.source.FixedFlowSource
/**
* Test suite for the [SimUps] class.
@@ -39,9 +39,9 @@ import org.opendc.simulator.resources.consumer.SimWorkConsumer
internal class SimUpsTest {
@Test
fun testSingleInlet() = runBlockingSimulation {
- val interpreter = SimResourceInterpreter(coroutineContext, clock)
- val source = SimPowerSource(interpreter, capacity = 100.0)
- val ups = SimUps(interpreter)
+ val engine = FlowEngine(coroutineContext, clock)
+ val source = SimPowerSource(engine, capacity = 100.0)
+ val ups = SimUps(engine)
source.connect(ups.newInlet())
ups.connect(SimpleInlet())
@@ -50,10 +50,10 @@ internal class SimUpsTest {
@Test
fun testDoubleInlet() = runBlockingSimulation {
- val interpreter = SimResourceInterpreter(coroutineContext, clock)
- val source1 = SimPowerSource(interpreter, capacity = 100.0)
- val source2 = SimPowerSource(interpreter, capacity = 100.0)
- val ups = SimUps(interpreter)
+ val engine = FlowEngine(coroutineContext, clock)
+ val source1 = SimPowerSource(engine, capacity = 100.0)
+ val source2 = SimPowerSource(engine, capacity = 100.0)
+ val ups = SimUps(engine)
source1.connect(ups.newInlet())
source2.connect(ups.newInlet())
@@ -67,10 +67,10 @@ internal class SimUpsTest {
@Test
fun testLoss() = runBlockingSimulation {
- val interpreter = SimResourceInterpreter(coroutineContext, clock)
- val source = SimPowerSource(interpreter, capacity = 100.0)
+ val engine = FlowEngine(coroutineContext, clock)
+ val source = SimPowerSource(engine, capacity = 100.0)
// https://download.schneider-electric.com/files?p_Doc_Ref=SPD_NRAN-66CK3D_EN
- val ups = SimUps(interpreter, idlePower = 4.0, lossCoefficient = 0.05)
+ val ups = SimUps(engine, idlePower = 4.0, lossCoefficient = 0.05)
source.connect(ups.newInlet())
ups.connect(SimpleInlet())
@@ -79,24 +79,24 @@ internal class SimUpsTest {
@Test
fun testDisconnect() = runBlockingSimulation {
- val interpreter = SimResourceInterpreter(coroutineContext, clock)
- val source1 = SimPowerSource(interpreter, capacity = 100.0)
- val source2 = SimPowerSource(interpreter, capacity = 100.0)
- val ups = SimUps(interpreter)
+ val engine = FlowEngine(coroutineContext, clock)
+ val source1 = SimPowerSource(engine, capacity = 100.0)
+ val source2 = SimPowerSource(engine, capacity = 100.0)
+ val ups = SimUps(engine)
source1.connect(ups.newInlet())
source2.connect(ups.newInlet())
- val consumer = spyk(SimWorkConsumer(100.0, utilization = 1.0))
+ val consumer = spyk(FixedFlowSource(100.0, utilization = 1.0))
val inlet = object : SimPowerInlet() {
- override fun createConsumer(): SimResourceConsumer = consumer
+ override fun createConsumer(): FlowSource = consumer
}
ups.connect(inlet)
ups.disconnect()
- verify { consumer.onEvent(any(), SimResourceEvent.Exit) }
+ verify { consumer.onEvent(any(), any(), FlowEvent.Exit) }
}
class SimpleInlet : SimPowerInlet() {
- override fun createConsumer(): SimResourceConsumer = SimWorkConsumer(100.0, utilization = 0.5)
+ override fun createConsumer(): FlowSource = FixedFlowSource(100.0, utilization = 0.5)
}
}