View Javadoc
1   package nl.tudelft.simulation.dsol.tutorial.mm1;
2   
3   import java.rmi.RemoteException;
4   
5   import javax.naming.NamingException;
6   
7   import org.djutils.event.Event;
8   import org.djutils.event.EventListener;
9   import org.djutils.logger.CategoryLogger;
10  import org.pmw.tinylog.Level;
11  
12  import nl.tudelft.simulation.dsol.SimRuntimeException;
13  import nl.tudelft.simulation.dsol.experiment.Replication;
14  import nl.tudelft.simulation.dsol.experiment.SingleReplication;
15  import nl.tudelft.simulation.dsol.logger.SimLogger;
16  import nl.tudelft.simulation.dsol.simulators.DevsSimulator;
17  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
18  import nl.tudelft.simulation.dsol.swing.gui.DsolApplication;
19  import nl.tudelft.simulation.dsol.swing.gui.control.DevsControlPanel;
20  import nl.tudelft.simulation.language.DsolException;
21  
22  /**
23   * M/M/1 Swing application that shows the events that are fired by the Simulator in the Console.
24   * <p>
25   * Copyright (c) 2002-2023 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
26   * for project information <a href="https://simulation.tudelft.nl/" target="_blank"> https://simulation.tudelft.nl</a>. The DSOL
27   * project is distributed under a three-clause BSD-style license, which can be found at
28   * <a href="https://https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">
29   * https://https://simulation.tudelft.nl/dsol/docs/latest/license.html</a>.
30   * </p>
31   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
32   */
33  public class MM1SwingApplicationEvents extends DsolApplication
34  {
35      /** the model. */
36      private MM1Model model;
37  
38      /** logger. */
39      private SimLogger logger;
40  
41      /**
42       * @param panel DsolPanel; the panel
43       * @param model MM1Model; the model
44       * @param devsSimulator DevsSimulatorInterface&lt;Double&gt;; the simulator
45       */
46      public MM1SwingApplicationEvents(final MM1Panel panel, final MM1Model model,
47              final DevsSimulator<Double> devsSimulator)
48      {
49          super(panel, "MM1SwingApplicationEvents");
50          this.model = model;
51          this.logger = devsSimulator.getLogger();
52          try
53          {
54              devsSimulator.scheduleEventAbs(1000.0, this, "terminate", null);
55          }
56          catch (SimRuntimeException exception)
57          {
58              this.logger.always().error(exception, "<init>");
59          }
60      }
61  
62      /** */
63      private static final long serialVersionUID = 1L;
64  
65      /**
66       * @param args String[]; arguments, expected to be empty
67       * @throws SimRuntimeException on error
68       * @throws RemoteException on error
69       * @throws NamingException on error
70       * @throws DsolException on error
71       */
72      public static void main(final String[] args) throws SimRuntimeException, RemoteException, NamingException, DsolException
73      {
74          CategoryLogger.setAllLogLevel(Level.TRACE);
75          DevsSimulator<Double> devsSimulator = new DevsSimulator<Double>("MM1SwingApplicationEvents");
76          MM1Model model = new MM1Model(devsSimulator);
77          new SimulatorEventLogger(devsSimulator);
78          Replication<Double> replication = new SingleReplication<Double>("rep1", 0.0, 0.0, 1000.0);
79          devsSimulator.initialize(model, replication);
80          DevsControlPanel.TimeDouble controlPanel = new DevsControlPanel.TimeDouble(model, devsSimulator);
81          new MM1SwingApplicationEvents(new MM1Panel(controlPanel, model), model, devsSimulator);
82      }
83  
84      /** stop the simulation. */
85      public void terminate()
86      {
87          System.out.println("average queue length = " + this.model.qN.getWeightedSampleMean());
88          System.out.println("average queue wait   = " + this.model.dN.getSampleMean());
89          System.out.println("average utilization  = " + this.model.uN.getWeightedSampleMean());
90      }
91  
92      /**
93       * Class to catch the events from the Simulator to check that they are right.
94       */
95      protected static class SimulatorEventLogger implements EventListener
96      {
97          /** */
98          private static final long serialVersionUID = 1L;
99  
100         /** */
101         private final DevsSimulator<Double> devsSimulator;
102 
103         /**
104          * @param devsSimulator DevsSimulator&lt;Double&gt;; the simulator to provide the events
105          */
106         SimulatorEventLogger(final DevsSimulator<Double> devsSimulator)
107         {
108             this.devsSimulator = devsSimulator;
109             devsSimulator.addListener(this, Replication.START_REPLICATION_EVENT);
110             devsSimulator.addListener(this, Replication.END_REPLICATION_EVENT);
111             devsSimulator.addListener(this, SimulatorInterface.START_EVENT);
112             devsSimulator.addListener(this, SimulatorInterface.STOP_EVENT);
113             devsSimulator.addListener(this, Replication.WARMUP_EVENT);
114             devsSimulator.addListener(this, SimulatorInterface.TIME_CHANGED_EVENT);
115         }
116 
117         /** {@inheritDoc} */
118         @Override
119         public void notify(final Event event) throws RemoteException
120         {
121             this.devsSimulator.getLogger().always().info(event.getType().toString());
122         }
123 
124     }
125 }