View Javadoc
1   package nl.tudelft.simulation.dsol.demo.flow.mm1;
2   
3   import java.rmi.RemoteException;
4   import java.util.SortedMap;
5   
6   import javax.naming.NamingException;
7   
8   import org.djutils.event.Event;
9   import org.djutils.event.EventListener;
10  import org.djutils.logger.CategoryLogger;
11  import org.djutils.stats.summarizers.Tally;
12  import org.djutils.stats.summarizers.TimestampWeightedTally;
13  import org.pmw.tinylog.Level;
14  
15  import nl.tudelft.simulation.dsol.SimRuntimeException;
16  import nl.tudelft.simulation.dsol.experiment.Experiment;
17  import nl.tudelft.simulation.dsol.experiment.Replication;
18  import nl.tudelft.simulation.dsol.simulators.DevsSimulator;
19  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
20  import nl.tudelft.simulation.dsol.swing.gui.inputparameters.TabbedParameterDialog;
21  
22  /**
23   * <p>
24   * Copyright (c) 2002-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
25   * for project information <a href="https://simulation.tudelft.nl/" target="_blank"> https://simulation.tudelft.nl</a>. The DSOL
26   * project is distributed under a three-clause BSD-style license, which can be found at
27   * <a href="https://https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">
28   * https://https://simulation.tudelft.nl/dsol/docs/latest/license.html</a>.
29   * </p>
30   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
31   */
32  public class MM1ExperimentApplication implements EventListener
33  {
34      /** */
35      private static final long serialVersionUID = 20230114L;
36  
37      /** */
38      private DevsSimulator<Double> simulator;
39  
40      /** */
41      private MM1Model model;
42  
43      /** */
44      private Experiment<Double, SimulatorInterface<Double>> experiment;
45  
46      /**
47       * Construct a console application.
48       * @throws SimRuntimeException on error
49       * @throws RemoteException on error
50       * @throws NamingException on error
51       */
52      protected MM1ExperimentApplication() throws SimRuntimeException, RemoteException, NamingException
53      {
54          this.simulator = new DevsSimulator<Double>("MM1ExperimentApplication");
55          this.model = new MM1Model(this.simulator);
56          new TabbedParameterDialog(this.model.getInputParameterMap());
57          this.experiment = new Experiment<>("mm1", this.simulator, this.model, 0.0, 0.0, 1000.0, 10);
58          this.experiment.addListener(this, Experiment.END_EXPERIMENT_EVENT);
59          this.experiment.addListener(this, Replication.END_REPLICATION_EVENT);
60          this.experiment.start();
61      }
62  
63      /** {@inheritDoc} */
64      @Override
65      public void notify(final Event event) throws RemoteException
66      {
67          if (event.getType().equals(Replication.END_REPLICATION_EVENT))
68          {
69              reportReplicationStatistics();
70          }
71          if (event.getType().equals(Experiment.END_EXPERIMENT_EVENT))
72          {
73              reportFinalStatistics();
74          }
75      }
76  
77      /**
78       * Report statistics at the end of the run.
79       */
80      protected void reportReplicationStatistics()
81      {
82          System.out.println("Statistics replication:");
83          System.out.println(Tally.reportHeader());
84          System.out.println(this.model.dN.reportLine());
85          System.out.println(Tally.reportFooter());
86          System.out.println();
87          System.out.println(TimestampWeightedTally.reportHeader());
88          System.out.println(this.model.qN.reportLine());
89          System.out.println(this.model.uN.reportLine());
90          System.out.println(TimestampWeightedTally.reportFooter());
91          System.out.println();
92      }
93  
94      /**
95       * Report statistics at the end of the run.
96       */
97      protected void reportFinalStatistics()
98      {
99          System.out.println("Final statistics:");
100         SortedMap<String, SortedMap<String, Tally>> stats = this.experiment.getSummaryStatistics();
101         for (String statMapKey : stats.keySet())
102         {
103             System.out.println("\nSummary statistic for: " + statMapKey);
104             System.out.println(Tally.reportHeader());
105             SortedMap<String, Tally> statMap = stats.get(statMapKey);
106             for (String statKey : statMap.keySet())
107             {
108                 Tally stat = statMap.get(statKey);
109                 System.out.println(stat.reportLine());
110             }
111             System.out.println(Tally.reportFooter());
112         }
113 
114         System.exit(0);
115     }
116 
117     /**
118      * @param args String[]; can be left empty
119      * @throws SimRuntimeException on error
120      * @throws RemoteException on error
121      * @throws NamingException on error
122      */
123     public static void main(final String[] args) throws SimRuntimeException, RemoteException, NamingException
124     {
125         CategoryLogger.setAllLogLevel(Level.WARNING);
126         new MM1ExperimentApplication();
127     }
128 
129 }