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

Working on REST impl.

- differentiate between push-send and pull-send
parent 8667cbe3
......@@ -5,3 +5,4 @@
.classpath
.idea/
.gradle/
build/
aspect Analysis {
// --- lookupTokenEndpointDefinition ---
inh TokenEndpointDefinition TokenEndpointDefinition.lookupTokenEndpointDefinition(TokenComponent token);
eq RagConnect.getEndpointDefinition().lookupTokenEndpointDefinition(TokenComponent token) {
inh java.util.List<TokenEndpointDefinition> TokenEndpointDefinition.lookupTokenEndpointDefinitions(TokenComponent token);
eq RagConnect.getEndpointDefinition().lookupTokenEndpointDefinitions(TokenComponent token) {
java.util.List<TokenEndpointDefinition> result = new java.util.ArrayList<>();
for (EndpointDefinition def : getEndpointDefinitionList()) {
if (def.isTokenEndpointDefinition() && def.asTokenEndpointDefinition().getToken().equals(token)) {
return def.asTokenEndpointDefinition();
result.add(def.asTokenEndpointDefinition());
}
}
return null;
return result;
}
// --- lookupDependencyDefinition ---
......@@ -22,6 +23,70 @@ aspect Analysis {
}
// --- isAlreadyDefined ---
syn boolean TokenEndpointDefinition.isAlreadyDefined() = lookupTokenEndpointDefinition(getToken()) != this;
syn boolean TokenEndpointDefinition.isAlreadyDefined() {
java.util.List<TokenEndpointDefinition> definitions = lookupTokenEndpointDefinitions(getToken());
java.util.Set<String> protocols = definitions
.stream()
.map(TokenEndpointDefinition::protocol)
.collect(java.util.stream.Collectors.toSet());
return definitions.size() > protocols.size();
}
syn boolean DependencyDefinition.isAlreadyDefined() = lookupDependencyDefinition(getSource().containingTypeDecl(), getID()) != this;
// --- protocol ---
syn String TokenEndpointDefinition.protocol();
eq ReceiveFromMqttDefinition.protocol() = "mqtt";
eq SendToMqttDefinition.protocol() = "mqtt";
eq ReceiveFromRestDefinition.protocol() = "rest";
eq SendToRestDefinition.protocol() = "rest";
// --- usesPROTOCOL ---
syn boolean RagConnect.usesMqtt() = !mqttEndpointDefinitions().isEmpty();
syn boolean RagConnect.usesRest() = !restEndpointDefinitions().isEmpty();
// --- mqttEndpointDefinitions ---
coll java.util.List<TokenEndpointDefinition> RagConnect.mqttEndpointDefinitions() [new java.util.ArrayList<>()] root RagConnect;
ReceiveFromMqttDefinition contributes this
to RagConnect.mqttEndpointDefinitions()
for ragconnect();
SendToMqttDefinition contributes this
to RagConnect.mqttEndpointDefinitions()
for ragconnect();
// --- restEndpointDefinitions ---
coll java.util.List<TokenEndpointDefinition> RagConnect.restEndpointDefinitions() [new java.util.ArrayList<>()] root RagConnect;
ReceiveFromRestDefinition contributes this
to RagConnect.restEndpointDefinitions()
for ragconnect();
SendToRestDefinition contributes this
to RagConnect.restEndpointDefinitions()
for ragconnect();
syn boolean MappingDefinitionType.assignableTo(JavaTypeUse target);
eq JavaMappingDefinitionType.assignableTo(JavaTypeUse target) = getType().assignableTo(target);
eq JavaArrayMappingDefinitionType.assignableTo(JavaTypeUse target) {
if (!target.getName().endsWith("[]")) { return false; }
return getType().assignableTo(new SimpleJavaTypeUse(target.getName().replace("[]", "")));
}
syn boolean JavaTypeUse.assignableTo(JavaTypeUse target) {
// target var = this;
return target.primitivePrettyPrint().equals(this.primitivePrettyPrint());
}
syn String JavaTypeUse.primitivePrettyPrint() {
switch(getName()) {
case "int":
case "Integer": return "int";
case "short":
case "Short": return "short";
case "long":
case "Long": return "long";
case "float":
case "Float": return "float";
case "double":
case "Double": return "double";
case "char":
case "Character": return "char";
default: return getName();
}
}
}
......@@ -23,9 +23,9 @@ aspect Errors {
when effectiveMappings().get(0) == null
to RagConnect.errors();
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().effectiveJavaTypeUse().prettyPrint().equals(
effectiveMappings().get(effectiveMappings().size() - 1).getToType().prettyPrint())
ReceiveTokenEndpointDefinition contributes error("to-type of last mapping (" + effectiveMappings().get(effectiveMappings().size() - 1).getToType().prettyPrint() + ") not assignable to type of the Token (" + getToken().effectiveJavaTypeUse().prettyPrint() + ")!")
when !effectiveMappings().get(effectiveMappings().size() - 1).getToType().assignableTo(
getToken().effectiveJavaTypeUse())
to RagConnect.errors();
SendTokenEndpointDefinition contributes error("Sending target token must be an NTA token!")
......
//TypeComponentMustache ;
//rel TypeComponentMustache.TypeComponent -> TypeComponent ;
MRagConnect ::= ReceiveDefinition:MReceiveDefinition* SendDefinition:MSendDefinition* MappingDefinition:MMappingDefinition* DependencyDefinition:MDependencyDefinition* RootTypeComponent:MTypeComponent* TokenComponent:MTokenComponent*;
MRagConnect ::= ReceiveDefinition:MReceiveDefinition* PushSendDefinition:MSendDefinition* PullSendDefinition:MSendDefinition* MappingDefinition:MMappingDefinition* DependencyDefinition:MDependencyDefinition* RootTypeComponent:MTypeComponent* TokenComponent:MTokenComponent*;
abstract MEndpointDefinition ::= InnerMappingDefinition:MInnerMappingDefinition*;
MReceiveDefinition : MEndpointDefinition;
MSendDefinition : MEndpointDefinition;
......
aspect Util {
static String ASTNode.capitalize(String s) {
return Character.toUpperCase(s.charAt(0)) + s.substring(1);
}
}
......@@ -31,13 +31,18 @@ aspect AttributesForMustache {
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 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();
// --- MEndpointDefinition ---
syn String MEndpointDefinition.preemptiveExpectedValue();
......@@ -51,6 +56,8 @@ aspect AttributesForMustache {
inh String MEndpointDefinition.mqttHandlerAttribute();
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 TokenComponent MEndpointDefinition.token() = endpointDef().getToken();
......@@ -88,9 +95,12 @@ aspect AttributesForMustache {
eq MReceiveDefinition.endpointDef() = getReceiveTokenEndpointDefinition();
eq MReceiveDefinition.firstInputVarName() = "message";
syn String MReceiveDefinition.newConnectionMethod() = getReceiveTokenEndpointDefinition().newConnectionMethod();
syn String MReceiveDefinition.connectParameterName() = getReceiveTokenEndpointDefinition().connectParameterName();
// --- MSendDefinition ---
eq MSendDefinition.preemptiveExpectedValue() = lastValue();
eq MSendDefinition.preemptiveReturn() = "return false;";
eq MSendDefinition.preemptiveReturn() = getSendTokenEndpointDefinition().preemptiveReturn();
eq MSendDefinition.endpointDef() = getSendTokenEndpointDefinition();
eq MSendDefinition.firstInputVarName() = "get" + tokenName() + "()";
......@@ -99,6 +109,7 @@ aspect AttributesForMustache {
syn String MSendDefinition.updateMethod() = getSendTokenEndpointDefinition().updateMethod();
syn String MSendDefinition.writeMethod() = getSendTokenEndpointDefinition().writeMethod();
syn String MSendDefinition.tokenResetMethod() = getSendTokenEndpointDefinition().tokenResetMethod();
syn boolean MSendDefinition.isPush() = getSendTokenEndpointDefinition().isPush();
// --- MMappingDefinition ---
syn String MMappingDefinition.toType() = getMappingDefinition().getToType().prettyPrint();
......@@ -112,7 +123,7 @@ aspect AttributesForMustache {
syn String MDependencyDefinition.dependencyMethod() = getDependencyDefinition().dependencyMethod();
syn String MDependencyDefinition.sourceParentTypeName() = getDependencyDefinition().getSource().containingTypeDecl().getName();
syn String MDependencyDefinition.internalRelationPrefix() = getDependencyDefinition().internalRelationPrefix();
syn nta MEndpointDefinition MDependencyDefinition.targetEndpointDefinition() {
syn nta MSendDefinition MDependencyDefinition.targetEndpointDefinition() {
return getDependencyDefinition().targetEndpointDefinition().toMustache();
}
......@@ -134,7 +145,12 @@ aspect AttributesForMustache {
result.setRagConnect(this);
for (EndpointDefinition def : getEndpointDefinitionList()) {
if (def.isSendTokenEndpointDefinition()) {
result.addSendDefinition(def.asSendTokenEndpointDefinition().toMustache());
SendTokenEndpointDefinition sendDef = def.asSendTokenEndpointDefinition();
if (sendDef.isPush()) {
result.addPushSendDefinition(sendDef.toMustache());
} else {
result.addPullSendDefinition(sendDef.toMustache());
}
} else {
result.addReceiveDefinition(def.asReceiveTokenEndpointDefinition().toMustache());
}
......@@ -166,34 +182,18 @@ aspect AttributesForMustache {
addInnerMappingDefinition(inner);
}
}
syn lazy MReceiveDefinition ReceiveTokenEndpointDefinition.toMustache();
eq ReceiveFromMqttDefinition.toMustache() {
syn lazy MReceiveDefinition ReceiveTokenEndpointDefinition.toMustache() {
MReceiveDefinition result = new MReceiveDefinition();
result.setReceiveTokenEndpointDefinition(this);
result.addInnerMappings();
return result;
}
eq ReceiveFromRestDefinition.toMustache() {
MReceiveDefinition result = new MReceiveDefinition();
System.err.println("REST not implemented!");
result.setReceiveTokenEndpointDefinition(this);
result.addInnerMappings();
return result;
}
syn lazy MSendDefinition SendTokenEndpointDefinition.toMustache();
eq SendToMqttDefinition.toMustache() {
syn lazy MSendDefinition SendTokenEndpointDefinition.toMustache() {
MSendDefinition result = new MSendDefinition();
result.setSendTokenEndpointDefinition(this);
result.addInnerMappings();
return result;
}
eq SendToRestDefinition.toMustache() {
MSendDefinition result = new MSendDefinition();
System.err.println("REST not implemented!");
result.setSendTokenEndpointDefinition(this);
result.addInnerMappings();
return result;
}
syn lazy MMappingDefinition MappingDefinition.toMustache() {
MMappingDefinition result = new MMappingDefinition();
result.setMappingDefinition(this);
......@@ -224,7 +224,7 @@ aspect AspectGeneration {
syn String TokenComponent.internalName() = getDependencySourceDefinitionList().isEmpty() ? externalName() : "_internal_" + getName();
syn String TokenComponent.externalName() = getName();
syn String TokenEndpointDefinition.connectMethod() = "connect" + getToken().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();
......@@ -237,6 +237,7 @@ aspect AspectGeneration {
syn String DependencyDefinition.internalRelationPrefix() = "_internal_" + getID();
syn String DependencyDefinition.internalTokenName() = getSource().internalName();
// -- MQTT names --
syn String RagConnect.mqttHandlerAttribute() = "_mqttHandler";
syn String RagConnect.mqttHandlerField() = "_mqttHandler";
......@@ -244,15 +245,40 @@ aspect AspectGeneration {
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();
......
aspect DefaultMappings {
private DefaultMappingDefinition RagConnect.baseDefaultMappingDefinitionFromBytes(String typeName) {
DefaultMappingDefinition result = new DefaultMappingDefinition();
result.setID("_DefaultBytesTo" + Character.toUpperCase(typeName.charAt(0)) + typeName.substring(1) + "Mapping");
result.setFromType(new JavaArrayMappingDefinitionType(new SimpleJavaTypeUse("byte")));
result.setFromVariableName("bytes");
result.setToType(new JavaMappingDefinitionType(new SimpleJavaTypeUse(typeName)));
return result;
}
private DefaultMappingDefinition RagConnect.baseDefaultMappingDefinitionToBytes(String typeName) {
DefaultMappingDefinition result = new DefaultMappingDefinition();
result.setID("_Default" + Character.toUpperCase(typeName.charAt(0)) + typeName.substring(1) + "ToBytesMapping");
result.setFromType(new JavaMappingDefinitionType(new SimpleJavaTypeUse(typeName)));
result.setFromVariableName("input");
result.setToType(new JavaArrayMappingDefinitionType(new SimpleJavaTypeUse("byte")));
return result;
}
private String RagConnect.baseDefaultMappingTypeNamePart(String typeName) {
return Character.toUpperCase(typeName.charAt(0)) + typeName.substring(1).replace("[]", "s");
return capitalize(typeName).replace("[]", "s");
}
private MappingDefinitionType RagConnect.baseDefaultMappingTypeFromName(String typeName) {
......@@ -38,103 +20,61 @@ aspect DefaultMappings {
return result;
}
syn nta DefaultMappingDefinition RagConnect.defaultBytesToIntMapping() {
DefaultMappingDefinition result = baseDefaultMappingDefinitionFromBytes("int");
result.setContent("return java.nio.ByteBuffer.wrap(bytes).getInt();");
return result;
}
syn nta DefaultMappingDefinition RagConnect.defaultBytesToIntMapping() = baseDefaultMappingDefinition(
"byte[]", "int", "return java.nio.ByteBuffer.wrap(input).getInt();");
syn nta DefaultMappingDefinition RagConnect.defaultBytesToShortMapping() = baseDefaultMappingDefinition(
"byte[]", "short", "return java.nio.ByteBuffer.wrap(input).getShort();");
syn nta DefaultMappingDefinition RagConnect.defaultBytesToLongMapping() = baseDefaultMappingDefinition(
"byte[]", "long", "return java.nio.ByteBuffer.wrap(input).getLong();");
syn nta DefaultMappingDefinition RagConnect.defaultBytesToFloatMapping() = baseDefaultMappingDefinition(
"byte[]", "float", "return java.nio.ByteBuffer.wrap(input).getFloat();");
syn nta DefaultMappingDefinition RagConnect.defaultBytesToDoubleMapping() = baseDefaultMappingDefinition(
"byte[]", "double", "return java.nio.ByteBuffer.wrap(input).getDouble();");
syn nta DefaultMappingDefinition RagConnect.defaultBytesToCharMapping() = baseDefaultMappingDefinition(
"byte[]", "char", "return java.nio.ByteBuffer.wrap(input).getChar();");
syn nta DefaultMappingDefinition RagConnect.defaultBytesToStringMapping() = baseDefaultMappingDefinition(
"byte[]", "String", "return new String(input);");
syn nta DefaultMappingDefinition RagConnect.defaultBytesToShortMapping() {
DefaultMappingDefinition result = baseDefaultMappingDefinitionFromBytes("short");
result.setContent("return java.nio.ByteBuffer.wrap(bytes).getShort();");
return result;
}
syn nta DefaultMappingDefinition RagConnect.defaultBytesToLongMapping() {
DefaultMappingDefinition result = baseDefaultMappingDefinitionFromBytes("long");
result.setContent("return java.nio.ByteBuffer.wrap(bytes).getLong();");
return result;
}
syn nta DefaultMappingDefinition RagConnect.defaultBytesToFloatMapping() {
DefaultMappingDefinition result = baseDefaultMappingDefinitionFromBytes("float");
result.setContent("return java.nio.ByteBuffer.wrap(bytes).getFloat();");
return result;
}
syn nta DefaultMappingDefinition RagConnect.defaultBytesToDoubleMapping() {
DefaultMappingDefinition result = baseDefaultMappingDefinitionFromBytes("double");
result.setContent("return java.nio.ByteBuffer.wrap(bytes).getDouble();");
return result;
}
syn nta DefaultMappingDefinition RagConnect.defaultBytesToCharMapping() {
DefaultMappingDefinition result = baseDefaultMappingDefinitionFromBytes("char");
result.setContent("return java.nio.ByteBuffer.wrap(bytes).getChar();");
return result;
}
syn nta DefaultMappingDefinition RagConnect.defaultBytesToStringMapping() {
DefaultMappingDefinition result = baseDefaultMappingDefinitionFromBytes("String");
result.setContent("return new String(bytes);");
return result;
}
syn nta DefaultMappingDefinition RagConnect.defaultIntToBytesMapping() {
DefaultMappingDefinition result = baseDefaultMappingDefinitionToBytes("int");
result.setContent("return java.nio.ByteBuffer.allocate(4).putInt(input).array();");
return result;
}
syn nta DefaultMappingDefinition RagConnect.defaultShortToBytesMapping() {
DefaultMappingDefinition result = baseDefaultMappingDefinitionToBytes("short");
result.setContent("return java.nio.ByteBuffer.allocate(2).putShort(input).array();");
return result;
}
syn nta DefaultMappingDefinition RagConnect.defaultLongToBytesMapping() {
DefaultMappingDefinition result = baseDefaultMappingDefinitionToBytes("long");
result.setContent("return java.nio.ByteBuffer.allocate(8).putLong(input).array();");
return result;
}
syn nta DefaultMappingDefinition RagConnect.defaultFloatToBytesMapping() {
DefaultMappingDefinition result = baseDefaultMappingDefinitionToBytes("float");
result.setContent("return java.nio.ByteBuffer.allocate(4).putFloat(input).array();");
return result;
}
syn nta DefaultMappingDefinition RagConnect.defaultDoubleToBytesMapping() {
DefaultMappingDefinition result = baseDefaultMappingDefinitionToBytes("double");
result.setContent("return java.nio.ByteBuffer.allocate(8).putDouble(input).array();");
return result;
}
syn nta DefaultMappingDefinition RagConnect.defaultCharToBytesMapping() {
DefaultMappingDefinition result = baseDefaultMappingDefinitionToBytes("char");
result.setContent("return java.nio.ByteBuffer.allocate(2).putChar(input).array();");
return result;
}
syn nta DefaultMappingDefinition RagConnect.defaultStringToBytesMapping() {
DefaultMappingDefinition result = baseDefaultMappingDefinitionToBytes("String");
result.setContent("return input.getBytes();");
return result;
}
syn nta DefaultMappingDefinition RagConnect.defaultIntToBytesMapping() = baseDefaultMappingDefinition(
"int", "byte[]", "return java.nio.ByteBuffer.allocate(Integer.BYTES).putInt(input).array();");
syn nta DefaultMappingDefinition RagConnect.defaultShortToBytesMapping() = baseDefaultMappingDefinition(
"short", "byte[]", "return java.nio.ByteBuffer.allocate(Short.BYTES).putShort(input).array();");
syn nta DefaultMappingDefinition RagConnect.defaultLongToBytesMapping() = baseDefaultMappingDefinition(
"long", "byte[]", "return java.nio.ByteBuffer.allocate(Long.BYTES).putLong(input).array();");
syn nta DefaultMappingDefinition RagConnect.defaultFloatToBytesMapping() = baseDefaultMappingDefinition(
"float", "byte[]", "return java.nio.ByteBuffer.allocate(Float.BYTES).putFloat(input).array();");
syn nta DefaultMappingDefinition RagConnect.defaultDoubleToBytesMapping() = baseDefaultMappingDefinition(
"double", "byte[]", "return java.nio.ByteBuffer.allocate(Double.BYTES).putDouble(input).array();");
syn nta DefaultMappingDefinition RagConnect.defaultCharToBytesMapping() = baseDefaultMappingDefinition(
"char", "byte[]", "return java.nio.ByteBuffer.allocate(Character.BYTES).putChar(input).array();");
syn nta DefaultMappingDefinition RagConnect.defaultStringToBytesMapping() = baseDefaultMappingDefinition(
"String", "byte[]", "return input.getBytes();");
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.defaultStringToIntMapping() = baseDefaultMappingDefinition(
"String", "int", "return Integer.parseInt(input);");
syn nta DefaultMappingDefinition RagConnect.defaultStringToShortMapping() = baseDefaultMappingDefinition(
"String", "short", "return Short.parseShort(input);");
syn nta DefaultMappingDefinition RagConnect.defaultStringToLongMapping() = baseDefaultMappingDefinition(
"String", "long", "return Long.parseLong(input);");
syn nta DefaultMappingDefinition RagConnect.defaultStringToFloatMapping() = baseDefaultMappingDefinition(
"String", "float", "return Float.parseFloat(input);");
syn nta DefaultMappingDefinition RagConnect.defaultStringToDoubleMapping() = baseDefaultMappingDefinition(
"String", "double", "return Double.parseDouble(input);");
syn nta DefaultMappingDefinition RagConnect.defaultStringToCharMapping() = baseDefaultMappingDefinition(
"String", "char", "return input.charAt(0);");
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);");
syn nta DefaultMappingDefinition RagConnect.defaultIntToStringMapping() = baseDefaultMappingDefinition(
"int", "String", "return String.valueOf(input);");
syn nta DefaultMappingDefinition RagConnect.defaultShortToStringMapping() = baseDefaultMappingDefinition(
"short", "String", "return String.valueOf(input);");
syn nta DefaultMappingDefinition RagConnect.defaultLongToStringMapping() = baseDefaultMappingDefinition(
"long", "String", "return String.valueOf(input);");
syn nta DefaultMappingDefinition RagConnect.defaultFloatToStringMapping() = baseDefaultMappingDefinition(
"float", "String", "return String.valueOf(input);");
syn nta DefaultMappingDefinition RagConnect.defaultDoubleToStringMapping() = baseDefaultMappingDefinition(
"double", "String", "return String.valueOf(input);");
syn nta DefaultMappingDefinition RagConnect.defaultCharToStringMapping() = baseDefaultMappingDefinition(
"char", "String", "return String.valueOf(input);");
}
aspect Mappings {
......@@ -300,7 +240,7 @@ aspect Mappings {
syn java.util.List<MappingDefinition> RagConnect.allMappingDefinitions() {
java.util.List<MappingDefinition> result = new java.util.ArrayList<>();
getMappingDefinitionList().iterator().forEachRemaining(result::add);
// byte[] converstion
// byte[] conversion
result.add(defaultBytesToIntMapping());
result.add(defaultBytesToShortMapping());
result.add(defaultBytesToLongMapping());
......@@ -315,7 +255,7 @@ aspect Mappings {
result.add(defaultDoubleToBytesMapping());
result.add(defaultCharToBytesMapping());
result.add(defaultStringToBytesMapping());
// string converstion
// string conversion
result.add(defaultStringToIntMapping());
result.add(defaultStringToShortMapping());
result.add(defaultStringToLongMapping());
......
......@@ -10,6 +10,10 @@ RagConnect ragconnect
private Iterable<String> makeMappingDefs(ArrayList<?> raw_mapping_defs) {
return () -> raw_mapping_defs.stream().map(raw -> ((Symbol) raw).value.toString()).iterator();
}
private TokenEndpointDefinition enableAlwaysApply(TokenEndpointDefinition def) {
def.setAlwaysApply(true);
return def;
}
:} ;
EndpointDefinition endpoint_definition
......@@ -29,10 +33,10 @@ EndpointDefinition endpoint_definition
EndpointDefinition endpoint_definition_type
= RECEIVE token_ref {: return new ReceiveFromMqttDefinition().setToken(token_ref); :}
| RECEIVE token_ref VIA MQTT {: return new ReceiveFromMqttDefinition().setToken(token_ref); :}
| RECEIVE token_ref VIA REST {: return new ReceiveFromRestDefinition().setToken(token_ref); :}
| RECEIVE token_ref VIA REST {: return enableAlwaysApply(new ReceiveFromRestDefinition()).setToken(token_ref); :}
| SEND token_ref {: return new SendToMqttDefinition().setToken(token_ref); :}
| SEND token_ref VIA MQTT {: return new SendToMqttDefinition().setToken(token_ref); :}
| SEND token_ref VIA REST {: return new SendToRestDefinition().setToken(token_ref); :}
| SEND token_ref VIA REST {: return enableAlwaysApply(new SendToRestDefinition()).setToken(token_ref); :}
;
TokenComponent token_ref
......
......@@ -14,9 +14,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Collection;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.*;
public class Compiler extends AbstractCompiler {
......@@ -62,18 +60,26 @@ public class Compiler extends AbstractCompiler {
}
printMessage("Writing output files");
// copy MqttHandler into outputDir
final String mqttHandlerFileName = "MqttHandler.jadd";
try {
InputStream inputStream = Compiler.class.getClassLoader().getResourceAsStream(mqttHandlerFileName);
if (inputStream == null) {
throw new CompilerException("Could not open " + mqttHandlerFileName);
final List<String> handlers = new ArrayList<>();
if (ragConnect.usesMqtt()) {
handlers.add("MqttHandler.jadd");
}
if (ragConnect.usesRest()) {
handlers.add("RestHandler.jadd");
}
// copy handlers into outputDir
for (String handlerFileName : handlers) {
try {
InputStream inputStream = Compiler.class.getClassLoader().getResourceAsStream(handlerFileName);
if (inputStream == null) {
throw new CompilerException("Could not open " + handlerFileName);
}
Files.copy(inputStream,
getConfiguration().outputDir().toPath().resolve(handlerFileName),
StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new CompilerException("Could not copy " + handlerFileName, e);
}
Files.copy(inputStream,
getConfiguration().outputDir().toPath().resolve(mqttHandlerFileName),
StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new CompilerException("Could not copy " + mqttHandlerFileName, e);
}
for (GrammarFile grammarFile : ragConnect.getProgram().getGrammarFileList()) {
Path outputFile = getConfiguration().outputDir().toPath().resolve(grammarFile.getFileName());
......@@ -90,7 +96,7 @@ public class Compiler extends AbstractCompiler {
new Compiler().run(args);
} catch (CompilerException e) {
System.err.println(e.getMessage());
System.exit(-1);
System.exit(1);
}
}
......@@ -162,6 +168,7 @@ public class Compiler extends AbstractCompiler {
parseGrammar(program, filename);
break;
case "connect":
case "ragconnect":
// process ragConnect
RagConnect parsedRagConnect = parseRagConnect(program, filename);
mergeRagConnectDefinitions(ragConnect, parsedRagConnect);
......
......@@ -21,6 +21,17 @@ public class SimpleMain {
// --- just testing byte[] conversion ---
public static void testing() {
System.out.println("---");
try {
Class<?> clazz = Class.forName("java.util.List");
System.out.println("clazz.getName() = " + clazz.getName());
System.out.println(Integer.class.isAssignableFrom(Integer.class));
System.out.println(new SimpleJavaTypeUse("int").assignableTo(new SimpleJavaTypeUse("Integer")));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println("---");
byte[] bytes;
int i = 1;
double d = 2.3d;
......@@ -102,8 +113,8 @@ public class SimpleMain {
}
public static void main(String[] args) {
// testing();
createManualAST();
testing();
// createManualAST();
}
private static void createManualAST() {
......
......@@ -83,7 +83,7 @@ public class MqttHandler {
String topicString = topic.toString();
java.util.List<java.util.function.Consumer<byte[]>> callbackList = callbacks.get(topicString);
if (callbackList == null || callbackList.isEmpty()) {
logger.debug("Got a message, but no callback to call. Forgot to unsubscribe?");
logger.debug("Got a message, but no callback to call. Forgot to subscribe?");