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
24
25
26
27
28
29
30
31
32
33 public class MM1SwingApplicationEvents extends DsolApplication
34 {
35
36 private MM1Model model;
37
38
39 private SimLogger logger;
40
41
42
43
44
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
67
68
69
70
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
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
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
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
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 }