summaryrefslogtreecommitdiff
path: root/opendc-core/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'opendc-core/src/main')
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/experiment/Experiment.kt34
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/experiment/Job.kt36
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/experiment/Scheduler.kt42
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/experiment/Task.kt64
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/experiment/User.kt40
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/Clock.kt (renamed from opendc-core/src/main/kotlin/nl/atlarge/opendc/experiment/ExperimentRunner.kt)17
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/Context.kt86
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/Interrupt.kt (renamed from opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/container/Room.kt)8
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/Kernel.kt (renamed from opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/Context.kt)42
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/Process.kt (renamed from opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/Kernel.kt)25
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/messaging/Envelope.kt (renamed from opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/messaging/Envelope.kt)18
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/messaging/Readable.kt (renamed from opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/messaging/Readable.kt)2
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/messaging/Writable.kt (renamed from opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/messaging/Writable.kt)9
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/ChannelContext.kt35
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/EntityContext.kt45
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/Simulator.kt44
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/clock/Clock.kt53
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/messaging/Channel.kt49
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/messaging/Port.kt33
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/omega/OmegaSimulator.kt233
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/AdjacencyList.kt202
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Component.kt13
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Edge.kt13
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Entity.kt8
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/ImmutableTopology.kt31
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/MutableTopology.kt22
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Node.kt77
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Topology.kt11
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/TopologyContext.kt (renamed from opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/machine/ProcessingUnit.kt)21
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/container/Datacenter.kt36
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/container/Rack.kt37
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/machine/Cpu.kt38
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/machine/Gpu.kt39
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/machine/Machine.kt88
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/network/NetworkUnit.kt34
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/power/PowerUnit.kt37
-rw-r--r--opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/storage/StorageUnit.kt34
37 files changed, 360 insertions, 1296 deletions
diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/experiment/Experiment.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/experiment/Experiment.kt
deleted file mode 100644
index a4f947c8..00000000
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/experiment/Experiment.kt
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * MIT License
- *
- * Copyright (c) 2017 atlarge-research
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-package nl.atlarge.opendc.experiment
-
-/**
- * A simulation of multiple simultaneous workloads running on top of a topology.
- *
- * @param id The unique identifier of the experiment.
- * @param name The name of the experiment.
- * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
- */
-data class Experiment(val id: Int, val name: String)
diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/experiment/Job.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/experiment/Job.kt
deleted file mode 100644
index 8c41735d..00000000
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/experiment/Job.kt
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * MIT License
- *
- * Copyright (c) 2017 atlarge-research
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-package nl.atlarge.opendc.experiment
-
-/**
- * A job that is submitted to a cloud network, which consists of one or multiple [Task]s.
- *
- * @param id The unique identifier of this job.
- * @param name The name of this job.
- * @param owner The user to which the job belongs.
- * @param tasks The tasks of which the job consists.
- * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
- */
-data class Job(val id: Int, val name: String, val owner: User, val tasks: Collection<Task>)
diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/experiment/Scheduler.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/experiment/Scheduler.kt
deleted file mode 100644
index 14a623a6..00000000
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/experiment/Scheduler.kt
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * MIT License
- *
- * Copyright (c) 2017 atlarge-research
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-package nl.atlarge.opendc.experiment
-
-import nl.atlarge.opendc.topology.Node
-
-/**
- * A task scheduler that is coupled to an [Node] in the topology of the cloud network.
- *
- * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
- */
-interface Scheduler<in E: Node<*>> {
- /**
- * Schedule the given jobs for the given entity.
- *
- * @param entity The entity in the cloud network topology representing the entity.
- * @param jobs The jobs that have been submitted to the cloud network.
- */
- fun schedule(entity: E, jobs: Set<Job>)
-}
diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/experiment/Task.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/experiment/Task.kt
deleted file mode 100644
index ec2eb2fa..00000000
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/experiment/Task.kt
+++ /dev/null
@@ -1,64 +0,0 @@
-/*
- * MIT License
- *
- * Copyright (c) 2017 atlarge-research
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-package nl.atlarge.opendc.experiment
-
-/**
- * A task represents some computation that is part of a [Job].
- *
- * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
- */
-data class Task(
- val id: Int,
- val dependencies: Set<Task>,
- val flops: Long
-) {
- /**
- * The remaining amount of flops to compute.
- */
- var remaining: Long = flops
- private set
-
- /**
- * A flag to indicate whether the task is finished.
- */
- var finished: Boolean = false
- private set
-
- /**
- * Consume the given amount of flops of this task.
- *
- * @param flops The total amount of flops to consume.
- */
- fun consume(flops: Long) {
- if (finished)
- return
- if (remaining <= flops) {
- finished = true
- remaining = 0
- } else {
- remaining -= flops
- }
- }
-}
diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/experiment/User.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/experiment/User.kt
deleted file mode 100644
index bb87a167..00000000
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/experiment/User.kt
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * MIT License
- *
- * Copyright (c) 2017 atlarge-research
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-package nl.atlarge.opendc.experiment
-
-import nl.atlarge.opendc.topology.Topology
-
-/**
- * A user of a cloud network that provides [Job]s for the simulation.
- *
- * <p>Each [User] in a simulation has its own logical view of the cloud network which is used to route its jobs in the
- * physical network.
- *
- * @param id The unique identifier of the user.
- * @param name The name of the user.
- * @param view The view of the user on the topology.
- * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
- */
-data class User(val id: Int, val name: String, val view: Topology)
diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/experiment/ExperimentRunner.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/Clock.kt
index e53c6e08..0328bbe6 100644
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/experiment/ExperimentRunner.kt
+++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/Clock.kt
@@ -22,18 +22,21 @@
* SOFTWARE.
*/
-package nl.atlarge.opendc.experiment
+package nl.atlarge.opendc.kernel
/**
- * An [ExperimentRunner] is responsible for executing an [Experiment].
+ * A tick represents a moment of time in which some work is done by an entity.
+ */
+typealias Tick = Long
+
+/**
+ * The clock of a simulation manages the simulation time of a simulation [Kernel].
*
* @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
*/
-interface ExperimentRunner {
+interface Clock {
/**
- * Run the given [Experiment] using this runner.
- *
- * @param experiment The experiment to run.
+ * The tick the clock is currently at.
*/
- fun run(experiment: Experiment)
+ val tick: Tick
}
diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/Context.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/Context.kt
new file mode 100644
index 00000000..600a9cee
--- /dev/null
+++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/Context.kt
@@ -0,0 +1,86 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2017 atlarge-research
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package nl.atlarge.opendc.kernel
+
+import nl.atlarge.opendc.kernel.messaging.Readable
+import nl.atlarge.opendc.kernel.messaging.Writable
+import nl.atlarge.opendc.topology.Entity
+import nl.atlarge.opendc.topology.Topology
+import nl.atlarge.opendc.topology.TopologyContext
+
+/**
+ * This interface provides a context for simulation [Process]es, which defines the environment in which the simulation
+ * is run and provides means of communicating with other entities in the environment and control its own behaviour.
+ *
+ * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
+ */
+interface Context<out E : Entity<*>> : Readable, Writable, TopologyContext {
+ /**
+ * The [Topology] over which the simulation is run.
+ */
+ val topology: Topology
+
+ /**
+ * The global [Clock] that keeps track of the simulation time.
+ */
+ val clock: Clock
+
+ /**
+ * The [Entity] in simulation by the [Process].
+ */
+ val entity: E
+
+ /**
+ * The observable state of an [Entity] in simulation, which is provided by the simulation context.
+ */
+ val <E : Entity<S>, S> E.state: S
+
+ /**
+ * Interrupt the [Process] of an [Entity] in simulation.
+ */
+ suspend fun Entity<*>.interrupt()
+
+ /**
+ * Suspend the [Process] of the [Entity] in simulation until the next tick has occurred in the simulation.
+ */
+ suspend fun tick(): Boolean
+
+ /**
+ * Suspend the [Process] of the [Entity] in simulation for <code>n</code> ticks before resuming execution.
+ *
+ * @param n The amount of ticks to suspend the process.
+ */
+ suspend fun wait(n: Int)
+
+ /**
+ * Update the observable state of the entity being simulated.
+ *
+ * Instead of directly mutating the entity, we create a new instance of the entity to prevent other objects
+ * referencing the old entity having their data changed.
+ *
+ * @param next The next state of the entity.
+ */
+ suspend fun <C : Context<E>, E : Entity<S>, S> C.update(next: S)
+}
diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/container/Room.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/Interrupt.kt
index 844d96a0..de7c5c6c 100644
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/container/Room.kt
+++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/Interrupt.kt
@@ -22,13 +22,11 @@
* SOFTWARE.
*/
-package nl.atlarge.opendc.topology.container
-
-import nl.atlarge.opendc.topology.Entity
+package nl.atlarge.opendc.kernel
/**
- * A physical room in a datacenter with relationships to the entities within the room.
+ * An [Interrupt] message is sent to a [Process] in order to interrupt its suspended state.
*
* @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
*/
-interface Room: Entity<Unit>
+object Interrupt: Throwable("The process has been interrupted by another entity")
diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/Context.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/Kernel.kt
index 61ba8192..9678db41 100644
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/Context.kt
+++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/Kernel.kt
@@ -22,47 +22,47 @@
* SOFTWARE.
*/
-package nl.atlarge.opendc.simulator
+package nl.atlarge.opendc.kernel
-import nl.atlarge.opendc.simulator.messaging.Readable
-import nl.atlarge.opendc.topology.Component
import nl.atlarge.opendc.topology.Entity
import nl.atlarge.opendc.topology.Topology
+import java.lang.Process
/**
- * The [Context] interface provides a context for a simulation kernel, which defines the environment in which the
- * simulation is run.
+ * A message-based discrete event simulator (DES). This interface allows running simulations over a [Topology].
+ * This discrete event simulator works by having entities in a [Topology] interchange messages between each other and
+ * updating their observable state accordingly.
+ *
+ * In order to run a simulation, a kernel needs to bootstrapped by an initial set of messages to be processed by
+ * entities in the topology of the simulation. Otherwise, the simulation will immediately exit.
*
* @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
*/
-interface Context<out T: Component<*>>: Readable {
+interface Kernel {
/**
* The [Topology] over which the simulation is run.
*/
val topology: Topology
/**
- * The [Component] that is simulated.
- */
- val component: T
-
- /**
- * The observable state of an [Entity] within the simulation is provided by context.
+ * Step through one cycle in the simulation. This method will process all events in a single tick, update the
+ * internal clock and then return the control to the user.
*/
- val <S> Entity<S>.state: S
+ fun step()
/**
- * Suspend the simulation kernel until the next tick occurs in the simulation.
+ * Run a simulation over the specified [Topology].
+ * This method will step through multiple cycles in the simulation until no more message exist in the queue.
*/
- suspend fun tick(): Boolean {
- wait(1)
- return true
- }
+ fun run()
/**
- * Suspend the simulation kernel for <code>n</code> ticks before resuming the execution.
+ * Schedule a message for processing by a [Process].
*
- * @param n The amount of ticks to suspend the simulation kernel.
+ * @param message The message to schedule.
+ * @param destination The destination of the message.
+ * @param sender The sender of the message.
+ * @param delay The amount of ticks to wait before processing the message.
*/
- suspend fun wait(n: Int)
+ fun schedule(message: Any?, destination: Entity<*>, sender: Entity<*>? = null, delay: Int = 0)
}
diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/Kernel.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/Process.kt
index 0e0a62a6..40fbefbf 100644
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/Kernel.kt
+++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/Process.kt
@@ -22,25 +22,30 @@
* SOFTWARE.
*/
-package nl.atlarge.opendc.simulator
+package nl.atlarge.opendc.kernel
-import nl.atlarge.opendc.topology.Component
+import nl.atlarge.opendc.topology.Entity
/**
- * A simulation kernel that simulates a single [Component] instance in a cloud network.
+ * A [Process] defines the behaviour of an [Entity] within simulation.
*
* @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
*/
-interface Kernel<in C: Context<*>> {
+interface Process<in E : Entity<*>> {
/**
- * This method is invoked to start the simulation of the [Component] associated with this [Kernel].
+ * This method is invoked to start the simulation an [Entity] associated with this [Process].
*
- * <p>This method is assumed to be running during the experiment, but should hand back control to the simulator at
- * some point by calling [Context.tick] to wait for the next tick to occur, which allows to allows other entity
- * simulators to do work in the current tick of the simulation.
+ * This method is assumed to be running during a simulation, but should hand back control to the simulator at
+ * some point by suspending the process. This allows other processes to do work in the current tick of the
+ * simulation.
+ * Suspending the process can be achieved by calling suspending method in the context:
+ * - [Context.tick] - Wait for the next tick to occur
+ * - [Context.wait] - Wait for `n` amount of ticks before resuming execution.
+ * - [Context.receive] - Wait for a message to be received in the mailbox of the [Entity] before resuming
+ * execution.
*
- * <p>If this method exists early, before the simulation has finished, the entity is assumed to be shutdown and its
+ * If this method exits early, before the simulation has finished, the entity is assumed to be shutdown and its
* simulation will not run any further.
*/
- suspend fun C.simulate()
+ suspend fun Context<E>.run()
}
diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/messaging/Envelope.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/messaging/Envelope.kt
index 2c09fe8a..61d1a0cf 100644
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/messaging/Envelope.kt
+++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/messaging/Envelope.kt
@@ -22,13 +22,15 @@
* SOFTWARE.
*/
-package nl.atlarge.opendc.simulator.messaging
+package nl.atlarge.opendc.kernel.messaging
-import nl.atlarge.opendc.topology.Node
+import nl.atlarge.opendc.kernel.Tick
+import nl.atlarge.opendc.topology.Entity
/**
* The envelope of a message that is received from a [Channel], also containing the metadata of the message.
*
+ * @param T The shape of the message inside the envelope.
* @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
*/
data class Envelope<out T>(
@@ -38,7 +40,17 @@ data class Envelope<out T>(
val message: T,
/**
+ * The tick at which the message should be delivered.
+ */
+ val tick: Tick,
+
+ /**
* The sender of the message.
*/
- val sender: Node<*>
+ val sender: Entity<*>?,
+
+ /**
+ * The destination of the message.
+ */
+ val destination: Entity<*>
)
diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/messaging/Readable.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/messaging/Readable.kt
index 161bcbd8..422c5668 100644
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/messaging/Readable.kt
+++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/messaging/Readable.kt
@@ -22,7 +22,7 @@
* SOFTWARE.
*/
-package nl.atlarge.opendc.simulator.messaging
+package nl.atlarge.opendc.kernel.messaging
/**
* A [Readable] instance allows objects to pull messages from the instance.
diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/messaging/Writable.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/messaging/Writable.kt
index c8b354b1..45c81e39 100644
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/messaging/Writable.kt
+++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/kernel/messaging/Writable.kt
@@ -22,12 +22,12 @@
* SOFTWARE.
*/
-package nl.atlarge.opendc.simulator.messaging
+package nl.atlarge.opendc.kernel.messaging
-import nl.atlarge.opendc.topology.Node
+import nl.atlarge.opendc.topology.Entity
/**
- * A [Writable] instance allows objects to send messages to it.
+ * A [Writable] instance allows entities to send messages.
*
* @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
*/
@@ -37,6 +37,7 @@ interface Writable {
*
* @param msg The message to send.
* @param sender The sender of the message.
+ * @param delay The number of ticks before the message should be received.
*/
- suspend fun send(msg: Any?, sender: Node<*>)
+ suspend fun Entity<*>.send(msg: Any?, sender: Entity<*>? = null, delay: Int = 0)
}
diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/ChannelContext.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/ChannelContext.kt
deleted file mode 100644
index dee55730..00000000
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/ChannelContext.kt
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * MIT License
- *
- * Copyright (c) 2017 atlarge-research
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-package nl.atlarge.opendc.simulator
-
-import nl.atlarge.opendc.simulator.messaging.Writable
-import nl.atlarge.opendc.topology.Edge
-
-/**
- * The context provided to a simulation kernel for communication channels between entities.
- *
- * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
- */
-interface ChannelContext<out T>: Context<Edge<T>>, Writable
diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/EntityContext.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/EntityContext.kt
deleted file mode 100644
index 3cbbea71..00000000
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/EntityContext.kt
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * MIT License
- *
- * Copyright (c) 2017 atlarge-research
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-package nl.atlarge.opendc.simulator
-
-import nl.atlarge.opendc.topology.Entity
-import nl.atlarge.opendc.topology.Node
-
-/**
- * The context provided to a simulation kernel for stateful entities in the topology.
- *
- * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
- */
-interface EntityContext<out T: Entity<*>>: Context<Node<T>> {
- /**
- * Update the state of the entity being simulated.
- *
- * <p>Instead of directly mutating the entity, we create a new instance of the entity to prevent other objects
- * referencing the old entity having their data changed.
- *
- * @param next The next state of the entity.
- */
- suspend fun <C: EntityContext<E>, E: Entity<S>, S> C.update(next: S)
-}
diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/Simulator.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/Simulator.kt
deleted file mode 100644
index 72494793..00000000
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/Simulator.kt
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * MIT License
- *
- * Copyright (c) 2017 atlarge-research
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-package nl.atlarge.opendc.simulator
-
-import nl.atlarge.opendc.topology.Topology
-
-/**
- * A [Simulator] runs a simulation over the specified topology.
- *
- * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
- */
-interface Simulator {
- /**
- * The [Topology] over which the simulation runs.
- */
- val topology: Topology
-
- /**
- * Run the next cycle in the simulation.
- */
- fun next()
-}
diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/clock/Clock.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/clock/Clock.kt
deleted file mode 100644
index 07377e4a..00000000
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/clock/Clock.kt
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * MIT License
- *
- * Copyright (c) 2017 atlarge-research
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-package nl.atlarge.opendc.simulator.clock
-
-/**
- * A tick represents a moment of time in which some work is done by an entity.
- */
-typealias Tick = Long
-
-/**
- * The clock of a simulation manages the ticks that have elapsed and schedules the tick events.
- *
- * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
- */
-interface Clock {
- /**
- * The tick the clock is currently at.
- */
- val tick: Tick
-
- /**
- *
- * @throws IllegalArgumentException
- */
- fun scheduleAt(tick: Tick, block: () -> Unit)
-
- /**
- * @throws IllegalArgumentException
- */
- fun scheduleAfter(n: Int, block: () -> Unit) = scheduleAt(tick + n, block)
-}
diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/messaging/Channel.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/messaging/Channel.kt
deleted file mode 100644
index 35407897..00000000
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/messaging/Channel.kt
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * MIT License
- *
- * Copyright (c) 2017 atlarge-research
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-package nl.atlarge.opendc.simulator.messaging
-
-import nl.atlarge.opendc.topology.Edge
-import nl.atlarge.opendc.topology.Node
-
-/**
- * A unidirectional communication channel between two [Node] instances as seen from one of the entities.
- *
- * <p>A [Channel] is viewed as a directed edge that connects two entities in the topology of a cloud network.
- *
- * @param T The shape of the label of the edge of this channel.
- * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
- */
-interface Channel<out T> {
- /**
- * The directed edge between two nodes which represents this unidirectional communication channel.
- */
- val edge: Edge<T>
-
- /**
- * The channel the message originates from.
- */
- val Envelope<*>.channel: Channel<T>
- get() = this@Channel
-}
diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/messaging/Port.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/messaging/Port.kt
deleted file mode 100644
index a7d7caba..00000000
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/messaging/Port.kt
+++ /dev/null
@@ -1,33 +0,0 @@
-/*
- * MIT License
- *
- * Copyright (c) 2017 atlarge-research
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-package nl.atlarge.opendc.simulator.messaging
-
-/**
- * A port connects multiple [Channel]s to an entity in the topology of a cloud network.
- *
- * @param C The shape of the channels that are connected to this port.
- * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
- */
-interface Port<out C: Channel<*>>: Iterable<C>
diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/omega/OmegaSimulator.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/omega/OmegaSimulator.kt
deleted file mode 100644
index dc3c0d0f..00000000
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/simulator/omega/OmegaSimulator.kt
+++ /dev/null
@@ -1,233 +0,0 @@
-/*
- * MIT License
- *
- * Copyright (c) 2017 atlarge-research
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-package nl.atlarge.opendc.simulator.omega
-
-import mu.KotlinLogging
-import nl.atlarge.opendc.simulator.*
-import nl.atlarge.opendc.simulator.clock.Clock
-import nl.atlarge.opendc.simulator.clock.Tick
-import nl.atlarge.opendc.simulator.messaging.Envelope
-import nl.atlarge.opendc.topology.*
-import java.util.*
-import kotlin.coroutines.experimental.*
-
-/**
- * The Omega simulator is the default [Simulator] implementation for the OpenDC core.
- *
- * <p>This simulator implementation is a single-threaded implementation running simulation kernels synchronously and
- * provides a single priority queue for all events (messages, ticks, etc) that occur in the components.
- *
- * <p>By default, [Kernel]s are resolved as part of the [Topology], meaning each [Component] in the topology also
- * implements its simulation logic by deriving from the [Kernel] interface.
- *
- * @param topology The topology to run the simulation over.
- * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
- */
-class OmegaSimulator(override val topology: Topology) : Simulator, Iterator<Unit> {
- /**
- * The logger instance to use for the simulator.
- */
- private val logger = KotlinLogging.logger {}
-
- /**
- * The registry of the simulation kernels used in the experiment.
- */
- private val registry: MutableMap<Component<*>, Context<*>?> = HashMap()
-
- /**
- * A mapping of the entities in the topology to their current state.
- */
- private val states: MutableMap<Entity<*>, Any?> = HashMap()
-
- /**
- * The clock of the simulator.
- */
- private val clock: OmegaClock = OmegaClock()
-
- /**
- * Initialize the simulator.
- */
- init {
- topology.forEach { node ->
- resolve(node)
- node.outgoingEdges.forEach { resolve(it) }
- }
-
- registry.values.forEach { context ->
- if (context == null)
- return@forEach
- @Suppress("UNCHECKED_CAST")
- val kernel = context.component.label as Kernel<Context<*>>
-
- // Start all kernel co-routines
- val block: suspend () -> Unit = { kernel.run { context.simulate() } }
- block.startCoroutine(KernelCoroutine())
- }
- }
-
- /**
- * Resolve the given [Component] to the [Kernel] of that component.
- *
- * @param component The component to resolve.
- * @return The [Kernel] that simulates that [Component].
- */
- fun <T : Component<*>> resolve(component: T): Context<T>? {
- @Suppress("UNCHECKED_CAST")
- return registry.computeIfAbsent(component, {
- if (component.label !is Kernel<*>)
- null
- else when (component) {
- is Node<*> -> OmegaEntityContext(component as Node<*>)
- is Edge<*> -> OmegaChannelContext(component as Edge<*>)
- else -> null
- }
- }) as Context<T>?
- }
-
- /**
- * Determine whether the simulator has a next non-empty cycle available.
- *
- * @return <code>true</code> if the simulator has a next non-empty cycle, <code>false</code> otherwise.
- */
- override fun hasNext(): Boolean = clock.queue.isNotEmpty()
-
- /**
- * Run the next cycle in the simulation.
- */
- override fun next() {
- clock.tick++
- while (true) {
- val (tick, block) = clock.queue.peek() ?: return
-
- if (tick > clock.tick) {
- // Tick has yet to occur
- // Jump in time to next event
- clock.tick = tick - 1
- break
- } else if (tick < clock.tick) {
- // Tick has already occurred
- logger.warn { "tick was not handled correctly" }
- }
- clock.queue.poll()
- block()
- }
- }
-
- /**
- * The co-routine which runs a simulation kernel.
- */
- private class KernelCoroutine : Continuation<Unit> {
- override val context: CoroutineContext = EmptyCoroutineContext
- override fun resume(value: Unit) {}
-
- override fun resumeWithException(exception: Throwable) {
- val currentThread = Thread.currentThread()
- currentThread.uncaughtExceptionHandler.uncaughtException(currentThread, exception)
- }
- }
-
- /**
- * The [Clock] for this [OmegaSimulator] that keeps track of the simulation time in ticks.
- */
- private inner class OmegaClock : Clock {
- override var tick: Tick = 0
- internal val queue: PriorityQueue<Pair<Tick, () -> Unit>> = PriorityQueue(Comparator.comparingLong { it.first })
-
- override fun scheduleAt(tick: Tick, block: () -> Unit) {
- queue.add(Pair(tick, block))
- }
- }
-
- /**
- * This internal class provides the default implementation for the [Context] interface for this simulator.
- */
- private abstract inner class OmegaAbstractContext<out T : Component<*>> : Context<T> {
- /**
- * The [Topology] over which the simulation is run.
- */
- override val topology: Topology = this@OmegaSimulator.topology
-
- /**
- * Retrieves and removes a single message from this channel suspending the caller while the channel is empty.
- *
- * @param block The block to process the message with.
- * @return The processed message.
- */
- suspend override fun <T> receive(block: Envelope<*>.(Any?) -> T): T {
- TODO("not implemented")
- }
-
- /**
- * The observable state of an [Entity] within the simulation is provided by the context of the simulation.
- */
- @Suppress("UNCHECKED_CAST")
- override val <S> Entity<S>.state: S
- get() = states.computeIfAbsent(this, { initialState }) as S
-
- /**
- * Suspend the simulation kernel for <code>n</code> ticks before resuming the execution.
- *
- * @param n The amount of ticks to suspend the simulation kernel, with <code>n > 0</code>
- */
- suspend override fun wait(n: Int) {
- require(n > 0) { "The amount of ticks to suspend must be a non-zero positive number" }
- return suspendCoroutine { cont ->
- clock.scheduleAfter(n, { cont.resume(Unit) })
- }
- }
- }
-
- /**
- * An internal class to provide [Context] for an entity within the simulation.
- */
- private inner class OmegaEntityContext<out T : Entity<*>>(override val component: Node<T>) : OmegaAbstractContext<Node<T>>(), EntityContext<T> {
- /**
- * Update the state of the entity being simulated.
- *
- * <p>Instead of directly mutating the entity, we create a new instance of the entity to prevent other objects
- * referencing the old entity having their data changed.
- *
- * @param next The next state of the entity.
- */
- suspend override fun <C : EntityContext<E>, E : Entity<S>, S> C.update(next: S) {
- states.put(component.entity as Entity<*>, next)
- }
- }
-
- /**
- * An internal class to provide the [Context] for an edge kernel within the simulation.
- */
- private inner class OmegaChannelContext<out T>(override val component: Edge<T>) : OmegaAbstractContext<Edge<T>>(), ChannelContext<T> {
- /**
- * Send the given message downstream.
- *
- * @param msg The message to send.
- * @param sender The sender of the message.
- */
- suspend override fun send(msg: Any?, sender: Node<*>) {
- TODO("not implemented")
- }
- }
-}
diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/AdjacencyList.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/AdjacencyList.kt
index 28154695..e2824f5d 100644
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/AdjacencyList.kt
+++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/AdjacencyList.kt
@@ -24,14 +24,32 @@
package nl.atlarge.opendc.topology
+import nl.atlarge.opendc.topology.Edge as BaseEdge
import java.util.concurrent.atomic.AtomicInteger
/**
+ * This module provides a [Topology] implementation backed internally by an adjacency list.
+ *
+ * This implementation is best suited for sparse graphs, where an adjacency matrix would take up too much space with
+ * empty cells.
+ *
+ * *Note that this implementation is not synchronized.*
+ */
+object AdjacencyList {
+ /**
+ * Return a [TopologyBuilder] that constructs the topology represents as an adjacency list.
+ *
+ * @return A [TopologyBuilder] instance.
+ */
+ fun builder(): TopologyBuilder = AdjacencyListTopologyBuilder()
+}
+
+/**
* A builder for [Topology] instances, which is backed by an adjacency list.
*
* @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
*/
-class AdjacencyListTopologyBuilder: TopologyBuilder {
+internal class AdjacencyListTopologyBuilder : TopologyBuilder {
/**
* Build a [Topology] instance from the current state of this builder.
*
@@ -43,41 +61,38 @@ class AdjacencyListTopologyBuilder: TopologyBuilder {
/**
* A [Topology] whose graph is represented as adjacency list.
*/
-internal class AdjacencyListTopology: MutableTopology {
- private val nextId: AtomicInteger = AtomicInteger(0)
- private val nodes: MutableList<Node<*>> = ArrayList()
-
+internal class AdjacencyListTopology : MutableTopology {
/**
- * Returns the size of the collection.
+ * The identifier for the next node in the graph.
*/
- override val size: Int = nodes.size
+ private var nextId: AtomicInteger = AtomicInteger(0)
/**
- * Checks if the specified element is contained in this collection.
+ * A mapping of nodes to their internal representation with the edges of the nodes.
*/
- override fun contains(element: Node<*>): Boolean = nodes.contains(element)
+ private var nodes: MutableMap<Entity<*>, Node> = HashMap()
+
+ // Topology
/**
- * Checks if all elements in the specified collection are contained in this collection.
+ * A unique identifier of this node within the topology.
*/
- override fun containsAll(elements: Collection<Node<*>>): Boolean = nodes.containsAll(elements)
+ override val Entity<*>.id: Int
+ get() = nodes[this]!!.id
/**
- * Returns `true` if the collection is empty (contains no elements), `false` otherwise.
+ * The set of ingoing edges of this node.
*/
- override fun isEmpty(): Boolean = nodes.isEmpty()
+ override val Entity<*>.ingoingEdges: MutableSet<BaseEdge<*>>
+ get() = nodes[this]!!.ingoingEdges
/**
- * Create a [Node] in this [Topology] for the given [Entity].
- *
- * @param entity The entity to create a node for.
- * @return The node created for the given entity.
+ * The set of outgoing edges of this node.
*/
- override fun <T : Entity<*>> node(entity: T): Node<T> {
- val node = AdjacencyListNode(nextId.getAndIncrement(), entity)
- nodes.add(node)
- return node
- }
+ override val Entity<*>.outgoingEdges: MutableSet<BaseEdge<*>>
+ get() = nodes[this]!!.outgoingEdges
+
+ // MutableTopology
/**
* Create a directed edge between two [Node]s in the topology.
@@ -88,33 +103,140 @@ internal class AdjacencyListTopology: MutableTopology {
* @param tag The tag of the edge that uniquely identifies the relationship the edge represents.
* @return The edge that has been created.
*/
- override fun <T> connect(from: Node<*>, to: Node<*>, label: T, tag: String?): Edge<T> {
- if (from !is AdjacencyListNode<*> || to !is AdjacencyListNode<*>)
- throw IllegalArgumentException()
- if (!from.validate(this) || !to.validate(this))
- throw IllegalArgumentException()
- val edge: Edge<T> = AdjacencyListEdge(label, tag, from, to)
+ override fun <T> connect(from: Entity<*>, to: Entity<*>, label: T, tag: String?): BaseEdge<T> {
+ if (!contains(from) || !contains(to))
+ throw IllegalArgumentException("One of the entities is not part of the topology")
+ val edge = Edge(label, tag, from, to)
from.outgoingEdges.add(edge)
to.ingoingEdges.add(edge)
return edge
}
+ // Cloneable
+
+ /**
+ * Create a copy of the graph.
+ *
+ * @return A new [Topology] instance with a copy of the graph.
+ */
+ override public fun clone(): Topology {
+ val copy = AdjacencyListTopology()
+ copy.nextId = AtomicInteger(nextId.get())
+ copy.nodes = HashMap(nodes)
+ return copy
+ }
+
+ // Set
+
+ /**
+ * Returns the size of the collection.
+ */
+ override val size: Int = nodes.size
+
+ /**
+ * Checks if the specified element is contained in this collection.
+ */
+ override fun contains(element: Entity<*>): Boolean = nodes.contains(element)
+
+ /**
+ * Checks if all elements in the specified collection are contained in this collection.
+ */
+ override fun containsAll(elements: Collection<Entity<*>>): Boolean = elements.all { nodes.containsKey(it) }
+
+ /**
+ * Returns `true` if the collection is empty (contains no elements), `false` otherwise.
+ */
+ override fun isEmpty(): Boolean = nodes.isEmpty()
+
+ // MutableSet
+
+ /**
+ * Add a node to the graph.
+ *
+ * @param element The element to add to this graph.
+ * @return `true` if the graph has changed, `false` otherwise.
+ */
+ override fun add(element: Entity<*>): Boolean = nodes.putIfAbsent(element, Node(nextId.getAndIncrement())) == null
+
+ /**
+ * Add all nodes in the specified collection to the graph.
+ *
+ * @param elements The nodes to add to this graph.
+ * @return `true` if the graph has changed, `false` otherwise.
+ */
+ override fun addAll(elements: Collection<Entity<*>>): Boolean = elements.any { add(it) }
+
+ /**
+ * Remove all nodes and their respective edges from the graph.
+ */
+ override fun clear() = nodes.clear()
+
+ /**
+ * Remove the given node and its edges from the graph.
+ *
+ * @param element The element to remove from the graph.
+ * @return `true` if the graph has changed, `false` otherwise.
+ */
+ override fun remove(element: Entity<*>): Boolean {
+ nodes[element]?.ingoingEdges?.forEach {
+ it.from.outgoingEdges.remove(it)
+ }
+ nodes[element]?.outgoingEdges?.forEach {
+ it.to.ingoingEdges.remove(it)
+ }
+ return nodes.keys.remove(element)
+ }
+
+
+ /**
+ * Remove all nodes in the given collection from the graph.
+ *
+ * @param elements The elements to remove from the graph.
+ * @return `true` if the graph has changed, `false` otherwise.
+ */
+ override fun removeAll(elements: Collection<Entity<*>>): Boolean = elements.any(this::remove)
+
/**
- * Returns an iterator over the elements of this object.
+ * Remove all nodes in the graph, except those in the specified collection.
+ *
+ * Take note that this method currently only guarantees a maximum runtime complexity of O(n^2).
+ *
+ * @param elements The elements to retain in the graph.
*/
- override fun iterator(): MutableIterator<Node<*>> = nodes.iterator()
+ override fun retainAll(elements: Collection<Entity<*>>): Boolean {
+ val iterator = nodes.keys.iterator()
+ var changed = false
+ while (iterator.hasNext()) {
+ val entity = iterator.next()
- internal inner class AdjacencyListNode<out T: Entity<*>>(override val id: Int, override val label: T): Node<T> {
- override var ingoingEdges: MutableSet<Edge<*>> = HashSet()
- override var outgoingEdges: MutableSet<Edge<*>> = HashSet()
- override fun toString(): String = label.toString()
- internal fun validate(instance: AdjacencyListTopology) = this@AdjacencyListTopology == instance
+ if (entity !in elements) {
+ iterator.remove()
+ changed = true
+ }
+ }
+ return changed
}
- internal class AdjacencyListEdge<out T>(override val label: T,
- override val tag: String?,
- override val from: Node<*>,
- override val to: Node<*>): Edge<T> {
- override fun toString(): String = label.toString()
+ /**
+ * Return a mutable iterator over the nodes of the graph.
+ *
+ * @return A [MutableIterator] over the nodes of the graph.
+ */
+ override fun iterator(): MutableIterator<Entity<*>> = nodes.keys.iterator()
+
+ /**
+ * The internal representation of a node within the graph.
+ */
+ internal data class Node(val id: Int) {
+ val ingoingEdges: MutableSet<BaseEdge<*>> = HashSet()
+ val outgoingEdges: MutableSet<BaseEdge<*>> = HashSet()
}
+
+ /**
+ * The internal representation of an edge within the graph.
+ */
+ internal class Edge<out T>(override val label: T,
+ override val tag: String?,
+ override val from: Entity<*>,
+ override val to: Entity<*>) : BaseEdge<T>
}
diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Component.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Component.kt
index 79b35e86..3c383892 100644
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Component.kt
+++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Component.kt
@@ -25,16 +25,9 @@
package nl.atlarge.opendc.topology
/**
- * A component within a [Topology], which is either an [Node] or an [Edge] representing the relationship between
+ * A component within a [Topology], which is either a node or an [Edge] representing the relationship between
* entities within a logical topology of a cloud network.
- *
- * <p>A [Component]'s label provides access to user-specified data.
- *
+ **
* @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
*/
-interface Component<out T> {
- /**
- * The label of this [Component].
- */
- val label: T
-}
+interface Component
diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Edge.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Edge.kt
index 22ad57c1..3be14dec 100644
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Edge.kt
+++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Edge.kt
@@ -25,12 +25,17 @@
package nl.atlarge.opendc.topology
/**
- * An edge that represents a directed relationship between exactly two [Node]s in a logical topology of a cloud network.
+ * An edge that represents a directed relationship between exactly two nodes in a logical topology of a cloud network.
*
* @param T The relationship type the edge represents within a logical topology.
* @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
*/
-interface Edge<out T>: Component<T> {
+interface Edge<out T> : Component {
+ /**
+ * The label of this edge.
+ */
+ val label: T
+
/**
* A tag to uniquely identify the relationship this edge represents.
*/
@@ -42,7 +47,7 @@ interface Edge<out T>: Component<T> {
* This property is not guaranteed to have a runtime complexity of <code>O(1)</code>, but must be at least
* <code>O(n)</code>, with respect to the size of the topology.
*/
- val from: Node<*>
+ val from: Entity<*>
/**
* The destination of the edge.
@@ -50,5 +55,5 @@ interface Edge<out T>: Component<T> {
* This property is not guaranteed to have a runtime complexity of <code>O(1)</code>, but must be at least
* <code>O(n)</code>, with respect to the size of the topology.
*/
- val to: Node<*>
+ val to: Entity<*>
}
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 e6f37cf6..66a31d77 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
@@ -25,10 +25,10 @@
package nl.atlarge.opendc.topology
/**
- * An entity within a cloud network.
+ * An entity within a cloud network, represented as a node within a topology.
*
- * <p>A [Entity] contains the immutable properties of this component given by the topology configuration at the start
- * of a simulation and remain unchanged during simulation.
+ * <p>A [Entity] contains immutable properties given by the topology configuration at the start of a simulation and
+ * remain unchanged during simulation.
*
* <p>In addition, other entities in a simulation have direct, immutable access to the observable state of this entity.
*
@@ -36,7 +36,7 @@ package nl.atlarge.opendc.topology
* a simulation.
* @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
*/
-interface Entity<out S> {
+interface Entity<out S> : Component {
/**
* The initial state of the entity.
*/
diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/ImmutableTopology.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/ImmutableTopology.kt
deleted file mode 100644
index 90ba5dc5..00000000
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/ImmutableTopology.kt
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * MIT License
- *
- * Copyright (c) 2017 atlarge-research
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-package nl.atlarge.opendc.topology
-/**
- * A [Topology] whose elements and structural relationships will never change.
- *
- * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
- */
-interface ImmutableTopology: Topology
diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/MutableTopology.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/MutableTopology.kt
index 0aa0d1b5..10a55e5b 100644
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/MutableTopology.kt
+++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/MutableTopology.kt
@@ -30,17 +30,9 @@ package nl.atlarge.opendc.topology
*
* @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
*/
-interface MutableTopology: Topology {
+interface MutableTopology : Topology, MutableSet<Entity<*>> {
/**
- * Create a [Node] in this [Topology] for the given [Entity].
- *
- * @param entity The entity to create a node for.
- * @return The node created for the given entity.
- */
- fun <T: Entity<*>> node(entity: T): Node<T>
-
- /**
- * Create a directed edge between two [Node]s in the topology.
+ * Create a directed, labeled edge between two nodes in the topology.
*
* @param from The source of the edge.
* @param to The destination of the edge.
@@ -48,23 +40,23 @@ interface MutableTopology: Topology {
* @param tag The tag of the edge that uniquely identifies the relationship the edge represents.
* @return The edge that has been created.
*/
- fun <T> connect(from: Node<*>, to: Node<*>, label: T, tag: String? = null): Edge<T>
+ fun <T> connect(from: Entity<*>, to: Entity<*>, label: T, tag: String? = null): Edge<T>
/**
- * Create a directed edge between two [Node]s in the topology.
+ * Create a directed, unlabeled edge between two nodes in the topology.
*
* @param from The source of the edge.
* @param to The destination of the edge.
* @param tag The tag of the edge that uniquely identifies the relationship the edge represents.
* @return The edge that has been created.
*/
- fun connect(from: Node<*>, to: Node<*>, tag: String? = null): Edge<Unit> = connect(from, to, Unit, tag)
+ fun connect(from: Entity<*>, to: Entity<*>, tag: String? = null): Edge<Unit> = connect(from, to, Unit, tag)
/**
- * Create a directed edge between two [Node]s in the topology.
+ * Create a directed, unlabeled edge between two nodes in the topology.
*
* @param dest The destination of the edge.
* @return The edge that has been created.
*/
- infix fun Node<*>.to(dest: Node<*>): Edge<Unit> = connect(this, dest)
+ infix fun Entity<*>.to(dest: Entity<*>): Edge<Unit> = connect(this, dest)
}
diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Node.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Node.kt
deleted file mode 100644
index ee1cde9b..00000000
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Node.kt
+++ /dev/null
@@ -1,77 +0,0 @@
-/*
- * MIT License
- *
- * Copyright (c) 2017 atlarge-research
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-package nl.atlarge.opendc.topology
-
-/**
- * A labeled node of graph representing an entity in a specific logical topology of a cloud network.
- *
- * <p>A [Node] is instantiated and managed by a [Topology] instance containing user-specified data in its label.
- *
- * @param T The entity type the node represents in a logical topology.
- * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
- */
-interface Node<out T: Entity<*>>: Component<T> {
- /**
- * A unique identifier of this node within the topology.
- */
- val id: Int
-
- /**
- * The set of ingoing edges of this node.
- */
- val ingoingEdges: Set<Edge<*>>
-
- /**
- * The set of outgoing edges of this node.
- */
- val outgoingEdges: Set<Edge<*>>
-
- /**
- * The [Entity] this node represents within a logical topology of a cloud network.
- */
- val entity: T
- get() = label
-}
-
-/**
- * Return the set of entities that are connected inwards to this node with the given tag.
- *
- * @param tag The tag of the edges to get.
- * @param T The shape of the label of these edges.
- * @return The entities of all edges whose destination is this node and have the given tag.
- */
-inline fun <reified T> Node<*>.ingoing(tag: String) =
- ingoingEdges.filter { it.tag == tag }.map { it.to.entity as T }.toSet()
-
-
-/**
- * Return the set of entities that are connected outwards to this node with the given tag.
- *
- * @param tag The tag of the edges to get.
- * @param T The shape of the label of these edges.
- * @return The entities of all edges whose source is this node and have the given tag.
- */
-inline fun <reified T> Node<*>.outgoing(tag: String) =
- outgoingEdges.filter { it.tag == tag }.map { it.to.entity as T }.toSet()
diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Topology.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Topology.kt
index 5d88334c..5b697bfb 100644
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Topology.kt
+++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/Topology.kt
@@ -28,8 +28,15 @@ package nl.atlarge.opendc.topology
* A graph data structure which represents the logical topology of a cloud network consisting of one or more
* datacenters.
*
- * <p>A topology is [Iterable] and allows implementation-dependent iteration of the nodes in the topology.
+ * A topology is [Iterable] and allows implementation-dependent iteration of the nodes in the topology.
*
* @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
*/
-interface Topology: Set<Node<*>>
+interface Topology : TopologyContext, Cloneable, Set<Entity<*>> {
+ /**
+ * Create a copy of the topology.
+ *
+ * @return A new [Topology] with a copy of the graph.
+ */
+ public override fun clone(): Topology
+}
diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/machine/ProcessingUnit.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/TopologyContext.kt
index a235133f..22e7dd94 100644
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/machine/ProcessingUnit.kt
+++ b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/TopologyContext.kt
@@ -22,28 +22,27 @@
* SOFTWARE.
*/
-package nl.atlarge.opendc.topology.machine
-
-import nl.atlarge.opendc.topology.Entity
+package nl.atlarge.opendc.topology
/**
- * An interface representing a generic processing unit which is placed into a [Machine].
+ * A [TopologyContext] represents the context for entities in a specific topology, providing access to the identifier
+ * and ingoing and outgoing edges of the [Entity] within a [Topology].
*
* @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
*/
-interface ProcessingUnit: Entity<Unit> {
+interface TopologyContext {
/**
- * The speed of this [ProcessingUnit] per core.
+ * A unique identifier of an [Entity] within the topology.
*/
- val speed: Int
+ val Entity<*>.id: Int
/**
- * The amount of cores within this [ProcessingUnit].
+ * The set of ingoing edges of an [Entity].
*/
- val cores: Int
+ val Entity<*>.ingoingEdges: Set<Edge<*>>
/**
- * The energy consumption of this [ProcessingUnit] in Kj/s.
+ * The set of outgoing edges of an [Entity].
*/
- val energyConsumption: Int
+ val Entity<*>.outgoingEdges: Set<Edge<*>>
}
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
deleted file mode 100644
index b484f70e..00000000
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/container/Datacenter.kt
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * MIT License
- *
- * Copyright (c) 2017 atlarge-research
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-package nl.atlarge.opendc.topology.container
-
-import nl.atlarge.opendc.topology.Entity
-
-/**
- * A representation of a facility used to house computer systems and associated components.
- *
- * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
- */
-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
deleted file mode 100644
index f9595adc..00000000
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/container/Rack.kt
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * MIT License
- *
- * Copyright (c) 2017 atlarge-research
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-package nl.atlarge.opendc.topology.container
-
-import nl.atlarge.opendc.topology.Entity
-
-/**
- * A type of physical steel and electronic framework that is designed to house servers, networking devices, cables and
- * other datacenter computing equipment.
- *
- * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
- */
-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
deleted file mode 100644
index 1e2135e8..00000000
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/machine/Cpu.kt
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * MIT License
- *
- * Copyright (c) 2017 atlarge-research
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-package nl.atlarge.opendc.topology.machine
-
-/**
- * A central processing unit.
- *
- * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
- */
-data class Cpu(
- override val speed: Int,
- override val cores: Int,
- override val energyConsumption: Int
-): 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
deleted file mode 100644
index a294db38..00000000
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/machine/Gpu.kt
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * MIT License
- *
- * Copyright (c) 2017 atlarge-research
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-package nl.atlarge.opendc.topology.machine
-
-/**
- * A graphics processing unit.
- *
- * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
- */
-class Gpu(
- override val speed: Int,
- override val cores: Int,
- override val energyConsumption: Int
-): 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
deleted file mode 100644
index b313b78b..00000000
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/machine/Machine.kt
+++ /dev/null
@@ -1,88 +0,0 @@
-/*
- * MIT License
- *
- * Copyright (c) 2017 atlarge-research
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-package nl.atlarge.opendc.topology.machine
-
-import nl.atlarge.opendc.experiment.Task
-import nl.atlarge.opendc.simulator.EntityContext
-import nl.atlarge.opendc.simulator.Kernel
-import nl.atlarge.opendc.topology.Entity
-import nl.atlarge.opendc.topology.outgoing
-import java.util.*
-
-/**
- * A Physical Machine (PM) inside a rack of a datacenter. It has a speed, and can be given a workload on which it will
- * work until finished or interrupted.
- *
- * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
- */
-class Machine: Entity<Machine.State>, Kernel<EntityContext<Machine>> {
- /**
- * 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)
-
- /**
- * Run the simulation kernel for this entity.
- */
- override suspend fun EntityContext<Machine>.simulate() {
- update(state.copy(status = Machine.Status.IDLE))
-
- val cpus = component.outgoing<Cpu>("cpu")
- val speed = cpus.fold(0, { acc, (speed, cores) -> acc + speed * cores })
- val task: Task
-
- val delay = Random().nextInt(1000) + 1
- wait(delay)
-
- loop@while (true) {
- val msg = receive()
- when (msg) {
- is Task -> {
- task = msg
- break@loop
- }
- else -> println("warning: unhandled message $msg")
- }
- }
-
- update(state.copy(status = Machine.Status.RUNNING))
- while (tick()) {
- task.consume(speed.toLong())
- }
- update(state.copy(status = Machine.Status.HALT))
- }
-}
diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/network/NetworkUnit.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/network/NetworkUnit.kt
deleted file mode 100644
index 9c294125..00000000
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/network/NetworkUnit.kt
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * MIT License
- *
- * Copyright (c) 2017 atlarge-research
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-package nl.atlarge.opendc.topology.network
-
-import nl.atlarge.opendc.topology.Entity
-
-/**
- * A generic interface for a network unit in a cloud network.
- *
- * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
- */
-interface NetworkUnit: Entity<Unit>
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
deleted file mode 100644
index 2938e530..00000000
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/power/PowerUnit.kt
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * MIT License
- *
- * Copyright (c) 2017 atlarge-research
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-package nl.atlarge.opendc.topology.power
-
-import nl.atlarge.opendc.topology.Entity
-
-/**
- * An [Entity] which provides power for other entities a cloud network to run.
- *
- * @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> {
- override val initialState = Unit
-}
diff --git a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/storage/StorageUnit.kt b/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/storage/StorageUnit.kt
deleted file mode 100644
index 8e53e365..00000000
--- a/opendc-core/src/main/kotlin/nl/atlarge/opendc/topology/storage/StorageUnit.kt
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * MIT License
- *
- * Copyright (c) 2017 atlarge-research
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-package nl.atlarge.opendc.topology.storage
-
-import nl.atlarge.opendc.topology.Entity
-
-/**
- * A generic interface for a storage unit in a cloud network.
- *
- * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
- */
-interface StorageUnit: Entity<Unit>