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 cz.cuni.amis.pogamut.base3d.worldview.object.Location;
25  import eis.eis2java.exception.TranslationException;
26  import eis.eis2java.translation.Java2Parameter;
27  import eis.eis2java.translation.Parameter2Java;
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  				
39  		
40  		if (!(p instanceof Function)) {
41  			String message = String.format("A location must be a function, received: %s.", p);
42  			throw new TranslationException(message);
43  		}
44  
45  		Function f = (Function) p;
46  
47  		// Check function name
48  		if (!f.getName().equals(LOCATION_KEYWORD)) {
49  			String message = String.format("A location needs to start with %s, not: %s in %s.", LOCATION_KEYWORD,
50  					f.getName(), f);
51  			throw new TranslationException(message);
52  		}
53  		List<Parameter> parameters = f.getParameters();
54  
55  		// Check number of parameters
56  		if (parameters.size() != 3) {
57  			String message = String.format("Expected exactly 3 parameters when parsing %s", f);
58  			throw new TranslationException(message);
59  		}
60  
61  		// Check type of parameters
62  		for (Parameter parameter : parameters) {
63  			if (!(parameter instanceof Numeral)) {
64  				String message = String.format("All arguments of %s should be numerical.", f);
65  				throw new TranslationException(message);
66  			}
67  		}
68  
69  		// Transform to location.
70  		double[] coord = new double[3];
71  		for (int i = 0; i < coord.length; i++) {
72  			Parameter parameter = parameters.get(i);
73  			coord[i] = ((Numeral) parameter).getValue().doubleValue();
74  		}
75  		return new Location(coord);
76  	}
77  
78  	@Override
79  	public Class<Location> translatesTo() {
80  		return Location.class;
81  	}
82  
83  	@Override
84  	public Parameter[] translate(Location o) throws TranslationException {
85  		return new Parameter[] { new Function(LOCATION_KEYWORD, 
86  				new Numeral(o.x), 
87  				new Numeral(o.y), 
88  				new Numeral(o.z)), };
89  	}
90  
91  	@Override
92  	public Class<? extends Location> translatesFrom() {
93  		return Location.class;
94  	}
95  
96  }