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