1
2
3
4
5
6
7
8
9
10
11
12
13 package org.gscg.singleuser.interactionlayer.business.statistics;
14
15 import java.io.Serializable;
16 import java.rmi.RemoteException;
17 import java.util.HashMap;
18 import java.util.Iterator;
19 import java.util.Map;
20
21 import nl.tudelft.simulation.content.HandlerInterface;
22 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
23 import nl.tudelft.simulation.event.Event;
24 import nl.tudelft.simulation.event.EventInterface;
25 import nl.tudelft.simulation.event.EventProducer;
26 import nl.tudelft.simulation.event.EventProducerInterface;
27 import nl.tudelft.simulation.event.EventType;
28 import nl.tudelft.simulation.logger.Logger;
29 import nl.tudelft.simulation.supplychain.content.OrderConfirmation;
30
31 import org.gscg.common.gui.exceptions.ReceivedUnknownEventException;
32 import org.gscg.common.interactionlayer.AnnounceInterface;
33 import org.gscg.common.interactionlayer.dataobjects.GamingPersistentData;
34 import org.gscg.singleuser.interactionlayer.SingleUserInteractionLayerInterface;
35
36 /***
37 * The CustomerStatistics subscribes to messages between a supplier and a
38 * customer. It collects the messages sent between these actors and based on
39 * their content statistics are generated. One might think of for example the
40 * average amount of an order per customer per product.
41 * <p>
42 * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
43 * Delft, the Netherlands. All rights reserved.
44 *
45 * See for project information <a href="http://www.simulation.tudelft.nl/">
46 * www.simulation.tudelft.nl </a>.
47 *
48 * The source code and binary code of this software is proprietary information
49 * of Delft University of Technology.
50 *
51 * @author <a
52 * href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
53 * van Houten </a>
54 * @version $Revision: 1.2 $ $Date: 2005/08/09 15:43:42 $
55 * @since 1.0.0
56 */
57 public class CustomerStatistics extends EventProducer implements
58 HandlerInterface, AnnounceInterface
59 {
60 /*** the serial version uid */
61 private static final long serialVersionUID = 11L;
62
63 /*** fired when an order commitment has been sent */
64 public static final EventType ORDERCOMMIT_SENT = new EventType(
65 "ORDERCOMMIT_SENT");
66
67 /*** fired to update a customer client-side statistic */
68 public static final EventType CUSTOMER_STATISTICS_NEW_PERSISTENT = new EventType(
69 "CUSTOMER_STATISTICS_NEW_PERSISTENT");
70
71 /*** fired in case of announce event when a gui is initialized */
72 public static final EventType CUSTOMER_STATISTICS_SENT_GUI_UPDATE = new EventType(
73 "CUSTOMER_STATISTICS_SENT_GUI_UPDATE");
74
75 /*** the simulator to use */
76 private SimulatorInterface simulator = null;
77
78 /*** the single user interaction layer */
79 private SingleUserInteractionLayerInterface singleUserInteractionLayer = null;
80
81 /*** the clientPersistentEventTypes */
82 private Map clientPersistentEventTypes = new HashMap();
83
84 /*** the persitent event types */
85 private Map perstentEventTypes = new HashMap();
86
87 /*** the list with persistents */
88 private Map persistents = new HashMap();
89
90 /***
91 * constructs a new CustomerStatistics
92 *
93 * @param simulator the simulator
94 * @param singleUserInteractionLayer the single user interaction layer
95 */
96 public CustomerStatistics(final SimulatorInterface simulator,
97 final SingleUserInteractionLayerInterface singleUserInteractionLayer)
98 {
99 this.simulator = simulator;
100 this.singleUserInteractionLayer = singleUserInteractionLayer;
101 this.addListener(singleUserInteractionLayer,
102 CustomerStatistics.CUSTOMER_STATISTICS_NEW_PERSISTENT);
103 try
104 {
105 this.singleUserInteractionLayer
106 .addEventType(CustomerStatistics.CUSTOMER_STATISTICS_NEW_PERSISTENT);
107 this.singleUserInteractionLayer.addEventTypeToAnnounceList(
108 CustomerStatistics.CUSTOMER_STATISTICS_SENT_GUI_UPDATE,
109 this);
110 } catch (RemoteException remoteException)
111 {
112 Logger.severe(this, "<init>", remoteException);
113 }
114 }
115
116 /***
117 * @see nl.tudelft.simulation.content.HandlerInterface#handleContent(java.io.Serializable)
118 */
119 public boolean handleContent(final Serializable content)
120 {
121 if (content instanceof OrderConfirmation)
122 {
123 OrderConfirmation data = (OrderConfirmation) content;
124 try
125 {
126 String persistentEventName = data.getReceiver().getName()
127 + " / " + data.getProduct().getName()
128 + " / committed orders";
129
130 String clientEventName = "customer: " + persistentEventName;
131
132 EventType persistentEventType = null;
133
134 if (!this.clientPersistentEventTypes
135 .containsKey(clientEventName))
136 {
137 persistentEventType = new EventType(persistentEventName);
138
139 this.perstentEventTypes.put(persistentEventType.toString(),
140 persistentEventType);
141
142 this.createPersistent("size of orders to "
143 + data.getReceiver().getName() + " ("
144 + data.getProduct().getName() + ")",
145 persistentEventType);
146
147 this.fireEvent(persistentEventType,
148 ((OrderConfirmation) content).getOrder()
149 .getAmount(), this.simulator
150 .getSimulatorTime());
151
152 EventType clientPersistentEventType = (EventType) this.clientPersistentEventTypes
153 .get(clientEventName);
154
155
156 this.singleUserInteractionLayer.addEventTypeToAnnounceList(
157 clientPersistentEventType, this);
158 this.singleUserInteractionLayer
159 .addEventType(clientPersistentEventType);
160 this.addListener(this.singleUserInteractionLayer,
161 clientPersistentEventType);
162
163
164 this
165 .fireEvent(new Event(
166 CustomerStatistics.CUSTOMER_STATISTICS_NEW_PERSISTENT,
167 this, clientPersistentEventType));
168 }
169
170
171 persistentEventType = (EventType) this.perstentEventTypes
172 .get(persistentEventName);
173
174 this.fireEvent(persistentEventType,
175 ((OrderConfirmation) content).getOrder().getAmount(),
176 this.simulator.getSimulatorTime());
177
178 } catch (RemoteException remoteException)
179 {
180 Logger.severe(this, "handleContent", remoteException);
181 }
182 }
183 return false;
184 }
185
186 /***
187 * @see nl.tudelft.simulation.event.EventProducer#fireEvent(nl.tudelft.simulation.event.EventInterface)
188 */
189 public synchronized EventInterface fireEvent(final EventInterface event)
190 {
191 return super.fireEvent(event);
192 }
193
194 /***
195 * Method createPersistent creates a new persistent
196 *
197 * @param name the name of the persistent
198 * @param persistentEventType the event for the persistent to listen to
199 */
200 private void createPersistent(final String name,
201 final EventType persistentEventType)
202 {
203 CustomPersistent persistent = null;
204 try
205 {
206 persistent = new CustomPersistent(name, this.simulator, this,
207 persistentEventType);
208 } catch (RemoteException remoteException)
209 {
210 Logger.severe(this, "createPersistent", remoteException);
211 }
212 persistent.initialize();
213
214
215 EventType clientPersistentEventType = new EventType("customer: "
216 + persistentEventType.toString());
217
218
219 this.persistents.put(clientPersistentEventType.toString(), persistent);
220
221
222 this.clientPersistentEventTypes.put(clientPersistentEventType
223 .toString(), clientPersistentEventType);
224 }
225
226
227 /***
228 * @see org.gscg.common.interactionlayer.AnnounceInterface#announce(nl.tudelft.simulation.event.EventType,
229 * boolean)
230 */
231 public void announce(final EventType eventType, final boolean announce)
232 {
233 if (this.clientPersistentEventTypes.containsKey(eventType.toString()))
234 {
235 CustomPersistent persistent = (CustomPersistent) this.persistents
236 .get(eventType.toString());
237
238 EventType clientPersisentEventType = (EventType) this.clientPersistentEventTypes
239 .get(eventType.toString());
240
241 this
242 .fireEvent(new Event(clientPersisentEventType, this,
243 new GamingPersistentData(persistent.getMin(),
244 persistent.getMax(), persistent.getN(),
245 persistent.getSampleMean(), persistent
246 .getSum(), persistent
247 .getSampleVariance(), persistent
248 .getDescription())));
249 return;
250 }
251 if (eventType
252 .equals(CustomerStatistics.CUSTOMER_STATISTICS_SENT_GUI_UPDATE))
253 {
254 for (Iterator it = this.clientPersistentEventTypes.keySet()
255 .iterator(); it.hasNext();)
256 {
257 EventType key = (EventType) this.clientPersistentEventTypes
258 .get(it.next());
259 this.fireEvent(new Event(
260 CustomerStatistics.CUSTOMER_STATISTICS_NEW_PERSISTENT,
261 this, key));
262 }
263 return;
264 }
265 new ReceivedUnknownEventException(this, "announce", eventType);
266 }
267
268 /***
269 * @return returns the client persistent event types
270 */
271 public Map getClientPersistentEventTypes()
272 {
273 return this.clientPersistentEventTypes;
274 }
275
276 /***
277 * The CustomerPersistent fires an event to update the client as soon as it
278 * is updated itself.
279 * <p>
280 * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628
281 * BX Delft, the Netherlands. All rights reserved.
282 *
283 * See for project information <a href="http://www.simulation.tudelft.nl/">
284 * www.simulation.tudelft.nl </a>.
285 *
286 * The source code and binary code of this software is proprietary
287 * information of Delft University of Technology.
288 *
289 * @author <a
290 * href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
291 * van Houten </a>
292 * @version $Revision: 1.2 $ $Date: 2005/08/09 15:43:42 $
293 * @since 1.0.0
294 */
295 private class CustomPersistent extends
296 nl.tudelft.simulation.dsol.statistics.Persistent
297 {
298 /*** the serial version uid */
299 private static final long serialVersionUID = 11L;
300
301 /*** the owner */
302 private EventProducerInterface owner = null;
303
304 /***
305 * constructs a new CustomPersistent
306 *
307 * @param name the name
308 * @param simulator the simulator
309 * @param eventProducer the event producer
310 * @param eventType the event type
311 * @throws RemoteException thrown in case of a network error
312 */
313 public CustomPersistent(final String name,
314 final SimulatorInterface simulator,
315 final EventProducerInterface eventProducer,
316 final EventType eventType) throws RemoteException
317 {
318 super(name, simulator, eventProducer, eventType);
319 this.owner = eventProducer;
320 }
321
322 /***
323 * @see nl.tudelft.simulation.event.EventListenerInterface#notify(nl.tudelft.simulation.event.EventInterface)
324 */
325 public void notify(final EventInterface event)
326 {
327 super.notify(event);
328
329 EventType clientPersistentEventType = (EventType) ((CustomerStatistics) this.owner)
330 .getClientPersistentEventTypes().get(
331 "customer: " + event.getType().toString());
332
333 ((CustomerStatistics) this.owner).fireEvent(new Event(
334 clientPersistentEventType, this,
335 new GamingPersistentData(this.getMin(), this.getMax(), this
336 .getN(), this.getSampleMean(), this.getSum(), this
337 .getSampleVariance(), this.getDescription())));
338 }
339 }
340 }