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