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 | 0 | 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 | 0 | 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 | 0 | return Velocity.class; |
49 | |
} |
50 | |
|
51 | |
@Override |
52 | |
public Velocity translate(Parameter p) throws TranslationException { |
53 | |
|
54 | 0 | if (!(p instanceof Function)) { |
55 | 0 | String message = String.format("A velocity must be a function, received: %s.", p); |
56 | 0 | throw new TranslationException(message); |
57 | |
} |
58 | |
|
59 | 0 | Function f = (Function) p; |
60 | |
|
61 | |
|
62 | 0 | if (!f.getName().equals(VELOCITY_KEYWORD)) { |
63 | 0 | String message = String.format("A velocity needs to start with %s, not: %s in %s.", VELOCITY_KEYWORD, |
64 | |
f.getName(), f); |
65 | 0 | throw new TranslationException(message); |
66 | |
} |
67 | 0 | List<Parameter> parameters = f.getParameters(); |
68 | |
|
69 | |
|
70 | 0 | if (parameters.size() != 3) { |
71 | 0 | String message = String.format("Expected exactly 3 parameters when parsing %s", f); |
72 | 0 | throw new TranslationException(message); |
73 | |
} |
74 | |
|
75 | |
|
76 | 0 | for (Parameter parameter : parameters) { |
77 | 0 | if (!(parameter instanceof Numeral)) { |
78 | 0 | String message = String.format("All arguments of %s should be numerical.", f); |
79 | 0 | throw new TranslationException(message); |
80 | |
} |
81 | |
} |
82 | |
|
83 | |
|
84 | 0 | double[] c = new double[3]; |
85 | 0 | for (int i = 0; i < c.length; i++) { |
86 | 0 | Parameter parameter = parameters.get(i); |
87 | 0 | c[i] = ((Numeral) parameter).getValue().doubleValue(); |
88 | |
} |
89 | 0 | return new Velocity(c[0], c[1], c[2]); |
90 | |
|
91 | |
} |
92 | |
|
93 | |
@Override |
94 | |
public Class<Velocity> translatesTo() { |
95 | 0 | return Velocity.class; |
96 | |
|
97 | |
} |
98 | |
|
99 | |
} |