View Javadoc
1   package nl.tudelft.simulation.dsol.experiment;
2   
3   import java.util.function.Predicate;
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.dsol.model.DsolModel;
10  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
11  import nl.tudelft.simulation.naming.context.ContextInterface;
12  import nl.tudelft.simulation.naming.context.Contextualized;
13  
14  /**
15   * The base class for a single replication of an Experiment.
16   * <p>
17   * Copyright (c) 2002-2025 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
18   * for project information <a href="https://simulation.tudelft.nl/dsol/manual/" target="_blank">DSOL Manual</a>. The DSOL
19   * project is distributed under a three-clause BSD-style license, which can be found at
20   * <a href="https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">DSOL License</a>.
21   * </p>
22   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a> relative types are the same.
23   * @param <T> the time type
24   */
25  public abstract class Replication<T extends Number & Comparable<T>> implements Contextualized, Treatment<T>
26  {
27      /** START_REPLICATION_EVENT is fired when a replication is started. */
28      public static final EventType START_REPLICATION_EVENT =
29              new EventType(new MetaData("START_REPLICATION_EVENT", "Replication started"));
30  
31      /** END_REPLICATION_EVENT is fired when a replication is finished. */
32      public static final EventType END_REPLICATION_EVENT =
33              new EventType(new MetaData("END_REPLICATION_EVENT", "Replication ended"));
34  
35      /** WARMUP_EVENT is fired when the warmup period is over, and statistics have to be reset. */
36      public static final EventType WARMUP_EVENT = new EventType(new MetaData("WARMUP_EVENT", "warmup time"));
37  
38      /** the run control for the replication. */
39      private RunControl<T> runControl;
40  
41      /** the context root of this replication. */
42      private ContextInterface context;
43  
44      /**
45       * Construct a stand-alone replication. Checking the validity of the arguments is left to the RunControl object.
46       * @param id the id of the replication; should be unique within the experiment.
47       * @param startTime the start time of the simulation.
48       * @param warmupPeriod the warmup period, included in the runlength (!)
49       * @param runLength the total length of the run, including the warm-up period.
50       * @throws NullPointerException when id, startTime, warmupPeriod or runLength is null
51       * @throws IllegalArgumentException when warmup period is negative, or run length is zero or negative, or when a context for
52       *             the replication cannot be created, or when the warmup time is longer than or equal to the runlength
53       */
54      public Replication(final String id, final T startTime, final T warmupPeriod, final T runLength)
55      {
56          this(new RunControl<T>(id, startTime, warmupPeriod, runLength));
57      }
58  
59      /**
60       * Construct a stand-alone replication using a RunControl to store the run information.
61       * @param runControl the run control for the replication
62       * @throws NullPointerException when runControl is null
63       */
64      public Replication(final RunControl<T> runControl)
65      {
66          Throw.whenNull(runControl, "runControl should not be null");
67          this.runControl = runControl;
68      }
69  
70      /**
71       * Return the RunControl belonging to the replication.
72       * @return the RunControl belonging to the replication
73       */
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 set new context
82       */
83      protected void setContext(final ContextInterface context)
84      {
85          this.context = context;
86      }
87  
88      @Override
89      public ContextInterface getContext()
90      {
91          return this.context;
92      }
93  
94      @Override
95      public String getId()
96      {
97          return getRunControl().getId();
98      }
99  
100     @Override
101     public void setDescription(final String description)
102     {
103         getRunControl().setDescription(description);
104     }
105 
106     @Override
107     public String getDescription()
108     {
109         return getRunControl().getDescription();
110     }
111 
112     @Override
113     public T getStartTime()
114     {
115         return getRunControl().getStartTime();
116     }
117 
118     @Override
119     public T getEndTime()
120     {
121         return getRunControl().getEndTime();
122     }
123 
124     @Override
125     public T getWarmupTime()
126     {
127         return getRunControl().getWarmupTime();
128     }
129 
130     @Override
131     public <M extends DsolModel<T, ? extends SimulatorInterface<T>>> Predicate<? super M> getStoppingCondition()
132     {
133         return getRunControl().getStoppingCondition();
134     }
135 
136     @Override
137     public <M extends DsolModel<T, ? extends SimulatorInterface<T>>> void setStoppingCondition(
138             final Predicate<? super M> stoppingCondition)
139     {
140         getRunControl().setStoppingCondition(stoppingCondition);
141     }
142 
143 }