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

Begin with REST impl.

- also fixed bug when using tokens with no type (must be treated implicitly as String)
parent 199d5bcb
Pipeline #7540 failed with stages
in 1 minute and 33 seconds
...@@ -7,32 +7,32 @@ aspect Errors { ...@@ -7,32 +7,32 @@ aspect Errors {
[new TreeSet<ErrorMessage>()] [new TreeSet<ErrorMessage>()]
root RagConnect; root RagConnect;
ReceiveFromMqttDefinition contributes error("Receive definition already defined for " + getToken().getName()) ReceiveTokenEndpointDefinition contributes error("Receive definition already defined for " + getToken().getName())
when isAlreadyDefined() when isAlreadyDefined()
to RagConnect.errors(); to RagConnect.errors();
ReceiveFromMqttDefinition contributes error("Receiving target token must not be an NTA token!") ReceiveTokenEndpointDefinition contributes error("Receiving target token must not be an NTA token!")
when getToken().getNTA() when getToken().getNTA()
to RagConnect.errors(); to RagConnect.errors();
// if first mapping is null, then suitableDefaultMapping() == null // if first mapping is null, then suitableDefaultMapping() == null
ReceiveFromMqttDefinition contributes error("No suitable default mapping found for type " + ReceiveTokenEndpointDefinition contributes error("No suitable default mapping found for type " +
((getMappingList().isEmpty()) ((getMappingList().isEmpty())
? getToken().getJavaTypeUse().prettyPrint() ? getToken().effectiveJavaTypeUse().prettyPrint()
: getMappingList().get(0).getFromType().prettyPrint())) : getMappingList().get(0).getFromType().prettyPrint()))
when effectiveMappings().get(0) == null when effectiveMappings().get(0) == null
to RagConnect.errors(); to RagConnect.errors();
ReceiveFromMqttDefinition contributes error("to-type of last mapping must be type of the Token!") ReceiveTokenEndpointDefinition contributes error("to-type of last mapping (" + effectiveMappings().get(effectiveMappings().size() - 1).getToType().prettyPrint() + ") does not match type of the Token (" + getToken().effectiveJavaTypeUse().prettyPrint() + ")!")
when getToken().getJavaTypeUse().prettyPrint().equals( when !getToken().effectiveJavaTypeUse().prettyPrint().equals(
effectiveMappings().get(effectiveMappings().size() - 1)) effectiveMappings().get(effectiveMappings().size() - 1).getToType().prettyPrint())
to RagConnect.errors(); to RagConnect.errors();
SendToMqttDefinition contributes error("Sending target token must be an NTA token!") SendTokenEndpointDefinition contributes error("Sending target token must be an NTA token!")
when !getToken().getNTA() when !getToken().getNTA()
to RagConnect.errors(); to RagConnect.errors();
SendToMqttDefinition contributes error("Send definition already defined for " + getToken().getName()) SendTokenEndpointDefinition contributes error("Send definition already defined for " + getToken().getName())
when isAlreadyDefined() when isAlreadyDefined()
to RagConnect.errors(); to RagConnect.errors();
...@@ -44,7 +44,7 @@ aspect Errors { ...@@ -44,7 +44,7 @@ aspect Errors {
when isAlreadyDefinedAsList() when isAlreadyDefinedAsList()
to RagConnect.errors(); to RagConnect.errors();
DependencyDefinition contributes error("There must be a send update definition targeting " + getSource().parentTypeypeAndName() + " for dependency definition " + getID()) DependencyDefinition contributes error("There must be a send endpoint definition targeting " + getSource().parentTypeypeAndName() + " for dependency definition " + getID())
when targetEndpointDefinition() == null when targetEndpointDefinition() == null
to RagConnect.errors(); to RagConnect.errors();
} }
......
...@@ -13,8 +13,8 @@ MTokenComponent; ...@@ -13,8 +13,8 @@ MTokenComponent;
rel MRagConnect.RagConnect -> RagConnect; rel MRagConnect.RagConnect -> RagConnect;
rel MInnerMappingDefinition.MappingDefinition -> MappingDefinition; rel MInnerMappingDefinition.MappingDefinition -> MappingDefinition;
rel MReceiveDefinition.ReceiveFromMqttDefinition -> ReceiveFromMqttDefinition; rel MReceiveDefinition.ReceiveTokenEndpointDefinition -> ReceiveTokenEndpointDefinition;
rel MSendDefinition.SendToMqttDefinition -> SendToMqttDefinition; rel MSendDefinition.SendTokenEndpointDefinition -> SendTokenEndpointDefinition;
rel MMappingDefinition.MappingDefinition -> MappingDefinition; rel MMappingDefinition.MappingDefinition -> MappingDefinition;
rel MDependencyDefinition.DependencyDefinition -> DependencyDefinition; rel MDependencyDefinition.DependencyDefinition -> DependencyDefinition;
rel MTypeComponent.TypeComponent -> TypeComponent; rel MTypeComponent.TypeComponent -> TypeComponent;
......
...@@ -10,7 +10,9 @@ aspect Navigation { ...@@ -10,7 +10,9 @@ aspect Navigation {
eq MRagConnect.getChild().ragconnect() = getRagConnect(); eq MRagConnect.getChild().ragconnect() = getRagConnect();
// --- containedFile // --- containedFile
eq Grammar.getChild().containedFile() = null;
eq RagConnect.getChild().containedFile() = null; eq RagConnect.getChild().containedFile() = null;
eq MRagConnect.getChild().containedFile() = null;
// --- containedFileName --- // --- containedFileName ---
eq RagConnect.getChild().containedFileName() = getFileName(); eq RagConnect.getChild().containedFileName() = getFileName();
...@@ -24,30 +26,33 @@ aspect Navigation { ...@@ -24,30 +26,33 @@ aspect Navigation {
syn TokenEndpointDefinition EndpointDefinition.asTokenEndpointDefinition() = null; syn TokenEndpointDefinition EndpointDefinition.asTokenEndpointDefinition() = null;
eq TokenEndpointDefinition.asTokenEndpointDefinition() = this; eq TokenEndpointDefinition.asTokenEndpointDefinition() = this;
// --- isSendToMqttDefinition --- // --- isSendTokenEndpointDefinition ---
syn boolean EndpointDefinition.isSendToMqttDefinition() = false; syn boolean EndpointDefinition.isSendTokenEndpointDefinition() = false;
eq SendToMqttDefinition.isSendToMqttDefinition() = true; eq SendTokenEndpointDefinition.isSendTokenEndpointDefinition() = true;
// --- asSendToMqttDefinition --- // --- asSendTokenEndpointDefinition ---
syn SendToMqttDefinition EndpointDefinition.asSendToMqttDefinition() = null; syn SendTokenEndpointDefinition EndpointDefinition.asSendTokenEndpointDefinition() = null;
eq SendToMqttDefinition.asSendToMqttDefinition() = this; eq SendTokenEndpointDefinition.asSendTokenEndpointDefinition() = this;
// --- asReceiveFromMqttDefinition --- // --- asReceiveTokenEndpointDefinition ---
syn ReceiveFromMqttDefinition EndpointDefinition.asReceiveFromMqttDefinition() = null; syn ReceiveTokenEndpointDefinition EndpointDefinition.asReceiveTokenEndpointDefinition() = null;
eq ReceiveFromMqttDefinition.asReceiveFromMqttDefinition() = this; eq ReceiveTokenEndpointDefinition.asReceiveTokenEndpointDefinition() = this;
// --- targetEndpointDefinition --- // --- targetEndpointDefinition ---
syn SendToMqttDefinition DependencyDefinition.targetEndpointDefinition() { syn SendTokenEndpointDefinition DependencyDefinition.targetEndpointDefinition() {
// resolve definition in here, as we do not need resolveMethod in any other place (yet) // resolve definition in here, as we do not need resolveMethod in any other place (yet)
for (EndpointDefinition endpointDefinition : ragconnect().getEndpointDefinitionList()) { for (EndpointDefinition endpointDefinition : ragconnect().getEndpointDefinitionList()) {
if (endpointDefinition.isSendToMqttDefinition() && if (endpointDefinition.isSendTokenEndpointDefinition() &&
endpointDefinition.asSendToMqttDefinition().getToken().equals(this.getTarget())) { endpointDefinition.asSendTokenEndpointDefinition().getToken().equals(this.getTarget())) {
return endpointDefinition.asSendToMqttDefinition(); return endpointDefinition.asSendTokenEndpointDefinition();
} }
} }
return null; return null;
} }
// --- effectiveJavaTypeUse (should be in preprocessor) ---
syn lazy JavaTypeUse TokenComponent.effectiveJavaTypeUse() = hasJavaTypeUse() ? getJavaTypeUse() : new SimpleJavaTypeUse("String");
// --- isDefaultMappingDefinition --- // --- isDefaultMappingDefinition ---
syn boolean MappingDefinition.isDefaultMappingDefinition() = false; syn boolean MappingDefinition.isDefaultMappingDefinition() = false;
eq DefaultMappingDefinition.isDefaultMappingDefinition() = true; eq DefaultMappingDefinition.isDefaultMappingDefinition() = true;
......
...@@ -2,13 +2,18 @@ RagConnect ::= EndpointDefinition* DependencyDefinition* MappingDefinition* Prog ...@@ -2,13 +2,18 @@ RagConnect ::= EndpointDefinition* DependencyDefinition* MappingDefinition* Prog
abstract EndpointDefinition ::= <AlwaysApply:boolean> ; abstract EndpointDefinition ::= <AlwaysApply:boolean> ;
rel EndpointDefinition.Mapping* -> MappingDefinition; rel EndpointDefinition.Mapping* <-> MappingDefinition.UsedAt*;
abstract TokenEndpointDefinition : EndpointDefinition; abstract TokenEndpointDefinition : EndpointDefinition;
rel TokenEndpointDefinition.Token -> TokenComponent; rel TokenEndpointDefinition.Token -> TokenComponent;
ReceiveFromMqttDefinition : TokenEndpointDefinition; abstract ReceiveTokenEndpointDefinition : TokenEndpointDefinition;
SendToMqttDefinition : TokenEndpointDefinition; abstract SendTokenEndpointDefinition : TokenEndpointDefinition;
ReceiveFromMqttDefinition : ReceiveTokenEndpointDefinition;
SendToMqttDefinition : SendTokenEndpointDefinition;
ReceiveFromRestDefinition : ReceiveTokenEndpointDefinition;
SendToRestDefinition : SendTokenEndpointDefinition;
DependencyDefinition ::= <ID>; DependencyDefinition ::= <ID>;
rel DependencyDefinition.Source <-> TokenComponent.DependencySourceDefinition*; rel DependencyDefinition.Source <-> TokenComponent.DependencySourceDefinition*;
......
...@@ -37,6 +37,8 @@ aspect AttributesForMustache { ...@@ -37,6 +37,8 @@ aspect AttributesForMustache {
syn String MRagConnect.mqttWaitUntilReadyMethod() = getRagConnect().mqttWaitUntilReadyMethod(); syn String MRagConnect.mqttWaitUntilReadyMethod() = getRagConnect().mqttWaitUntilReadyMethod();
syn String MRagConnect.mqttCloseMethod() = getRagConnect().mqttCloseMethod(); syn String MRagConnect.mqttCloseMethod() = getRagConnect().mqttCloseMethod();
syn String MRagConnect.restHandlerAttribute() = getRagConnect().restHandlerAttribute();
// --- MEndpointDefinition --- // --- MEndpointDefinition ---
syn String MEndpointDefinition.preemptiveExpectedValue(); syn String MEndpointDefinition.preemptiveExpectedValue();
syn String MEndpointDefinition.preemptiveReturn(); syn String MEndpointDefinition.preemptiveReturn();
...@@ -49,6 +51,7 @@ aspect AttributesForMustache { ...@@ -49,6 +51,7 @@ aspect AttributesForMustache {
inh String MEndpointDefinition.mqttHandlerAttribute(); inh String MEndpointDefinition.mqttHandlerAttribute();
syn String MEndpointDefinition.handlerAttribute() = endpointDef().handlerAttribute();
syn String MEndpointDefinition.connectMethod() = endpointDef().connectMethod(); syn String MEndpointDefinition.connectMethod() = endpointDef().connectMethod();
syn TokenComponent MEndpointDefinition.token() = endpointDef().getToken(); syn TokenComponent MEndpointDefinition.token() = endpointDef().getToken();
syn boolean MEndpointDefinition.alwaysApply() = endpointDef().getAlwaysApply(); syn boolean MEndpointDefinition.alwaysApply() = endpointDef().getAlwaysApply();
...@@ -82,20 +85,20 @@ aspect AttributesForMustache { ...@@ -82,20 +85,20 @@ aspect AttributesForMustache {
// --- MReceiveDefinition --- // --- MReceiveDefinition ---
eq MReceiveDefinition.preemptiveExpectedValue() = "get" + tokenName() + "()"; eq MReceiveDefinition.preemptiveExpectedValue() = "get" + tokenName() + "()";
eq MReceiveDefinition.preemptiveReturn() = "return;"; eq MReceiveDefinition.preemptiveReturn() = "return;";
eq MReceiveDefinition.endpointDef() = getReceiveFromMqttDefinition(); eq MReceiveDefinition.endpointDef() = getReceiveTokenEndpointDefinition();
eq MReceiveDefinition.firstInputVarName() = "message"; eq MReceiveDefinition.firstInputVarName() = "message";
// --- MSendDefinition --- // --- MSendDefinition ---
eq MSendDefinition.preemptiveExpectedValue() = lastValue(); eq MSendDefinition.preemptiveExpectedValue() = lastValue();
eq MSendDefinition.preemptiveReturn() = "return false;"; eq MSendDefinition.preemptiveReturn() = "return false;";
eq MSendDefinition.endpointDef() = getSendToMqttDefinition(); eq MSendDefinition.endpointDef() = getSendTokenEndpointDefinition();
eq MSendDefinition.firstInputVarName() = "get" + tokenName() + "()"; eq MSendDefinition.firstInputVarName() = "get" + tokenName() + "()";
syn String MSendDefinition.sendTopic() = getSendToMqttDefinition().sendTopic(); syn String MSendDefinition.sendTopic() = getSendTokenEndpointDefinition().sendTopic();
syn String MSendDefinition.lastValue() = getSendToMqttDefinition().lastValue(); syn String MSendDefinition.lastValue() = getSendTokenEndpointDefinition().lastValue();
syn String MSendDefinition.updateMethod() = getSendToMqttDefinition().updateMethod(); syn String MSendDefinition.updateMethod() = getSendTokenEndpointDefinition().updateMethod();
syn String MSendDefinition.writeMethod() = getSendToMqttDefinition().writeMethod(); syn String MSendDefinition.writeMethod() = getSendTokenEndpointDefinition().writeMethod();
syn String MSendDefinition.tokenResetMethod() = getSendToMqttDefinition().tokenResetMethod(); syn String MSendDefinition.tokenResetMethod() = getSendTokenEndpointDefinition().tokenResetMethod();
// --- MMappingDefinition --- // --- MMappingDefinition ---
syn String MMappingDefinition.toType() = getMappingDefinition().getToType().prettyPrint(); syn String MMappingDefinition.toType() = getMappingDefinition().getToType().prettyPrint();
...@@ -122,7 +125,7 @@ aspect AttributesForMustache { ...@@ -122,7 +125,7 @@ aspect AttributesForMustache {
// --- MTokenComponent --- // --- MTokenComponent ---
syn String MTokenComponent.parentTypeName() = getTokenComponent().containingTypeDecl().getName(); syn String MTokenComponent.parentTypeName() = getTokenComponent().containingTypeDecl().getName();
syn String MTokenComponent.name() = getTokenComponent().getName(); syn String MTokenComponent.name() = getTokenComponent().getName();
syn String MTokenComponent.javaType() = getTokenComponent().getJavaTypeUse().prettyPrint(); syn String MTokenComponent.javaType() = getTokenComponent().effectiveJavaTypeUse().prettyPrint();
syn String MTokenComponent.internalName() = getTokenComponent().internalName(); syn String MTokenComponent.internalName() = getTokenComponent().internalName();
// --- toMustache --- // --- toMustache ---
...@@ -130,10 +133,10 @@ aspect AttributesForMustache { ...@@ -130,10 +133,10 @@ aspect AttributesForMustache {
MRagConnect result = new MRagConnect(); MRagConnect result = new MRagConnect();
result.setRagConnect(this); result.setRagConnect(this);
for (EndpointDefinition def : getEndpointDefinitionList()) { for (EndpointDefinition def : getEndpointDefinitionList()) {
if (def.isSendToMqttDefinition()) { if (def.isSendTokenEndpointDefinition()) {
result.addSendDefinition(def.asSendToMqttDefinition().toMustache()); result.addSendDefinition(def.asSendTokenEndpointDefinition().toMustache());
} else { } else {
result.addReceiveDefinition(def.asReceiveFromMqttDefinition().toMustache()); result.addReceiveDefinition(def.asReceiveTokenEndpointDefinition().toMustache());
} }
} }
for (MappingDefinition def : allMappingDefinitions()) { for (MappingDefinition def : allMappingDefinitions()) {
...@@ -163,15 +166,31 @@ aspect AttributesForMustache { ...@@ -163,15 +166,31 @@ aspect AttributesForMustache {
addInnerMappingDefinition(inner); addInnerMappingDefinition(inner);
} }
} }
syn lazy MReceiveDefinition ReceiveFromMqttDefinition.toMustache() { syn lazy MReceiveDefinition ReceiveTokenEndpointDefinition.toMustache();
eq ReceiveFromMqttDefinition.toMustache() {
MReceiveDefinition result = new MReceiveDefinition();
result.setReceiveTokenEndpointDefinition(this);
result.addInnerMappings();
return result;
}
eq ReceiveFromRestDefinition.toMustache() {
MReceiveDefinition result = new MReceiveDefinition(); MReceiveDefinition result = new MReceiveDefinition();
result.setReceiveFromMqttDefinition(this); System.err.println("REST not implemented!");
result.setReceiveTokenEndpointDefinition(this);
result.addInnerMappings();
return result;
}
syn lazy MSendDefinition SendTokenEndpointDefinition.toMustache();
eq SendToMqttDefinition.toMustache() {
MSendDefinition result = new MSendDefinition();
result.setSendTokenEndpointDefinition(this);
result.addInnerMappings(); result.addInnerMappings();
return result; return result;
} }
syn lazy MSendDefinition SendToMqttDefinition.toMustache() { eq SendToRestDefinition.toMustache() {
MSendDefinition result = new MSendDefinition(); MSendDefinition result = new MSendDefinition();
result.setSendToMqttDefinition(this); System.err.println("REST not implemented!");
result.setSendTokenEndpointDefinition(this);
result.addInnerMappings(); result.addInnerMappings();
return result; return result;
} }
...@@ -206,11 +225,11 @@ aspect AspectGeneration { ...@@ -206,11 +225,11 @@ aspect AspectGeneration {
syn String TokenComponent.externalName() = getName(); syn String TokenComponent.externalName() = getName();
syn String TokenEndpointDefinition.connectMethod() = "connect" + getToken().getName(); syn String TokenEndpointDefinition.connectMethod() = "connect" + getToken().getName();
syn String SendToMqttDefinition.sendTopic() = "_topic_" + getToken().getName(); syn String SendTokenEndpointDefinition.sendTopic() = "_topic_" + getToken().getName();
syn String SendToMqttDefinition.lastValue() = "_lastValue" + getToken().getName(); syn String SendTokenEndpointDefinition.lastValue() = "_lastValue" + getToken().getName();
syn String SendToMqttDefinition.updateMethod() = "_update_" + getToken().getName(); syn String SendTokenEndpointDefinition.updateMethod() = "_update_" + getToken().getName();
syn String SendToMqttDefinition.writeMethod() = "_writeLastValue_" + getToken().getName(); syn String SendTokenEndpointDefinition.writeMethod() = "_writeLastValue_" + getToken().getName();
syn String SendToMqttDefinition.tokenResetMethod() = "get" + getToken().getName() + "_reset"; syn String SendTokenEndpointDefinition.tokenResetMethod() = "get" + getToken().getName() + "_reset";
syn String MappingDefinition.methodName() = "_apply_" + getID(); syn String MappingDefinition.methodName() = "_apply_" + getID();
syn String DependencyDefinition.dependencyMethod() = "add" + syn String DependencyDefinition.dependencyMethod() = "add" +
Character.toUpperCase(getID().charAt(0)) + Character.toUpperCase(getID().charAt(0)) +
...@@ -225,13 +244,24 @@ aspect AspectGeneration { ...@@ -225,13 +244,24 @@ aspect AspectGeneration {
syn String RagConnect.mqttWaitUntilReadyMethod() = "MqttWaitUntilReady"; syn String RagConnect.mqttWaitUntilReadyMethod() = "MqttWaitUntilReady";
syn String RagConnect.mqttCloseMethod() = "MqttCloseConnections"; syn String RagConnect.mqttCloseMethod() = "MqttCloseConnections";
syn String RagConnect.restHandlerAttribute() = "_restHandler";
syn String RagConnect.restHandlerField() = "_restHandler";
syn String TokenEndpointDefinition.handlerAttribute();
eq ReceiveFromMqttDefinition.handlerAttribute() = mqttHandlerAttribute();
eq SendToMqttDefinition.handlerAttribute() = mqttHandlerAttribute();
eq ReceiveFromRestDefinition.handlerAttribute() = restHandlerAttribute();
eq SendToRestDefinition.handlerAttribute() = restHandlerAttribute();
// naming copy attributes // naming copy attributes
// --- mqttHandlerAttribute --- // --- mqttHandlerAttribute ---
inh String EndpointDefinition.mqttHandlerAttribute(); inh String EndpointDefinition.mqttHandlerAttribute();
inh String MappingDefinition.mqttHandlerAttribute();
inh String DependencyDefinition.mqttHandlerAttribute();
eq RagConnect.getChild().mqttHandlerAttribute() = mqttHandlerAttribute(); eq RagConnect.getChild().mqttHandlerAttribute() = mqttHandlerAttribute();
// --- restHandlerAttribute ---
inh String EndpointDefinition.restHandlerAttribute();
eq RagConnect.getChild().restHandlerAttribute() = restHandlerAttribute();
// --- rootNodeName --- // --- rootNodeName ---
syn String ASTNode.rootNodeName() = rootNode.getName(); syn String ASTNode.rootNodeName() = rootNode.getName();
...@@ -296,7 +326,7 @@ aspect GrammarExtension { ...@@ -296,7 +326,7 @@ aspect GrammarExtension {
if (!getName().equals("")) { if (!getName().equals("")) {
b.append(internalName()).append(":"); b.append(internalName()).append(":");
} }
getJavaTypeUse().generateAbstractGrammar(b); effectiveJavaTypeUse().generateAbstractGrammar(b);
b.append(">"); b.append(">");
if (getNTA()) { if (getNTA()) {
b.append("/"); b.append("/");
......
...@@ -18,6 +18,26 @@ aspect DefaultMappings { ...@@ -18,6 +18,26 @@ aspect DefaultMappings {
return result; return result;
} }
private String RagConnect.baseDefaultMappingTypeNamePart(String typeName) {
return Character.toUpperCase(typeName.charAt(0)) + typeName.substring(1).replace("[]", "s");
}
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;
}
syn nta DefaultMappingDefinition RagConnect.defaultBytesToIntMapping() { syn nta DefaultMappingDefinition RagConnect.defaultBytesToIntMapping() {
DefaultMappingDefinition result = baseDefaultMappingDefinitionFromBytes("int"); DefaultMappingDefinition result = baseDefaultMappingDefinitionFromBytes("int");
result.setContent("return java.nio.ByteBuffer.wrap(bytes).getInt();"); result.setContent("return java.nio.ByteBuffer.wrap(bytes).getInt();");
...@@ -101,17 +121,31 @@ aspect DefaultMappings { ...@@ -101,17 +121,31 @@ aspect DefaultMappings {
result.setContent("return input.getBytes();"); result.setContent("return input.getBytes();");
return result; return result;
} }
syn nta DefaultMappingDefinition RagConnect.defaultStringToIntMapping() = baseDefaultMappingDefinition("String", "int", "return Integer.parseInteger(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);");
syn nta DefaultMappingDefinition RagConnect.defaultIntToStringMapping() = baseDefaultMappingDefinition("int", "String", "return String.valueOf(input);");
syn nta DefaultMappingDefinition RagConnect.defaultShortToStringMapping() = baseDefaultMappingDefinition("int", "String", "return String.valueOf(input);");
syn nta DefaultMappingDefinition RagConnect.defaultLongToStringMapping() = baseDefaultMappingDefinition("int", "String", "return String.valueOf(input);");
syn nta DefaultMappingDefinition RagConnect.defaultFloatToStringMapping() = baseDefaultMappingDefinition("int", "String", "return String.valueOf(input);");
syn nta DefaultMappingDefinition RagConnect.defaultDoubleToStringMapping() = baseDefaultMappingDefinition("int", "String", "return String.valueOf(input);");
syn nta DefaultMappingDefinition RagConnect.defaultCharToStringMapping() = baseDefaultMappingDefinition("int", "String", "return String.valueOf(input);");
} }
aspect Mappings { aspect Mappings {
// --- effectiveMappings --- // --- effectiveMappings ---
syn java.util.List<MappingDefinition> EndpointDefinition.effectiveMappings(); syn java.util.List<MappingDefinition> EndpointDefinition.effectiveMappings();
eq ReceiveFromMqttDefinition.effectiveMappings() { eq ReceiveTokenEndpointDefinition.effectiveMappings() {
// if there is a first mapping, check if its input type is byte[]. // if there is a first mapping, check if it is suitable.
// or if no mappings are specified. // or if no mappings are specified.
// then prepend the suitable default mapping // then prepend the suitable default mapping
java.util.List<MappingDefinition> result; java.util.List<MappingDefinition> result;
if (getMappingList().isEmpty() || !getMappingList().get(0).getFromType().isByteArray()) { if (getMappingList().isEmpty() || !hasSuitableEdgeMapping()) {
result = new java.util.ArrayList(); result = new java.util.ArrayList();
result.add(suitableDefaultMapping()); result.add(suitableDefaultMapping());
result.addAll(getMappingList()); result.addAll(getMappingList());
...@@ -120,13 +154,12 @@ aspect Mappings { ...@@ -120,13 +154,12 @@ aspect Mappings {
} }
return result; return result;
} }
eq SendToMqttDefinition.effectiveMappings() { eq SendTokenEndpointDefinition.effectiveMappings() {
// if there is a mapping, check if the output type of the last mapping is byte[]. // if there is a mapping, check if it is suitable.
// or if no mappings are specified. // or if no mappings are specified.
// then append the suitable default mapping // then append the suitable default mapping
java.util.List<MappingDefinition> result; java.util.List<MappingDefinition> result;
int numMappings = getMappingList().size(); if (getMappingList().isEmpty() || !hasSuitableEdgeMapping()) {
if (numMappings == 0 || !getMappingList().get(numMappings - 1).getToType().isByteArray()) {
result = new java.util.ArrayList(); result = new java.util.ArrayList();
result.addAll(getMappingList()); result.addAll(getMappingList());
result.add(suitableDefaultMapping()); result.add(suitableDefaultMapping());
...@@ -136,8 +169,20 @@ aspect Mappings { ...@@ -136,8 +169,20 @@ aspect Mappings {
return result; return result;
} }
// --- 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);
eq ReceiveFromMqttDefinition.isSuitableEdgeMapping(MappingDefinition def) = def.getFromType().isByteArray();
eq SendToMqttDefinition.isSuitableEdgeMapping(MappingDefinition def) = def.getToType().isByteArray();
eq ReceiveFromRestDefinition.isSuitableEdgeMapping(MappingDefinition def) = def.getFromType().isString();
eq SendToRestDefinition.isSuitableEdgeMapping(MappingDefinition def) = def.getToType().isString();
// --- isPrimitiveType --- // --- isPrimitiveType ---
syn boolean TokenComponent.isPrimitiveType() = getJavaTypeUse().isPrimitiveType(); syn boolean TokenComponent.isPrimitiveType() = effectiveJavaTypeUse().isPrimitiveType();
syn boolean JavaTypeUse.isPrimitiveType() = false; syn boolean JavaTypeUse.isPrimitiveType() = false;
eq SimpleJavaTypeUse.isPrimitiveType() { eq SimpleJavaTypeUse.isPrimitiveType() {
switch(getName()) { switch(getName()) {
...@@ -202,15 +247,60 @@ aspect Mappings { ...@@ -202,15 +247,60 @@ aspect Mappings {
default: return null; default: return null;
} }
} }
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;
}
}
// --- isByteArray --- // --- isByteArray ---
syn boolean MappingDefinitionType.isByteArray() = false; syn boolean MappingDefinitionType.isByteArray() = false;
eq JavaArrayMappingDefinitionType.isByteArray() = getType().getName().equals("byte"); eq JavaArrayMappingDefinitionType.isByteArray() = getType().getName().equals("byte");
// --- isString ---
syn boolean MappingDefinitionType.isString() = false;
eq JavaMappingDefinitionType.isString() = getType().getName().equals("String");
// --- allMappingDefinitions --- // --- allMappingDefinitions ---
syn java.util.List<MappingDefinition> RagConnect.allMappingDefinitions() { syn java.util.List<MappingDefinition> RagConnect.allMappingDefinitions() {
java.util.List<MappingDefinition> result = new java.util.ArrayList<>(); java.util.List<MappingDefinition> result = new java.util.ArrayList<>();
getMappingDefinitionList().iterator().forEachRemaining(result::add); getMappingDefinitionList().iterator().forEachRemaining(result::add);
// byte[] converstion
result.add(defaultBytesToIntMapping()); result.add(defaultBytesToIntMapping());
result.add(defaultBytesToShortMapping()); result.add(defaultBytesToShortMapping());
result.add(defaultBytesToLongMapping()); result.add(defaultBytesToLongMapping());
...@@ -225,6 +315,19 @@ aspect Mappings { ...@@ -225,6 +315,19 @@ aspect Mappings {
result.add(defaultDoubleToBytesMapping()); result.add(defaultDoubleToBytesMapping());
result.add(defaultCharToBytesMapping()); result.add(defaultCharToBytesMapping());
result.add(defaultStringToBytesMapping()); result.add(defaultStringToBytesMapping());
// string converstion
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());