Machine Shop

In this example, we'll learn how to interrupt a process because of more important tasks. The example is adopted from the SimPy Machine Shop Example.

The example covers interrupt and preemptive resources.

The example comprises a workshop with n identical machines. A stream of jobs (enough to keep the machines busy) arrives. Each machine breaks down periodically. Repairs are carried out by one repairman. The repairman has other, less important tasks to perform, too. Broken machines preempt these tasks. The repairman continues them when he is done with the machine repair. The workshop works continuously.

A machine has two processes:

  1. working implements the actual behaviour of the machine (producing parts).
  2. break_machine periodically interrupts the working process to simulate the machine failure.

In kalasim there can only be one generating process per component. So to model the wear, we use a separate MachineWear which is interrupting the machine in case of failure.

The repairman’s other job is also a process (implemented by otherJob). The repairman itself is a preemptive resource with a capacity of 1. The machine repairing has a priority of 1, while the other job has a priority of 2 (the smaller the number, the higher the priority).

import org.apache.commons.math3.distribution.RealDistribution
import org.kalasim.*

val RANDOM_SEED: Int = 42
val PT_MEAN: Double = 10.0 // Avg. processing time in minutes
val PT_SIGMA: Double = 2.0 // Sigma of processing time
val MTTF: Double = 300.0 // Mean time to failure in minutes
val REPAIR_TIME: Double = 30.0 // Time it takes to repair a machine in minutes
val JOB_DURATION: Double = 30.0 // Duration of other jobs in minutes
val NUM_MACHINES: Int = 10   // Number of machines in the machine shop
val WEEKS: Int = 4   // Simulation time in weeks
val SIM_TIME: Number = WEEKS * 7 * 24 * 60  // Simulation time in minutes

fun main() {

    class Machine : Component() {
        var madeParts: Int = 0
            private set

        val timePerPart: RealDistribution = normal(PT_MEAN, PT_SIGMA)

        override fun process(): Sequence<Component> = sequence {
            while (true) {
                // start working on a new part
                log("building a new part")
                log("finished building part")

    /** Break the machine every now and then. */
    class MachineWear(val machine: Machine, val repairMan: Resource) : Component(
        process = MachineWear::breakMachine
    ) {

        fun breakMachine(): Sequence<Component> = sequence {
            val timeToFailure = exponential(MTTF)

            while (true) {

                // handle the rare case that the model
                if (machine.isInterrupted) continue



                require(!isBumped(repairMan)) { "productive tools must not be bumped" }


                require(!machine.isInterrupted) { "machine must not be interrupted at end of wear cycle" }

    createSimulation(true, randomSeed = RANDOM_SEED) {

        val repairMan = Resource("mechanic", preemptive = true)

        // create N machines and wear components
        val tools = (1..NUM_MACHINES).map {
            Machine().also { MachineWear(it, repairMan) }

        // define the other jobs as object expression
        object : Component("side jobs") {
            override fun process() = sequence {
                while (true) {
                    request(ResourceRequest(repairMan, priority = Priority(-1)))

                    if (isBumped(repairMan)) {
                        log("other job was bumped")


        // Run simulation

        // Analysis

        tools.forEach { println("${} made ${it.madeParts} parts.") }