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

René Schöne's avatar
René Schöne committed
3
  private String RagConnect.baseDefaultMappingTypeNamePart(String typeName) {
René Schöne's avatar
René Schöne committed
4
    return capitalize(typeName).replace("[]", "s");
René Schöne's avatar
René Schöne committed
5
6
7
8
9
10
11
12
  }

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

René Schöne's avatar
René Schöne committed
13
  private DefaultMappingDefinition RagConnect.createDefaultMappingDefinition(String prefix, String fromTypeName, String toTypeName, String content) {
René Schöne's avatar
René Schöne committed
14
    DefaultMappingDefinition result = new DefaultMappingDefinition();
René Schöne's avatar
René Schöne committed
15
    result.setID(prefix + baseDefaultMappingTypeNamePart(fromTypeName) + "To" + baseDefaultMappingTypeNamePart(toTypeName) + "Mapping");
René Schöne's avatar
René Schöne committed
16
17
18
19
20
21
22
    result.setFromType(baseDefaultMappingTypeFromName(fromTypeName));
    result.setFromVariableName("input");
    result.setToType(baseDefaultMappingTypeFromName(toTypeName));
    result.setContent(content);
    return result;
  }

René Schöne's avatar
René Schöne committed
23
24
25
26
27
28
29
30
  private DefaultMappingDefinition RagConnect.baseDefaultMappingDefinition(String fromTypeName, String toTypeName, String content) {
    return createDefaultMappingDefinition("_Default", fromTypeName, toTypeName, content);
  }

  private DefaultMappingDefinition RagConnect.treeDefaultMappingDefinition(String fromTypeName, String toTypeName, String content) {
    return createDefaultMappingDefinition("_TreeDefault", fromTypeName, toTypeName, content);
  }

31
32
  syn nta DefaultMappingDefinition RagConnect.defaultBytesToBooleanMapping() = baseDefaultMappingDefinition(
      "byte[]", "boolean", "return input[0] == (byte) 1;");
René Schöne's avatar
René Schöne committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  syn nta DefaultMappingDefinition RagConnect.defaultBytesToIntMapping() = baseDefaultMappingDefinition(
      "byte[]", "int", "return java.nio.ByteBuffer.wrap(input).getInt();");
  syn nta DefaultMappingDefinition RagConnect.defaultBytesToShortMapping() = baseDefaultMappingDefinition(
      "byte[]", "short", "return java.nio.ByteBuffer.wrap(input).getShort();");
  syn nta DefaultMappingDefinition RagConnect.defaultBytesToLongMapping() = baseDefaultMappingDefinition(
      "byte[]", "long", "return java.nio.ByteBuffer.wrap(input).getLong();");
  syn nta DefaultMappingDefinition RagConnect.defaultBytesToFloatMapping() = baseDefaultMappingDefinition(
      "byte[]", "float", "return java.nio.ByteBuffer.wrap(input).getFloat();");
  syn nta DefaultMappingDefinition RagConnect.defaultBytesToDoubleMapping() = baseDefaultMappingDefinition(
      "byte[]", "double", "return java.nio.ByteBuffer.wrap(input).getDouble();");
  syn nta DefaultMappingDefinition RagConnect.defaultBytesToCharMapping() = baseDefaultMappingDefinition(
      "byte[]", "char", "return java.nio.ByteBuffer.wrap(input).getChar();");
  syn nta DefaultMappingDefinition RagConnect.defaultBytesToStringMapping() = baseDefaultMappingDefinition(
      "byte[]", "String", "return new String(input);");
47

René Schöne's avatar
René Schöne committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  syn nta DefaultMappingDefinition RagConnect.defaultBytesToTreeMapping(String typeName) {
    return treeDefaultMappingDefinition("byte[]", typeName,
      "String content = new String(input);\n" +
      "com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();\n" +
      "com.fasterxml.jackson.core.JsonFactory factory = new com.fasterxml.jackson.core.JsonFactory();\n" +
      "com.fasterxml.jackson.core.JsonParser parser = factory.createParser(content);\n" +
      typeName + " result = " + typeName + ".deserialize((com.fasterxml.jackson.databind.JsonNode)mapper.readTree(parser));\n" +
      "parser.close();\n" +
      "return result;"
    );
  }
  syn nta DefaultMappingDefinition RagConnect.defaultTreeToBytesMapping(String typeName) {
    return treeDefaultMappingDefinition(typeName, "byte[]",
      "java.io.ByteArrayOutputStream outputStream = new java.io.ByteArrayOutputStream();\n" +
      "com.fasterxml.jackson.core.JsonFactory factory = new com.fasterxml.jackson.core.JsonFactory();\n" +
      "com.fasterxml.jackson.core.JsonGenerator generator = factory.createGenerator(outputStream, com.fasterxml.jackson.core.JsonEncoding.UTF8);\n"+
      "input.serialize(generator);\n" +
      "generator.flush();\n" +
      "return outputStream.toString().getBytes();"
    );
  }

70
71
  syn nta DefaultMappingDefinition RagConnect.defaultBooleanToBytesMapping() = baseDefaultMappingDefinition(
        "boolean", "byte[]", "return java.nio.ByteBuffer.allocate(1).put((byte) (input ? 1 : 0)).array();");
René Schöne's avatar
René Schöne committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  syn nta DefaultMappingDefinition RagConnect.defaultIntToBytesMapping() = baseDefaultMappingDefinition(
      "int", "byte[]", "return java.nio.ByteBuffer.allocate(Integer.BYTES).putInt(input).array();");
  syn nta DefaultMappingDefinition RagConnect.defaultShortToBytesMapping() = baseDefaultMappingDefinition(
      "short", "byte[]", "return java.nio.ByteBuffer.allocate(Short.BYTES).putShort(input).array();");
  syn nta DefaultMappingDefinition RagConnect.defaultLongToBytesMapping() = baseDefaultMappingDefinition(
      "long", "byte[]", "return java.nio.ByteBuffer.allocate(Long.BYTES).putLong(input).array();");
  syn nta DefaultMappingDefinition RagConnect.defaultFloatToBytesMapping() = baseDefaultMappingDefinition(
      "float", "byte[]", "return java.nio.ByteBuffer.allocate(Float.BYTES).putFloat(input).array();");
  syn nta DefaultMappingDefinition RagConnect.defaultDoubleToBytesMapping() = baseDefaultMappingDefinition(
      "double", "byte[]", "return java.nio.ByteBuffer.allocate(Double.BYTES).putDouble(input).array();");
  syn nta DefaultMappingDefinition RagConnect.defaultCharToBytesMapping() = baseDefaultMappingDefinition(
      "char", "byte[]", "return java.nio.ByteBuffer.allocate(Character.BYTES).putChar(input).array();");
  syn nta DefaultMappingDefinition RagConnect.defaultStringToBytesMapping() = baseDefaultMappingDefinition(
      "String", "byte[]", "return input.getBytes();");
René Schöne's avatar
René Schöne committed
86

87
88
  syn nta DefaultMappingDefinition RagConnect.defaultStringToBooleanMapping() = baseDefaultMappingDefinition(
      "String", "boolean", "return Boolean.parseBoolean(input);");
René Schöne's avatar
René Schöne committed
89
90
91
92
93
94
95
96
97
98
99
100
  syn nta DefaultMappingDefinition RagConnect.defaultStringToIntMapping() = baseDefaultMappingDefinition(
      "String", "int", "return Integer.parseInt(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);");
René Schöne's avatar
René Schöne committed
101

102
103
  syn nta DefaultMappingDefinition RagConnect.defaultBooleanToStringMapping() = baseDefaultMappingDefinition(
      "boolean", "String", "return String.valueOf(input);");
René Schöne's avatar
René Schöne committed
104
105
106
107
108
109
110
111
112
113
114
115
  syn nta DefaultMappingDefinition RagConnect.defaultIntToStringMapping() = baseDefaultMappingDefinition(
      "int", "String", "return String.valueOf(input);");
  syn nta DefaultMappingDefinition RagConnect.defaultShortToStringMapping() = baseDefaultMappingDefinition(
      "short", "String", "return String.valueOf(input);");
  syn nta DefaultMappingDefinition RagConnect.defaultLongToStringMapping() = baseDefaultMappingDefinition(
      "long", "String", "return String.valueOf(input);");
  syn nta DefaultMappingDefinition RagConnect.defaultFloatToStringMapping() = baseDefaultMappingDefinition(
      "float", "String", "return String.valueOf(input);");
  syn nta DefaultMappingDefinition RagConnect.defaultDoubleToStringMapping() = baseDefaultMappingDefinition(
      "double", "String", "return String.valueOf(input);");
  syn nta DefaultMappingDefinition RagConnect.defaultCharToStringMapping() = baseDefaultMappingDefinition(
      "char", "String", "return String.valueOf(input);");
