1
2
3
4
5
6
7
8
9
10
11
12
13 package org.gscg.experiment;
14
15 import java.io.IOException;
16 import java.net.URL;
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import javax.vecmath.Point3d;
21
22 import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
23 import nl.tudelft.simulation.logger.Logger;
24 import nl.tudelft.simulation.supplychain.banking.FixedCost;
25 import nl.tudelft.simulation.supplychain.product.Product;
26 import nl.tudelft.simulation.supplychain.roles.Role;
27
28 import org.gscg.common.gui.images.LatLonToSmallMap;
29 import org.gscg.game.Game;
30 import org.gscg.game.GameGlobalData;
31 import org.gscg.gameactors.GameManufacturer;
32 import org.gscg.gameactors.GameManufacturerInteractive;
33 import org.gscg.gameactors.GameSupplier;
34 import org.jdom.Element;
35 import org.jdom.Namespace;
36 import org.jdom.input.SAXBuilder;
37
38 /***
39 * The ManufacturerParser parses xml-based experiments into their java objects.
40 * <p>
41 *
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.5 $ $Date: 2005/08/10 11:23:30 $
55 * @since 1.0.0
56 */
57 public class ManufacturerParser
58 {
59 /*** builder the xerces parser with validation turned on */
60 private static SAXBuilder builder = new SAXBuilder(
61 "org.apache.xerces.parsers.SAXParser", false);
62 static
63 {
64
65 builder.setFeature("http://xml.org/sax/features/validation", true);
66 builder.setFeature("http://apache.org/xml/features/validation/schema",
67 true);
68 }
69
70 /*** for debugging */
71 private transient static final boolean DEBUG = false;
72
73 /***
74 * constructs a new ManufacturerParser This is a Utility Class.
75 */
76 protected ManufacturerParser()
77 {
78
79 }
80
81 /***
82 * parses a product xml-file.
83 *
84 * @param schema the schema for validation of the xml-file
85 * @param input the inputstream
86 * @param supplyChainData the global supply chain data
87 * @param simulator the simulator
88 * @return Manufacturers the manufacturers
89 * @throws IOException whenever parsing fails
90 */
91 public static Manufacturers parseManufacturers(final String schema,
92 final URL input, final GameGlobalData supplyChainData,
93 final DEVSSimulatorInterface simulator) throws IOException
94 {
95 builder
96 .setProperty(
97 "http://apache.org/xml/properties/schema/external-schemaLocation",
98 "http://www.simulation.tudelft.nl " + schema);
99 try
100 {
101 Element rootElement = builder.build(input).getRootElement();
102 Manufacturers manufacturers = new Manufacturers();
103
104 List elements = rootElement.getChildren();
105
106 for (int i = 0; i < elements.size(); i++)
107 {
108 GameManufacturer manufacturer = ManufacturerParser
109 .parseManufacturer((Element) elements.get(i),
110 supplyChainData, simulator);
111 if (!Manufacturers.addManufacturer(manufacturer))
112 {
113 Logger
114 .severe(
115 ManufacturerParser.class,
116 "parseManufacturers",
117 "ManufacturerParser was unable to add manufacturer with name: "
118 + manufacturer.getName()
119 + ". Probably there is already a manufacturer with the same name. Check the .xml description.");
120 }
121 }
122 if (ManufacturerParser.DEBUG)
123 {
124 System.out
125 .println("DEBUG -- ManufacturerParser: Number of Manufacturers: "
126 + Manufacturers.getManufacturersArray().length);
127 }
128 return manufacturers;
129 } catch (Exception exception)
130 {
131 exception.printStackTrace();
132 Logger.severe(ManufacturerParser.class, "parseManufacturer",
133 exception);
134 throw new IOException(exception.getMessage());
135 }
136 }
137
138
139
140 /***
141 * parses the manufacturer element
142 *
143 * @param element the xml-element *
144 * @param supplyChainData the global supply chain data
145 * @param simulator the simulator to use
146 * @return GameManufacturer
147 * @throws Exception on failure
148 */
149 private static GameManufacturer parseManufacturer(final Element element,
150 final GameGlobalData supplyChainData,
151 final DEVSSimulatorInterface simulator) throws Exception
152 {
153 String name = null;
154 String nickName = null;
155 double lat = Double.NaN;
156 double lon = Double.NaN;
157 double initialBalance = Double.NaN;
158 double fixedCosts = Double.NaN;
159 Product[] products = null;
160 Double[] amounts = null;
161 GameSupplier[] suppliers = null;
162
163 Namespace space = element.getNamespace();
164 if (element.getChild("name", space) != null)
165 {
166 name = (element.getChildText("name", space));
167 }
168 if (ManufacturerParser.DEBUG)
169 {
170 System.out.println("DEBUG -- ManufacturerParser: Parsing manufacturer: "
171 + name);
172 }
173 if (element.getChild("nickname", space) != null)
174 {
175 nickName = (element.getChildText("nickname", space));
176 }
177 if (element.getChild("lat", space) != null)
178 {
179 lat = new Double(element.getChildText("lat", space)).doubleValue();
180 }
181 if (element.getChild("lon", space) != null)
182 {
183 lon = new Double(element.getChildText("lon", space)).doubleValue();
184 }
185 if (element.getChild("initial_balance", space) != null)
186 {
187 initialBalance = new Double(element.getChildText("initial_balance",
188 space)).doubleValue();
189 }
190 if (element.getChild("fixed_costs_per_game_time_unit", space) != null)
191 {
192 fixedCosts = new Double(element.getChildText(
193 "fixed_costs_per_game_time_unit", space)).doubleValue();
194 }
195 if (element.getChild("products", space) != null)
196 {
197 products = ManufacturerParser.parseProduct(element.getChild(
198 "products", space));
199 amounts = ManufacturerParser.parseAmount(element.getChild(
200 "products", space));
201 }
202 if (element.getChild("suppliers", space) != null)
203 {
204 suppliers = ManufacturerParser.parseSupplier(element.getChild(
205 "suppliers", space));
206 }
207
208
209 GameManufacturerInteractive manufacturer = new GameManufacturerInteractive(
210 supplyChainData, name, simulator, new Point3d(lon, lat, 0.0),
211 new Role[]{}, Game.getBank(), initialBalance, products,
212 amounts, suppliers);
213 manufacturer.setSmallMapLocation(LatLonToSmallMap.getSmallMapLocation(
214 lon, lat, 238, 176));
215 manufacturer.setLocationDescription(nickName);
216
217 new FixedCost(manufacturer, manufacturer.getBankAccount(),
218 "fixed cost", 1, fixedCosts);
219 return manufacturer;
220 }
221
222 /***
223 * Parses an element representing a list of products
224 *
225 * @param element the element to parse
226 * @return returns an array with products
227 * @throws Exception thrown in case of an unknown name
228 */
229 private static Product[] parseProduct(final Element element)
230 throws Exception
231 {
232 List products = element.getChildren();
233 List result = new ArrayList();
234 for (int i = 0; i < products.size(); i++)
235 {
236 Element product = (Element) products.get(i);
237 Namespace space = element.getNamespace();
238 String name = null;
239 if (product.getChild("product", space) != null)
240 {
241 name = (product.getChildText("product", space));
242 }
243 result.add(Products.getProduct(name));
244 }
245 return (Product[]) result.toArray(new Product[result.size()]);
246 }
247
248 /***
249 * Parses an element representing a list of products
250 *
251 * @param element the element to parse
252 * @return returns an array with products
253 * @throws Exception thrown in case of an unknown name
254 */
255 private static Double[] parseAmount(final Element element) throws Exception
256 {
257 List products = element.getChildren();
258 List result = new ArrayList();
259 for (int i = 0; i < products.size(); i++)
260 {
261 Element product = (Element) products.get(i);
262 Namespace space = element.getNamespace();
263 double amount = Double.NaN;
264 if (product.getChild("initial_amount", space) != null)
265 {
266 amount = new Double(product.getChildText("initial_amount",
267 space)).doubleValue();
268 }
269 result.add(new Double(amount));
270 }
271 return (Double[]) result.toArray(new Double[result.size()]);
272 }
273
274 /***
275 * Parses a element representing a list of suppliers
276 *
277 * @param element the element to parse
278 * @return returns an array with suppliers
279 * @throws Exception thrown in case of an unknown name
280 */
281 private static GameSupplier[] parseSupplier(final Element element)
282 throws Exception
283 {
284 List suppliers = element.getChildren();
285 List result = new ArrayList();
286 for (int i = 0; i < suppliers.size(); i++)
287 {
288 Element supplier = (Element) suppliers.get(i);
289 Namespace space = element.getNamespace();
290 String name = null;
291 if (supplier.getChild("supplier", space) != null)
292 {
293 name = (supplier.getChildText("supplier", space));
294 }
295 Object actor = Suppliers.getSupplier(name);
296 if (actor != null)
297 {
298 result.add(actor);
299 } else
300 {
301 System.err
302 .println("ManufacturerParser: parseSupplier: no supplier found with name: "
303 + name);
304 }
305 }
306 return (GameSupplier[]) result.toArray(new GameSupplier[result.size()]);
307 }
308 }