View Javadoc

1   /*
2    * @(#)GameSupplier.java Apr 25, 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.gameactors;
15  
16  import java.net.URL;
17  
18  import javax.media.j3d.Bounds;
19  import javax.vecmath.Point2d;
20  import javax.vecmath.Point3d;
21  
22  import nl.tudelft.simulation.actor.messagehandlers.HandleAllMessages;
23  import nl.tudelft.simulation.actor.messagehandlers.MessageHandlerInterface;
24  import nl.tudelft.simulation.dsol.experiment.TimeUnit;
25  import nl.tudelft.simulation.dsol.experiment.TimeUnitInterface;
26  import nl.tudelft.simulation.dsol.simulators.AnimatorInterface;
27  import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
28  import nl.tudelft.simulation.dsol.statistics.charts.XYChart;
29  import nl.tudelft.simulation.jstats.distributions.DistConstant;
30  import nl.tudelft.simulation.jstats.distributions.DistContinuous;
31  import nl.tudelft.simulation.jstats.streams.StreamInterface;
32  import nl.tudelft.simulation.language.d3.BoundingBox;
33  import nl.tudelft.simulation.language.d3.DirectedPoint;
34  import nl.tudelft.simulation.language.io.URLResource;
35  import nl.tudelft.simulation.logger.Logger;
36  import nl.tudelft.simulation.messaging.devices.reference.FaxDevice;
37  import nl.tudelft.simulation.supplychain.actor.SupplyChainActor;
38  import nl.tudelft.simulation.supplychain.banking.Bank;
39  import nl.tudelft.simulation.supplychain.banking.BankAccount;
40  import nl.tudelft.simulation.supplychain.content.Content;
41  import nl.tudelft.simulation.supplychain.content.LeanContentStore;
42  import nl.tudelft.simulation.supplychain.content.Order;
43  import nl.tudelft.simulation.supplychain.product.Product;
44  import nl.tudelft.simulation.supplychain.reference.Supplier;
45  import nl.tudelft.simulation.supplychain.roles.Role;
46  import nl.tudelft.simulation.supplychain.stock.Stock;
47  
48  import org.gscg.common.GameActorInterface;
49  import org.gscg.common.geo.CalculateLatLonDistance;
50  import org.gscg.experiment.HandlerParser;
51  import org.gscg.gameleader.animation2D.GisActorAnimation;
52  import org.jdom.Element;
53  import org.jdom.input.SAXBuilder;
54  
55  /***
56   * The GameSupplier extends a Supplier from the supplychain project and provides
57   * additional game-specific functionalities.
58   * <p>
59   * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
60   * Delft, the Netherlands. All rights reserved.
61   * 
62   * See for project information <a href="http://www.simulation.tudelft.nl/">
63   * www.simulation.tudelft.nl </a>.
64   * 
65   * The source code and binary code of this software is proprietary information
66   * of Delft University of Technology.
67   * 
68   * @author <a
69   *         href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">Alexander
70   *         Verbraeck </a>
71   * @version $Revision: 1.2 $ $Date: 2005/08/10 11:23:30 $
72   * @since 1.0.0 <br>
73   */
74  
75  public class GameSupplier extends Supplier implements GameActorInterface
76  {
77  	/*** builder the xerces parser with validation turned on */
78  	private static SAXBuilder builder = new SAXBuilder(
79  			"org.apache.xerces.parsers.SAXParser", true);
80  
81  	static
82  	{
83  		// turns on Schema Validation with Xerces
84  		GameSupplier.builder.setFeature(
85  				"http://xml.org/sax/features/validation", true);
86  		GameSupplier.builder.setFeature(
87  				"http://apache.org/xml/features/validation/schema", true);
88  
89  		// Let's find the XSD file
90  		String xsd = URLResource.getResource("/handler.xsd").toExternalForm();
91  		GameSupplier.builder
92  				.setProperty(
93  						"http://apache.org/xml/properties/schema/external-schemaLocation",
94  						"http://www.simulation.tudelft.nl/dsol " + xsd);
95  	}
96  
97  	/*** the serial version uid */
98  	private static final long serialVersionUID = 12L;
99  
100 	/*** for debugging */
101 	private static final boolean DEBUG = true;
102 
103 	/*** the location on the small map */
104 	private DirectedPoint smallMapLocation = new DirectedPoint();
105 
106 	/*** the animation used for serialization */
107 	private GisActorAnimation animation = null;
108 
109 	/***
110 	 * constructs a new GameSupplier; used when dragging and dropping an
111 	 * actor
112 	 * 
113 	 * @param name the name
114 	 * @param simulator the simulator
115 	 * @param position the position
116 	 * @param bank the bank
117 	 */
118 	public GameSupplier(final String name,
119 			final DEVSSimulatorInterface simulator, final Point3d position,
120 			final Bank bank)
121 	{
122 		super(name, simulator, position, new Role[]{}, bank);
123 		this.initAnimation();
124 	}
125 
126 	/***
127 	 * constructs a new GameSupplier
128 	 * 
129 	 * @param name the name
130 	 * @param simulator the simulator
131 	 * @param position the position on the map
132 	 * @param roles the roles to implement
133 	 * @param bank the bank
134 	 * @param product the product
135 	 * @param amount the amount
136 	 */
137 	public GameSupplier(final String name,
138 			final DEVSSimulatorInterface simulator, final Point3d position,
139 			final Role[] roles, final Bank bank, final Product[] product,
140 			final Double[] amount)
141 	{
142 		this(name, simulator, position, roles, bank, 0.0, product, amount);
143 	}
144 
145 	/***
146 	 * constructs a new GameSupplier
147 	 * 
148 	 * @param name the name
149 	 * @param simulator the simulator
150 	 * @param position the position on the map
151 	 * @param roles the roles to implement
152 	 * @param bank the bank
153 	 * @param initialBankAccount the initial bank account
154 	 * @param product the product
155 	 * @param amount the amount
156 	 */
157 	public GameSupplier(final String name,
158 			final DEVSSimulatorInterface simulator, final Point3d position,
159 			final Role[] roles, final Bank bank,
160 			final double initialBankAccount, final Product[] product,
161 			final Double[] amount)
162 	{
163 		super(name, simulator, position, roles, bank, initialBankAccount);
164 		super.setContentStore(new LeanContentStore(this, simulator));
165 		// give the retailer some stock
166 		Stock stock = new Stock(this);
167 		for (int i = 0; i < product.length; i++)
168 		{
169 			stock.addStock(product[i], amount[i].doubleValue(), amount[i]
170 					.doubleValue()
171 					* product[i].getUnitMarketPrice());
172 		}
173 		super.setInitialStock(stock);
174 
175 		// We initialize GameSupplier
176 		this.init();
177 
178 		// may be initialized earlier during modeling of supply chain game
179 		if (this.animation == null)
180 		{
181 			this.initAnimation();
182 		}
183 	}
184 
185 	/***
186 	 * Initializes the animation.
187 	 */
188 	private void initAnimation()
189 	{
190 		// Let's give the suplier its corresponding image
191 		this.animation = new GisActorAnimation(
192 				this,
193 				this.simulator,
194 				GameSupplier.class
195 						.getResource("/nl/tudelft/simulation/supplychain/images/Supplier.gif"));
196 	}
197 
198 	/***
199 	 * Method init
200 	 */
201 	private void init()
202 	{
203 		try
204 		{
205 
206 			StreamInterface stream = this.simulator.getReplication().getStream(
207 					"default");
208 			double hour = TimeUnit.convert(1.0, TimeUnitInterface.HOUR,
209 					this.simulator);
210 			DistContinuous hourDist = new DistConstant(stream, hour);
211 			//
212 			// give the actor a fax device which is checked every hour
213 			FaxDevice fax = new FaxDevice("DellFax", this.simulator);
214 			super.addSendingDevice(fax);
215 			MessageHandlerInterface secretary = new HandleAllMessages(this);
216 			super.addReceivingDevice(fax, secretary, hourDist);
217 
218 			// look for our personal xml file with handler description
219 			URL url = URLResource.getResource("/" + this.name
220 					+ "_computer_controlled.xml");
221 			if (url == null)
222 			{
223 				url = URLResource.getResource("/supplier_default_handler.xml");
224 				if (GameSupplier.DEBUG)
225 				{
226 					System.out.println("DEBUG -- GameSupplier: " + this.name
227 							+ " is using the default handler.");
228 				}
229 			}
230 			Logger
231 					.info(
232 							this,
233 							"init",
234 							"Using: "
235 									+ url.toExternalForm()
236 									+ " as the file containing the configuration for the handlers.");
237 			// let's parse the xml file data to handlers
238 			Element rootElement = GameDistributor.builder.build(url)
239 					.getRootElement();
240 			HandlerParser.parseAndAddHandler(this, rootElement);
241 
242 			//
243 			// CHARTS
244 			//
245 			if (this.simulator instanceof AnimatorInterface)
246 			{
247 				XYChart bankChart = new XYChart(this.simulator, "BankAccount "
248 						+ this.name);
249 				bankChart.add("bank account", this.bankAccount,
250 						BankAccount.BANK_ACCOUNT_CHANGED_EVENT);
251 			}
252 		} catch (Exception exception)
253 		{
254 			Logger.severe(this, "init", exception);
255 		}
256 	}
257 
258 	/***
259 	 * @see nl.tudelft.simulation.supplychain.actor.SupplyChainActor#sendContent(nl.tudelft.simulation.supplychain.content.Content,
260 	 *      double)
261 	 */
262 	public void sendContent(final Content content,
263 			final double administrativeDelay)
264 	{
265 		// we snoop the orders for statististical analysis
266 		if (content instanceof Order)
267 		{
268 			// let's send to the statistics
269 		}
270 		super.sendContent(content, administrativeDelay);
271 	}
272 
273 	/***
274 	 * @see nl.tudelft.simulation.dsol.animation.LocatableInterface#getBounds()
275 	 */
276 	public Bounds getBounds()
277 	{
278 		return new BoundingBox(1.0, 1.0, 0.0);
279 	}
280 
281 	/***
282 	 * @return Returns the smallMapLocation.
283 	 */
284 	public DirectedPoint getSmallMapLocation()
285 	{
286 		return this.smallMapLocation;
287 	}
288 
289 	/***
290 	 * @param smallMapLocation The smallMapLocation to set.
291 	 */
292 	public void setSmallMapLocation(final DirectedPoint smallMapLocation)
293 	{
294 		this.smallMapLocation = smallMapLocation;
295 	}
296 
297 	/***
298 	 * @see nl.tudelft.simulation.actor.ActorInterface#getName()
299 	 */
300 	public String getName()
301 	{
302 		return this.name;
303 	}
304 
305 	/***
306 	 * @return Returns the animation
307 	 */
308 	protected GisActorAnimation getAnimation()
309 	{
310 		return this.animation;
311 	}
312 
313 	/***
314 	 * @see nl.tudelft.simulation.supplychain.actor.SupplyChainActor#calculateDistance(nl.tudelft.simulation.supplychain.actor.SupplyChainActor)
315 	 */
316 	public double calculateDistance(final SupplyChainActor actor)
317 	{
318 		return new CalculateLatLonDistance().getDistance(new Point2d(
319 				this.location.x, this.location.y), new Point2d(actor
320 				.getLocation().x, actor.getLocation().y));
321 	}
322 }