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.Velocity;
25 import cz.cuni.amis.pogamut.ut2004.utils.UnrealUtils;
26 import eis.iilang.Function;
27 import eis.iilang.Numeral;
28 import eis.iilang.Parameter;
29 import nl.tudelft.goal.EIS2Java.exception.TranslationException;
30 import nl.tudelft.goal.EIS2Java.translation.Java2Parameter;
31 import nl.tudelft.goal.EIS2Java.translation.Parameter2Java;
32
33 public class VelocityTranslator implements Java2Parameter<Velocity>, Parameter2Java<Velocity> {
34
35 public static final String VELOCITY_KEYWORD = "velocity";
36
37 @Override
38 public Parameter[] translate(Velocity o) throws TranslationException {
39 return new Parameter[] { new Function(VELOCITY_KEYWORD,
40 new Numeral(UnrealUtils.unrealDegreeToDegree((int) o.getX())),
41 new Numeral(UnrealUtils.unrealDegreeToDegree((int) o.getY())),
42 new Numeral(UnrealUtils.unrealDegreeToDegree((int) o.getZ())) )
43 };
44 }
45
46 @Override
47 public Class<? extends Velocity> translatesFrom() {
48 return Velocity.class;
49 }
50
51 @Override
52 public Velocity translate(Parameter p) throws TranslationException {
53
54 if (!(p instanceof Function)) {
55 String message = String.format("A velocity must be a function, received: %s.", p);
56 throw new TranslationException(message);
57 }
58
59 Function f = (Function) p;
60
61
62 if (!f.getName().equals(VELOCITY_KEYWORD)) {
63 String message = String.format("A velocity needs to start with %s, not: %s in %s.", VELOCITY_KEYWORD,
64 f.getName(), f);
65 throw new TranslationException(message);
66 }
67 List<Parameter> parameters = f.getParameters();
68
69
70 if (parameters.size() != 3) {
71 String message = String.format("Expected exactly 3 parameters when parsing %s", f);
72 throw new TranslationException(message);
73 }
74
75
76 for (Parameter parameter : parameters) {
77 if (!(parameter instanceof Numeral)) {
78 String message = String.format("All arguments of %s should be numerical.", f);
79 throw new TranslationException(message);
80 }
81 }
82
83
84 double[] c = new double[3];
85 for (int i = 0; i < c.length; i++) {
86 Parameter parameter = parameters.get(i);
87 c[i] = ((Numeral) parameter).getValue().doubleValue();
88 }
89 return new Velocity(c[0], c[1], c[2]);
90
91 }
92
93 @Override
94 public Class<Velocity> translatesTo() {
95 return Velocity.class;
96
97 }
98
99 }