diff options
9 files changed, 46 insertions, 12 deletions
diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/Simulator.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/Simulator.kt index ee8d5072..3ad16da5 100644 --- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/Simulator.kt +++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/Simulator.kt @@ -148,13 +148,12 @@ class Simulator(val topology: Topology, private val mapping: Map<Component<*>, C */ suspend override fun <T> receive(block: Envelope<*>.(Any?) -> T): T = suspendCoroutine {} - /** - * The observable state of an [Entity] within the simulation is provided by context. - */ + * The observable state of an [Entity] within the simulation is provided by context. + */ @Suppress("UNCHECKED_CAST") override val <S> Entity<S>.state: S - get() = states[this] as S + get() = states.computeIfAbsent(this, { initialState }) as S /** * Update the state of the entity being simulated. @@ -201,7 +200,7 @@ class Simulator(val topology: Topology, private val mapping: Map<Component<*>, C */ @Suppress("UNCHECKED_CAST") override val <S> Entity<S>.state: S - get() = states[this] as S + get() = states.computeIfAbsent(this, { initialState }) as S /** * Suspend the simulation kernel for <code>n</code> ticks before resuming the execution. diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/impl/MachineKernel.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/impl/MachineKernel.kt index e34c7060..f81014e0 100644 --- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/impl/MachineKernel.kt +++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/impl/MachineKernel.kt @@ -34,6 +34,9 @@ class MachineKernel(ctx: EntityContext<Machine>): AbstractEntityKernel<Machine>( suspend override fun EntityContext<Machine>.run() { println("${this}: Initialising!") + println("${this}: ${component.entity.state}") + update(Machine.State(Machine.Status.IDLE)) + println("${this}: ${component.entity.state}") val cpus = component.outgoingEdges().filter { it.tag == "cpu" }.map { it.to.entity as Cpu } val speed = cpus.fold(0, { acc, (speed, cores) -> acc + speed * cores }) diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Entity.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Entity.kt index 44264a35..e6f37cf6 100644 --- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Entity.kt +++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Entity.kt @@ -36,4 +36,9 @@ package nl.atlarge.opendc.topology * a simulation. * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl) */ -interface Entity<out S> +interface Entity<out S> { + /** + * The initial state of the entity. + */ + val initialState: S +} diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/container/Datacenter.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/container/Datacenter.kt index 2b464f15..b484f70e 100644 --- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/container/Datacenter.kt +++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/container/Datacenter.kt @@ -31,4 +31,6 @@ import nl.atlarge.opendc.topology.Entity * * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl) */ -class Datacenter: Entity<Unit> +class Datacenter: Entity<Unit> { + override val initialState = Unit +} diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/container/Rack.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/container/Rack.kt index 043ad31a..f9595adc 100644 --- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/container/Rack.kt +++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/container/Rack.kt @@ -32,4 +32,6 @@ import nl.atlarge.opendc.topology.Entity * * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl) */ -class Rack: Entity<Unit> +class Rack: Entity<Unit> { + override val initialState = Unit +} diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/machine/Cpu.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/machine/Cpu.kt index e06ad00c..1e2135e8 100644 --- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/machine/Cpu.kt +++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/machine/Cpu.kt @@ -33,4 +33,6 @@ data class Cpu( override val speed: Int, override val cores: Int, override val energyConsumption: Int -): ProcessingUnit +): ProcessingUnit { + override val initialState = Unit +} diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/machine/Gpu.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/machine/Gpu.kt index f15847e4..a294db38 100644 --- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/machine/Gpu.kt +++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/machine/Gpu.kt @@ -33,5 +33,7 @@ class Gpu( override val speed: Int, override val cores: Int, override val energyConsumption: Int -): ProcessingUnit +): ProcessingUnit { + override val initialState = Unit +} diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/machine/Machine.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/machine/Machine.kt index 396339a2..138abee7 100644 --- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/machine/Machine.kt +++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/machine/Machine.kt @@ -32,4 +32,21 @@ import nl.atlarge.opendc.topology.Entity * * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl) */ -class Machine: Entity<Unit> +class Machine: Entity<Machine.State> { + /** + * The status of a machine. + */ + enum class Status { + HALT, IDLE, RUNNING + } + + /** + * The shape of the state of a [Machine] entity. + */ + data class State(val status: Status) + + /** + * The initial state of a [Machine] entity. + */ + override val initialState = State(Status.HALT) +} diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/power/PowerUnit.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/power/PowerUnit.kt index e016a12b..2938e530 100644 --- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/power/PowerUnit.kt +++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/power/PowerUnit.kt @@ -32,4 +32,6 @@ import nl.atlarge.opendc.topology.Entity * @param output The power output of the power unit in Watt. * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl) */ -class PowerUnit(val output: Double): Entity<Unit> +class PowerUnit(val output: Double): Entity<Unit> { + override val initialState = Unit +} |
