1
2
3
4
5
6
7
8
9
10 package nl.tudelft.simulation.dsol.formalisms.flow.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.formalisms.flow.StationInterface;
19 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
20 import nl.tudelft.simulation.event.Event;
21 import nl.tudelft.simulation.event.EventInterface;
22 import nl.tudelft.simulation.jstats.statistics.Persistent;
23 import nl.tudelft.simulation.logger.Logger;
24
25 /***
26 * A Utilization <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 Dec 8, 2003 <br>
35 * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
36 * Jacobs </a>
37 */
38 public class Utilization extends Persistent
39 {
40 /*** initialzed the tally */
41 private boolean initialized = false;
42
43 /*** simulator */
44 private SimulatorInterface simulator = null;
45
46 /***
47 * constructs a new Utilization
48 *
49 * @param description the description of this utilization
50 * @param simulator the simulator
51 * @param target the target
52 * @throws RemoteException on network failure
53 */
54 public Utilization(final String description,
55 final SimulatorInterface simulator, final StationInterface target)
56 throws RemoteException
57 {
58 super(description);
59 this.simulator = simulator;
60 target.addListener(this, StationInterface.RECEIVE_EVENT, false);
61 target.addListener(this, StationInterface.RELEASE_EVENT, false);
62 this.simulator
63 .addListener(this, SimulatorInterface.WARMUP_EVENT, false);
64 this.simulator.addListener(this,
65 SimulatorInterface.END_OF_REPLICATION_EVENT, false);
66 ContextUtil.bindToContext(simulator, this);
67 }
68
69 /***
70 * @see nl.tudelft.simulation.event.EventListenerInterface
71 * #notify(nl.tudelft.simulation.event.EventInterface)
72 */
73 public void notify(final EventInterface event)
74 {
75 try
76 {
77 if (event.getSource().equals(this.simulator))
78 {
79 if (event.getType().equals(SimulatorInterface.WARMUP_EVENT))
80 {
81 this.initialized = true;
82 this.simulator.removeListener(this,
83 SimulatorInterface.WARMUP_EVENT);
84 super.initialize();
85 return;
86 }
87 if (event.getType().equals(
88 SimulatorInterface.END_OF_REPLICATION_EVENT))
89 {
90 this.simulator.removeListener(this,
91 SimulatorInterface.END_OF_REPLICATION_EVENT);
92 this.endOfReplication();
93 return;
94 }
95 } else if (this.initialized)
96 {
97 super.notify(event);
98 }
99 } catch (RemoteException remoteException)
100 {
101 Logger.warning(this, "notify", remoteException);
102 }
103 }
104
105 /***
106 * endOfReplication is invoked to store the final results
107 */
108 protected void endOfReplication()
109 {
110 try
111 {
112 String[] parts = nl.tudelft.simulation.naming.context.ContextUtil
113 .resolveKey(this).split("/");
114 String key = "";
115 for (int i = 0; i < parts.length; i++)
116 {
117 if (i != parts.length - 2)
118 {
119 key = key + parts[i] + "/";
120 }
121 }
122 key = key.substring(0, key.length() - 1);
123 nl.tudelft.simulation.jstats.statistics.Tally tally = null;
124 try
125 {
126 tally = (nl.tudelft.simulation.jstats.statistics.Tally) new InitialContext()
127 .lookup(key);
128 } catch (NamingException exception)
129 {
130 tally = new nl.tudelft.simulation.jstats.statistics.Tally(
131 this.description);
132 new InitialContext().bind(key, tally);
133 tally.initialize();
134 }
135 tally.notify(new Event(null, this, new Double(this.sampleMean)));
136 } catch (Exception exception)
137 {
138 Logger.warning(this, "endOfReplication", exception);
139 }
140 }
141 }