View Javadoc

1   /*
2    * StockStatisticsjava Created @ Jul 11, 2004
3    * 
4    * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
5    * Delft, the Netherlands. All rights reserved.
6    * 
7    * See for project information <a href="http://www.simulation.tudelft.nl/">
8    * www.simulation.tudelft.nl </a>.
9    * 
10   * The source code and binary code of this software is proprietary information
11   * of Delft University of Technology.
12   */
13  
14  package org.gscg.singleuser.interactionlayer.business.statistics;
15  
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.event.Event;
22  import nl.tudelft.simulation.event.EventInterface;
23  import nl.tudelft.simulation.event.EventListenerInterface;
24  import nl.tudelft.simulation.event.EventProducer;
25  import nl.tudelft.simulation.event.EventType;
26  import nl.tudelft.simulation.event.TimedEvent;
27  import nl.tudelft.simulation.jstats.charts.xy.XYChart;
28  import nl.tudelft.simulation.jstats.charts.xy.XYSeries;
29  import nl.tudelft.simulation.logger.Logger;
30  import nl.tudelft.simulation.supplychain.actor.Trader;
31  import nl.tudelft.simulation.supplychain.product.Product;
32  import nl.tudelft.simulation.supplychain.stock.StockInterface;
33  import nl.tudelft.simulation.supplychain.stock.StockUpdateData;
34  
35  import org.gscg.common.interactionlayer.AnnounceInterface;
36  import org.gscg.singleuser.interactionlayer.SingleUserInteractionLayerInterface;
37  
38  /***
39   * The StockStatistics subscribes to stock changed events fired by the stock of
40   * an actor. During execution of the simulation, all the stock account changed
41   * events are stored. When a player becomes online, its client side charts are
42   * updated. And as long as the player is online, future stock changed events are
43   * stored and immediately sent to the client-side gui.
44   * <p>
45   * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
46   * Delft, the Netherlands. All rights reserved.
47   * 
48   * See for project information <a href="http://www.simulation.tudelft.nl/">
49   * www.simulation.tudelft.nl </a>.
50   * 
51   * The source code and binary code of this software is proprietary information
52   * of Delft University of Technology.
53   * 
54   * @author <a
55   *         href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
56   *         van Houten </a>
57   * @version $Revision: 1.2 $ $Date: 2005/08/09 15:43:42 $
58   * @since 1.0.0
59   */
60  public class StockStatistics extends EventProducer implements
61  		EventListenerInterface, AnnounceInterface
62  {
63  	/*** the serial version uid */
64  	private static final long serialVersionUID = 11L;
65  
66  	/*** the actual amount */
67  	public static final int ACTUAL_AMOUNT = 0;
68  
69  	/*** the ordered amount */
70  	public static final int ORDERED_AMOUNT = 1;
71  
72  	/*** the claimed amount */
73  	public static final int CLAIMED_AMOUNT = 2;
74  
75  	/*** fired in case of bank changed events */
76  	public static final EventType STOCK_CHANGED_EVENT = new EventType(
77  			"STOCK_CHANGED_EVENT");
78  
79  	/*** the XYSeries for the stock */
80  	private XYSeries[][] series = null;
81  
82  	/*** the single user interaction layer */
83  	private SingleUserInteractionLayerInterface owner = null;
84  
85  	/*** maps a product name to a number */
86  	private Map productToNumber = new HashMap();
87  
88  	/***
89  	 * constructs a new StockStatistics
90  	 * 
91  	 * @param owner the owner
92  	 * @param trader the trader
93  	 * @param numberOfDays the number of days in the simulation
94  	 */
95  	public StockStatistics(final SingleUserInteractionLayerInterface owner,
96  			final Trader trader, final int numberOfDays)
97  	{
98  		super();
99  		this.owner = owner;
100 		try
101 		{
102 			trader.getStock().addListener(this,
103 					StockInterface.STOCK_CHANGE_EVENT);
104 
105 			this.addListener(owner, StockStatistics.STOCK_CHANGED_EVENT);
106 			this.owner.addEventType(StockStatistics.STOCK_CHANGED_EVENT);
107 			this.owner.addEventTypeToAnnounceList(
108 					StockStatistics.STOCK_CHANGED_EVENT, this);
109 
110 			int numberProducts = this.owner.getGlobalSupplyChainData()
111 					.getProducts().size();
112 			this.series = new XYSeries[numberProducts][3];
113 
114 			Iterator i = this.owner.getGlobalSupplyChainData().getProducts()
115 					.iterator();
116 
117 			int counter = 0;
118 
119 			while (i.hasNext())
120 			{
121 				Product product = (Product) i.next();
122 				this.series[counter][StockStatistics.ACTUAL_AMOUNT] = new XYSeries(
123 						product.getName() + "#actual", XYChart.XLINEAR_YLINEAR,
124 						numberOfDays);
125 				this.series[counter][StockStatistics.CLAIMED_AMOUNT] = new XYSeries(
126 						product.getName() + "#committed",
127 						XYChart.XLINEAR_YLINEAR, numberOfDays);
128 				this.series[counter][StockStatistics.ORDERED_AMOUNT] = new XYSeries(
129 						product.getName() + "#ordered",
130 						XYChart.XLINEAR_YLINEAR, numberOfDays);
131 
132 				this.productToNumber.put(product.getName(),
133 						new Integer(counter));
134 				counter++;
135 			}
136 		} catch (RemoteException remoteException)
137 		{
138 			Logger.severe(this, "<init>", remoteException);
139 		}
140 	}
141 
142 	/***
143 	 * @see org.gscg.common.interactionlayer.AnnounceInterface#announce(nl.tudelft.simulation.event.EventType,
144 	 *      boolean)
145 	 */
146 	public void announce(final EventType eventType, final boolean announce)
147 	{
148 		try
149 		{
150 			this.owner.notifyAnnounced(new Event(
151 					StockStatistics.STOCK_CHANGED_EVENT, this, this.series));
152 		} catch (RemoteException remoteException)
153 		{
154 			Logger.severe(this, "<init>", remoteException);
155 		}
156 	}
157 
158 	/***
159 	 * @see nl.tudelft.simulation.event.EventListenerInterface#notify(nl.tudelft.simulation.event.EventInterface)
160 	 */
161 	public void notify(final EventInterface event) throws RemoteException
162 	{
163 		if (event.getType().equals(StockInterface.STOCK_CHANGE_EVENT))
164 		{
165 			StockUpdateData data = (StockUpdateData) event.getContent();
166 
167 			// lookup the correct series based on the product name
168 			int number = ((Integer) this.productToNumber.get(data
169 					.getProductName())).intValue();
170 
171 			this.series[number][StockStatistics.ACTUAL_AMOUNT]
172 					.notify(new TimedEvent(StockInterface.STOCK_CHANGE_EVENT,
173 							this, new Double(data.getActualAmount()),
174 							((TimedEvent) event).getTimeStamp()));
175 
176 			this.series[number][StockStatistics.CLAIMED_AMOUNT]
177 					.notify(new TimedEvent(StockInterface.STOCK_CHANGE_EVENT,
178 							this, new Double(data.getClaimedAmount()),
179 							((TimedEvent) event).getTimeStamp()));
180 
181 			this.series[number][StockStatistics.ORDERED_AMOUNT]
182 					.notify(new TimedEvent(StockInterface.STOCK_CHANGE_EVENT,
183 							this, new Double(data.getOrderedAmount()),
184 							((TimedEvent) event).getTimeStamp()));
185 
186 			this.fireEvent(new TimedEvent(StockStatistics.STOCK_CHANGED_EVENT,
187 					this, event.getContent(), ((TimedEvent) event)
188 							.getTimeStamp()));
189 		}
190 	}
191 }