1
2
3
4
5
6
7
8
9
10 package nl.tudelft.simulation.dsol.statistics;
11
12 import java.rmi.RemoteException;
13
14 import javax.naming.InitialContext;
15 import javax.naming.NamingException;
16
17 import nl.tudelft.simulation.dsol.context.ContextUtil;
18 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
19 import nl.tudelft.simulation.event.Event;
20 import nl.tudelft.simulation.event.EventInterface;
21 import nl.tudelft.simulation.event.EventProducerInterface;
22 import nl.tudelft.simulation.event.EventType;
23 import nl.tudelft.simulation.logger.Logger;
24
25 /***
26 * The Tally extends the tally and links this it to the dsol framework <br>
27 * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
28 * University of Technology </a>, the Netherlands. <br>
29 * See for project information <a
30 * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
31 * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
32 * License (GPL) </a>, no warranty <br>
33 *
34 * @version 1.0 18.08.2003 <br>
35 * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
36 * Jacobs </a>
37 */
38 public class Tally extends nl.tudelft.simulation.jstats.statistics.Tally
39 {
40 /*** the simulator */
41 private SimulatorInterface simulator = null;
42
43 /*** after the END_OF_REPLICATION we stop */
44 private boolean stopped = false;
45
46 /***
47 * constructs a new Tally.
48 *
49 * @param description refers to the description of this Tally.
50 * @param simulator the simulator to schedule on.
51 * @throws RemoteException on network failure.
52 */
53 public Tally(final String description, final SimulatorInterface simulator)
54 throws RemoteException
55 {
56 super(description);
57 this.simulator = simulator;
58 if (this.simulator.getSimulatorTime() > this.simulator.getReplication()
59 .getRunControl().getWarmupPeriod())
60 {
61 this.initialize();
62 } else
63 {
64 this.simulator.addListener(this, SimulatorInterface.WARMUP_EVENT,
65 false);
66 }
67 this.simulator.addListener(this,
68 SimulatorInterface.END_OF_REPLICATION_EVENT, false);
69 ContextUtil.bindToContext(simulator, this);
70 }
71
72 /***
73 * constructs a new Tally
74 *
75 * @param description the description of this tally.
76 * @param simulator the simulator to schedule on.
77 * @param target the target on which to subscribe.
78 * @param field the field which is counted
79 * @throws RemoteException on network failure.
80 */
81 public Tally(final String description, final SimulatorInterface simulator,
82 final EventProducerInterface target, final EventType field)
83 throws RemoteException
84 {
85 this(description, simulator);
86 target.addListener(this, field, false);
87 }
88
89 /***
90 * @see nl.tudelft.simulation.event.EventListenerInterface
91 * #notify(nl.tudelft.simulation.event.EventInterface)
92 */
93 public void notify(final EventInterface event)
94 {
95 if(this.stopped)
96 {
97 return;
98 }
99 try
100 {
101 if (event.getSource().equals(this.simulator))
102 {
103 if (event.getType().equals(SimulatorInterface.WARMUP_EVENT))
104 {
105 this.simulator.removeListener(this,
106 SimulatorInterface.WARMUP_EVENT);
107 super.initialize();
108 return;
109 }
110 if (event.getType().equals(
111 SimulatorInterface.END_OF_REPLICATION_EVENT))
112 {
113 this.stopped = true;
114 this.simulator.removeListener(this,
115 SimulatorInterface.END_OF_REPLICATION_EVENT);
116 this.endOfReplication();
117 return;
118 }
119 } else if (this.isInitialized())
120 {
121 super.notify(event);
122 }
123 } catch (RemoteException remoteException)
124 {
125 Logger.warning(this, "notify", remoteException);
126 }
127 }
128
129 /***
130 * endOfReplication is invoked to store the final results
131 */
132 protected void endOfReplication()
133 {
134 try
135 {
136 String[] parts = nl.tudelft.simulation.naming.context.ContextUtil
137 .resolveKey(this).split("/");
138 String key = "";
139 for (int i = 0; i < parts.length; i++)
140 {
141 if (i != parts.length - 2)
142 {
143 key = key + parts[i] + "/";
144 }
145 }
146 key = key.substring(0, key.length() - 1);
147 nl.tudelft.simulation.jstats.statistics.Tally tally = null;
148 try
149 {
150 tally = (nl.tudelft.simulation.jstats.statistics.Tally) new InitialContext()
151 .lookup(key);
152 } catch (NamingException exception)
153 {
154 tally = new nl.tudelft.simulation.jstats.statistics.Tally(
155 this.description);
156 new InitialContext().bind(key, tally);
157 tally.initialize();
158 }
159 tally.notify(new Event(null, this, new Double(this.sampleMean)));
160 } catch (Exception exception)
161 {
162 Logger.warning(this, "endOfReplication", exception);
163 }
164 }
165 }