View Javadoc
1   package nl.tudelft.simulation.dsol.experiment;
2   
3   import java.util.LinkedHashMap;
4   import java.util.Map;
5   
6   import org.djutils.exceptions.Throw;
7   
8   import nl.tudelft.simulation.jstats.streams.StreamInterface;
9   
10  /**
11   * StreamSeedUpdater updates the streams based on a stored map of replication numbers to seed numbers.
12   * <p>
13   * Copyright (c) 2021-2023 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
14   * for project information <a href="https://simulation.tudelft.nl/dsol/manual/" target="_blank">DSOL Manual</a>. The DSOL
15   * project is distributed under a three-clause BSD-style license, which can be found at
16   * <a href="https://https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">DSOL License</a>.
17   * </p>
18   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
19   */
20  public class StreamSeedUpdater implements StreamUpdater
21  {
22      /** */
23      private static final long serialVersionUID = 20210408L;
24  
25      /** the mapping of replication numbers to seeds for each of the streams, identified by id. */
26      private final Map<String, Map<Integer, Long>> streamSeedMap = new LinkedHashMap<>();
27  
28      /** the fallback stream updater in case the stream or the replication is not in the seed map. */
29      private StreamUpdater fallbackStreamUpdater;
30  
31      /**
32       * Construct a new StreamSeedUpdater object an initialize it with the seed map.
33       * @param streamSeedMap Map&lt;String, Map&lt;Integer, Long&gt;&gt;; the stored seed maps
34       */
35      public StreamSeedUpdater(final Map<String, Map<Integer, Long>> streamSeedMap)
36      {
37          this.streamSeedMap.putAll(streamSeedMap);
38          this.fallbackStreamUpdater = new SimpleStreamUpdater();
39      }
40  
41      /** {@inheritDoc} */
42      @Override
43      public void updateSeed(final String streamId, final StreamInterface stream, final int replicationNumber)
44      {
45          if (this.streamSeedMap.containsKey(streamId) && this.streamSeedMap.get(streamId).containsKey(replicationNumber))
46          {
47              stream.setSeed(this.streamSeedMap.get(streamId).get(replicationNumber));
48          }
49          else
50          {
51              // streamId not found in seed map -- fall back to other method
52              this.fallbackStreamUpdater.updateSeed(streamId, stream, replicationNumber);
53          }
54      }
55  
56      /**
57       * Return the fallback stream updater in case the stream or the replication is not in the seed map.
58       * @return fallbackStreamUpdater StreamUpdater; the fallback stream updater in case the stream or the replication is not in
59       *         the seed map.
60       */
61      public StreamUpdater getFallbackStreamUpdater()
62      {
63          return this.fallbackStreamUpdater;
64      }
65  
66      /**
67       * Set a new fallback stream updater in case the stream or the replication is not in the seed map.
68       * @param fallbackStreamUpdater StreamUpdater; the new fallback stream updater in case the stream or the replication is not
69       *            in the seed map.
70       * @throws NullPointerException when fallbackStreamUpdater is null
71       */
72      public void setFallbackStreamUpdater(final StreamUpdater fallbackStreamUpdater)
73      {
74          Throw.whenNull(fallbackStreamUpdater, "fallbackStreamUpdater cannot be null");
75          this.fallbackStreamUpdater = fallbackStreamUpdater;
76      }
77  
78      /**
79       * Return the available seed maps for all stored streams, mapping stream ids to seed maps.
80       * @return Map&lt;String, Map&lt;Integer, Long&gt;&gt;; the stored seed maps
81       */
82      public Map<String, Map<Integer, Long>> getStreamSeedMap()
83      {
84          return this.streamSeedMap;
85      }
86  
87  }