View Javadoc
1   package nl.tudelft.simulation.dsol.model;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import org.djutils.event.LocalEventProducer;
7   import org.djutils.exceptions.Throw;
8   
9   import nl.tudelft.simulation.dsol.experiment.StreamInformation;
10  import nl.tudelft.simulation.dsol.model.inputparameters.AbstractInputParameter;
11  import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterException;
12  import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterMap;
13  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
14  import nl.tudelft.simulation.dsol.statistics.SimulationStatistic;
15  import nl.tudelft.simulation.jstats.streams.MersenneTwister;
16  
17  /**
18   * AbstractDsolModel, an abstract helper class to easily construct a DsolModel. The model automatically acts as an
19   * EventProducer, so events can be sent to statistics gathering classes. <br>
20   * <br>
21   * Copyright (c) 2003-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
22   * for project information <a href="https://www.simulation.tudelft.nl/" target="_blank">www.simulation.tudelft.nl</a>. The
23   * source code and binary code of this software is proprietary information of Delft University of Technology.
24   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
25   * @param <T> the time type
26   * @param <S> the simulator type to use
27   */
28  public abstract class AbstractDsolModel<T extends Number & Comparable<T>, S extends SimulatorInterface<T>> extends LocalEventProducer
29          implements DsolModel<T, S>
30  {
31      /** */
32      private static final long serialVersionUID = 20181117L;
33  
34      /** the simulator. */
35      @SuppressWarnings("checkstyle:visibilitymodifier")
36      protected S simulator;
37  
38      /** the input parameters. */
39      @SuppressWarnings("checkstyle:visibilitymodifier")
40      protected InputParameterMap inputParameterMap = new InputParameterMap("model", "Model parameters", "Model parameters", 1.0);
41  
42      /** the output statistics. */
43      @SuppressWarnings("checkstyle:visibilitymodifier")
44      protected List<SimulationStatistic<T>> outputStatistics = new ArrayList<>();
45  
46      /** streams used in the replication. */
47      @SuppressWarnings("checkstyle:visibilitymodifier")
48      protected StreamInformation streamInformation;
49  
50      /**
51       * Construct a DSOL model and set the simulator.
52       * @param simulator S; the simulator to use for this model
53       * @throws NullPointerException when simulator is null
54       */
55      public AbstractDsolModel(final S simulator)
56      {
57          this(simulator, new StreamInformation(new MersenneTwister(10L)));
58      }
59  
60      /**
61       * Construct a DSOL model and set the simulator as well as the initial streams, so they can be used in the constructor of
62       * the model.
63       * @param simulator S; the simulator to use for this model
64       * @param streamInformation StreamInformation; the streams that have been prepared in a StreamInformation class
65       * @throws NullPointerException when simulator or streamInformation is null
66       */
67      public AbstractDsolModel(final S simulator, final StreamInformation streamInformation)
68      {
69          Throw.whenNull(simulator, "simulator cannot be null");
70          this.simulator = simulator;
71          setStreamInformation(streamInformation);
72      }
73  
74      /** {@inheritDoc} */
75      @Override
76      public void setStreamInformation(final StreamInformation streamInformation)
77      {
78          Throw.whenNull(streamInformation, "streamInformation cannot be null");
79          this.streamInformation = streamInformation;
80      }
81  
82      /** {@inheritDoc} */
83      @Override
84      public StreamInformation getStreamInformation()
85      {
86          return this.streamInformation;
87      }
88  
89      /** {@inheritDoc} */
90      @Override
91      public S getSimulator()
92      {
93          return this.simulator;
94      }
95  
96      /** {@inheritDoc} */
97      @Override
98      public InputParameterMap getInputParameterMap()
99      {
100         return this.inputParameterMap;
101     }
102 
103     /**
104      * Add an input parameter to the list of input parameters.
105      * @param inputParameter AbstractInputParameter&lt;?,?&gt;; the input parameter to add
106      * @throws InputParameterException in case an input parameter with the same key already exists
107      */
108     public void addInputParameter(final AbstractInputParameter<?, ?> inputParameter) throws InputParameterException
109     {
110         this.inputParameterMap.add(inputParameter);
111     }
112 
113     /**
114      * Retrieve the value of an input parameter from the map of input parameters, based on a key. The key can use the 'dot
115      * notation' to access values in sub-maps of input parameters.
116      * @param key String; the key of the input parameter to retrieve
117      * @return the value belonging to the key, or null if the key could not be found
118      * @throws InputParameterException in case the input parameter with this key does not exist
119      */
120     public Object getInputParameter(final String key) throws InputParameterException
121     {
122         return this.inputParameterMap.get(key).getCalculatedValue();
123     }
124 
125     /** {@inheritDoc} */
126     @Override
127     public List<SimulationStatistic<T>> getOutputStatistics()
128     {
129         return this.outputStatistics;
130     }
131 
132 }