Generation.jadd 12.4 KB
Newer Older
René Schöne's avatar
Cleanup    
René Schöne committed
1
/*
2
3
Design considerations
- InnerMappingDefinition needed for iteration attribute (first, last) - not easily possible with list-relation
René Schöne's avatar
René Schöne committed
4
5
6
*/

aspect AttributesForMustache {
7
8
  // --- MRagConnect ---
  eq MRagConnect.getRootTypeComponent(int i).isFirst() = i == 0;
René Schöne's avatar
René Schöne committed
9

10
  syn String MRagConnect.closeMethod() = "ragconnectCloseConnections";
11
12
  syn String MRagConnect.mqttHandlerAttribute() = "_mqttHandler";
  syn String MRagConnect.mqttHandlerField() = "_mqttHandler";
13
  syn String MRagConnect.mqttSetupWaitUntilReadyMethod() = "ragconnectSetupMqttWaitUntilReady";
René Schöne's avatar
René Schöne committed
14

15
16
  syn String MRagConnect.restHandlerAttribute() = "_restHandler";
  syn String MRagConnect.restHandlerField() = "_restHandler";
René Schöne's avatar
René Schöne committed
17

18
19
20
21
22
23
24
  // --- MEndpointDefinition ---
  syn String MEndpointDefinition.preemptiveExpectedValue();
  syn String MEndpointDefinition.preemptiveReturn();
  syn TokenEndpointDefinition MEndpointDefinition.endpointDef();
  syn String MEndpointDefinition.firstInputVarName();

  eq MEndpointDefinition.getInnerMappingDefinition(int i).isLast() = i == getNumInnerMappingDefinition() - 1;
René Schöne's avatar
René Schöne committed
25
  eq MEndpointDefinition.getInnerMappingDefinition(int i).inputVarName() = i == 0 ? firstInputVarName() : getInnerMappingDefinition(i - 1).outputVarName();
26

27
28
  syn String MEndpointDefinition.connectParameterName() = "uriString";
  syn String MEndpointDefinition.connectMethod() = "connect" + tokenName();
29
30
31
32
33
  syn TokenComponent MEndpointDefinition.token() = endpointDef().getToken();
  syn boolean MEndpointDefinition.alwaysApply() = endpointDef().getAlwaysApply();
  syn String MEndpointDefinition.parentTypeName() = token().containingTypeDecl().getName();
  syn String MEndpointDefinition.tokenName() = token().getName();
  syn MInnerMappingDefinition MEndpointDefinition.lastDefinition() = getInnerMappingDefinition(getNumInnerMappingDefinition() - 1);
34
  syn String MEndpointDefinition.lastDefinitionToType() = lastDefinition().toType();
René Schöne's avatar
René Schöne committed
35
  syn String MEndpointDefinition.lastResult() = lastDefinition().outputVarName();
36
  syn String MEndpointDefinition.condition() {
37
    if (lastDefinition().mappingDef().getToType().isArray()) {
38
      return "java.util.Arrays.equals(" + preemptiveExpectedValue() + ", " + lastResult() + ")";
René Schöne's avatar
René Schöne committed
39
    }
40
    if (token().isPrimitiveType() && lastDefinition().mappingDef().getToType().isPrimitiveType()) {
41
      return preemptiveExpectedValue() + " == " + lastResult();
René Schöne's avatar
René Schöne committed
42
    }
43
    if (lastDefinition().mappingDef().isDefaultMappingDefinition()) {
44
      return preemptiveExpectedValue() + " != null && " + preemptiveExpectedValue() + ".equals(" + lastResult() + ")";
René Schöne's avatar
René Schöne committed
45
    }
46
    return preemptiveExpectedValue() + " != null ? " + preemptiveExpectedValue() + ".equals(" + lastResult() + ") : " + lastResult() + " == null";
René Schöne's avatar
René Schöne committed
47
48
49
50
51
  }

  // --- MInnerMappingDefinition ---
  inh boolean MInnerMappingDefinition.isLast();
  inh String MInnerMappingDefinition.inputVarName();
52
53
54
  syn String MInnerMappingDefinition.toType() = mappingDef().getToType().prettyPrint();
  syn String MInnerMappingDefinition.methodName() = getMMappingDefinition().methodName();
  syn MappingDefinition MInnerMappingDefinition.mappingDef() = getMMappingDefinition().getMappingDefinition();
René Schöne's avatar
René Schöne committed
55
  syn String MInnerMappingDefinition.outputVarName() = "result" + methodName();  // we do not need "_" in between here, because methodName begins with one
René Schöne's avatar
René Schöne committed
56

57
58
59
  // --- MReceiveDefinition ---
  eq MReceiveDefinition.preemptiveExpectedValue() = "get" + tokenName() + "()";
  eq MReceiveDefinition.preemptiveReturn() = "return;";
René Schöne's avatar
René Schöne committed
60
  eq MReceiveDefinition.endpointDef() = getReceiveTokenEndpointDefinition();
61
62
63
64
  eq MReceiveDefinition.firstInputVarName() = "message";

  // --- MSendDefinition ---
  eq MSendDefinition.preemptiveExpectedValue() = lastValue();
65
  eq MSendDefinition.preemptiveReturn() = "return false;";
René Schöne's avatar
René Schöne committed
66
  eq MSendDefinition.endpointDef() = getSendTokenEndpointDefinition();
67
68
  eq MSendDefinition.firstInputVarName() = "get" + tokenName() + "()";

69
  syn String MSendDefinition.sender() = "_sender_" + tokenName();
70
71
72
73
  syn String MSendDefinition.lastValue() = "_lastValue" + tokenName();
  syn String MSendDefinition.updateMethod() = "_update_" + tokenName();
  syn String MSendDefinition.writeMethod() = "_writeLastValue_" + tokenName();
  syn String MSendDefinition.tokenResetMethod() = "get" + tokenName() + "_reset";
René Schöne's avatar
René Schöne committed
74
  syn boolean MSendDefinition.shouldSendValue() = endpointDef().shouldSendValue();
René Schöne's avatar
René Schöne committed
75
76
77

  // --- MMappingDefinition ---
  syn String MMappingDefinition.toType() = getMappingDefinition().getToType().prettyPrint();
78
  syn String MMappingDefinition.methodName() = "_apply_" + getMappingDefinition().getID();
René Schöne's avatar
René Schöne committed
79
80
81
82
83
84
  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();
85
  syn String MDependencyDefinition.dependencyMethod() = "add" + capitalize(getDependencyDefinition().getID());
René Schöne's avatar
René Schöne committed
86
  syn String MDependencyDefinition.sourceParentTypeName() = getDependencyDefinition().getSource().containingTypeDecl().getName();
87
  syn String MDependencyDefinition.internalRelationPrefix() = "_internal_" + getDependencyDefinition().getID();
René Schöne's avatar
René Schöne committed
88
  syn nta MSendDefinition MDependencyDefinition.targetEndpointDefinition() {
89
    return getDependencyDefinition().targetEndpointDefinition().toMustache();
René Schöne's avatar
René Schöne committed
90
91
92
93
  }

  // --- MTypeComponent ---
  syn String MTypeComponent.name() = getTypeComponent().getName();
94
  inh boolean MTypeComponent.isFirst();
95
96
97
98

  // --- MTokenComponent ---
  syn String MTokenComponent.parentTypeName() = getTokenComponent().containingTypeDecl().getName();
  syn String MTokenComponent.name() = getTokenComponent().getName();
René Schöne's avatar
René Schöne committed
99
  syn String MTokenComponent.javaType() = getTokenComponent().effectiveJavaTypeUse().prettyPrint();
René Schöne's avatar
René Schöne committed
100
  syn String MTokenComponent.internalName() = getTokenComponent().needProxyToken() ? "_internal_" + name() : externalName();
101
  syn String MTokenComponent.externalName() = name();
René Schöne's avatar
René Schöne committed
102
103
104
105
106
107
108
109
  syn MSendDefinition MTokenComponent.normalTokenSendDef() {
    for (TokenEndpointDefinition endpointDef : getTokenComponent().getTokenEndpointDefinitionList()) {
      if (endpointDef.shouldSendValue()) {
        return endpointDef.asSendTokenEndpointDefinition().toMustache();
      }
    }
    return null;
  }
René Schöne's avatar
René Schöne committed
110

111
  // --- toMustache ---
112
113
114
  syn lazy MRagConnect RagConnect.toMustache() {
    MRagConnect result = new MRagConnect();
    result.setRagConnect(this);
115
    for (EndpointDefinition def : getEndpointDefinitionList()) {
René Schöne's avatar
René Schöne committed
116
      if (def.isSendTokenEndpointDefinition()) {
René Schöne's avatar
René Schöne committed
117
        SendTokenEndpointDefinition sendDef = def.asSendTokenEndpointDefinition();
118
        result.addSendDefinition(sendDef.toMustache());
René Schöne's avatar
René Schöne committed
119
      } else {
René Schöne's avatar
René Schöne committed
120
        result.addReceiveDefinition(def.asReceiveTokenEndpointDefinition().toMustache());
René Schöne's avatar
René Schöne committed
121
122
123
      }
    }
    for (MappingDefinition def : allMappingDefinitions()) {
124
      result.addMappingDefinition(def.toMustache());
René Schöne's avatar
René Schöne committed
125
126
    }
    for (DependencyDefinition def : getDependencyDefinitionList()) {
127
      result.addDependencyDefinition(def.toMustache());
René Schöne's avatar
René Schöne committed
128
129
    }
    for (TokenComponent token : getProgram().allTokenComponents()) {
René Schöne's avatar
René Schöne committed
130
      if (token.needProxyToken()) {
René Schöne's avatar
René Schöne committed
131
132
        result.addTokenComponent(token.toMustache());
      }
133
134
135
    }
    for (Component child : rootNode.getComponentList()) {
      if (child.isTypeComponent()) {
René Schöne's avatar
René Schöne committed
136
        result.addRootTypeComponent(child.asTypeComponent().toMustache());
137
      }
René Schöne's avatar
René Schöne committed
138
    }
René Schöne's avatar
René Schöne committed
139
140
141
142
143
    // MHandler ::= <ClassName> <Construction> <AttributeName> <FieldName> <InUse:boolean>;
    result.addHandler(new MHandler("MqttServerHandler", "new MqttServerHandler(\"RagConnectMQTT\")",
                                   result.mqttHandlerAttribute(), result.mqttHandlerField(), usesMqtt));
    result.addHandler(new MHandler("RestServerHandler", "new RestServerHandler(\"RagConnectREST\")",
                                   result.restHandlerAttribute(), result.restHandlerField(), usesRest));
René Schöne's avatar
René Schöne committed
144
145
    return result;
  }
146

147
148
  protected void MEndpointDefinition.addInnerMappings() {
    for (MappingDefinition def : endpointDef().effectiveMappings()) {
149
      MInnerMappingDefinition inner = new MInnerMappingDefinition();
150
      inner.setMMappingDefinition(def.toMustache());
151
      addInnerMappingDefinition(inner);
René Schöne's avatar
René Schöne committed
152
153
    }
  }
René Schöne's avatar
René Schöne committed
154

René Schöne's avatar
René Schöne committed
155
  syn lazy MReceiveDefinition ReceiveTokenEndpointDefinition.toMustache() {
156
    MReceiveDefinition result = new MReceiveDefinition();
René Schöne's avatar
René Schöne committed
157
158
159
160
    result.setReceiveTokenEndpointDefinition(this);
    result.addInnerMappings();
    return result;
  }
René Schöne's avatar
René Schöne committed
161

René Schöne's avatar
René Schöne committed
162
  syn lazy MSendDefinition SendTokenEndpointDefinition.toMustache() {
163
    MSendDefinition result = new MSendDefinition();
René Schöne's avatar
René Schöne committed
164
    result.setSendTokenEndpointDefinition(this);
René Schöne's avatar
René Schöne committed
165
166
167
    result.addInnerMappings();
    return result;
  }
René Schöne's avatar
René Schöne committed
168

169
  syn lazy MMappingDefinition MappingDefinition.toMustache() {
René Schöne's avatar
René Schöne committed
170
171
172
173
    MMappingDefinition result = new MMappingDefinition();
    result.setMappingDefinition(this);
    return result;
  }
René Schöne's avatar
René Schöne committed
174

175
  syn lazy MDependencyDefinition DependencyDefinition.toMustache() {
René Schöne's avatar
René Schöne committed
176
177
178
179
    MDependencyDefinition result = new MDependencyDefinition();
    result.setDependencyDefinition(this);
    return result;
  }
René Schöne's avatar
René Schöne committed
180

181
  syn lazy MTypeComponent TypeComponent.toMustache() {
René Schöne's avatar
René Schöne committed
182
183
    MTypeComponent result = new MTypeComponent();
    result.setTypeComponent(this);
184
185
    return result;
  }
René Schöne's avatar
René Schöne committed
186

187
188
189
  syn lazy MTokenComponent TokenComponent.toMustache() {
    MTokenComponent result = new MTokenComponent();
    result.setTokenComponent(this);
René Schöne's avatar
René Schöne committed
190
    for (DependencyDefinition def : getDependencySourceDefinitionList()) {
191
      result.addDependencyDefinition(def.toMustache());
René Schöne's avatar
René Schöne committed
192
193
194
195
196
    }
    return result;
  }
}

