Skip to content
Snippets Groups Projects
Commit 1e30ed27 authored by René Schöne's avatar René Schöne
Browse files

WIP: Mustache2YAML

- finished transformation
- remove indentation from mustache templates
- store YAML in file when --printYaml is given
parent 3cd357c6
No related branches found
No related tags found
No related merge requests found
aspect Printing { aspect Printing {
String ASTNode.PRINT_INDENT = " "; String ASTNode.PRINT_INDENT = " ";
syn String Document.prettyPrint() { syn String Document.prettyPrint() {
StringBuilder sb = new StringBuilder("#start\n"); StringBuilder sb = new StringBuilder();
sb.append("# RagConnect created at ").append(java.time.Instant.now()).append("\n");
for (ComplexElement element : getComplexElementList()) { for (ComplexElement element : getComplexElementList()) {
element.prettyPrint(sb, false, ""); element.prettyPrint(sb, false, "");
} }
if (sb.charAt(sb.length() - 1) != '\n') { if (sb.charAt(sb.length() - 1) != '\n') {
sb.append("\n"); sb.append("\n");
} }
sb.append("#end");
return sb.toString(); return sb.toString();
} }
syn StringBuilder Element.prettyPrint(StringBuilder sb, boolean printIndent, String indent); syn StringBuilder Element.prettyPrint(StringBuilder sb, boolean printIndent, String indent);
......
...@@ -3,6 +3,7 @@ aspect MustacheNodesToYAML { ...@@ -3,6 +3,7 @@ aspect MustacheNodesToYAML {
Document doc = new Document(); Document doc = new Document();
MappingElement root = new MappingElement(); MappingElement root = new MappingElement();
root.addKeyValuePair("rootNodeName", StringElement.of(rootNodeName())); root.addKeyValuePair("rootNodeName", StringElement.of(rootNodeName()));
root.addKeyValuePair("closeMethod", StringElement.of(closeMethod()));
root.addKeyValuePair("usesMqtt", ValueElement.of(usesMqtt)); root.addKeyValuePair("usesMqtt", ValueElement.of(usesMqtt));
root.addKeyValuePair("usesRest", ValueElement.of(usesRest)); root.addKeyValuePair("usesRest", ValueElement.of(usesRest));
// mqtt // mqtt
...@@ -18,7 +19,7 @@ aspect MustacheNodesToYAML { ...@@ -18,7 +19,7 @@ aspect MustacheNodesToYAML {
inner.addKeyValuePair("name", StringElement.of(comp.name())); inner.addKeyValuePair("name", StringElement.of(comp.name()));
rootTypeComponents.addElement(inner); rootTypeComponents.addElement(inner);
} }
root.addKeyValuePair("rootTypeComponents", rootTypeComponents); root.addKeyValuePair("RootTypeComponents", rootTypeComponents);
// rest // rest
root.addKeyValuePair("restHandlerField", StringElement.of(restHandlerField())); root.addKeyValuePair("restHandlerField", StringElement.of(restHandlerField()));
...@@ -63,92 +64,93 @@ aspect MustacheNodesToYAML { ...@@ -63,92 +64,93 @@ aspect MustacheNodesToYAML {
return doc; return doc;
} }
syn Element MReceiveDefinition.toYAML() { syn MappingElement MEndpointDefinition.toYAML() {
MappingElement inner = new MappingElement(); MappingElement result = new MappingElement();
inner.addKeyValuePair("parentTypeName", StringElement.of(parentTypeName())); result.addKeyValuePair("parentTypeName", StringElement.of(parentTypeName()));
inner.addKeyValuePair("connectMethod", StringElement.of(connectMethod())); result.addKeyValuePair("connectMethod", StringElement.of(connectMethod()));
inner.addKeyValuePair("connectParameterName", StringElement.of(connectParameterName())); result.addKeyValuePair("connectParameterName", StringElement.of(connectParameterName()));
inner.addKeyValuePair("lastDefinitionToType", StringElement.of(lastDefinitionToType())); result.addKeyValuePair("lastDefinitionToType", StringElement.of(lastDefinitionToType()));
inner.addKeyValuePair("resultVarPrefix", StringElement.of(resultVarPrefix())); result.addKeyValuePair("resultVarPrefix", StringElement.of(resultVarPrefix()));
inner.addKeyValuePair("lastDefinitionName", StringElement.of(lastDefinitionName())); result.addKeyValuePair("lastDefinitionName", StringElement.of(lastDefinitionName()));
inner.addKeyValuePair("preemptiveReturn", StringElement.of(preemptiveReturn())); result.addKeyValuePair("preemptiveReturn", StringElement.of(preemptiveReturn()));
inner.addKeyValuePair("alwaysApply", ValueElement.of(alwaysApply())); result.addKeyValuePair("alwaysApply", ValueElement.of(alwaysApply()));
inner.addKeyValuePair("condition", StringElement.of(condition())); result.addKeyValuePair("condition", StringElement.of(
inner.addKeyValuePair("loggingEnabledForReads", ValueElement.of(loggingEnabledForReads)); condition().replace("\"", "\\\"").replace("\n", "\\n")));
inner.addKeyValuePair("tokenName", StringElement.of(tokenName())); result.addKeyValuePair("lastResult", StringElement.of(lastResult()));
inner.addKeyValuePair("lastResult", StringElement.of(lastResult())); result.addKeyValuePair("InnerMappingDefinitions", innerMappingDefinitionsAsListElement());
// inner.addKeyValuePair("first", ValueElement.of(isFirst())); result.addKeyValuePair("tokenName", StringElement.of(tokenName()));
// parentTypeName return result;
// connectMethod
// connectParameterName
// lastDefinitionToType
// resultVarPrefix
// lastDefinitionName
// #InnerMappingDefinitions -> MInnerMappingDefinition
// last
// toType
// methodName
// inputVarName
// preemptiveReturn
// alwaysApply
// condition - special chars
// loggingEnabledForReads
// tokenName
// lastResult
return inner;
} }
syn Element MSendDefinition.toYAML() { syn MappingElement MReceiveDefinition.toYAML() {
MappingElement inner = new MappingElement(); MappingElement result = super.toYAML();
// #SendDefinitions -> MSendDefinition result.addKeyValuePair("loggingEnabledForReads", ValueElement.of(loggingEnabledForReads));
// parentTypeName return result;
// sender }
// lastValue
// connectMethod syn MappingElement MSendDefinition.toYAML() {
// connectParameterName MappingElement result = super.toYAML();
// loggingEnabledForWrites result.addKeyValuePair("sender", StringElement.of(sender()));
// updateMethod result.addKeyValuePair("lastValue", StringElement.of(lastValue()));
// writeMethod result.addKeyValuePair("loggingEnabledForWrites", ValueElement.of(loggingEnabledForWrites));
// tokenResetMethod result.addKeyValuePair("updateMethod", StringElement.of(updateMethod()));
// lastResult result.addKeyValuePair("writeMethod", StringElement.of(writeMethod()));
return inner; result.addKeyValuePair("tokenResetMethod", StringElement.of(tokenResetMethod()));
return result;
} }
syn Element MMappingDefinition.toYAML() { syn Element MMappingDefinition.toYAML() {
MappingElement inner = new MappingElement(); MappingElement result = new MappingElement();
// #MappingDefinitions -> MMappingDefinition result.addKeyValuePair("toType", StringElement.of(toType()));
// toType result.addKeyValuePair("methodName", StringElement.of(methodName()));
// methodName result.addKeyValuePair("fromType", StringElement.of(fromType()));
// fromType result.addKeyValuePair("fromVariableName", StringElement.of(fromVariableName()));
// fromVariableName result.addKeyValuePair("content", StringElement.of(
// content - special chars content().replace("\"", "\\\"").replace("\n", "\\n")));
return inner; return result;
} }
syn Element MDependencyDefinition.toYAML() { syn Element MDependencyDefinition.toYAML() {
MappingElement inner = new MappingElement(); MappingElement result = new MappingElement();
// #DependencyDefinitions -> MDependencyDefinition result.addKeyValuePair("targetParentTypeName", StringElement.of(targetParentTypeName()));
// targetParentTypeName result.addKeyValuePair("dependencyMethod", StringElement.of(dependencyMethod()));
// dependencyMethod result.addKeyValuePair("sourceParentTypeName", StringElement.of(sourceParentTypeName()));
// sourceParentTypeName result.addKeyValuePair("internalRelationPrefix", StringElement.of(internalRelationPrefix()));
// internalRelationPrefix return result;
return inner;
} }
syn Element MTokenComponent.toYAML() { syn Element MTokenComponent.toYAML() {
MappingElement inner = new MappingElement(); MappingElement result = new MappingElement();
// #TokenComponents -> MTokenComponent result.addKeyValuePair("parentTypeName", StringElement.of(parentTypeName()));
// parentTypeName result.addKeyValuePair("name", StringElement.of(name()));
// name result.addKeyValuePair("javaType", StringElement.of(javaType()));
// javaType result.addKeyValuePair("internalName", StringElement.of(internalName()));
// internalName ListElement dependencyDefinitions = new ListElement();
// #DependencyDefinitions -> MDependencyDefinition for (MDependencyDefinition def : getDependencyDefinitionList()) {
// targetParentTypeName MappingElement inner = new MappingElement();
// internalRelationPrefix inner.addKeyValuePair("targetParentTypeName", StringElement.of(def.targetParentTypeName()));
// #targetEndpointDefinition -> MSendDefinition inner.addKeyValuePair("internalRelationPrefix", StringElement.of(def.internalRelationPrefix()));
// updateMethod MappingElement targetEndpointDefinition = new MappingElement();
// writeMethod targetEndpointDefinition.addKeyValuePair("updateMethod", StringElement.of(def.targetEndpointDefinition().updateMethod()));
return inner; targetEndpointDefinition.addKeyValuePair("writeMethod", StringElement.of(def.targetEndpointDefinition().writeMethod()));
inner.addKeyValuePair("targetEndpointDefinition", targetEndpointDefinition);
dependencyDefinitions.addElement(inner);
}
result.addKeyValuePair("DependencyDefinitions", dependencyDefinitions);
return result;
}
ListElement MEndpointDefinition.innerMappingDefinitionsAsListElement() {
ListElement innerMappingDefinitions = new ListElement();
for (MInnerMappingDefinition def : getInnerMappingDefinitionList()) {
MappingElement inner = new MappingElement();
inner.addKeyValuePair("toType", StringElement.of(def.toType()));
inner.addKeyValuePair("methodName", StringElement.of(def.methodName()));
inner.addKeyValuePair("inputVarName", StringElement.of(def.inputVarName()));
inner.addKeyValuePair("last", ValueElement.of(def.isLast()));
innerMappingDefinitions.addElement(inner);
}
return innerMappingDefinitions;
} }
} }
...@@ -70,7 +70,9 @@ public class Compiler extends AbstractCompiler { ...@@ -70,7 +70,9 @@ public class Compiler extends AbstractCompiler {
if (optionPrintYaml.value()) { if (optionPrintYaml.value()) {
ASTNode.rootNode = ragConnect.getProgram().resolveTypeDecl(optionRootNode.value()); ASTNode.rootNode = ragConnect.getProgram().resolveTypeDecl(optionRootNode.value());
System.out.println(ragConnect.toMustache().toYAML().prettyPrint()); String yamlContent = ragConnect.toMustache().toYAML().prettyPrint();
System.out.println(yamlContent);
writeToFile(getConfiguration().outputDir().toPath().resolve("RagConnect.yml"), yamlContent);
return 0; return 0;
} }
...@@ -98,9 +100,9 @@ public class Compiler extends AbstractCompiler { ...@@ -98,9 +100,9 @@ public class Compiler extends AbstractCompiler {
} }
for (GrammarFile grammarFile : ragConnect.getProgram().getGrammarFileList()) { for (GrammarFile grammarFile : ragConnect.getProgram().getGrammarFileList()) {
Path outputFile = getConfiguration().outputDir().toPath().resolve(grammarFile.getFileName()); Path outputFile = getConfiguration().outputDir().toPath().resolve(grammarFile.getFileName());
sendToFile(outputFile, grammarFile.generateAbstractGrammar()); writeToFile(outputFile, grammarFile.generateAbstractGrammar());
} }
sendToFile(getConfiguration().outputDir().toPath().resolve("RagConnect.jadd"), ragConnect.generateAspect(optionRootNode.value())); writeToFile(getConfiguration().outputDir().toPath().resolve("RagConnect.jadd"), ragConnect.generateAspect(optionRootNode.value()));
return 0; return 0;
} }
...@@ -139,7 +141,7 @@ public class Compiler extends AbstractCompiler { ...@@ -139,7 +141,7 @@ public class Compiler extends AbstractCompiler {
System.out.println(message); System.out.println(message);
} }
private void sendToFile(Path path, String str) throws CompilerException { private void writeToFile(Path path, String str) throws CompilerException {
try (BufferedWriter writer = Files.newBufferedWriter(path)) { try (BufferedWriter writer = Files.newBufferedWriter(path)) {
writer.append(str); writer.append(str);
} catch (Exception e) { } catch (Exception e) {
......
public void {{targetParentTypeName}}.{{dependencyMethod}}({{sourceParentTypeName}} source) { public void {{targetParentTypeName}}.{{dependencyMethod}}({{sourceParentTypeName}} source) {
add{{internalRelationPrefix}}Source(source); add{{internalRelationPrefix}}Source(source);
} }
String scheme,host, path; String scheme,host, path;
java.net.URI uri; java.net.URI uri;
try { try {
uri = new java.net.URI({{connectParameterName}}); uri = new java.net.URI({{connectParameterName}});
scheme = uri.getScheme(); scheme = uri.getScheme();
host = uri.getHost(); host = uri.getHost();
path = uri.getPath(); path = uri.getPath();
} catch (java.net.URISyntaxException e) { } catch (java.net.URISyntaxException e) {
System.err.println(e.getMessage()); // Maybe re-throw error? System.err.println(e.getMessage()); // Maybe re-throw error?
return false; return false;
} }
protected static {{toType}} ASTNode.{{methodName}}({{fromType}} {{fromVariableName}}) throws Exception { protected static {{toType}} ASTNode.{{methodName}}({{fromType}} {{fromVariableName}}) throws Exception {
{{{content}}}{{!maybe print line by line to get better indentation}} {{{content}}}{{!maybe print line by line to get better indentation}}
} }
...@@ -6,22 +6,22 @@ aspect ROS2RAG { ...@@ -6,22 +6,22 @@ aspect ROS2RAG {
{{#usesRest}}{{restHandlerField}}.close();{{/usesRest}} {{#usesRest}}{{restHandlerField}}.close();{{/usesRest}}
} }
{{#ReceiveDefinitions}} {{#ReceiveDefinitions}}
{{> receiveDefinition}} {{> receiveDefinition}}
{{/ReceiveDefinitions}} {{/ReceiveDefinitions}}
{{#SendDefinitions}} {{#SendDefinitions}}
{{> sendDefinition}} {{> sendDefinition}}
{{/SendDefinitions}} {{/SendDefinitions}}
{{#MappingDefinitions}} {{#MappingDefinitions}}
{{> mappingDefinition}} {{> mappingDefinition}}
{{/MappingDefinitions}} {{/MappingDefinitions}}
{{#DependencyDefinitions}} {{#DependencyDefinitions}}
{{> dependencyDefinition}} {{> dependencyDefinition}}
{{/DependencyDefinitions}} {{/DependencyDefinitions}}
{{#TokenComponents}} {{#TokenComponents}}
{{> tokenComponent}} {{> tokenComponent}}
{{/TokenComponents}} {{/TokenComponents}}
} }
public boolean {{parentTypeName}}.{{connectMethod}}(String {{connectParameterName}}) throws java.io.IOException { public boolean {{parentTypeName}}.{{connectMethod}}(String {{connectParameterName}}) throws java.io.IOException {
{{>handleUri}} {{>handleUri}}
java.util.function.Consumer<byte[]> consumer = message -> { java.util.function.Consumer<byte[]> consumer = message -> {
{{> mappingApplication}} {{> mappingApplication}}
{{#loggingEnabledForReads}} {{#loggingEnabledForReads}}
System.out.println("[Receive] " + {{connectParameterName}} + " -> {{tokenName}} = " + {{lastResult}}); System.out.println("[Receive] " + {{connectParameterName}} + " -> {{tokenName}} = " + {{lastResult}});
{{/loggingEnabledForReads}} {{/loggingEnabledForReads}}
set{{tokenName}}({{lastResult}}); set{{tokenName}}({{lastResult}});
}; };
switch (scheme) { switch (scheme) {
{{#usesMqtt}} {{#usesMqtt}}
case "mqtt": return {{mqttHandlerAttribute}}().newConnection(uri, consumer); case "mqtt": return {{mqttHandlerAttribute}}().newConnection(uri, consumer);
{{/usesMqtt}} {{/usesMqtt}}
{{#usesRest}} {{#usesRest}}
case "rest": return {{restHandlerAttribute}}().newPUTConnection(uri, input -> { case "rest": return {{restHandlerAttribute}}().newPUTConnection(uri, input -> {
consumer.accept(input.getBytes()); consumer.accept(input.getBytes());
}); });
{{/usesRest}} {{/usesRest}}
default: default:
System.err.println("Unknown protocol '" + scheme + "'."); System.err.println("Unknown protocol '" + scheme + "'.");
return false; return false;
}
} }
}
private Runnable {{parentTypeName}}.{{sender}} = null; private Runnable {{parentTypeName}}.{{sender}} = null;
private byte[] {{parentTypeName}}.{{lastValue}} = null; private byte[] {{parentTypeName}}.{{lastValue}} = null;
public boolean {{parentTypeName}}.{{connectMethod}}(String {{connectParameterName}}, boolean writeCurrentValue) { public boolean {{parentTypeName}}.{{connectMethod}}(String {{connectParameterName}}, boolean writeCurrentValue) {
{{>handleUri}} {{>handleUri}}
switch (scheme) { switch (scheme) {
{{#usesMqtt}} {{#usesMqtt}}
case "mqtt": case "mqtt":
// MqttHandler handler = {{mqttHandlerAttribute}}().resolveHandler(uri);{{!optimize later}} // MqttHandler handler = {{mqttHandlerAttribute}}().resolveHandler(uri);{{!optimize later}}
{{sender}} = () -> { {{sender}} = () -> {
{{#loggingEnabledForWrites}} {{#loggingEnabledForWrites}}
System.out.println("[Send] {{tokenName}} = " + get{{tokenName}}() + " -> " + {{connectParameterName}}); System.out.println("[Send] {{tokenName}} = " + get{{tokenName}}() + " -> " + {{connectParameterName}});
{{/loggingEnabledForWrites}} {{/loggingEnabledForWrites}}
try { try {
{{mqttHandlerAttribute}}().publish(uri, {{lastValue}}); {{mqttHandlerAttribute}}().publish(uri, {{lastValue}});
} catch (java.io.IOException e) { } catch (java.io.IOException e) {
e.printStackTrace(); e.printStackTrace();
}
};
{{updateMethod}}();
if (writeCurrentValue) {
{{writeMethod}}();
} }
break; };
{{/usesMqtt}} {{updateMethod}}();
{{#usesRest}} if (writeCurrentValue) {
case "rest": {{writeMethod}}();
{{restHandlerAttribute}}().newGETConnection(uri, () -> { }
{{updateMethod}}(); break;
return new String({{lastValue}}); {{/usesMqtt}}
}); {{#usesRest}}
break; case "rest":
{{/usesRest}} {{restHandlerAttribute}}().newGETConnection(uri, () -> {
default: {{updateMethod}}();
System.err.println("Unknown protocol '" + scheme + "'."); return new String({{lastValue}});
return false; });
} break;
return true; {{/usesRest}}
default:
System.err.println("Unknown protocol '" + scheme + "'.");
return false;
} }
return true;
}
protected boolean {{parentTypeName}}.{{updateMethod}}() { protected boolean {{parentTypeName}}.{{updateMethod}}() {
{{tokenResetMethod}}(); {{tokenResetMethod}}();
{{> mappingApplication}} {{> mappingApplication}}
{{lastValue}} = {{lastResult}}; {{lastValue}} = {{lastResult}};
// normally we would return true here. unless no connect method was called so far to initialize {{sender}} yet // normally we would return true here. unless no connect method was called so far to initialize {{sender}} yet
return {{sender}} != null; return {{sender}} != null;
} }
protected void {{parentTypeName}}.{{writeMethod}}() { protected void {{parentTypeName}}.{{writeMethod}}() {
{{sender}}.run(); {{sender}}.run();
} }
public {{parentTypeName}} {{parentTypeName}}.set{{name}}({{javaType}} value) { public {{parentTypeName}} {{parentTypeName}}.set{{name}}({{javaType}} value) {
set{{internalName}}(value); set{{internalName}}(value);
{{#DependencyDefinitions}} {{#DependencyDefinitions}}
for ({{targetParentTypeName}} target : get{{internalRelationPrefix}}TargetList()) { for ({{targetParentTypeName}} target : get{{internalRelationPrefix}}TargetList()) {
{{#targetEndpointDefinition}} {{#targetEndpointDefinition}}
{{!#isPush}} {{!#isPush}}
if (target.{{updateMethod}}()) { if (target.{{updateMethod}}()) {
target.{{writeMethod}}(); target.{{writeMethod}}();
}
{{!/isPush}}
{{/targetEndpointDefinition}}
} }
{{/DependencyDefinitions}} {{!/isPush}}
return this; {{/targetEndpointDefinition}}
} }
{{/DependencyDefinitions}}
return this;
}
public {{javaType}} {{parentTypeName}}.get{{name}}() { public {{javaType}} {{parentTypeName}}.get{{name}}() {
return get{{internalName}}(); return get{{internalName}}();
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment