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 counter extends the counter 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 href="http://www.simulation.tudelft.nl">
30 * 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 Counter extends nl.tudelft.simulation.jstats.statistics.Counter
39 {
40 /*** the simulator to subscribe to and from */
41 private SimulatorInterface simulator = null;
42
43 /*** we stopped the counter */
44 private boolean stopped = false;
45
46 /***
47 * constructs a new Counter
48 *
49 * @param description refers to the description of this counter
50 * @param simulator the simulator
51 * @throws RemoteException on network failure
52 */
53 public Counter(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 Counter
74 *
75 * @param description the description
76 * @param simulator the simulator of this model
77 * @param target the target on which to count
78 * @param field the field which is counted
79 * @throws RemoteException on network failure
80 */
81 public Counter(final String description,
82 final SimulatorInterface simulator,
83 final EventProducerInterface target, final EventType field)
84 throws RemoteException
85 {
86 this(description, simulator);
87 target.addListener(this, field, false);
88 }
89
90 /***
91 * @see nl.tudelft.simulation.event.EventListenerInterface
92 * #notify(nl.tudelft.simulation.event.EventInterface)
93 */
94 public void notify(final EventInterface event)
95 {
96 if (this.stopped)
97 {
98 return;
99 }
100 try
101 {
102 if (event.getSource().equals(this.simulator))
103 {
104 if (event.getType().equals(SimulatorInterface.WARMUP_EVENT))
105 {
106 this.simulator.removeListener(this,
107 SimulatorInterface.WARMUP_EVENT);
108 super.initialize();
109 return;
110 }
111 if (event.getType().equals(
112 SimulatorInterface.END_OF_REPLICATION_EVENT))
113 {
114 this.stopped = true;
115 this.simulator.removeListener(this,
116 SimulatorInterface.END_OF_REPLICATION_EVENT);
117 this.endOfReplication();
118 return;
119 }
120 } else if (this.isInitialized())
121 {
122 super.notify(event);
123 }
124 } catch (RemoteException remoteException)
125 {
126 Logger.warning(this, "notify", remoteException);
127 }
128 }
129
130 /***
131 * endOfReplication is invoked to store the final results
132 */
133 protected void endOfReplication()
134 {
135 try
136 {
137 String[] parts = nl.tudelft.simulation.naming.context.ContextUtil
138 .resolveKey(this).split("/");
139 String key = "";
140 for (int i = 0; i < parts.length; i++)
141 {
142 if (i != parts.length - 2)
143 {
144 key = key + parts[i] + "/";
145 }
146 }
147 key = key.substring(0, key.length() - 1);
148 nl.tudelft.simulation.jstats.statistics.Tally tally = null;
149 try
150 {
151 tally = (nl.tudelft.simulation.jstats.statistics.Tally) new InitialContext()
152 .lookup(key);
153 } catch (NamingException exception)
154 {
155 tally = new nl.tudelft.simulation.jstats.statistics.Tally(
156 this.description);
157 new InitialContext().bind(key, tally);
158 tally.initialize();
159 }
160 tally.notify(new Event(null, this, new Long(this.count)));
161 } catch (Exception exception)
162 {
163 Logger.warning(this, "endOfReplication", exception);
164 }
165 }
166 }