116
117
118
119
}

aspect Mappings {
  // --- effectiveMappings ---
120
  syn java.util.List<MappingDefinition> EndpointDefinition.effectiveMappings() {
121
    java.util.List<MappingDefinition> result;
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
    if (isReceiveTokenEndpointDefinition() || isReceiveTypeEndpointDefinition()) {
      // if no mappings are specified, or if first mapping is not suitable.
      // then prepend the suitable default mapping
      if (getMappingList().isEmpty() || !getMappingList().get(0).getFromType().isByteArray()) {
        result = new java.util.ArrayList();
        result.add(suitableReceiveDefaultMapping());
        result.addAll(getMappingList());
      } else {
        result = getMappingList();
      }
    } else if (isSendTokenEndpointDefinition() || isSendTypeEndpointDefinition()) {
      // if no mappings are specified, or if last mapping is not suitable
      // then append the suitable default mapping
      if (getMappingList().isEmpty() || !getMappingList().get(getMappingList().size() - 1).getToType().isByteArray()) {
        result = new java.util.ArrayList(getMappingList());
        result.add(suitableSendDefaultMapping());
      } else {
        result = getMappingList();
      }
141
    } else {
142
      throw new RuntimeException("Unknown endpoint definition: " + this);
143
144
145
    }
    return result;
  }
René Schöne's avatar
René Schöne committed
146

René Schöne's avatar
René Schöne committed
147
  // --- isPrimitiveType ---
René Schöne's avatar
René Schöne committed
148
  syn boolean TokenComponent.isPrimitiveType() = effectiveJavaTypeUse().isPrimitiveType();
René Schöne's avatar
René Schöne committed
149
150
151
  syn boolean JavaTypeUse.isPrimitiveType() = false;
  eq SimpleJavaTypeUse.isPrimitiveType() {
    switch(getName()) {
152
      case "boolean":
René Schöne's avatar
René Schöne committed
153
154
155
156
157
158
159
160
161
162
      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
163
164
165
166
167
168
  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
169

170
171
172
  // --- suitableReceiveDefaultMapping ---
  syn DefaultMappingDefinition EndpointDefinition.suitableReceiveDefaultMapping() {
    switch (targetTypeName()) {
173
174
      case "boolean":
      case "Boolean": return ragconnect().defaultBytesToBooleanMapping();
175
      case "int":
176
      case "Integer": return ragconnect().defaultBytesToIntMapping();
177
      case "short":
178
      case "Short": return ragconnect().defaultBytesToShortMapping();
179
      case "long":
180
      case "Long": return ragconnect().defaultBytesToLongMapping();
181
      case "float":
182
      case "Float": return ragconnect().defaultBytesToFloatMapping();
183
      case "double":
184
      case "Double": return ragconnect().defaultBytesToDoubleMapping();
185
      case "char":
186
187
      case "Character": return ragconnect().defaultBytesToCharMapping();
      case "String": return ragconnect().defaultBytesToStringMapping();
René Schöne's avatar
René Schöne committed
188
189
190
191
192
193
194
      default:
        try {
            TypeDecl typeDecl = program().resolveTypeDecl(targetTypeName());
            return ragconnect().defaultBytesToTreeMapping(typeDecl.getName());
        } catch (Exception ignore) {}
        System.err.println("Could not find suitable default mapping for " + targetTypeName() + " on " + this);
        return null;
195
196
    }
  }
197
198
199
  // --- suitableSendDefaultMapping ---
  syn DefaultMappingDefinition EndpointDefinition.suitableSendDefaultMapping() {
    switch (targetTypeName()) {
200
201
      case "boolean":
      case "Boolean": return ragconnect().defaultBooleanToBytesMapping();
202
      case "int":
203
      case "Integer": return ragconnect().defaultIntToBytesMapping();
204
      case "short":
205
      case "Short": return ragconnect().defaultShortToBytesMapping();
206
      case "long":
207
      case "Long": return ragconnect().defaultLongToBytesMapping();
208
      case "float":
209
      case "Float": return ragconnect().defaultFloatToBytesMapping();
210
      case "double":
211
      case "Double": return ragconnect().defaultDoubleToBytesMapping();
212
      case "char":
213
214
      case "Character": return ragconnect().defaultCharToBytesMapping();
      case "String": return ragconnect().defaultStringToBytesMapping();
René Schöne's avatar
René Schöne committed
215
216
217
218
219
220
221
      default:
        try {
            TypeDecl typeDecl = program().resolveTypeDecl(targetTypeName());
            return ragconnect().defaultTreeToBytesMapping(typeDecl.getName());
        } catch (Exception ignore) {}
        System.err.println("Could not find suitable default mapping for " + targetTypeName() + " on " + this);
        return null;
222
223
    }
  }
224
225
226
227
228
229
230
231
232

  // --- targetTypeName ---
  syn String EndpointDefinition.targetTypeName();
  eq ReceiveTokenEndpointDefinition.targetTypeName() {
    return getMappingList().isEmpty() ?
           getToken().effectiveJavaTypeUse().getName() :
           getMappingList().get(0).getFromType().prettyPrint();
  }
  eq ReceiveTypeEndpointDefinition.targetTypeName() {
René Schöne's avatar
René Schöne committed
233
234
235
    return getMappingList().isEmpty() ?
           getType().getTypeDecl().getName() :
           getMappingList().get(0).getFromType().prettyPrint();
236
237
238
239
240
241
242
  }
  eq SendTokenEndpointDefinition.targetTypeName() {
    return getMappingList().isEmpty() ?
           getToken().effectiveJavaTypeUse().getName() :
           getMappingList().get(getMappingList().size() - 1).getToType().prettyPrint();
  }
  eq SendTypeEndpointDefinition.targetTypeName() {
René Schöne's avatar
René Schöne committed
243
244
245
    return getMappingList().isEmpty() ?
           getType().getTypeDecl().getName() :
           getMappingList().get(getMappingList().size() - 1).getToType().prettyPrint();
246
247
  }

248
249
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
//  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;
//    }
//  }
288
289
290
291
292

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

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

297
  // --- allMappingDefinitions ---
298
  syn java.util.List<MappingDefinition> RagConnect.allMappingDefinitions() {
299
    java.util.List<MappingDefinition> result = new java.util.ArrayList<>();
René Schöne's avatar
René Schöne committed
300
    // user-defined mappings
René Schöne's avatar
René Schöne committed
301
    allMappingDefinitionList().iterator().forEachRemaining(result::add);
René Schöne's avatar
René Schöne committed
302
    // byte[] <-> primitive conversion
303
    result.add(defaultBytesToBooleanMapping());
304
305
306
307
308
309
310
    result.add(defaultBytesToIntMapping());
    result.add(defaultBytesToShortMapping());
    result.add(defaultBytesToLongMapping());
    result.add(defaultBytesToFloatMapping());
    result.add(defaultBytesToDoubleMapping());
    result.add(defaultBytesToCharMapping());
    result.add(defaultBytesToStringMapping());
311
    result.add(defaultBooleanToBytesMapping());
312
313
314
315
316
317
318
    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
319
320
321
322
323
    // byte[] <-> tree conversion
    for (TypeDecl typeDecl : getProgram().typeDecls()) {
      result.add(defaultBytesToTreeMapping(typeDecl.getName()));
      result.add(defaultTreeToBytesMapping(typeDecl.getName()));
    }
324
//    // string conversion
325
//    result.add(defaultStringToBooleanMapping());
326
327
328
329
330
331
//    result.add(defaultStringToIntMapping());
//    result.add(defaultStringToShortMapping());
//    result.add(defaultStringToLongMapping());
//    result.add(defaultStringToFloatMapping());
//    result.add(defaultStringToDoubleMapping());
//    result.add(defaultStringToCharMapping());
332
//    result.add(defaultBooleanToStringMapping());
333
334
335
336
337
338
//    result.add(defaultIntToStringMapping());
//    result.add(defaultShortToStringMapping());
//    result.add(defaultLongToStringMapping());
//    result.add(defaultFloatToStringMapping());
//    result.add(defaultDoubleToStringMapping());
//    result.add(defaultCharToStringMapping());
339
340
341
    return result;
  }
}