1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
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
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
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 }