Mappings.jrag 13.6 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()) {
103
104
105
106
107
108
109
110
111
      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
112
113
114
115
116
117
118
  // --- 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);
119
120
  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
121

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

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

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

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

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