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