Generation.jadd 23.1 KB
Newer Older
René Schöne's avatar
René Schöne committed
1
2
3
4
5
6
7
8
9
10
aspect GenerationUtils {
  public static final String ASTNode.aspectIndent = "  ";

  public String ASTNode.ind(int n) {
    StringBuilder s = new StringBuilder();
    for (int i = 0; i < n; i++) {
      s.append(aspectIndent);
    }
    return s.toString();
  }
René Schöne's avatar
René Schöne committed
11
12
13
14
15
16
17
18
19
20
21

  // --- prettyPrint ---
  syn String MappingDefinitionType.prettyPrint();
  eq JavaMappingDefinitionType.prettyPrint() = getType().getName();
  eq JavaArrayMappingDefinitionType.prettyPrint() = getType().getName() + "[]";

  syn String JavaTypeUse.prettyPrint() {
    StringBuilder sb = new StringBuilder();
    generateAbstractGrammar(sb);
    return sb.toString();
  }
René Schöne's avatar
René Schöne committed
22
23
}

René Schöne's avatar
René Schöne committed
24
25
26
27
28
29
30
31
32
33
/* Open questions
- Should all string constants be defined on the normal AST, or on the special mustache AST?
*/

aspect AttributesForMustache {
  // --- MRos2Rag ---

  // --- MUpdateDefinition ---
  syn String MUpdateDefinition.preemptiveExpectedValue();
  syn String MUpdateDefinition.preemptiveReturn();
34
35
  syn TokenUpdateDefinition MUpdateDefinition.updateDef();
  syn String MUpdateDefinition.firstInputVarName();
René Schöne's avatar
René Schöne committed
36

37
  eq MUpdateDefinition.getInnerMappingDefinition(int i).isLast() = i == getNumInnerMappingDefinition();
René Schöne's avatar
René Schöne committed
38
  eq MUpdateDefinition.getInnerMappingDefinition().resultVarPrefix() = resultVarPrefix();
39
40
  eq MUpdateDefinition.getInnerMappingDefinition(int i).inputVarName() = i == 0 ? firstInputVarName() : resultVarPrefix() + getInnerMappingDefinition(i - 1).getMappingDefinition().methodName();
  eq MRos2Rag.getChild().mqttUpdaterField() = getRos2Rag().mqttUpdaterField();
René Schöne's avatar
René Schöne committed
41

42
  syn String MUpdateDefinition.mqttUpdaterAttribute() = updateDef().mqttUpdaterAttribute();
René Schöne's avatar
René Schöne committed
43
44
45
46
47
  syn String MUpdateDefinition.connectMethod() = updateDef().connectMethod();
  syn TokenComponent MUpdateDefinition.token() = updateDef().getToken();
  syn boolean MUpdateDefinition.alwaysApply() = updateDef().getAlwaysApply();
  syn String MUpdateDefinition.resultVarPrefix() = "result";  // we do not need "_" here, because methodName begins with one
  syn String MUpdateDefinition.parentTypeName() = token().containingTypeDecl().getName();
48
49
  syn String MUpdateDefinition.tokenName() = token().getName();
  syn MInnerMappingDefinition MUpdateDefinition.lastDefinition() = getInnerMappingDefinition(getNumInnerMappingDefinition() - 1);
René Schöne's avatar
René Schöne committed
50
51
  syn String MUpdateDefinition.lastDefinitionType() = lastDefinition().ToType();
  syn String MUpdateDefinition.lastDefinitionName() = lastDefinition().methodName();
52
  syn String MUpdateDefinition.lastResult() = resultVarPrefix() + lastDefinitionName();
René Schöne's avatar
René Schöne committed
53
54
  syn String MUpdateDefinition.condition() {
    if (lastDefinition().getMappingDefinition().getToType().isArray()) {
55
      return "java.util.Arrays.equals(" + preemptiveExpectedValue() + ", " + lastResult() + ")";
René Schöne's avatar
René Schöne committed
56
57
    }
    if (token().isPrimitiveType() && lastDefinition().getMappingDefinition().getToType().isPrimitiveType()) {
58
      return preemptiveExpectedValue() + " == " + lastResult();
René Schöne's avatar
René Schöne committed
59
60
    }
    if (lastDefinition().getMappingDefinition().isDefaultMappingDefinition()) {
61
      return preemptiveExpectedValue() + " != null && " + preemptiveExpectedValue() + ".equals(" + lastResult() + ")";
René Schöne's avatar
René Schöne committed
62
    }
63
    return preemptiveExpectedValue() + " != null ? " + preemptiveExpectedValue() + ".equals(" + lastResult() + ") : " + lastResult() + " == null";
René Schöne's avatar
René Schöne committed
64
65
66
67
68
69
70
71
72
73
  }

  // --- MInnerMappingDefinition ---
  inh boolean MInnerMappingDefinition.isLast();
  inh String MInnerMappingDefinition.resultVarPrefix();
  syn String MInnerMappingDefinition.ToType() = getMappingDefinition().getToType().prettyPrint();
  syn String MInnerMappingDefinition.methodName() = getMappingDefinition().methodName();
  inh String MInnerMappingDefinition.inputVarName();

  // --- MReadDefinition ---
74
  eq MReadDefinition.preemptiveExpectedValue() = "get" + tokenName() + "()";
René Schöne's avatar
René Schöne committed
75
76
  eq MReadDefinition.preemptiveReturn() = "return;";
  eq MReadDefinition.updateDef() = getReadFromMqttDefinition();
77
  eq MReadDefinition.firstInputVarName() = "message";
René Schöne's avatar
René Schöne committed
78
79
80
81
82

  // --- MWriteDefinition ---
  eq MWriteDefinition.preemptiveExpectedValue() = lastValue();
  eq MWriteDefinition.preemptiveReturn() = "return false;";
  eq MWriteDefinition.updateDef() = getWriteToMqttDefinition();
83
  eq MWriteDefinition.firstInputVarName() = "get" + tokenName() + "()";
René Schöne's avatar
René Schöne committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

  syn String MWriteDefinition.writeTopic() = getWriteToMqttDefinition().writeTopic();
  syn String MWriteDefinition.lastValue() = getWriteToMqttDefinition().lastValue();
  syn String MWriteDefinition.updateMethod() = getWriteToMqttDefinition().updateMethod();
  syn String MWriteDefinition.writeMethod() = getWriteToMqttDefinition().writeMethod();
  syn String MWriteDefinition.tokenResetMethod() = getWriteToMqttDefinition().tokenResetMethod();

  // --- MMappingDefinition ---
  syn String MMappingDefinition.toType() = getMappingDefinition().getToType().prettyPrint();
  syn String MMappingDefinition.methodName() = getMappingDefinition().methodName();
  syn String MMappingDefinition.fromType() = getMappingDefinition().getFromType().prettyPrint();
  syn String MMappingDefinition.fromVariableName() = getMappingDefinition().getFromVariableName();
  syn String MMappingDefinition.content() = getMappingDefinition().getContent();

  // --- MDependencyDefinition ---
  syn String MDependencyDefinition.targetParentTypeName() = getDependencyDefinition().getTarget().containingTypeDecl().getName();
  syn String MDependencyDefinition.dependencyMethod() = getDependencyDefinition().dependencyMethod();
  syn String MDependencyDefinition.sourceParentTypeName() = getDependencyDefinition().getSource().containingTypeDecl().getName();
  syn String MDependencyDefinition.internalRelationPrefix() = getDependencyDefinition().internalRelationPrefix();
  syn nta MUpdateDefinition MDependencyDefinition.targetUpdateDefinition() {
104
    return getDependencyDefinition().targetUpdateDefinition().toMustache();
René Schöne's avatar
René Schöne committed
105
106
107
108
  }

  // --- MTypeComponent ---
  syn String MTypeComponent.name() = getTypeComponent().getName();
109
110
111
112
113
114
115
116
  inh String MTypeComponent.mqttUpdaterAttribute();
  inh String MTypeComponent.mqttUpdaterField();

  // --- MTokenComponent ---
  syn String MTokenComponent.parentTypeName() = getTokenComponent().containingTypeDecl().getName();
  syn String MTokenComponent.name() = getTokenComponent().getName();
  syn String MTokenComponent.javaType() = getTokenComponent().getJavaTypeUse().prettyPrint();
  syn String MTokenComponent.internalName() = getTokenComponent().internalName();
René Schöne's avatar
René Schöne committed
117

118
119
  // --- toMustache ---
  syn lazy MRos2Rag Ros2Rag.toMustache() {
René Schöne's avatar
René Schöne committed
120
    MRos2Rag result = new MRos2Rag();
121
    result.setRos2Rag(this);
René Schöne's avatar
René Schöne committed
122
123
    for (UpdateDefinition def : getUpdateDefinitionList()) {
      if (def.isWriteToMqttDefinition()) {
124
        result.addWriteDefinition(def.asWriteToMqttDefinition().toMustache());
René Schöne's avatar
René Schöne committed
125
      } else {
126
        result.addReadDefinition(def.asReadFromMqttDefinition().toMustache());
René Schöne's avatar
René Schöne committed
127
128
129
      }
    }
    for (MappingDefinition def : allMappingDefinitions()) {
130
      result.addMappingDefinition(def.toMustache());
René Schöne's avatar
René Schöne committed
131
132
    }
    for (DependencyDefinition def : getDependencyDefinitionList()) {
133
      result.addDependencyDefinition(def.toMustache());
René Schöne's avatar
René Schöne committed
134
135
    }
    for (TokenComponent token : getProgram().allTokenComponents()) {
136
137
138
139
140
141
      result.addTokenComponent(token.toMustache());
    }
    for (Component child : rootNode.getComponentList()) {
      if (child.isTypeComponent()) {
        result.addRootTypeChildren(child.asTypeComponent().toMustache());
      }
René Schöne's avatar
René Schöne committed
142
143
144
    }
    return result;
  }
145

René Schöne's avatar
René Schöne committed
146
//MInnerMappingDefinition.MappingDefinition -> MappingDefinition;
147
148
149
150
151
  protected void MUpdateDefinition.addInnerMappings() {
    for (MappingDefinition def : updateDef().effectiveMappings()) {
      MInnerMappingDefinition inner = new MInnerMappingDefinition();
      inner.setMappingDefinition(def);
      addInnerMappingDefinition(inner);
René Schöne's avatar
René Schöne committed
152
153
    }
  }
154
  syn lazy MReadDefinition ReadFromMqttDefinition.toMustache() {
René Schöne's avatar
René Schöne committed
155
156
157
158
159
    MReadDefinition result = new MReadDefinition();
    result.setReadFromMqttDefinition(this);
    result.addInnerMappings();
    return result;
  }
160
  syn lazy MWriteDefinition WriteToMqttDefinition.toMustache() {
René Schöne's avatar
René Schöne committed
161
162
163
164
165
    MWriteDefinition result = new MWriteDefinition();
    result.setWriteToMqttDefinition(this);
    result.addInnerMappings();
    return result;
  }
166
  syn lazy MMappingDefinition MappingDefinition.toMustache() {
René Schöne's avatar
René Schöne committed
167
168
169
170
    MMappingDefinition result = new MMappingDefinition();
    result.setMappingDefinition(this);
    return result;
  }
171
  syn lazy MDependencyDefinition DependencyDefinition.toMustache() {
René Schöne's avatar
René Schöne committed
172
173
174
175
    MDependencyDefinition result = new MDependencyDefinition();
    result.setDependencyDefinition(this);
    return result;
  }
176
  syn lazy MTypeComponent TypeComponent.toMustache() {
René Schöne's avatar
René Schöne committed
177
178
    MTypeComponent result = new MTypeComponent();
    result.setTypeComponent(this);
179
180
181
182
183
    return result;
  }
  syn lazy MTokenComponent TokenComponent.toMustache() {
    MTokenComponent result = new MTokenComponent();
    result.setTokenComponent(this);
René Schöne's avatar
René Schöne committed
184
    for (DependencyDefinition def : getDependencySourceDefinitionList()) {
185
      result.addDependencyDefinition(def.toMustache());
René Schöne's avatar
René Schöne committed
186
187
188
189
190
    }
    return result;
  }
}

