it.unibo.alchemist.model.timedistributions.AbstractDistribution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of alchemist-implementationbase Show documentation
Show all versions of alchemist-implementationbase Show documentation
Abstract, incarnation independent implementations of the Alchemist's interfaces. Provides support for those who want to write incarnations.
/*
* 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.timedistributions;
import it.unibo.alchemist.model.Environment;
import it.unibo.alchemist.model.Node;
import it.unibo.alchemist.model.Time;
import it.unibo.alchemist.model.TimeDistribution;
import javax.annotation.Nonnull;
import java.io.Serial;
/**
* This class provides, through a template method pattern, an utility that
* ensures that the distribution does not trigger events before its initial
* scheduling time.
*
* @param concentration type
*/
public abstract class AbstractDistribution implements TimeDistribution {
@Serial
private static final long serialVersionUID = -8906648194668569179L;
private Time tau;
private boolean schedulable;
private final Time startTime;
/**
* @param start
* initial time
*/
public AbstractDistribution(final Time start) {
tau = start;
startTime = start;
}
/**
* Allows subclasses to set the next putative time. Use with care.
*
* @param t
* the new time
*/
protected final void setNextOccurrence(final Time t) {
this.tau = t;
}
@Override
public final void update(
final @Nonnull Time currentTime,
final boolean hasBeenExecuted,
final double additionalParameter,
final @Nonnull Environment environment
) {
if (!schedulable && currentTime.compareTo(startTime) >= 0) {
/*
* If the simulation time is beyond the startTime for this reaction,
* it can start being scheduled normally.
*/
schedulable = true;
}
/*
* If the current time is not past the starting time for this reaction,
* it should not be used.
*/
updateStatus(schedulable ? currentTime : startTime, hasBeenExecuted, additionalParameter, environment);
}
@Override
public final Time getNextOccurence() {
return tau;
}
/**
* Implement this method to update the distribution's internal status.
*
* @param currentTime
* current time
* @param executed
* true if the reaction whose this distribution has been
* associated has just been executed
* @param param
* optional parameter passed by the reaction
* @param environment
* the current environment
*/
protected abstract void updateStatus(Time currentTime, boolean executed, double param, Environment environment);
@Override
public abstract AbstractDistribution cloneOnNewNode(@Nonnull Node destination, @Nonnull Time currentTime);
}