Commit adc5e0a8 authored by René Schöne's avatar René Schöne
Browse files

Moved string constants into mustache nodes.

parent 43adaf2a
aspect Analysis {
// --- lookupTokenEndpointDefinition ---
inh java.util.List<TokenEndpointDefinition> TokenEndpointDefinition.lookupTokenEndpointDefinitions(TokenComponent token);
eq RagConnect.getEndpointDefinition().lookupTokenEndpointDefinitions(TokenComponent token) {
eq RagConnect.getEndpointDefinition().lookupTokenEndpointDefinitions(TokenComponent token) = lookupTokenEndpointDefinitions(token);
syn java.util.List<TokenEndpointDefinition> RagConnect.lookupTokenEndpointDefinitions(TokenComponent token) {
java.util.List<TokenEndpointDefinition> result = new java.util.ArrayList<>();
for (EndpointDefinition def : getEndpointDefinitionList()) {
if (def.isTokenEndpointDefinition() && def.asTokenEndpointDefinition().getToken().equals(token)) {
......
......@@ -12,7 +12,8 @@ MTypeComponent;
MTokenComponent;
rel MRagConnect.RagConnect -> RagConnect;
rel MInnerMappingDefinition.MappingDefinition -> MappingDefinition;
// useless comment
rel MInnerMappingDefinition.MMappingDefinition -> MMappingDefinition;
rel MReceiveDefinition.ReceiveTokenEndpointDefinition -> ReceiveTokenEndpointDefinition;
rel MSendDefinition.SendTokenEndpointDefinition -> SendTokenEndpointDefinition;
rel MMappingDefinition.MappingDefinition -> MappingDefinition;
......
......@@ -23,60 +23,76 @@ aspect GenerationUtils {
/* Open questions
- Should all string constants be defined on the normal AST, or on the special mustache AST?
Design considerations
- InnerMappingDefinition needed for iteration attribute (first, last) - not easily possible with list-relation
*/
aspect AttributesForMustache {
// --- MRagConnect ---
eq MRagConnect.getChild().mqttHandlerAttribute() = mqttHandlerAttribute();
eq MRagConnect.getChild().restHandlerAttribute() = restHandlerAttribute();
eq MRagConnect.getChild().mqttHandlerField() = mqttHandlerField();
eq MRagConnect.getRootTypeComponent(int i).isFirst() = i == 0;
syn boolean MRagConnect.usesMqtt() = getRagConnect().usesMqtt();
syn String MRagConnect.mqttHandlerAttribute() = getRagConnect().mqttHandlerAttribute();
syn String MRagConnect.mqttHandlerField() = getRagConnect().mqttHandlerField();
syn String MRagConnect.mqttSetHostMethod() = getRagConnect().mqttSetHostMethod();
syn String MRagConnect.mqttWaitUntilReadyMethod() = getRagConnect().mqttWaitUntilReadyMethod();
syn String MRagConnect.mqttCloseMethod() = getRagConnect().mqttCloseMethod();
syn String MRagConnect.mqttHandlerAttribute() = "_mqttHandler";
syn String MRagConnect.mqttHandlerField() = "_mqttHandler";
syn String MRagConnect.mqttSetHostMethod() = "MqttSetHost";
syn String MRagConnect.mqttWaitUntilReadyMethod() = "MqttWaitUntilReady";
syn String MRagConnect.mqttCloseMethod() = "MqttCloseConnections";
syn boolean MRagConnect.usesRest() = getRagConnect().usesRest();
syn String MRagConnect.restHandlerAttribute() = getRagConnect().restHandlerAttribute();
syn String MRagConnect.restHandlerField() = getRagConnect().restHandlerField();
syn String MRagConnect.restSetPortMethod() = getRagConnect().restSetPortMethod();
syn String MRagConnect.restCloseMethod() = getRagConnect().restCloseMethod();
syn String MRagConnect.restHandlerAttribute() = "_restHandler";
syn String MRagConnect.restHandlerField() = "_restHandler";
syn String MRagConnect.restSetPortMethod() = "RestSetPort";
syn String MRagConnect.restCloseMethod() = "RestCloseConnections";
// --- MEndpointDefinition ---
syn String MEndpointDefinition.preemptiveExpectedValue();
syn String MEndpointDefinition.preemptiveReturn();
syn TokenEndpointDefinition MEndpointDefinition.endpointDef();
syn String MEndpointDefinition.firstInputVarName();
syn String MEndpointDefinition.newConnectionMethod();
eq MEndpointDefinition.getInnerMappingDefinition(int i).isLast() = i == getNumInnerMappingDefinition() - 1;
eq MEndpointDefinition.getInnerMappingDefinition().resultVarPrefix() = resultVarPrefix();
eq MEndpointDefinition.getInnerMappingDefinition(int i).inputVarName() = i == 0 ? firstInputVarName() : resultVarPrefix() + getInnerMappingDefinition(i - 1).getMappingDefinition().methodName();
eq MEndpointDefinition.getInnerMappingDefinition(int i).inputVarName() = i == 0 ? firstInputVarName() : resultVarPrefix() + getInnerMappingDefinition(i - 1).methodName();
inh String MEndpointDefinition.mqttHandlerAttribute();
inh String MEndpointDefinition.restHandlerAttribute();
syn String MEndpointDefinition.connectParameterName() = endpointDef().connectParameterName();
syn String MEndpointDefinition.newConnectionMethod() = endpointDef().newConnectionMethod();
syn String MEndpointDefinition.handlerAttribute() = endpointDef().handlerAttribute();
syn String MEndpointDefinition.connectMethod() = endpointDef().connectMethod();
syn String MEndpointDefinition.connectParameterName() {
switch (endpointDef().protocol()) {
case "mqtt": return "topic";
case "rest": return "path";
default: return null;
}
}
syn String MEndpointDefinition.handlerAttribute() {
switch (endpointDef().protocol()) {
case "mqtt": return mqttHandlerAttribute();
case "rest": return restHandlerAttribute();
default: return null;
}
}
syn String MEndpointDefinition.connectMethod() = "connect" + tokenName() + (ragconnect().lookupTokenEndpointDefinitions(token()).size() > 1 ? "Via" + capitalize(endpointDef().protocol()) : "");
syn TokenComponent MEndpointDefinition.token() = endpointDef().getToken();
syn boolean MEndpointDefinition.alwaysApply() = endpointDef().getAlwaysApply();
syn String MEndpointDefinition.resultVarPrefix() = "result"; // we do not need "_" here, because methodName begins with one
syn String MEndpointDefinition.parentTypeName() = token().containingTypeDecl().getName();
syn String MEndpointDefinition.tokenName() = token().getName();
syn MInnerMappingDefinition MEndpointDefinition.lastDefinition() = getInnerMappingDefinition(getNumInnerMappingDefinition() - 1);
syn String MEndpointDefinition.lastDefinitionToType() = lastDefinition().ToType();
syn String MEndpointDefinition.lastDefinitionToType() = lastDefinition().toType();
syn String MEndpointDefinition.lastDefinitionName() = lastDefinition().methodName();
syn String MEndpointDefinition.lastResult() = resultVarPrefix() + lastDefinitionName();
syn String MEndpointDefinition.condition() {
if (lastDefinition().getMappingDefinition().getToType().isArray()) {
if (lastDefinition().mappingDef().getToType().isArray()) {
return "java.util.Arrays.equals(" + preemptiveExpectedValue() + ", " + lastResult() + ")";
}
if (token().isPrimitiveType() && lastDefinition().getMappingDefinition().getToType().isPrimitiveType()) {
if (token().isPrimitiveType() && lastDefinition().mappingDef().getToType().isPrimitiveType()) {
return preemptiveExpectedValue() + " == " + lastResult();
}
if (lastDefinition().getMappingDefinition().isDefaultMappingDefinition()) {
if (lastDefinition().mappingDef().isDefaultMappingDefinition()) {
return preemptiveExpectedValue() + " != null && " + preemptiveExpectedValue() + ".equals(" + lastResult() + ")";
}
return preemptiveExpectedValue() + " != null ? " + preemptiveExpectedValue() + ".equals(" + lastResult() + ") : " + lastResult() + " == null";
......@@ -85,9 +101,10 @@ aspect AttributesForMustache {
// --- 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();
syn String MInnerMappingDefinition.toType() = mappingDef().getToType().prettyPrint();
syn String MInnerMappingDefinition.methodName() = getMMappingDefinition().methodName();
syn MappingDefinition MInnerMappingDefinition.mappingDef() = getMMappingDefinition().getMappingDefinition();
// --- MReceiveDefinition ---
eq MReceiveDefinition.preemptiveExpectedValue() = "get" + tokenName() + "()";
......@@ -95,34 +112,52 @@ aspect AttributesForMustache {
eq MReceiveDefinition.endpointDef() = getReceiveTokenEndpointDefinition();
eq MReceiveDefinition.firstInputVarName() = "message";
syn String MReceiveDefinition.newConnectionMethod() = getReceiveTokenEndpointDefinition().newConnectionMethod();
syn String MReceiveDefinition.connectParameterName() = getReceiveTokenEndpointDefinition().connectParameterName();
syn String MReceiveDefinition.newConnectionMethod() {
switch (endpointDef().protocol()) {
case "mqtt": return "newConnection";
case "rest": return "newPUTConnection";
default: return null;
}
}
// --- MSendDefinition ---
eq MSendDefinition.preemptiveExpectedValue() = lastValue();
eq MSendDefinition.preemptiveReturn() = getSendTokenEndpointDefinition().preemptiveReturn();
eq MSendDefinition.preemptiveReturn() {
switch (endpointDef().protocol()) {
case "mqtt": return "return false;";
case "rest": return "throw e;"; // e is Exception variable
default: return null;
}
}
eq MSendDefinition.endpointDef() = getSendTokenEndpointDefinition();
eq MSendDefinition.firstInputVarName() = "get" + tokenName() + "()";
syn String MSendDefinition.sendTopic() = getSendTokenEndpointDefinition().sendTopic();
syn String MSendDefinition.lastValue() = getSendTokenEndpointDefinition().lastValue();
syn String MSendDefinition.updateMethod() = getSendTokenEndpointDefinition().updateMethod();
syn String MSendDefinition.writeMethod() = getSendTokenEndpointDefinition().writeMethod();
syn String MSendDefinition.tokenResetMethod() = getSendTokenEndpointDefinition().tokenResetMethod();
syn String MSendDefinition.sendTopic() = "_topic_" + tokenName();
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";
syn boolean MSendDefinition.isPush() = getSendTokenEndpointDefinition().isPush();
syn String MSendDefinition.newConnectionMethod() {
switch (endpointDef().protocol()) {
case "mqtt": return null;
case "rest": return "newGETConnection";
default: return null;
}
}
// --- MMappingDefinition ---
syn String MMappingDefinition.toType() = getMappingDefinition().getToType().prettyPrint();
syn String MMappingDefinition.methodName() = getMappingDefinition().methodName();
syn String MMappingDefinition.methodName() = "_apply_" + getMappingDefinition().getID();
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.dependencyMethod() = "add" + capitalize(getDependencyDefinition().getID());
syn String MDependencyDefinition.sourceParentTypeName() = getDependencyDefinition().getSource().containingTypeDecl().getName();
syn String MDependencyDefinition.internalRelationPrefix() = getDependencyDefinition().internalRelationPrefix();
syn String MDependencyDefinition.internalRelationPrefix() = "_internal_" + getDependencyDefinition().getID();
syn nta MSendDefinition MDependencyDefinition.targetEndpointDefinition() {
return getDependencyDefinition().targetEndpointDefinition().toMustache();
}
......@@ -137,7 +172,8 @@ aspect AttributesForMustache {
syn String MTokenComponent.parentTypeName() = getTokenComponent().containingTypeDecl().getName();
syn String MTokenComponent.name() = getTokenComponent().getName();
syn String MTokenComponent.javaType() = getTokenComponent().effectiveJavaTypeUse().prettyPrint();
syn String MTokenComponent.internalName() = getTokenComponent().internalName();
syn String MTokenComponent.internalName() = getTokenComponent().getDependencySourceDefinitionList().isEmpty() ? externalName() : "_internal_" + name();
syn String MTokenComponent.externalName() = name();
// --- toMustache ---
syn lazy MRagConnect RagConnect.toMustache() {
......@@ -178,7 +214,7 @@ aspect AttributesForMustache {
protected void MEndpointDefinition.addInnerMappings() {
for (MappingDefinition def : endpointDef().effectiveMappings()) {
MInnerMappingDefinition inner = new MInnerMappingDefinition();
inner.setMappingDefinition(def);
inner.setMMappingDefinition(def.toMustache());
addInnerMappingDefinition(inner);
}
}
......@@ -220,74 +256,11 @@ aspect AttributesForMustache {
}
aspect AspectGeneration {
// naming convention attributes
syn String TokenComponent.internalName() = getDependencySourceDefinitionList().isEmpty() ? externalName() : "_internal_" + getName();
syn String TokenComponent.externalName() = getName();
syn String TokenEndpointDefinition.connectMethod() = "connect" + getToken().getName() + (lookupTokenEndpointDefinitions(getToken()).size() > 1 ? "Via" + capitalize(protocol()) : "");
syn String SendTokenEndpointDefinition.sendTopic() = "_topic_" + getToken().getName();
syn String SendTokenEndpointDefinition.lastValue() = "_lastValue" + getToken().getName();
syn String SendTokenEndpointDefinition.updateMethod() = "_update_" + getToken().getName();
syn String SendTokenEndpointDefinition.writeMethod() = "_writeLastValue_" + getToken().getName();
syn String SendTokenEndpointDefinition.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();
syn String DependencyDefinition.internalTokenName() = getSource().internalName();
// -- MQTT names --
syn String RagConnect.mqttHandlerAttribute() = "_mqttHandler";
syn String RagConnect.mqttHandlerField() = "_mqttHandler";
syn String RagConnect.mqttSetHostMethod() = "MqttSetHost";
syn String RagConnect.mqttWaitUntilReadyMethod() = "MqttWaitUntilReady";
syn String RagConnect.mqttCloseMethod() = "MqttCloseConnections";
// -- REST names --
syn String RagConnect.restSetPortMethod() = "RestSetPort";
syn String RagConnect.restCloseMethod() = "RestCloseConnections";
syn String RagConnect.restHandlerAttribute() = "_restHandler";
syn String RagConnect.restHandlerField() = "_restHandler";
// -- endpoint names --
syn String TokenEndpointDefinition.handlerAttribute();
eq ReceiveFromMqttDefinition.handlerAttribute() = mqttHandlerAttribute();
eq SendToMqttDefinition.handlerAttribute() = mqttHandlerAttribute();
eq ReceiveFromRestDefinition.handlerAttribute() = restHandlerAttribute();
eq SendToRestDefinition.handlerAttribute() = restHandlerAttribute();
syn String TokenEndpointDefinition.newConnectionMethod();
eq ReceiveFromMqttDefinition.newConnectionMethod() = "newConnection";
eq SendToMqttDefinition.newConnectionMethod() = null;
eq ReceiveFromRestDefinition.newConnectionMethod() = "newPUTConnection";
eq SendToRestDefinition.newConnectionMethod() = "newGETConnection";
syn String TokenEndpointDefinition.connectParameterName();
eq ReceiveFromMqttDefinition.connectParameterName() = "topic";
eq SendToMqttDefinition.connectParameterName() = "topic";
eq ReceiveFromRestDefinition.connectParameterName() = "path";
eq SendToRestDefinition.connectParameterName() = "path";
syn String SendTokenEndpointDefinition.preemptiveReturn();
eq SendToMqttDefinition.preemptiveReturn() = "return false;";
eq SendToRestDefinition.preemptiveReturn() = "throw e;"; // e is Exception variable
syn boolean SendTokenEndpointDefinition.isPush();
eq SendToMqttDefinition.isPush() = true;
eq SendToRestDefinition.isPush() = false;
// naming copy attributes
// --- mqttHandlerAttribute ---
inh String EndpointDefinition.mqttHandlerAttribute();
eq RagConnect.getChild().mqttHandlerAttribute() = mqttHandlerAttribute();
// --- restHandlerAttribute ---
inh String EndpointDefinition.restHandlerAttribute();
eq RagConnect.getChild().restHandlerAttribute() = restHandlerAttribute();
// --- rootNodeName ---
syn String ASTNode.rootNodeName() = rootNode.getName();
......@@ -331,10 +304,11 @@ aspect RelationGeneration {
}
syn nta Relation DependencyDefinition.getRelationToCreate() {
String internalRelationPrefix = toMustache().internalRelationPrefix();
BidirectionalRelation result = new BidirectionalRelation();
NavigableRole left = new ListRole(internalRelationPrefix() + "Source");
NavigableRole left = new ListRole(internalRelationPrefix + "Source");
left.setType(getTarget().containingTypeDecl());
NavigableRole right = new ListRole(internalRelationPrefix() + "Target");
NavigableRole right = new ListRole(internalRelationPrefix + "Target");
right.setType(getSource().containingTypeDecl());
result.setLeft(left);
result.setRight(right);
......@@ -350,7 +324,7 @@ aspect GrammarExtension {
}
b.append("<");
if (!getName().equals("")) {
b.append(internalName()).append(":");
b.append(toMustache().internalName()).append(":");
}
effectiveJavaTypeUse().generateAbstractGrammar(b);
b.append(">");
......
{{lastDefinitionToType}} {{resultVarPrefix}}{{lastDefinitionName}};
try {
{{#InnerMappingDefinitions}}
{{^last}}{{ToType}} {{/last}}{{resultVarPrefix}}{{methodName}} = {{methodName}}({{inputVarName}});
{{^last}}{{toType}} {{/last}}{{resultVarPrefix}}{{methodName}} = {{methodName}}({{inputVarName}});
{{/InnerMappingDefinitions}}
} catch (Exception e) {
e.printStackTrace();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment