Mappings.jrag 15.6 KB
Newer Older
1
2
aspect DefaultMappings {

3
  private DefaultMappingDefinition RagConnect.baseDefaultMappingDefinitionFromBytes(String typeName) {
4
5
6
7
8
9
10
11
    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;
  }

12
  private DefaultMappingDefinition RagConnect.baseDefaultMappingDefinitionToBytes(String typeName) {
13
14
15
16
17
18
19
20
    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;
  }

René Schöne's avatar
René Schöne committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  private String RagConnect.baseDefaultMappingTypeNamePart(String typeName) {
    return Character.toUpperCase(typeName.charAt(0)) + typeName.substring(1).replace("[]", "s");
  }

  private MappingDefinitionType RagConnect.baseDefaultMappingTypeFromName(String typeName) {
    return typeName.endsWith("[]") ?
        new JavaArrayMappingDefinitionType(new SimpleJavaTypeUse(typeName.replace("[]", ""))) :
        new JavaMappingDefinitionType(new SimpleJavaTypeUse(typeName));
  }

  private DefaultMappingDefinition RagConnect.baseDefaultMappingDefinition(String fromTypeName, String toTypeName, String content) {
    DefaultMappingDefinition result = new DefaultMappingDefinition();
    result.setID("_Default" + baseDefaultMappingTypeNamePart(fromTypeName) + "To" + baseDefaultMappingTypeNamePart(toTypeName) + "Mapping");
    result.setFromType(baseDefaultMappingTypeFromName(fromTypeName));
    result.setFromVariableName("input");
    result.setToType(baseDefaultMappingTypeFromName(toTypeName));
    result.setContent(content);
    return result;
  }

41
  syn nta DefaultMappingDefinition RagConnect.defaultBytesToIntMapping() {
42
43
44
45
46
    DefaultMappingDefinition result = baseDefaultMappingDefinitionFromBytes("int");
    result.setContent("return java.nio.ByteBuffer.wrap(bytes).getInt();");
    return result;
  }

47
  syn nta DefaultMappingDefinition RagConnect.defaultBytesToShortMapping() {
48
49
50
51
52
    DefaultMappingDefinition result = baseDefaultMappingDefinitionFromBytes("short");
    result.setContent("return java.nio.ByteBuffer.wrap(bytes).getShort();");
    return result;
  }

53
  syn nta DefaultMappingDefinition RagConnect.defaultBytesToLongMapping() {
54
55
56
57
58
    DefaultMappingDefinition result = baseDefaultMappingDefinitionFromBytes("long");
    result.setContent("return java.nio.ByteBuffer.wrap(bytes).getLong();");
    return result;
  }

59
  syn nta DefaultMappingDefinition RagConnect.defaultBytesToFloatMapping() {
60
61
62
63
64
    DefaultMappingDefinition result = baseDefaultMappingDefinitionFromBytes("float");
    result.setContent("return java.nio.ByteBuffer.wrap(bytes).getFloat();");
    return result;
  }

65
  syn nta DefaultMappingDefinition RagConnect.defaultBytesToDoubleMapping() {
66
67
68
69
70
    DefaultMappingDefinition result = baseDefaultMappingDefinitionFromBytes("double");
    result.setContent("return java.nio.ByteBuffer.wrap(bytes).getDouble();");
    return result;
  }

71
  syn nta DefaultMappingDefinition RagConnect.defaultBytesToCharMapping() {
72
73
74
75
76
    DefaultMappingDefinition result = baseDefaultMappingDefinitionFromBytes("char");
    result.setContent("return java.nio.ByteBuffer.wrap(bytes).getChar();");
    return result;
  }

77
  syn nta DefaultMappingDefinition RagConnect.defaultBytesToStringMapping() {
78
79
80
81
82
    DefaultMappingDefinition result = baseDefaultMappingDefinitionFromBytes("String");
    result.setContent("return new String(bytes);");
    return result;
  }

83
  syn nta DefaultMappingDefinition RagConnect.defaultIntToBytesMapping() {
84
85
86
87
88
    DefaultMappingDefinition result = baseDefaultMappingDefinitionToBytes("int");
    result.setContent("return java.nio.ByteBuffer.allocate(4).putInt(input).array();");
    return result;
  }

89
  syn nta DefaultMappingDefinition RagConnect.defaultShortToBytesMapping() {
90
91
92
93
94
    DefaultMappingDefinition result = baseDefaultMappingDefinitionToBytes("short");
    result.setContent("return java.nio.ByteBuffer.allocate(2).putShort(input).array();");
    return result;
  }

95
  syn nta DefaultMappingDefinition RagConnect.defaultLongToBytesMapping() {
96
97
98
99
100
    DefaultMappingDefinition result = baseDefaultMappingDefinitionToBytes("long");
    result.setContent("return java.nio.ByteBuffer.allocate(8).putLong(input).array();");
    return result;
  }

101
  syn nta DefaultMappingDefinition RagConnect.defaultFloatToBytesMapping() {
102
103
104
105
106
    DefaultMappingDefinition result = baseDefaultMappingDefinitionToBytes("float");
    result.setContent("return java.nio.ByteBuffer.allocate(4).putFloat(input).array();");
    return result;
  }

107
  syn nta DefaultMappingDefinition RagConnect.defaultDoubleToBytesMapping() {
108
109
110
111
112
    DefaultMappingDefinition result = baseDefaultMappingDefinitionToBytes("double");
    result.setContent("return java.nio.ByteBuffer.allocate(8).putDouble(input).array();");
    return result;
  }

113
  syn nta DefaultMappingDefinition RagConnect.defaultCharToBytesMapping() {
114
115
116
117
118
    DefaultMappingDefinition result = baseDefaultMappingDefinitionToBytes("char");
    result.setContent("return java.nio.ByteBuffer.allocate(2).putChar(input).array();");
    return result;
  }

119
  syn nta DefaultMappingDefinition RagConnect.defaultStringToBytesMapping() {
120
121
122
123
    DefaultMappingDefinition result = baseDefaultMappingDefinitionToBytes("String");
    result.setContent("return input.getBytes();");
    return result;
  }
René Schöne's avatar
René Schöne committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137

  syn nta DefaultMappingDefinition RagConnect.defaultStringToIntMapping() = baseDefaultMappingDefinition("String", "int", "return Integer.parseInteger(input);");
  syn nta DefaultMappingDefinition RagConnect.defaultStringToShortMapping() = baseDefaultMappingDefinition("String", "short", "return Short.parseShort(input);");
  syn nta DefaultMappingDefinition RagConnect.defaultStringToLongMapping() = baseDefaultMappingDefinition("String", "long", "return Long.parseLong(input);");
  syn nta DefaultMappingDefinition RagConnect.defaultStringToFloatMapping() = baseDefaultMappingDefinition("String", "float", "return Float.parseFloat(input);");
  syn nta DefaultMappingDefinition RagConnect.defaultStringToDoubleMapping() = baseDefaultMappingDefinition("String", "double", "return Double.parseDouble(input);");
  syn nta DefaultMappingDefinition RagConnect.defaultStringToCharMapping() = baseDefaultMappingDefinition("String", "char", "return input.charAt(0);");

  syn nta DefaultMappingDefinition RagConnect.defaultIntToStringMapping() = baseDefaultMappingDefinition("int", "String", "return String.valueOf(input);");
  syn nta DefaultMappingDefinition RagConnect.defaultShortToStringMapping() = baseDefaultMappingDefinition("int", "String", "return String.valueOf(input);");
  syn nta DefaultMappingDefinition RagConnect.defaultLongToStringMapping() = baseDefaultMappingDefinition("int", "String", "return String.valueOf(input);");
  syn nta DefaultMappingDefinition RagConnect.defaultFloatToStringMapping() = baseDefaultMappingDefinition("int", "String", "return String.valueOf(input);");
  syn nta DefaultMappingDefinition RagConnect.defaultDoubleToStringMapping() = baseDefaultMappingDefinition("int", "String", "return String.valueOf(input);");
  syn nta DefaultMappingDefinition RagConnect.defaultCharToStringMapping() = baseDefaultMappingDefinition("int", "String", "return String.valueOf(input);");
138
139
140
141
}

aspect Mappings {
  // --- effectiveMappings ---
142
  syn java.util.List<MappingDefinition> EndpointDefinition.effectiveMappings();
René Schöne's avatar
René Schöne committed
143
144
  eq ReceiveTokenEndpointDefinition.effectiveMappings() {
    // if there is a first mapping, check if it is suitable.
145
146
147
    //  or if no mappings are specified.
    // then prepend the suitable default mapping
    java.util.List<MappingDefinition> result;
René Schöne's avatar
René Schöne committed
148
    if (getMappingList().isEmpty() || !hasSuitableEdgeMapping()) {
149
150
151
152
153
154
155
156
      result = new java.util.ArrayList();
      result.add(suitableDefaultMapping());
      result.addAll(getMappingList());
    } else {
      result = getMappingList();
    }
    return result;
  }
René Schöne's avatar
René Schöne committed
157
158
  eq SendTokenEndpointDefinition.effectiveMappings() {
    // if there is a mapping, check if it is suitable.
159
160
161
    //  or if no mappings are specified.
    // then append the suitable default mapping
    java.util.List<MappingDefinition> result;
René Schöne's avatar
René Schöne committed
162
    if (getMappingList().isEmpty() || !hasSuitableEdgeMapping()) {
163
164
165
166
167
168
169
170
171
      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
172
173
174
175
176
177
178
179
180
181
182
183
  // --- hasSuitableEdgeMapping ---
  syn boolean TokenEndpointDefinition.hasSuitableEdgeMapping();
  eq ReceiveTokenEndpointDefinition.hasSuitableEdgeMapping() = isSuitableEdgeMapping(getMappingList().get(0));
  eq SendTokenEndpointDefinition.hasSuitableEdgeMapping() = isSuitableEdgeMapping(getMappingList().get(getMappingList().size() - 1));

  // --- isSuitableEdgeMapping(def) ---
  syn boolean TokenEndpointDefinition.isSuitableEdgeMapping(MappingDefinition def);
  eq ReceiveFromMqttDefinition.isSuitableEdgeMapping(MappingDefinition def) = def.getFromType().isByteArray();
  eq SendToMqttDefinition.isSuitableEdgeMapping(MappingDefinition def) = def.getToType().isByteArray();
  eq ReceiveFromRestDefinition.isSuitableEdgeMapping(MappingDefinition def) = def.getFromType().isString();
  eq SendToRestDefinition.isSuitableEdgeMapping(MappingDefinition def) = def.getToType().isString();

René Schöne's avatar
René Schöne committed
184
  // --- isPrimitiveType ---
René Schöne's avatar
René Schöne committed
185
  syn boolean TokenComponent.isPrimitiveType() = effectiveJavaTypeUse().isPrimitiveType();
René Schöne's avatar
René Schöne committed
186
187
188
189
190
191
192
193
194
195
196
197
198
  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
199
200
201
202
203
204
  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
205

206
  // --- suitableDefaultMapping ---
207
  syn DefaultMappingDefinition EndpointDefinition.suitableDefaultMapping();
208
  eq ReceiveFromMqttDefinition.suitableDefaultMapping() {
209
210
211
    String typeName = getMappingList().isEmpty() ?
        getToken().getJavaTypeUse().getName() :
        getMappingList().get(0).getFromType().prettyPrint();
212
213
    switch(typeName) {
      case "int":
214
      case "Integer": return ragconnect().defaultBytesToIntMapping();
215
      case "short":
216
      case "Short": return ragconnect().defaultBytesToShortMapping();
217
      case "long":
218
      case "Long": return ragconnect().defaultBytesToLongMapping();
219
      case "float":
220
      case "Float": return ragconnect().defaultBytesToFloatMapping();
221
      case "double":
222
      case "Double": return ragconnect().defaultBytesToDoubleMapping();
223
      case "char":
224
225
      case "Character": return ragconnect().defaultBytesToCharMapping();
      case "String": return ragconnect().defaultBytesToStringMapping();
226
227
228
      default: return null;
    }
  }
229
  eq SendToMqttDefinition.suitableDefaultMapping() {
230
231
232
    String typeName = getMappingList().isEmpty() ?
        getToken().getJavaTypeUse().getName() :
        getMappingList().get(getMappingList().size() - 1).getFromType().prettyPrint();
233
234
    switch(typeName) {
      case "int":
235
      case "Integer": return ragconnect().defaultIntToBytesMapping();
236
      case "short":
237
      case "Short": return ragconnect().defaultShortToBytesMapping();
238
      case "long":
239
      case "Long": return ragconnect().defaultLongToBytesMapping();
240
      case "float":
241
      case "Float": return ragconnect().defaultFloatToBytesMapping();
242
      case "double":
243
      case "Double": return ragconnect().defaultDoubleToBytesMapping();
244
      case "char":
245
246
      case "Character": return ragconnect().defaultCharToBytesMapping();
      case "String": return ragconnect().defaultStringToBytesMapping();
247
248
249
      default: return null;
    }
  }
René Schöne's avatar
René Schöne committed
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
  eq ReceiveFromRestDefinition.suitableDefaultMapping() {
    String typeName = getMappingList().isEmpty() ?
        getToken().getJavaTypeUse().getName() :
        getMappingList().get(0).getFromType().prettyPrint();
    switch(typeName) {
      case "int":
      case "Integer": return ragconnect().defaultStringToIntMapping();
      case "short":
      case "Short": return ragconnect().defaultStringToShortMapping();
      case "long":
      case "Long": return ragconnect().defaultStringToLongMapping();
      case "float":
      case "Float": return ragconnect().defaultStringToFloatMapping();
      case "double":
      case "Double": return ragconnect().defaultStringToDoubleMapping();
      case "char":
      case "Character": return ragconnect().defaultStringToCharMapping();
      default: return null;
    }
  }
  eq SendToRestDefinition.suitableDefaultMapping() {
    String typeName = getMappingList().isEmpty() ?
        getToken().getJavaTypeUse().getName() :
        getMappingList().get(getMappingList().size() - 1).getFromType().prettyPrint();
    switch(typeName) {
      case "int":
      case "Integer": return ragconnect().defaultIntToStringMapping();
      case "short":
      case "Short": return ragconnect().defaultShortToStringMapping();
      case "long":
      case "Long": return ragconnect().defaultLongToStringMapping();
      case "float":
      case "Float": return ragconnect().defaultFloatToStringMapping();
      case "double":
      case "Double": return ragconnect().defaultDoubleToStringMapping();
      case "char":
      case "Character": return ragconnect().defaultCharToStringMapping();
      default: return null;
    }
  }
290
291
292
293
294

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

René Schöne's avatar
René Schöne committed
295
296
297
298
  // --- isString ---
  syn boolean MappingDefinitionType.isString() = false;
  eq JavaMappingDefinitionType.isString() = getType().getName().equals("String");

299
  // --- allMappingDefinitions ---
300
  syn java.util.List<MappingDefinition> RagConnect.allMappingDefinitions() {
301
302
    java.util.List<MappingDefinition> result = new java.util.ArrayList<>();
    getMappingDefinitionList().iterator().forEachRemaining(result::add);
René Schöne's avatar
René Schöne committed
303
    // byte[] converstion
304
305
306
307
308
309
310
311
312
313
314
315
316
317
    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());
René Schöne's avatar
René Schöne committed
318
319
320
321
322
323
324
325
326
327
328
329
330
    // string converstion
    result.add(defaultStringToIntMapping());
    result.add(defaultStringToShortMapping());
    result.add(defaultStringToLongMapping());
    result.add(defaultStringToFloatMapping());
    result.add(defaultStringToDoubleMapping());
    result.add(defaultStringToCharMapping());
    result.add(defaultIntToStringMapping());
    result.add(defaultShortToStringMapping());
    result.add(defaultLongToStringMapping());
    result.add(defaultFloatToStringMapping());
    result.add(defaultDoubleToStringMapping());
    result.add(defaultCharToStringMapping());
331
332
333
    return result;
  }
}