René Schöne's avatar
René Schöne committed
197
aspect AspectGeneration {
René Schöne's avatar
René Schöne committed
198
199
  // --- rootNodeName ---
  syn String ASTNode.rootNodeName() = rootNode.getName();
René Schöne's avatar
René Schöne committed
200

201
  public String RagConnect.generateAspect(String rootNodeName) {
René Schöne's avatar
René Schöne committed
202
    rootNode = getProgram().resolveTypeDecl(rootNodeName);
René Schöne's avatar
René Schöne committed
203
204
205
    return toMustache().generateAspect();
  }

206
  public String MRagConnect.generateAspect() {
René Schöne's avatar
René Schöne committed
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
    StringBuilder sb = new StringBuilder();
    com.github.mustachejava.reflect.ReflectionObjectHandler roh = new com.github.mustachejava.reflect.ReflectionObjectHandler() {
      @Override
      public com.github.mustachejava.Binding createBinding(String name, final com.github.mustachejava.TemplateContext tc, com.github.mustachejava.Code code) {
        return new com.github.mustachejava.reflect.GuardedBinding(this, name, tc, code) {
          @Override
          protected synchronized com.github.mustachejava.util.Wrapper getWrapper(String name, java.util.List<Object> scopes) {
            com.github.mustachejava.util.Wrapper wrapper = super.getWrapper(name, scopes);
            if (wrapper instanceof com.github.mustachejava.reflect.MissingWrapper) {
              throw new com.github.mustachejava.MustacheException(name + " not found in " + tc);
            }
            return wrapper;
          }
        };
      }
    };
    com.github.mustachejava.DefaultMustacheFactory mf = new com.github.mustachejava.DefaultMustacheFactory();
    mf.setObjectHandler(roh);
225
226
    com.github.mustachejava.Mustache m = mf.compile("ragconnect.mustache");
    m.execute(new java.io.PrintWriter(new org.jastadd.ragconnect.compiler.AppendableWriter(sb)), this);
227
    return sb.toString();
René Schöne's avatar
René Schöne committed
228
229
  }
}
230
231

