View Javadoc
1   package nl.tudelft.simulation.dsol.experiment;
2   
3   import java.io.Serializable;
4   
5   import org.djutils.event.EventType;
6   import org.djutils.exceptions.Throw;
7   import org.djutils.metadata.MetaData;
8   
9   import nl.tudelft.simulation.naming.context.ContextInterface;
10  import nl.tudelft.simulation.naming.context.Contextualized;
11  
12  /**
13   * The base class for a single replication of an Experiment.
14   * <p>
15   * Copyright (c) 2002-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
16   * for project information <a href="https://simulation.tudelft.nl/" target="_blank"> https://simulation.tudelft.nl</a>. The DSOL
17   * project is distributed under a three-clause BSD-style license, which can be found at
18   * <a href="https://https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">
19   * https://https://simulation.tudelft.nl/dsol/docs/latest/license.html</a>.
20   * </p>
21   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a> relative types are the same.
22   * @param <T> the time type
23   */
24  public abstract class Replication<T extends Number & Comparable<T>> implements Contextualized, Treatment<T>, Serializable
25  {
26      /** The default serial version UID for serializable classes. */
27      private static final long serialVersionUID = 20210404L;
28  
29      /** START_REPLICATION_EVENT is fired when a replication is started. */
30      public static final EventType START_REPLICATION_EVENT =
31              new EventType(new MetaData("START_REPLICATION_EVENT", "Replication started"));
32  
33      /** END_REPLICATION_EVENT is fired when a replication is finished. */
34      public static final EventType END_REPLICATION_EVENT =
35              new EventType(new MetaData("END_REPLICATION_EVENT", "Replication ended"));
36  
37      /** WARMUP_EVENT is fired when the warmup period is over, and statistics have to be reset. */
38      public static final EventType WARMUP_EVENT = new EventType(new MetaData("WARMUP_EVENT", "warmup time"));
39  
40      /** the run control for the replication. */
41      private RunControl<T> runControl;
42  
43      /** the context root of this replication. */
44      private ContextInterface context;
45  
46      /**
47       * Construct a stand-alone replication. Checking the validity of the arguments is left to the RunControl object.
48       * @param id String; the id of the replication; should be unique within the experiment.
49       * @param startTime T; the start time of the simulation.
50       * @param warmupPeriod T; the warmup period, included in the runlength (!)
51       * @param runLength T; the total length of the run, including the warm-up period.
52       * @throws NullPointerException when id, startTime, warmupPeriod or runLength is null
53       * @throws IllegalArgumentException when warmup period is negative, or run length is zero or negative, or when a context for
54       *             the replication cannot be created, or when the warmup time is longer than or equal to the runlength
55       */
56      public Replication(final String id, final T startTime, final T warmupPeriod, final T runLength)
57      {
58          this(new RunControl<T>(id, startTime, warmupPeriod, runLength));
59      }
60  
61      /**
62       * Construct a stand-alone replication using a RunControl to store the run information.
63       * @param runControl RunControlInterface; the run control for the replication
64       * @throws NullPointerException when runControl is null
65       */
66      public Replication(final RunControl<T> runControl)
67      {
68          Throw.whenNull(runControl, "runControl should not be null");
69          this.runControl = runControl;
70      }
71  
72      /** {@inheritDoc} */
73      @Override
74      public RunControl<T> getRunControl()
75      {
76          return this.runControl;
77      }
78  
79      /**
80       * Set the context; method is protected so only subclasses can use this setter.
81       * @param context ContextInterface; set new context
82       */
83      protected void setContext(final ContextInterface context)
84      {
85          this.context = context;
86      }
87  
88      /** {@inheritDoc} */
89      @Override
90      public ContextInterface getContext()
91      {
92          return this.context;
93      }
94  
95  }