View Javadoc
1   package org.opentrafficsim.road.network.factory.xml.parser;
2   
3   import java.util.HashMap;
4   import java.util.List;
5   import java.util.Map;
6   
7   import javax.naming.NamingException;
8   
9   import org.djunits.value.vdouble.scalar.Duration;
10  import org.djunits.value.vdouble.scalar.Time;
11  import org.opentrafficsim.core.dsol.OTSReplication;
12  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
13  import org.opentrafficsim.road.network.OTSRoadNetwork;
14  import org.opentrafficsim.road.network.factory.xml.XmlParserException;
15  import org.opentrafficsim.road.network.factory.xml.utils.StreamInformation;
16  import org.opentrafficsim.xml.generated.RANDOMSTREAM;
17  import org.opentrafficsim.xml.generated.RANDOMSTREAM.REPLICATION;
18  import org.opentrafficsim.xml.generated.RUN;
19  
20  import nl.tudelft.simulation.dsol.experiment.Experiment;
21  import nl.tudelft.simulation.dsol.experiment.Treatment;
22  import nl.tudelft.simulation.jstats.streams.StreamInterface;
23  
24  /**
25   * RunParser parses the XML nodes of the RUN tag, including the RANDOMSTREAM tags. <br>
26   * <br>
27   * Copyright (c) 2003-2018 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
28   * for project information <a href="https://www.simulation.tudelft.nl/" target="_blank">www.simulation.tudelft.nl</a>. The
29   * source code and binary code of this software is proprietary information of Delft University of Technology.
30   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
31   */
32  public final class RunParser
33  {
34      /** */
35      private RunParser()
36      {
37          // utility class
38      }
39  
40      /**
41       * Parse the RUN tag in the OTS XML file.
42       * @param run the RUN tag
43       * @param otsNetwork the network
44       * @param streamMap the map with the random streams to be filled
45       * @param simulator the simulator to defined the experiment for
46       * @return experiment on the basis of the information in the RUN tag
47       * @throws XmlParserException on parsing error
48       */
49      public static Experiment.TimeDoubleUnit<OTSSimulatorInterface> parseRun(final OTSRoadNetwork otsNetwork, final RUN run,
50              final Map<String, StreamInformation> streamMap, final OTSSimulatorInterface simulator) throws XmlParserException
51      {
52          int numberReplications = run.getNUMBERREPLICATIONS() == null ? 1 : run.getNUMBERREPLICATIONS().intValue();
53          Time startTime = run.getSTARTTIME() == null ? Time.ZERO : run.getSTARTTIME();
54          Duration warmupPeriod = run.getWARMUPPERIOD() == null ? Duration.ZERO : run.getWARMUPPERIOD();
55          Duration runLength = run.getRUNLENGTH() == null ? Duration.ZERO : run.getRUNLENGTH();
56  
57          Map<String, StreamInterface> streams = new HashMap<>();
58          if (run.getRANDOMSTREAMS() == null)
59          {
60              for (String streamId : new String[] {"default", "generation"})
61              {
62                  Map<Integer, Long> seedMap = new HashMap<>();
63                  for (int rep = 1; rep <= numberReplications; rep++)
64                  {
65                      seedMap.put(rep, (long) streamId.hashCode() + rep);
66                  }
67                  StreamInformation streamInformation = new StreamInformation(streamId, seedMap);
68                  streamMap.put(streamId, streamInformation);
69                  streams.put(streamId, streamInformation.getStream());
70              }
71          }
72          else
73          {
74              List<RANDOMSTREAM> streamTags = run.getRANDOMSTREAMS().getRANDOMSTREAM();
75              for (RANDOMSTREAM streamTag : streamTags)
76              {
77                  String streamId = streamTag.getID();
78                  Map<Integer, Long> seedMap = new HashMap<>();
79                  for (REPLICATION rep : streamTag.getREPLICATION())
80                  {
81                      seedMap.put(rep.getID().intValue(), rep.getSEED().longValue());
82                  }
83                  StreamInformation streamInformation = new StreamInformation(streamId, seedMap);
84                  streamMap.put(streamId, streamInformation);
85                  streams.put(streamId, streamInformation.getStream());
86              }
87          }
88  
89          Experiment.TimeDoubleUnit<OTSSimulatorInterface> experiment = new Experiment.TimeDoubleUnit<>();
90  
91          Treatment.TimeDoubleUnit treatment = new Treatment.TimeDoubleUnit(experiment, "Treatment for " + otsNetwork.getId(),
92                  startTime, warmupPeriod, runLength);
93          experiment.setTreatment(treatment);
94          for (
95  
96                  int replicationNr = 1; replicationNr <= numberReplications; replicationNr++)
97          {
98              try
99              {
100                 OTSReplication replication = new OTSReplication("Replication " + replicationNr, experiment);
101                 replication.setStreams(streams);
102                 // TODO: the seeds need to be updated with the start of each new replication. Maybe change in DSOL.
103             }
104             catch (NamingException exception)
105             {
106                 throw new XmlParserException("Problems generating replicaton", exception);
107             }
108         }
109 
110         return experiment;
111     }
112 }