All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.unibo.alchemist.model.physics.reactions.PhysicsUpdate.kt Maven / Gradle / Ivy

There is a newer version: 35.0.1
Show newest version
/*
 * Copyright (C) 2010-2023, Danilo Pianini and contributors
 * listed, for each module, in the respective subproject's build.gradle.kts file.
 *
 * This file is part of Alchemist, and is distributed under the terms of the
 * GNU General Public License, with a linking exception,
 * as described in the file LICENSE in the Alchemist distribution's top directory.
 */

package it.unibo.alchemist.model.physics.reactions

import it.unibo.alchemist.model.Action
import it.unibo.alchemist.model.Actionable
import it.unibo.alchemist.model.Condition
import it.unibo.alchemist.model.Dependency
import it.unibo.alchemist.model.Environment
import it.unibo.alchemist.model.GlobalReaction
import it.unibo.alchemist.model.Time
import it.unibo.alchemist.model.TimeDistribution
import it.unibo.alchemist.model.physics.PhysicsDependency
import it.unibo.alchemist.model.physics.environments.Dynamics2DEnvironment
import it.unibo.alchemist.model.timedistributions.DiracComb
import org.danilopianini.util.ImmutableListSet
import org.danilopianini.util.ListSet

/**
 * A global Reaction responsible for updating the physics of an [Dynamics2DEnvironment].
 */
class PhysicsUpdate(
    /**
     * The environment to update.
     */
    val environment: Dynamics2DEnvironment,
    override val timeDistribution: TimeDistribution,
) : GlobalReaction {

    constructor(
        environment: Dynamics2DEnvironment,
        updateRate: Double = 30.0,
    ) : this(environment, DiracComb(updateRate))

    override val outboundDependencies: ListSet = ListSet.of(PhysicsDependency)
        get() = ImmutableListSet.copyOf(field)

    override val inboundDependencies: ListSet = ListSet.of()
        get() = ImmutableListSet.copyOf(field)

    override val rate: Double get() = timeDistribution.rate

    override val tau: Time get() = timeDistribution.nextOccurence

    override var actions: List> = listOf()

    override var conditions: List> = listOf()

    override fun compareTo(other: Actionable): Int = tau.compareTo(other.tau)

    override fun canExecute(): Boolean = conditions.all { it.isValid }

    override fun execute() {
        environment.updatePhysics(1 / rate)
        timeDistribution.update(timeDistribution.nextOccurence, true, 1.0, environment)
    }

    override fun update(currentTime: Time, hasBeenExecuted: Boolean, environment: Environment) = Unit

    override fun initializationComplete(atTime: Time, environment: Environment) = Unit
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy