Mappings.jrag 9.25 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
aspect DefaultMappings {

  private DefaultMappingDefinition Ros2Rag.baseDefaultMappingDefinitionFromBytes(String typeName) {
    DefaultMappingDefinition result = new DefaultMappingDefinition();
    result.setID("_DefaultBytesTo" + Character.toUpperCase(typeName.charAt(0)) + typeName.substring(1) + "Mapping");
    result.setFromType(new JavaArrayMappingDefinitionType(new SimpleJavaTypeUse("byte")));
    result.setFromVariableName("bytes");
    result.setToType(new JavaMappingDefinitionType(new SimpleJavaTypeUse(typeName)));
    return result;
  }

  private DefaultMappingDefinition Ros2Rag.baseDefaultMappingDefinitionToBytes(String typeName) {
    DefaultMappingDefinition result = new DefaultMappingDefinition();
    result.setID("_Default" + Character.toUpperCase(typeName.charAt(0)) + typeName.substring(1) + "ToBytesMapping");
    result.setFromType(new JavaMappingDefinitionType(new SimpleJavaTypeUse(typeName)));
    result.setFromVariableName("input");
    result.setToType(new JavaArrayMappingDefinitionType(new SimpleJavaTypeUse("byte")));
    return result;
  }

  syn nta DefaultMappingDefinition Ros2Rag.defaultBytesToIntMapping() {
    DefaultMappingDefinition result = baseDefaultMappingDefinitionFromBytes("int");
    result.setContent("return java.nio.ByteBuffer.wrap(bytes).getInt();");
    return result;
  }

  syn nta DefaultMappingDefinition Ros2Rag.defaultBytesToShortMapping() {
    DefaultMappingDefinition result = baseDefaultMappingDefinitionFromBytes("short");
    result.setContent("return java.nio.ByteBuffer.wrap(bytes).getShort();");
    return result;
  }

  syn nta DefaultMappingDefinition Ros2Rag.defaultBytesToLongMapping() {
    DefaultMappingDefinition result = baseDefaultMappingDefinitionFromBytes("long");
    result.setContent("return java.nio.ByteBuffer.wrap(bytes).getLong();");
    return result;
  }

  syn nta DefaultMappingDefinition Ros2Rag.defaultBytesToFloatMapping() {
    DefaultMappingDefinition result = baseDefaultMappingDefinitionFromBytes("float");
    result.setContent("return java.nio.ByteBuffer.wrap(bytes).getFloat();");
    return result;
  }

  syn nta DefaultMappingDefinition Ros2Rag.defaultBytesToDoubleMapping() {
    DefaultMappingDefinition result = baseDefaultMappingDefinitionFromBytes("double");
    result.setContent("return java.nio.ByteBuffer.wrap(bytes).getDouble();");
    return result;
  }

  syn nta DefaultMappingDefinition Ros2Rag.defaultBytesToCharMapping() {
    DefaultMappingDefinition result = baseDefaultMappingDefinitionFromBytes("char");
    result.setContent("return java.nio.ByteBuffer.wrap(bytes).getChar();");
    return result;
  }

  syn nta DefaultMappingDefinition Ros2Rag.defaultBytesToStringMapping() {
    DefaultMappingDefinition result = baseDefaultMappingDefinitionFromBytes("String");
    result.setContent("return new String(bytes);");
    return result;
  }

  syn nta DefaultMappingDefinition Ros2Rag.defaultIntToBytesMapping() {
    DefaultMappingDefinition result = baseDefaultMappingDefinitionToBytes("int");
    result.setContent("return java.nio.ByteBuffer.allocate(4).putInt(input).array();");
    return result;
  }

  syn nta DefaultMappingDefinition Ros2Rag.defaultShortToBytesMapping() {
    DefaultMappingDefinition result = baseDefaultMappingDefinitionToBytes("short");
    result.setContent("return java.nio.ByteBuffer.allocate(2).putShort(input).array();");
    return result;
  }

  syn nta DefaultMappingDefinition Ros2Rag.defaultLongToBytesMapping() {
    DefaultMappingDefinition result = baseDefaultMappingDefinitionToBytes("long");
    result.setContent("return java.nio.ByteBuffer.allocate(8).putLong(input).array();");
    return result;
  }

  syn nta DefaultMappingDefinition Ros2Rag.defaultFloatToBytesMapping() {
    DefaultMappingDefinition result = baseDefaultMappingDefinitionToBytes("float");
    result.setContent("return java.nio.ByteBuffer.allocate(4).putFloat(input).array();");
    return result;
  }

  syn nta DefaultMappingDefinition Ros2Rag.defaultDoubleToBytesMapping() {
    DefaultMappingDefinition result = baseDefaultMappingDefinitionToBytes("double");
    result.setContent("return java.nio.ByteBuffer.allocate(8).putDouble(input).array();");
    return result;
  }

  syn nta DefaultMappingDefinition Ros2Rag.defaultCharToBytesMapping() {
    DefaultMappingDefinition result = baseDefaultMappingDefinitionToBytes("char");
    result.setContent("return java.nio.ByteBuffer.allocate(2).putChar(input).array();");
    return result;
  }

  syn nta DefaultMappingDefinition Ros2Rag.defaultStringToBytesMapping() {
    DefaultMappingDefinition result = baseDefaultMappingDefinitionToBytes("String");
    result.setContent("return input.getBytes();");
    return result;
  }
}