aspect RelationGeneration {
232
  syn java.util.List<Relation> RagConnect.additionalRelations() {
233
234
235
236
237
238
239
240
    java.util.List<Relation> result = new java.util.ArrayList<>();
    for (DependencyDefinition dd : getDependencyDefinitionList()) {
      result.add(dd.getRelationToCreate());
    }
    return result;
  }

  syn nta Relation DependencyDefinition.getRelationToCreate() {
241
    String internalRelationPrefix = toMustache().internalRelationPrefix();
242
    BidirectionalRelation result = new BidirectionalRelation();
243
    NavigableRole left = new ListRole(internalRelationPrefix + "Source");
244
    left.setType(getTarget().containingTypeDecl());
245
    NavigableRole right = new ListRole(internalRelationPrefix + "Target");
246
247
248
    right.setType(getSource().containingTypeDecl());
    result.setLeft(left);
    result.setRight(right);
249
    result.addComment(new WhitespaceComment("\n"));
250
251
252
253
254
255
256
257
258
259
260
    return result;
  }
}

aspect GrammarExtension {
  refine BackendAbstractGrammar public void TokenComponent.generateAbstractGrammar(StringBuilder b) {
    if (getNTA()) {
      b.append("/");
    }
    b.append("<");
    if (!getName().equals("")) {
261
      b.append(toMustache().internalName()).append(":");
262
    }
René Schöne's avatar
René Schöne committed
263
    effectiveJavaTypeUse().generateAbstractGrammar(b);
264
265
266
267
268
269
    b.append(">");
    if (getNTA()) {
      b.append("/");
    }
  }
}