View Javadoc

1   /*
2    * DemandStatistics.java 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.gameleader.interactionlayer.statistics;
15  
16  import java.io.Serializable;
17  import java.rmi.RemoteException;
18  import java.util.HashMap;
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.XYSeries;
28  import nl.tudelft.simulation.logger.Logger;
29  import nl.tudelft.simulation.supplychain.content.InternalDemand;
30  import nl.tudelft.simulation.supplychain.roles.DemandGenerationRole;
31  
32  import org.gscg.common.gui.statistics.data.DemandStatisticsChangedData;
33  import org.gscg.common.interactionlayer.AnnounceInterface;
34  import org.gscg.common.interactionlayer.timecontrol.GlobalRowOrColumnNumber;
35  import org.gscg.gameactors.GameMarket;
36  import org.gscg.gameleader.interactionlayer.GameLeaderInteractionLayer;
37  
38  /***
39   * The DemandStatistics subscribes to demand changed events fired by the an
40   * actor. During execution of the simulation, all the bank account changed
41   * events are stored. When a player becomes online, its client side chart is
42   * updated. And as long as the player is online, future bank account changed
43   * events are 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/03 08:52:51 $
58   * @since 1.0.0 <br>
59   * 
60   */
61  public class DemandStatistics extends EventProducer implements
62  		EventListenerInterface, AnnounceInterface, Serializable
63  {
64  	/*** the serial version uid */
65  	private static final long serialVersionUID = 11L;
66  
67  	/*** the XYSeries for the demand statistics */
68  	private Map series = new HashMap();
69  
70  	/*** the game leader interaction layer */
71  	private GameLeaderInteractionLayer owner = null;
72  
73  	/*** the owner */
74  	private GameMarket pcMarket = null;
75  
76  	/*** the event type to use */
77  	private EventType eventType = null;
78  
79  	/***
80  	 * constructs a new DemandStatistics
81  	 * 
82  	 * @param owner the owner
83  	 * @param pcMarket the pc market
84  	 */
85  	public DemandStatistics(final GameLeaderInteractionLayer owner,
86  			final GameMarket pcMarket)
87  	{
88  		super();
89  		this.owner = owner;
90  		this.pcMarket = pcMarket;
91  		this.eventType = new EventType("CUSTOMER_DEMAND_"
92  				+ this.pcMarket.getName());
93  		this.pcMarket.getDemandGenerationRole().addListener(this,
94  				DemandGenerationRole.DEMAND_GENERATED_EVENT);
95  		this.addListener(owner, this.eventType);
96  		this.owner.addStatisticEventType(this.eventType);
97  		this.owner.addEventTypeToAnnounceList(this.eventType, this);
98  	}
99  
100 	/***
101 	 * @see org.gscg.common.interactionlayer.AnnounceInterface#announce(nl.tudelft.simulation.event.EventType,
102 	 *      boolean)
103 	 */
104 	public void announce(final EventType eventType, final boolean announce)
105 	{
106 		try
107 		{
108 			Map map = new HashMap();
109 			map.putAll(this.series);
110 			this.owner.notify(new Event(this.eventType, this, map));
111 		} catch (RemoteException remoteException)
112 		{
113 			Logger.severe(this, "announce", remoteException);
114 		}
115 	}
116 
117 	/***
118 	 * @see nl.tudelft.simulation.event.EventListenerInterface#notify(nl.tudelft.simulation.event.EventInterface)
119 	 */
120 	public void notify(final EventInterface event) throws RemoteException
121 	{
122 		if (event.getType().equals(DemandGenerationRole.DEMAND_GENERATED_EVENT))
123 		{
124 			InternalDemand demand = (InternalDemand) event.getContent();
125 
126 			if (this.series.get(demand.getProduct().getName()) == null)
127 			{
128 				XYSeries serie = new XYSeries(
129 						"Demand for " + demand.getProduct().getName() + " of "
130 								+ this.pcMarket.getName(),
131 						nl.tudelft.simulation.jstats.charts.xy.XYChart.XLINEAR_YLINEAR,
132 						GlobalRowOrColumnNumber.getNumberOfDays());
133 				this.series.put(demand.getProduct().getName(), serie);
134 			}
135 			TimedEvent timedEvent = new TimedEvent(this.eventType, null,
136 					new Double(demand.getAmount()), ((TimedEvent) event)
137 							.getTimeStamp());
138 			((XYSeries) this.series.get(demand.getProduct().getName()))
139 					.notify(timedEvent);
140 			super.fireEvent(new Event(this.eventType, this,
141 					new DemandStatisticsChangedData(demand.getProduct()
142 							.getName(), timedEvent)));
143 		}
144 	}
145 }