Mappings.jrag 13.5 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
13
14
15
16
17
18
19
20
21
22
  }

  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;
  }

René Schöne's avatar
René Schöne committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  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);");
37

René Schöne's avatar
René Schöne committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  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
52

René Schöne's avatar
René Schöne committed
53
54
55
56
57
58
59
60
61
62
63
64
  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
65

René Schöne's avatar
René Schöne committed
66
67
68
69
70
71
72
73
74
75
76
77
  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);");
78
79
80
81
}

aspect Mappings {
  // --- effectiveMappings ---
82
  syn java.util.List<MappingDefinition> EndpointDefinition.effectiveMappings();
René Schöne's avatar
René Schöne committed
83
84
  eq ReceiveTokenEndpointDefinition.effectiveMappings() {
    // if there is a first mapping, check if it is suitable.
85
86
87
    //  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
88
    if (getMappingList().isEmpty() || !hasSuitableEdgeMapping()) {
89
90
91
92
93
94
95
96
      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
97
98
  eq SendTokenEndpointDefinition.effectiveMappings() {
    // if there is a mapping, check if it is suitable.
99
100
101
    //  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
102
    if (getMappingList().isEmpty() || !hasSuitableEdgeMapping()) {
René Schöne's avatar
Cleanup    
René Schöne committed
103
      result = new java.util.ArrayList(getMappingList());
104
105
106
107
108
109
110
      result.add(suitableDefaultMapping());
    } else {
      result = getMappingList();
    }
    return result;
  }

René Schöne's avatar
René Schöne committed
111
112
113
114
115
116
117
  // --- 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);
118
119
  eq ReceiveTokenEndpointDefinition.isSuitableEdgeMapping(MappingDefinition def) = def.getFromType().isByteArray();
  eq SendTokenEndpointDefinition.isSuitableEdgeMapping(MappingDefinition def) = def.getToType().isByteArray();
René Schöne's avatar
René Schöne committed
120

René Schöne's avatar
René Schöne committed
121
  // --- isPrimitiveType ---
René Schöne's avatar
René Schöne committed
122
  syn boolean TokenComponent.isPrimitiveType() = effectiveJavaTypeUse().isPrimitiveType();
René Schöne's avatar
René Schöne committed
123
124
125
126
127
128
129
130
131
132
133
134
135
  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
136
137
138
139
140
141
  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
142

143
  // --- suitableDefaultMapping ---
144
  syn DefaultMappingDefinition EndpointDefinition.suitableDefaultMapping();
145
  eq ReceiveTokenEndpointDefinition.suitableDefaultMapping() {
146
    String typeName = getMappingList().isEmpty() ?
147
        getToken().effectiveJavaTypeUse().getName() :
148
        getMappingList().get(0).getFromType().prettyPrint();
149
150
    switch(typeName) {
      case "int":
151
      case "Integer": return ragconnect().defaultBytesToIntMapping();
152
      case "short":
153
      case "Short": return ragconnect().defaultBytesToShortMapping();
154
      case "long":
155
      case "Long": return ragconnect().defaultBytesToLongMapping();
156
      case "float":
157
      case "Float": return ragconnect().defaultBytesToFloatMapping();
158
      case "double":
159
      case "Double": return ragconnect().defaultBytesToDoubleMapping();
160
      case "char":
161
162
      case "Character": return ragconnect().defaultBytesToCharMapping();
      case "String": return ragconnect().defaultBytesToStringMapping();
163
164
165
      default: return null;
    }
  }
166
  eq SendTokenEndpointDefinition.suitableDefaultMapping() {
167
    String typeName = getMappingList().isEmpty() ?
168
        getToken().effectiveJavaTypeUse().getName() :
169
        getMappingList().get(getMappingList().size() - 1).getFromType().prettyPrint();
170
171
    switch(typeName) {
      case "int":
172
      case "Integer": return ragconnect().defaultIntToBytesMapping();
173
      case "short":
174
      case "Short": return ragconnect().defaultShortToBytesMapping();
175
      case "long":
176
      case "Long": return ragconnect().defaultLongToBytesMapping();
177
      case "float":
178
      case "Float": return ragconnect().defaultFloatToBytesMapping();
179
      case "double":
180
      case "Double": return ragconnect().defaultDoubleToBytesMapping();
181
      case "char":
182
183
      case "Character": return ragconnect().defaultCharToBytesMapping();
      case "String": return ragconnect().defaultStringToBytesMapping();
184
185
186
      default: return null;
    }
  }
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
//  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;
//    }
//  }
227
228
229
230
231

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

232
233
234
//  // --- isString ---
//  syn boolean MappingDefinitionType.isString() = false;
//  eq JavaMappingDefinitionType.isString() = getType().getName().equals("String");
René Schöne's avatar
René Schöne committed
235

236
  // --- allMappingDefinitions ---
237
  syn java.util.List<MappingDefinition> RagConnect.allMappingDefinitions() {
238
239
    java.util.List<MappingDefinition> result = new java.util.ArrayList<>();
    getMappingDefinitionList().iterator().forEachRemaining(result::add);
René Schöne's avatar
René Schöne committed
240
    // byte[] conversion
241
242
243
244
245
246
247
248
249
250
251
252
253
254
    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());
255
256
257
258
259
260
261
262
263
264
265
266
267
//    // string conversion
//    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());
268
269
270
    return result;
  }
}