View Javadoc

1   /**
2    * BaseUnrealEnvironment, an implementation of the environment interface standard that 
3    * facilitates the connection between GOAL and the UT2004 engine. 
4    * 
5    * Copyright (C) 2012 BaseUnrealEnvironment authors.
6    * 
7    * This program is free software: you can redistribute it and/or modify it under
8    * the terms of the GNU General Public License as published by the Free Software
9    * Foundation, either version 3 of the License, or (at your option) any later
10   * version.
11   * 
12   * This program is distributed in the hope that it will be useful, but WITHOUT
13   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14   * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15   * details.
16   * 
17   * You should have received a copy of the GNU General Public License along with
18   * this program. If not, see <http://www.gnu.org/licenses/>.
19   */
20  package nl.tudelft.goal.unreal.translators;
21  
22  import java.util.List;
23  
24  import nl.tudelft.goal.EIS2Java.exception.TranslationException;
25  import nl.tudelft.goal.EIS2Java.translation.Java2Parameter;
26  import nl.tudelft.goal.EIS2Java.translation.Parameter2Java;
27  import cz.cuni.amis.pogamut.base3d.worldview.object.Location;
28  import eis.iilang.Function;
29  import eis.iilang.Numeral;
30  import eis.iilang.Parameter;
31  
32  public class LocationTranslator implements Parameter2Java<Location>, Java2Parameter<Location> {
33  
34  	public static String LOCATION_KEYWORD = "location";
35  
36  	@Override
37  	public Location translate(Parameter p) throws TranslationException {
38  		if (!(p instanceof Function)) {
39  			String message = String.format("A location must be a function, received: %s.", p);
40  			throw new TranslationException(message);
41  		}
42  
43  		Function f = (Function) p;
44  
45  		// Check function name
46  		if (!f.getName().equals(LOCATION_KEYWORD)) {
47  			String message = String.format("A location needs to start with %s, not: %s in %s.", LOCATION_KEYWORD,
48  					f.getName(), f);
49  			throw new TranslationException(message);
50  		}
51  		List<Parameter> parameters = f.getParameters();
52  
53  		// Check number of parameters
54  		if (parameters.size() != 3) {
55  			String message = String.format("Expected exactly 3 parameters when parsing %s", f);
56  			throw new TranslationException(message);
57  		}
58  
59  		// Check type of parameters
60  		for (Parameter parameter : parameters) {
61  			if (!(parameter instanceof Numeral)) {
62  				String message = String.format("All arguments of %s should be numerical.", f);
63  				throw new TranslationException(message);
64  			}
65  		}
66  
67  		// Transform to location.
68  		double[] coord = new double[3];
69  		for (int i = 0; i < coord.length; i++) {
70  			Parameter parameter = parameters.get(i);
71  			coord[i] = ((Numeral) parameter).getValue().doubleValue();
72  		}
73  		return new Location(coord);
74  	}
75  
76  	@Override
77  	public Class<Location> translatesTo() {
78  		return Location.class;
79  	}
80  
81  	@Override
82  	public Parameter[] translate(Location o) throws TranslationException {
83  		return new Parameter[] { new Function(LOCATION_KEYWORD, 
84  				new Numeral(o.x), 
85  				new Numeral(o.y), 
86  				new Numeral(o.z)), };
87  	}
88  
89  	@Override
90  	public Class<? extends Location> translatesFrom() {
91  		return Location.class;
92  	}
93  
94  }