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 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
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
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
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
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 }