StreamSeedUpdater.java

  1. package nl.tudelft.simulation.dsol.experiment;

  2. import java.util.LinkedHashMap;
  3. import java.util.Map;

  4. import org.djutils.exceptions.Throw;

  5. import nl.tudelft.simulation.jstats.streams.StreamInterface;

  6. /**
  7.  * StreamSeedUpdater updates the streams based on a stored map of replication numbers to seed numbers.
  8.  * <p>
  9.  * Copyright (c) 2021-2025 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
  10.  * for project information <a href="https://simulation.tudelft.nl/dsol/manual/" target="_blank">DSOL Manual</a>. The DSOL
  11.  * project is distributed under a three-clause BSD-style license, which can be found at
  12.  * <a href="https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">DSOL License</a>.
  13.  * </p>
  14.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  15.  */
  16. public class StreamSeedUpdater implements StreamUpdater
  17. {
  18.     /** */
  19.     private static final long serialVersionUID = 20210408L;

  20.     /** the mapping of replication numbers to seeds for each of the streams, identified by id. */
  21.     private final Map<String, Map<Integer, Long>> streamSeedMap = new LinkedHashMap<>();

  22.     /** the fallback stream updater in case the stream or the replication is not in the seed map. */
  23.     private StreamUpdater fallbackStreamUpdater;

  24.     /**
  25.      * Construct a new StreamSeedUpdater object an initialize it with the seed map.
  26.      * @param streamSeedMap Map&lt;String, Map&lt;Integer, Long&gt;&gt;; the stored seed maps
  27.      */
  28.     public StreamSeedUpdater(final Map<String, Map<Integer, Long>> streamSeedMap)
  29.     {
  30.         this.streamSeedMap.putAll(streamSeedMap);
  31.         this.fallbackStreamUpdater = new SimpleStreamUpdater();
  32.     }

  33.     @Override
  34.     public void updateSeed(final String streamId, final StreamInterface stream, final int replicationNumber)
  35.     {
  36.         if (this.streamSeedMap.containsKey(streamId) && this.streamSeedMap.get(streamId).containsKey(replicationNumber))
  37.         {
  38.             stream.setSeed(this.streamSeedMap.get(streamId).get(replicationNumber));
  39.         }
  40.         else
  41.         {
  42.             // streamId not found in seed map -- fall back to other method
  43.             this.fallbackStreamUpdater.updateSeed(streamId, stream, replicationNumber);
  44.         }
  45.     }

  46.     /**
  47.      * Return the fallback stream updater in case the stream or the replication is not in the seed map.
  48.      * @return fallbackStreamUpdater StreamUpdater; the fallback stream updater in case the stream or the replication is not in
  49.      *         the seed map.
  50.      */
  51.     public StreamUpdater getFallbackStreamUpdater()
  52.     {
  53.         return this.fallbackStreamUpdater;
  54.     }

  55.     /**
  56.      * Set a new fallback stream updater in case the stream or the replication is not in the seed map.
  57.      * @param fallbackStreamUpdater StreamUpdater; the new fallback stream updater in case the stream or the replication is not
  58.      *            in the seed map.
  59.      * @throws NullPointerException when fallbackStreamUpdater is null
  60.      */
  61.     public void setFallbackStreamUpdater(final StreamUpdater fallbackStreamUpdater)
  62.     {
  63.         Throw.whenNull(fallbackStreamUpdater, "fallbackStreamUpdater cannot be null");
  64.         this.fallbackStreamUpdater = fallbackStreamUpdater;
  65.     }

  66.     /**
  67.      * Return the available seed maps for all stored streams, mapping stream ids to seed maps.
  68.      * @return Map&lt;String, Map&lt;Integer, Long&gt;&gt;; the stored seed maps
  69.      */
  70.     public Map<String, Map<Integer, Long>> getStreamSeedMap()
  71.     {
  72.         return this.streamSeedMap;
  73.     }

  74. }