1
2
3
4
5
6
7
8
9
10
11
12
13 package org.gscg.gameactors.statistics;
14
15 import java.io.Serializable;
16 import java.rmi.RemoteException;
17 import java.util.ArrayList;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Set;
24
25 import nl.tudelft.simulation.event.Event;
26 import nl.tudelft.simulation.event.EventInterface;
27 import nl.tudelft.simulation.event.EventProducer;
28 import nl.tudelft.simulation.event.EventType;
29 import nl.tudelft.simulation.event.TimedEvent;
30 import nl.tudelft.simulation.jstats.charts.xy.XYChart;
31 import nl.tudelft.simulation.jstats.charts.xy.XYSeries;
32 import nl.tudelft.simulation.supplychain.content.Order;
33 import nl.tudelft.simulation.supplychain.content.OrderBasedOnQuote;
34 import nl.tudelft.simulation.supplychain.content.OrderStandAlone;
35
36 import org.gscg.common.gui.exceptions.ReceivedUnknownEventException;
37 import org.gscg.common.gui.statistics.data.DemandStatisticsChangedData;
38 import org.gscg.common.interactionlayer.timecontrol.GlobalRowOrColumnNumber;
39
40 /***
41 * The StatisticOrderAverageUnitPriceHandler is used to collect statistics with
42 * regards to ordersPerProduct.
43 * <p>
44 * (c) copyright 2005 <a href="http://www.simulation.tudelft.nl">Delft
45 * University of Technology </a>, the Netherlands. <br>
46 * See for project information <a
47 * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
48 *
49 * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
50 * Delft, the Netherlands. All rights reserved.
51 *
52 * See for project information <a href="http://www.simulation.tudelft.nl/">
53 * www.simulation.tudelft.nl </a>.
54 *
55 * The source code and binary code of this software are proprietary information
56 * of Delft University of Technology.
57 *
58 * @author <a
59 * href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
60 * van Houten </a>
61 * @version $Revision: 1.1 $ $Date: 2005/08/03 08:52:49 $
62 * @since 1.1.3
63 */
64 public class StatisticOrderAverageUnitPriceHandler extends EventProducer
65 implements StatisticContentHandlerInterface
66 {
67 /*** the serial version uid */
68 private static final long serialVersionUID = 11L;
69
70 /*** debugging */
71 private static final boolean DEBUG = false;
72
73 /*** the event type to use */
74 private EventType eventType = null;
75
76 /*** the prefix to use */
77 private String prefix = null;
78
79 /*** the owner */
80 private RegionInterface owner = null;
81
82 /*** the map for the products and their XY Series per product */
83 private Map xySeriesPerProduct = null;
84
85 /*** the map with the last average order values per product */
86 private Map lastValuesPerProduct = null;
87
88 /*** the map with ordersPerProduct based on product name */
89 private Map ordersPerProduct = null;
90
91 /***
92 * constructs a new StatisticOrderAverageUnitPriceHandler
93 *
94 * @param owner the owner of the handler
95 * @param prefix the prefix
96 */
97 public StatisticOrderAverageUnitPriceHandler(final RegionInterface owner,
98 final String prefix)
99 {
100 super();
101 this.owner = owner;
102 this.prefix = prefix;
103 this.eventType = new EventType(this.prefix
104 + this.owner.getName().toUpperCase());
105
106 this.ordersPerProduct = new HashMap();
107 this.xySeriesPerProduct = new HashMap();
108 this.lastValuesPerProduct = new HashMap();
109
110
111 GlobalRowOrColumnNumber.getGlobalRowOrColumnNumber()
112 .getCustomEventProducer().addListener(this,
113 GlobalRowOrColumnNumber.UPDATE_CURRENT_WEEK);
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 OrderBasedOnQuote
122 || content instanceof OrderStandAlone)
123 {
124 Order order = (Order) content;
125 if (!this.xySeriesPerProduct.containsKey(order.getProduct()
126 .getName()))
127 {
128
129 this.xySeriesPerProduct.put(order.getProduct().getName(),
130 new XYSeries(order.getProduct().getName(),
131 XYChart.XLINEAR_YLINEAR,
132 GlobalRowOrColumnNumber.getNumberOfDays()));
133
134
135 this.ordersPerProduct.put(order.getProduct().getName(),
136 new ArrayList());
137
138
139 this.lastValuesPerProduct.put(order.getProduct().getName(),
140 new Double(0.0));
141 }
142
143
144 List list = (List) this.ordersPerProduct.get(order.getProduct()
145 .getName());
146 list.add(order);
147 }
148 return true;
149 }
150
151 /***
152 * @see nl.tudelft.simulation.event.EventListenerInterface#notify(nl.tudelft.simulation.event.EventInterface)
153 */
154 public void notify(final EventInterface event) throws RemoteException
155 {
156 if (event.getType().equals(GlobalRowOrColumnNumber.UPDATE_CURRENT_WEEK))
157 {
158
159
160 for (Iterator it = this.ordersPerProduct.keySet().iterator(); it
161 .hasNext();)
162 {
163 Object key = it.next();
164 List orders = (List) this.ordersPerProduct.get(key);
165
166 TimedEvent timedEvent = null;
167 if (orders.size() > 0)
168 {
169 double unitPrices = 0.0;
170 for (int i = 0; i < orders.size(); i++)
171 {
172 unitPrices += ((Order) orders.get(i)).getPrice()
173 / ((Order) orders.get(i)).getAmount();
174 }
175
176 double averageMarketPrice = unitPrices / orders.size();
177
178 if (StatisticOrderAverageUnitPriceHandler.DEBUG)
179 {
180 System.out
181 .println("StatisticOrderAverageUnitPriceHandler: region: "
182 + this.owner.getName()
183 + " average market price for product "
184 + key + ": " + averageMarketPrice);
185 }
186 timedEvent = new TimedEvent(this.eventType, this,
187 new Double(averageMarketPrice),
188 GlobalRowOrColumnNumber.getCurrentNumberDay());
189
190
191 this.lastValuesPerProduct.put(key, new Double(
192 averageMarketPrice));
193
194
195 orders.clear();
196 } else
197 {
198
199 timedEvent = new TimedEvent(this.eventType, this,
200 this.lastValuesPerProduct.get(key),
201 GlobalRowOrColumnNumber.getCurrentNumberDay());
202 }
203
204
205 XYSeries serie = (XYSeries) this.xySeriesPerProduct.get(key);
206 serie.notify(timedEvent);
207
208
209 super.fireEvent(new Event(this.eventType, this,
210 new DemandStatisticsChangedData(key.toString(),
211 timedEvent)));
212
213 }
214 return;
215 }
216 new ReceivedUnknownEventException(this, "notify", event.getType());
217 }
218
219 /***
220 * @see org.gscg.gameactors.statistics.StatisticContentHandlerInterface#getStatisticEventTypes()
221 */
222 public EventType[] getStatisticEventTypes()
223 {
224 return new EventType[]{this.eventType};
225 }
226
227 /***
228 * @see org.gscg.gameactors.statistics.StatisticContentHandlerInterface#getSeries()
229 */
230 public Set getSeries()
231 {
232 return new HashSet(this.xySeriesPerProduct.values());
233 }
234
235 /***
236 * @see org.gscg.gameactors.statistics.StatisticContentHandlerInterface#getRegion()
237 */
238 public RegionInterface getRegion()
239 {
240 return this.owner;
241 }
242 }