René Schöne's avatar
René Schöne committed
191
aspect AspectGeneration {
René Schöne's avatar
René Schöne committed
192
  // naming convention attributes
193
194
  syn String TokenComponent.internalName() = getDependencySourceDefinitionList().isEmpty() ? externalName() : "_internal_" + getName();
  syn String TokenComponent.externalName() = getName();
René Schöne's avatar
René Schöne committed
195
196
197
198
199
200
201
202
203
204
205
206

  syn String TokenUpdateDefinition.connectMethod() = "connect" + getToken().getName();
  syn String WriteToMqttDefinition.writeTopic() = "_topic_" + getToken().getName();
  syn String WriteToMqttDefinition.lastValue() = "_lastValue" + getToken().getName();
  syn String WriteToMqttDefinition.updateMethod() = "_update_" + getToken().getName();
  syn String WriteToMqttDefinition.writeMethod() = "_writeLastValue_" + getToken().getName();
  syn String WriteToMqttDefinition.tokenResetMethod() = "get" + getToken().getName() + "_reset";
  syn String MappingDefinition.methodName() = "_apply_" + getID();
  syn String DependencyDefinition.dependencyMethod() = "add" +
    Character.toUpperCase(getID().charAt(0)) +
    getID().substring(1);
  syn String DependencyDefinition.internalRelationPrefix() = "_internal_" + getID();
207
  syn String DependencyDefinition.internalTokenName() = getSource().internalName();
René Schöne's avatar
René Schöne committed
208
209
210
211
212
213
214
215

  syn String Ros2Rag.mqttUpdaterAttribute() = "_mqttUpdater";
  syn String Ros2Rag.mqttUpdaterField() = "_mqttUpdater";

  syn String Ros2Rag.mqttSetHostMethod() = "MqttSetHost";
  syn String Ros2Rag.mqttWaitUntilReadyMethod() = "MqttWaitUntilReady";
  syn String Ros2Rag.mqttCloseMethod() = "MqttCloseConnections";

René Schöne's avatar
René Schöne committed
216
217
218
219
220
221
  // naming copy attributes
  // --- mqttUpdaterAttribute ---
  inh String UpdateDefinition.mqttUpdaterAttribute();
  inh String MappingDefinition.mqttUpdaterAttribute();
  inh String DependencyDefinition.mqttUpdaterAttribute();
  eq Ros2Rag.getChild().mqttUpdaterAttribute() = mqttUpdaterAttribute();
René Schöne's avatar
René Schöne committed
222

René Schöne's avatar
René Schöne committed
223
  // --- mqttUpdaterField ---
René Schöne's avatar
René Schöne committed
224

René Schöne's avatar
René Schöne committed
225
226
  // --- rootNodeName ---
  syn String ASTNode.rootNodeName() = rootNode.getName();
René Schöne's avatar
René Schöne committed
227

René Schöne's avatar
René Schöne committed
228
  // mustache specific nodes
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//  syn nta TypeComponentMustache TypeComponent.mustache() {
//    TypeComponentMustache result = new TypeComponentMustache();
//    result.setTypeComponent(this);
//    return result;
//  }
//  syn String TypeComponentMustache.name() = getTypeComponent().getName();
//  inh String TypeComponentMustache.mqttUpdaterAttribute();
//  inh String TypeComponentMustache.mqttUpdaterField();
//
//  // mustache specific attributes
//  syn java.util.List<TypeComponentMustache> Ros2Rag.rootTypeChildren() {
//    java.util.List<TypeComponentMustache> result = new java.util.ArrayList<>();
//    for (Component child : rootNode.getComponentList()) {
//      if (child.isTypeComponent()){
//        result.add(child.asTypeComponent().mustache());
//      }
//    }
//    return result;
//  }
René Schöne's avatar
René Schöne committed
248
249
250
251
252

  public String Ros2Rag.generateAspect(String rootNodeName) {
    StringBuilder sb = new StringBuilder();
    rootNode = getProgram().resolveTypeDecl(rootNodeName);
    com.github.mustachejava.MustacheFactory mf = new com.github.mustachejava.DefaultMustacheFactory();
253
    com.github.mustachejava.Mustache m = mf.compile("ros2rag.mustache");
René Schöne's avatar
René Schöne committed
254
    m.execute(new java.io.PrintWriter(new org.jastadd.ros2rag.compiler.AppendableWriter(sb)), this);
255
256
257
//    generateMqttAspect(sb);
//    generateGrammarExtension(sb);
    return sb.toString();
René Schöne's avatar
René Schöne committed
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//
//  public void Ros2Rag.generateMqttAspect(StringBuilder sb) {
//    String rootNodeName = rootNode.getName();
//    com.github.mustachejava.MustacheFactory mf = new com.github.mustachejava.DefaultMustacheFactory();
//    com.github.mustachejava.Mustache m = mf.compile("mqtt.mustache");
//    m.execute(new java.io.PrintWriter(new org.jastadd.ros2rag.compiler.AppendableWriter(sb)), this);
//  }
//
//  public void Ros2Rag.generateGrammarExtension(StringBuilder sb) {
//    sb.append("aspect ROS2RAG {\n");
//
//    for (UpdateDefinition def : getUpdateDefinitionList()) {
//      def.generateAspect(sb);
//    }
//    for (MappingDefinition def : allMappingDefinitions()) {
//      def.generateAspect(sb);
//    }
//    for (DependencyDefinition def : getDependencyDefinitionList()) {
//      def.generateAspect(sb);
//    }
//    for (TokenComponent token : getProgram().allTokenComponents()) {
//      token.generateAspect(sb);
//    }
//
//    sb.append("}\n");
//  }
//
//  abstract void UpdateDefinition.generateAspect(StringBuilder sb);
//
//  String TokenUpdateDefinition.generateMappingApplication(StringBuilder sb, int indent,
//      String initialInputVariableName) {
//    final String resultVariablePrefix = "result";  // we do not need "_" here, because methodName begins with one
//    String inputVariableName = initialInputVariableName;
//    // last variable need to be declared before begin of try
//    MappingDefinition lastDefinition = effectiveMappings().get(effectiveMappings().size() - 1);
//    sb.append(ind(indent)).append(lastDefinition.getToType().prettyPrint()).append(" ")
//      .append(resultVariablePrefix).append(lastDefinition.methodName()).append(";\n");
//    sb.append(ind(indent)).append("try {\n");
//    for (MappingDefinition mappingDefinition : effectiveMappings()) {
//      String resultVariableName = resultVariablePrefix + mappingDefinition.methodName();
//      sb.append(ind(indent + 1));
//      if (mappingDefinition != lastDefinition) {
//        sb.append(mappingDefinition.getToType().prettyPrint()).append(" ");
//      }
//      sb.append(resultVariablePrefix).append(mappingDefinition.methodName())
//        .append(" = ").append(mappingDefinition.methodName()).append("(")
//        .append(inputVariableName).append(");\n");
//      inputVariableName = resultVariableName;
//    }
//    sb.append(ind(indent)).append("} catch (Exception e) {\n");
//    sb.append(ind(indent + 1)).append("e.printStackTrace();\n");
//    sb.append(ind(indent + 1)).append(preemptiveReturnStatement()).append("\n");
//    sb.append(ind(indent)).append("}\n");
//    if (!getAlwaysApply()) {
//      MappingDefinition lastMapping = effectiveMappings().get(effectiveMappings().size() - 1);
//      sb.append(ind(indent)).append("if (");
//      if (lastMapping.getToType().isArray()) {
//        sb.append("java.util.Arrays.equals(").append(preemptiveExpectedValue())
//          .append(", ").append(inputVariableName).append(")");
//      } else {
//        sb.append(preemptiveExpectedValue());
//        if (getToken().isPrimitiveType() && lastMapping.getToType().isPrimitiveType()) {
//          sb.append(" == ").append(inputVariableName);
//        } else if (lastMapping.isDefaultMappingDefinition()) {
//          sb.append(" != null && ").append(preemptiveExpectedValue()).append(".equals(")
//            .append(inputVariableName).append(")");
//        } else {
//          sb.append(" != null ? ").append(preemptiveExpectedValue()).append(".equals(")
//            .append(inputVariableName).append(")").append(" : ")
//            .append(inputVariableName).append(" == null");
//        }
//      }
//      sb.append(") { ").append(preemptiveReturnStatement()).append(" }\n");
//    }
//    return inputVariableName;
//  }
//
//  syn String TokenUpdateDefinition.preemptiveExpectedValue();
//  eq ReadFromMqttDefinition.preemptiveExpectedValue() = "get" + getToken().getName() + "()";
//  eq WriteToMqttDefinition.preemptiveExpectedValue() = lastValue();
//
//  syn String TokenUpdateDefinition.preemptiveReturnStatement();
//  eq ReadFromMqttDefinition.preemptiveReturnStatement() = "return;";
//  eq WriteToMqttDefinition.preemptiveReturnStatement() = "return false;";
//
//  @Override
//  void ReadFromMqttDefinition.generateAspect(StringBuilder sb) {
//    sb.append(ind(1)).append("public void ").append(getToken().containingTypeDecl().getName()).append(".")
//      .append(connectMethod()).append("(String topic) {\n");
//    sb.append(ind(2)).append(mqttUpdaterAttribute()).append("().newConnection(topic, message -> {\n");
//    String lastResult = generateMappingApplication(sb, 3, "message");
//    if (loggingEnabledForReads) {
//      sb.append(ind(3)).append("System.out.println(\"[Read] \" + topic + \" -> ")
//        .append(getToken().getName()).append(" = \" + ").append(lastResult)
//        .append(");\n");
//    }
//    sb.append(ind(3)).append("set").append(getToken().getName()).append("(").append(lastResult).append(");\n");
//    sb.append(ind(2)).append("});\n");
//    sb.append(ind(1)).append("}\n\n");
//  }
//
//  @Override
//  void WriteToMqttDefinition.generateAspect(StringBuilder sb) {
//    String parentTypeName = getToken().containingTypeDecl().getName();
//    // fields
//    sb.append(ind(1)).append("private String ").append(parentTypeName).append(".")
//      .append(writeTopic()).append(" = null;\n");
//    sb.append(ind(1)).append("private byte[] ").append(parentTypeName).append(".")
//      .append(lastValue()).append(" = null;\n");
//
//    // connect method
//    sb.append(ind(1)).append("public void ").append(parentTypeName).append(".")
//      .append(connectMethod()).append("(String topic, boolean writeCurrentValue) {\n");
//    sb.append(ind(2)).append(writeTopic()).append(" = topic;\n");
//    sb.append(ind(2)).append(updateMethod()).append("();\n");
//    sb.append(ind(2)).append("if (writeCurrentValue) {\n");
//    sb.append(ind(3)).append(writeMethod()).append("();\n");
//    sb.append(ind(2)).append("}\n");
//    sb.append(ind(1)).append("}\n\n");
//
//    // update method
//    sb.append(ind(1)).append("protected boolean ").append(parentTypeName).append(".")
//      .append(updateMethod()).append("() {\n");
//    sb.append(ind(2)).append(tokenResetMethod()).append("();\n");
//    String lastResult = generateMappingApplication(sb, 2, "get" + getToken().getName() + "()");
//    sb.append(ind(2)).append(lastValue()).append(" = ").append(lastResult).append(";\n");
//    sb.append(ind(2)).append("return true;\n");
//    sb.append(ind(1)).append("}\n\n");
//
//    // write method
//    sb.append(ind(1)).append("protected void ").append(parentTypeName).append(".")
//      .append(writeMethod()).append("() {\n");
//    if (loggingEnabledForWrites) {
//      sb.append(ind(2)).append("System.out.println(\"[Write] ").append(getToken().getName())
//        .append(" = \" + ")
//        .append("get").append(getToken().getName()).append("() + \" -> \" + ")
//        .append(writeTopic()).append(");\n");
//    }
//    // _mqttUpdater().publish(${writeTopic()}, ${lastValue()});
//    sb.append(ind(2)).append(mqttUpdaterAttribute()).append("().publish(")
//      .append(writeTopic()).append(", ").append(lastValue()).append(");\n");
//    sb.append(ind(1)).append("}\n\n");
//  }
//
//  void MappingDefinition.generateAspect(StringBuilder sb) {
//    sb.append(ind(1)).append("protected static ").append(getToType().prettyPrint())
//      .append(" ASTNode.").append(methodName()).append("(")
//      .append(getFromType().prettyPrint()).append(" ").append(getFromVariableName())
//      .append(") throws Exception {\n");
//    for (String line : getContent().split("\n")) {
//      if (!line.trim().isEmpty()) {
//        sb.append(ind(2)).append(line).append("\n");
//      }
//    }
//    sb.append(ind(1)).append("}\n\n");
//  }
//
//  void DependencyDefinition.generateAspect(StringBuilder sb) {
//    String targetParentTypeName = getTarget().containingTypeDecl().getName();
//    String sourceParentTypeName = getSource().containingTypeDecl().getName();
//
//    // dependency method
//    sb.append(ind(1)).append("public void ").append(targetParentTypeName).append(".")
//      .append(dependencyMethod()).append("(").append(sourceParentTypeName).append(" source) {\n");
//    sb.append(ind(2)).append("add").append(internalRelationPrefix()).append("Source(source);\n");
//    sb.append(ind(1)).append("}\n\n");
//  }
//
//  void TokenComponent.generateAspect(StringBuilder sb) {
//    if (getDependencySourceDefinitionList().isEmpty()) { return; }
//
//    String parentTypeName = containingTypeDecl().getName();
//    // virtual setter
//    sb.append(ind(1)).append("public ").append(parentTypeName).append(" ")
//      .append(parentTypeName).append(".set").append(getName()).append("(")
//      .append(getJavaTypeUse().prettyPrint()).append(" value) {\n");
//    sb.append(ind(2)).append("set").append(internalName()).append("(value);\n");
//
//    for (DependencyDefinition dependencyDefinition : getDependencySourceDefinitionList()) {
//      String targetParentTypeName = dependencyDefinition.getTarget().containingTypeDecl().getName();
//      sb.append(ind(2)).append("for (").append(targetParentTypeName).append(" target : get")
//        .append(dependencyDefinition.internalRelationPrefix()).append("TargetList()) {\n");
//      sb.append(ind(3)).append("if (target.")
//        .append(dependencyDefinition.targetUpdateDefinition().updateMethod())
//        .append("()) {\n");
//      sb.append(ind(4)).append("target.")
//        .append(dependencyDefinition.targetUpdateDefinition().writeMethod())
//        .append("();\n");
//      sb.append(ind(3)).append("}\n");
//      sb.append(ind(2)).append("}\n");
//    }
//    sb.append(ind(2)).append("return this;\n");
//    sb.append(ind(1)).append("}\n\n");
//
//    // virtual getter
//    sb.append(ind(1)).append("public ").append(getJavaTypeUse().prettyPrint())
//      .append(" ").append(parentTypeName).append(".get").append(getName()).append("() {\n");
//    sb.append(ind(2)).append("return get").append(internalName()).append("();\n");
//    sb.append(ind(1)).append("}\n\n");
//  }
René Schöne's avatar
René Schöne committed
459
}
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497

aspect RelationGeneration {
  syn java.util.List<Relation> Ros2Rag.additionalRelations() {
    java.util.List<Relation> result = new java.util.ArrayList<>();
    for (DependencyDefinition dd : getDependencyDefinitionList()) {
      result.add(dd.getRelationToCreate());
    }
    return result;
  }

  syn nta Relation DependencyDefinition.getRelationToCreate() {
    BidirectionalRelation result = new BidirectionalRelation();
    NavigableRole left = new ListRole(internalRelationPrefix() + "Source");
    left.setType(getTarget().containingTypeDecl());
    NavigableRole right = new ListRole(internalRelationPrefix() + "Target");
    right.setType(getSource().containingTypeDecl());
    result.setLeft(left);
    result.setRight(right);
    return result;
  }
}

aspect GrammarExtension {
  refine BackendAbstractGrammar public void TokenComponent.generateAbstractGrammar(StringBuilder b) {
    if (getNTA()) {
      b.append("/");
    }
    b.append("<");
    if (!getName().equals("")) {
      b.append(internalName()).append(":");
    }
    getJavaTypeUse().generateAbstractGrammar(b);
    b.append(">");
    if (getNTA()) {
      b.append("/");
    }
  }
}