aspect Mappings {
  // --- effectiveMappings ---
  syn java.util.List<MappingDefinition> UpdateDefinition.effectiveMappings();
  eq ReadFromMqttDefinition.effectiveMappings() {
    // if there is a first mapping, check if its input type is byte[].
    //  or if no mappings are specified.
    // then prepend the suitable default mapping
    java.util.List<MappingDefinition> result;
    if (getMappingList().size() == 0 || !getMappingList().get(0).getFromType().isByteArray()) {
      result = new java.util.ArrayList();
      result.add(suitableDefaultMapping());
      result.addAll(getMappingList());
    } else {
      result = getMappingList();
    }
    return result;
  }
  eq WriteToMqttDefinition.effectiveMappings() {
    // if there is a mapping, check if the output type of the last mapping is byte[].
    //  or if no mappings are specified.
    // then append the suitable default mapping
    java.util.List<MappingDefinition> result;
    int numMappings = getMappingList().size();
    if (numMappings == 0 || !getMappingList().get(numMappings - 1).getToType().isByteArray()) {
      result = new java.util.ArrayList();
      result.addAll(getMappingList());
      result.add(suitableDefaultMapping());
    } else {
      result = getMappingList();
    }
    return result;
  }

René Schöne's avatar
René Schöne committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
  // --- isPrimitiveType ---
  syn boolean TokenComponent.isPrimitiveType() = getJavaTypeUse().isPrimitiveType();
  syn boolean JavaTypeUse.isPrimitiveType() = false;
  eq SimpleJavaTypeUse.isPrimitiveType() {
    switch(getName()) {
      case "int":
      case "short":
      case "long":
      case "float":
      case "double":
      case "char":
      case "byte": return true;
      default: return false;
    }
  }
René Schöne's avatar
René Schöne committed
154
155
156
157
158
159
  syn boolean MappingDefinitionType.isPrimitiveType() = false;
  eq JavaMappingDefinitionType.isPrimitiveType() = getType().isPrimitiveType();

  // --- isArray ---
  syn boolean MappingDefinitionType.isArray() = false;
  eq JavaArrayMappingDefinitionType.isArray() = true;
René Schöne's avatar
René Schöne committed
160

161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
  // --- suitableDefaultMapping ---
  syn DefaultMappingDefinition UpdateDefinition.suitableDefaultMapping();
  eq ReadFromMqttDefinition.suitableDefaultMapping() {
    String typeName = getToken().getJavaTypeUse().getName();
    switch(typeName) {
      case "int":
      case "Integer": return ros2rag().defaultBytesToIntMapping();
      case "short":
      case "Short": return ros2rag().defaultBytesToShortMapping();
      case "long":
      case "Long": return ros2rag().defaultBytesToLongMapping();
      case "float":
      case "Float": return ros2rag().defaultBytesToFloatMapping();
      case "double":
      case "Double": return ros2rag().defaultBytesToDoubleMapping();
      case "char":
      case "Character": return ros2rag().defaultBytesToCharMapping();
      case "String": return ros2rag().defaultBytesToStringMapping();
      default: return null;
    }
  }
  eq WriteToMqttDefinition.suitableDefaultMapping() {
    String typeName = getToken().getJavaTypeUse().getName();
    switch(typeName) {
      case "int":
      case "Integer": return ros2rag().defaultIntToBytesMapping();
      case "short":
      case "Short": return ros2rag().defaultShortToBytesMapping();
      case "long":
      case "Long": return ros2rag().defaultLongToBytesMapping();
      case "float":
      case "Float": return ros2rag().defaultFloatToBytesMapping();
      case "double":
      case "Double": return ros2rag().defaultDoubleToBytesMapping();
      case "char":
      case "Character": return ros2rag().defaultCharToBytesMapping();
      case "String": return ros2rag().defaultStringToBytesMapping();
      default: return null;
    }
  }

  // --- isByteArray ---
  syn boolean MappingDefinitionType.isByteArray() = false;
  eq JavaArrayMappingDefinitionType.isByteArray() = getType().getName().equals("byte");

  // --- allMappingDefinitions ---
  syn java.util.List<MappingDefinition> Ros2Rag.allMappingDefinitions() {
    java.util.List<MappingDefinition> result = new java.util.ArrayList<>();
    getMappingDefinitionList().iterator().forEachRemaining(result::add);
    result.add(defaultBytesToIntMapping());
    result.add(defaultBytesToShortMapping());
    result.add(defaultBytesToLongMapping());
    result.add(defaultBytesToFloatMapping());
    result.add(defaultBytesToDoubleMapping());
    result.add(defaultBytesToCharMapping());
    result.add(defaultBytesToStringMapping());
    result.add(defaultIntToBytesMapping());
    result.add(defaultShortToBytesMapping());
    result.add(defaultLongToBytesMapping());
    result.add(defaultFloatToBytesMapping());
    result.add(defaultDoubleToBytesMapping());
    result.add(defaultCharToBytesMapping());
    result.add(defaultStringToBytesMapping());
    return result;
  }
}