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
aspect Printing {
String ASTNode.PRINT_INDENT = " ";
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()) {
element.prettyPrint(sb, false, "");
}
if (sb.charAt(sb.length() - 1) != '\n') {
sb.append("\n");
}
sb.append("#end");
return sb.toString();
}
syn StringBuilder Element.prettyPrint(StringBuilder sb, boolean printIndent, String indent);
......
......@@ -3,6 +3,7 @@ aspect MustacheNodesToYAML {
Document doc = new Document();
MappingElement root = new MappingElement();
root.addKeyValuePair("rootNodeName", StringElement.of(rootNodeName()));
root.addKeyValuePair("closeMethod", StringElement.of(closeMethod()));
root.addKeyValuePair("usesMqtt", ValueElement.of(usesMqtt));
root.addKeyValuePair("usesRest", ValueElement.of(usesRest));
// mqtt
......@@ -18,7 +19,7 @@ aspect MustacheNodesToYAML {
inner.addKeyValuePair("name", StringElement.of(comp.name()));
rootTypeComponents.addElement(inner);
}
root.addKeyValuePair("rootTypeComponents", rootTypeComponents);
root.addKeyValuePair("RootTypeComponents", rootTypeComponents);
// rest
root.addKeyValuePair("restHandlerField", StringElement.of(restHandlerField()));
......@@ -63,92 +64,93 @@ aspect MustacheNodesToYAML {
return doc;
}
syn Element MReceiveDefinition.toYAML() {
MappingElement inner = new MappingElement();
inner.addKeyValuePair("parentTypeName", StringElement.of(parentTypeName()));
inner.addKeyValuePair("connectMethod", StringElement.of(connectMethod()));
inner.addKeyValuePair("connectParameterName", StringElement.of(connectParameterName()));
inner.addKeyValuePair("lastDefinitionToType", StringElement.of(lastDefinitionToType()));
inner.addKeyValuePair("resultVarPrefix", StringElement.of(resultVarPrefix()));
inner.addKeyValuePair("lastDefinitionName", StringElement.of(lastDefinitionName()));
inner.addKeyValuePair("preemptiveReturn", StringElement.of(preemptiveReturn()));
inner.addKeyValuePair("alwaysApply", ValueElement.of(alwaysApply()));
inner.addKeyValuePair("condition", StringElement.of(condition()));
inner.addKeyValuePair("loggingEnabledForReads", ValueElement.of(loggingEnabledForReads));
inner.addKeyValuePair("tokenName", StringElement.of(tokenName()));
inner.addKeyValuePair("lastResult", StringElement.of(lastResult()));
// inner.addKeyValuePair("first", ValueElement.of(isFirst()));
// parentTypeName
// connectMethod
// connectParameterName
// lastDefinitionToType
// resultVarPrefix
// lastDefinitionName
// #InnerMappingDefinitions -> MInnerMappingDefinition
// last
// toType
// methodName
// inputVarName
// preemptiveReturn
// alwaysApply
// condition - special chars
// loggingEnabledForReads
// tokenName
// lastResult
return inner;
syn MappingElement MEndpointDefinition.toYAML() {
MappingElement result = new MappingElement();
result.addKeyValuePair("parentTypeName", StringElement.of(parentTypeName()));
result.addKeyValuePair("connectMethod", StringElement.of(connectMethod()));
result.addKeyValuePair("connectParameterName", StringElement.of(connectParameterName()));
result.addKeyValuePair("lastDefinitionToType", StringElement.of(lastDefinitionToType()));
result.addKeyValuePair("resultVarPrefix", StringElement.of(resultVarPrefix()));
result.addKeyValuePair("lastDefinitionName", StringElement.of(lastDefinitionName()));
result.addKeyValuePair("preemptiveReturn", StringElement.of(preemptiveReturn()));
result.addKeyValuePair("alwaysApply", ValueElement.of(alwaysApply()));
result.addKeyValuePair("condition", StringElement.of(
condition().replace("\"", "\\\"").replace("\n", "\\n")));
result.addKeyValuePair("lastResult", StringElement.of(lastResult()));
result.addKeyValuePair("InnerMappingDefinitions", innerMappingDefinitionsAsListElement());
result.addKeyValuePair("tokenName", StringElement.of(tokenName()));
return result;
}
syn Element MSendDefinition.toYAML() {
MappingElement inner = new MappingElement();
// #SendDefinitions -> MSendDefinition
// parentTypeName
// sender
// lastValue
// connectMethod
// connectParameterName
// loggingEnabledForWrites
// updateMethod
// writeMethod
// tokenResetMethod
// lastResult
return inner;
syn MappingElement MReceiveDefinition.toYAML() {
MappingElement result = super.toYAML();
result.addKeyValuePair("loggingEnabledForReads", ValueElement.of(loggingEnabledForReads));
return result;
}
syn MappingElement MSendDefinition.toYAML() {
MappingElement result = super.toYAML();
result.addKeyValuePair("sender", StringElement.of(sender()));
result.addKeyValuePair("lastValue", StringElement.of(lastValue()));
result.addKeyValuePair("loggingEnabledForWrites", ValueElement.of(loggingEnabledForWrites));
result.addKeyValuePair("updateMethod", StringElement.of(updateMethod()));
result.addKeyValuePair("writeMethod", StringElement.of(writeMethod()));
result.addKeyValuePair("tokenResetMethod", StringElement.of(tokenResetMethod()));
return result;
}
syn Element MMappingDefinition.toYAML() {
MappingElement inner = new MappingElement();
// #MappingDefinitions -> MMappingDefinition
// toType
// methodName
// fromType
// fromVariableName
// content - special chars
return inner;
MappingElement result = new MappingElement();
result.addKeyValuePair("toType", StringElement.of(toType()));
result.addKeyValuePair("methodName", StringElement.of(methodName()));
result.addKeyValuePair("fromType", StringElement.of(fromType()));
result.addKeyValuePair("fromVariableName", StringElement.of(fromVariableName()));
result.addKeyValuePair("content", StringElement.of(
content().replace("\"", "\\\"").replace("\n", "\\n")));
return result;
}
syn Element MDependencyDefinition.toYAML() {
MappingElement inner = new MappingElement();
// #DependencyDefinitions -> MDependencyDefinition
// targetParentTypeName
// dependencyMethod
// sourceParentTypeName
// internalRelationPrefix
return inner;
MappingElement result = new MappingElement();
result.addKeyValuePair("targetParentTypeName", StringElement.of(targetParentTypeName()));
result.addKeyValuePair("dependencyMethod", StringElement.of(dependencyMethod()));
result.addKeyValuePair("sourceParentTypeName", StringElement.of(sourceParentTypeName()));
result.addKeyValuePair("internalRelationPrefix", StringElement.of(internalRelationPrefix()));
return result;
}
syn Element MTokenComponent.toYAML() {
MappingElement inner = new MappingElement();
// #TokenComponents -> MTokenComponent
// parentTypeName
// name
// javaType
// internalName
// #DependencyDefinitions -> MDependencyDefinition
// targetParentTypeName
// internalRelationPrefix
// #targetEndpointDefinition -> MSendDefinition
// updateMethod
// writeMethod
return inner;
MappingElement result = new MappingElement();
result.addKeyValuePair("parentTypeName", StringElement.of(parentTypeName()));
result.addKeyValuePair("name", StringElement.of(name()));
result.addKeyValuePair("javaType", StringElement.of(javaType()));
result.addKeyValuePair("internalName", StringElement.of(internalName()));
ListElement dependencyDefinitions = new ListElement();
for (MDependencyDefinition def : getDependencyDefinitionList()) {
MappingElement inner = new MappingElement();
inner.addKeyValuePair("targetParentTypeName", StringElement.of(def.targetParentTypeName()));
inner.addKeyValuePair("internalRelationPrefix", StringElement.of(def.internalRelationPrefix()));
MappingElement targetEndpointDefinition = new MappingElement();
targetEndpointDefinition.addKeyValuePair("updateMethod", StringElement.of(def.targetEndpointDefinition().updateMethod()));
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 {
if (optionPrintYaml.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;
}
......@@ -98,9 +100,9 @@ public class Compiler extends AbstractCompiler {
}
for (GrammarFile grammarFile : ragConnect.getProgram().getGrammarFileList()) {
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;
}
......@@ -139,7 +141,7 @@ public class Compiler extends AbstractCompiler {
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)) {
writer.append(str);
} catch (Exception e) {
......
public void {{targetParentTypeName}}.{{dependencyMethod}}({{sourceParentTypeName}} source) {
add{{internalRelationPrefix}}Source(source);
}
public void {{targetParentTypeName}}.{{dependencyMethod}}({{sourceParentTypeName}} source) {
add{{internalRelationPrefix}}Source(source);
}
String scheme,host, path;
java.net.URI uri;
try {
uri = new java.net.URI({{connectParameterName}});
scheme = uri.getScheme();
host = uri.getHost();
path = uri.getPath();
} catch (java.net.URISyntaxException e) {
System.err.println(e.getMessage()); // Maybe re-throw error?
return false;
}
String scheme,host, path;
java.net.URI uri;
try {
uri = new java.net.URI({{connectParameterName}});
scheme = uri.getScheme();
host = uri.getHost();
path = uri.getPath();
} catch (java.net.URISyntaxException e) {
System.err.println(e.getMessage()); // Maybe re-throw error?
return false;
}
protected static {{toType}} ASTNode.{{methodName}}({{fromType}} {{fromVariableName}}) throws Exception {
{{{content}}}{{!maybe print line by line to get better indentation}}
}
protected static {{toType}} ASTNode.{{methodName}}({{fromType}} {{fromVariableName}}) throws Exception {
{{{content}}}{{!maybe print line by line to get better indentation}}
}
......@@ -6,22 +6,22 @@ aspect ROS2RAG {
{{#usesRest}}{{restHandlerField}}.close();{{/usesRest}}
}
{{#ReceiveDefinitions}}
{{> receiveDefinition}}
{{> receiveDefinition}}
{{/ReceiveDefinitions}}
{{#SendDefinitions}}
{{> sendDefinition}}
{{> sendDefinition}}
{{/SendDefinitions}}
{{#MappingDefinitions}}
{{> mappingDefinition}}
{{> mappingDefinition}}
{{/MappingDefinitions}}
{{#DependencyDefinitions}}
{{> dependencyDefinition}}
{{> dependencyDefinition}}
{{/DependencyDefinitions}}
{{#TokenComponents}}
{{> tokenComponent}}
{{> tokenComponent}}
{{/TokenComponents}}
}
public boolean {{parentTypeName}}.{{connectMethod}}(String {{connectParameterName}}) throws java.io.IOException {
{{>handleUri}}
java.util.function.Consumer<byte[]> consumer = message -> {
{{> mappingApplication}}
{{#loggingEnabledForReads}}
System.out.println("[Receive] " + {{connectParameterName}} + " -> {{tokenName}} = " + {{lastResult}});
{{/loggingEnabledForReads}}
set{{tokenName}}({{lastResult}});
};
switch (scheme) {
{{#usesMqtt}}
case "mqtt": return {{mqttHandlerAttribute}}().newConnection(uri, consumer);
{{/usesMqtt}}
{{#usesRest}}
case "rest": return {{restHandlerAttribute}}().newPUTConnection(uri, input -> {
consumer.accept(input.getBytes());
});
{{/usesRest}}
default:
System.err.println("Unknown protocol '" + scheme + "'.");
return false;
}
public boolean {{parentTypeName}}.{{connectMethod}}(String {{connectParameterName}}) throws java.io.IOException {
{{>handleUri}}
java.util.function.Consumer<byte[]> consumer = message -> {
{{> mappingApplication}}
{{#loggingEnabledForReads}}
System.out.println("[Receive] " + {{connectParameterName}} + " -> {{tokenName}} = " + {{lastResult}});
{{/loggingEnabledForReads}}
set{{tokenName}}({{lastResult}});
};
switch (scheme) {
{{#usesMqtt}}
case "mqtt": return {{mqttHandlerAttribute}}().newConnection(uri, consumer);
{{/usesMqtt}}
{{#usesRest}}
case "rest": return {{restHandlerAttribute}}().newPUTConnection(uri, input -> {
consumer.accept(input.getBytes());
});
{{/usesRest}}
default:
System.err.println("Unknown protocol '" + scheme + "'.");
return false;
}
}
private Runnable {{parentTypeName}}.{{sender}} = null;
private byte[] {{parentTypeName}}.{{lastValue}} = null;
private Runnable {{parentTypeName}}.{{sender}} = null;
private byte[] {{parentTypeName}}.{{lastValue}} = null;
public boolean {{parentTypeName}}.{{connectMethod}}(String {{connectParameterName}}, boolean writeCurrentValue) {
{{>handleUri}}
switch (scheme) {
{{#usesMqtt}}
case "mqtt":
// MqttHandler handler = {{mqttHandlerAttribute}}().resolveHandler(uri);{{!optimize later}}
{{sender}} = () -> {
{{#loggingEnabledForWrites}}
System.out.println("[Send] {{tokenName}} = " + get{{tokenName}}() + " -> " + {{connectParameterName}});
{{/loggingEnabledForWrites}}
try {
{{mqttHandlerAttribute}}().publish(uri, {{lastValue}});
} catch (java.io.IOException e) {
e.printStackTrace();
}
};
{{updateMethod}}();
if (writeCurrentValue) {
{{writeMethod}}();
public boolean {{parentTypeName}}.{{connectMethod}}(String {{connectParameterName}}, boolean writeCurrentValue) {
{{>handleUri}}
switch (scheme) {
{{#usesMqtt}}
case "mqtt":
// MqttHandler handler = {{mqttHandlerAttribute}}().resolveHandler(uri);{{!optimize later}}
{{sender}} = () -> {
{{#loggingEnabledForWrites}}
System.out.println("[Send] {{tokenName}} = " + get{{tokenName}}() + " -> " + {{connectParameterName}});
{{/loggingEnabledForWrites}}
try {
{{mqttHandlerAttribute}}().publish(uri, {{lastValue}});
} catch (java.io.IOException e) {
e.printStackTrace();
}
break;
{{/usesMqtt}}
{{#usesRest}}
case "rest":
{{restHandlerAttribute}}().newGETConnection(uri, () -> {
{{updateMethod}}();
return new String({{lastValue}});
});
break;
{{/usesRest}}
default:
System.err.println("Unknown protocol '" + scheme + "'.");
return false;
}
return true;
};
{{updateMethod}}();
if (writeCurrentValue) {
{{writeMethod}}();
}
break;
{{/usesMqtt}}
{{#usesRest}}
case "rest":
{{restHandlerAttribute}}().newGETConnection(uri, () -> {
{{updateMethod}}();
return new String({{lastValue}});
});
break;
{{/usesRest}}
default:
System.err.println("Unknown protocol '" + scheme + "'.");
return false;
}
return true;
}
protected boolean {{parentTypeName}}.{{updateMethod}}() {
{{tokenResetMethod}}();
{{> mappingApplication}}
{{lastValue}} = {{lastResult}};
// normally we would return true here. unless no connect method was called so far to initialize {{sender}} yet
return {{sender}} != null;
}
protected boolean {{parentTypeName}}.{{updateMethod}}() {
{{tokenResetMethod}}();
{{> mappingApplication}}
{{lastValue}} = {{lastResult}};
// normally we would return true here. unless no connect method was called so far to initialize {{sender}} yet
return {{sender}} != null;
}
protected void {{parentTypeName}}.{{writeMethod}}() {
{{sender}}.run();
}
protected void {{parentTypeName}}.{{writeMethod}}() {
{{sender}}.run();
}
public {{parentTypeName}} {{parentTypeName}}.set{{name}}({{javaType}} value) {
set{{internalName}}(value);
{{#DependencyDefinitions}}
for ({{targetParentTypeName}} target : get{{internalRelationPrefix}}TargetList()) {
{{#targetEndpointDefinition}}
{{!#isPush}}
if (target.{{updateMethod}}()) {
target.{{writeMethod}}();
}
{{!/isPush}}
{{/targetEndpointDefinition}}
public {{parentTypeName}} {{parentTypeName}}.set{{name}}({{javaType}} value) {
set{{internalName}}(value);
{{#DependencyDefinitions}}
for ({{targetParentTypeName}} target : get{{internalRelationPrefix}}TargetList()) {
{{#targetEndpointDefinition}}
{{!#isPush}}
if (target.{{updateMethod}}()) {
target.{{writeMethod}}();
}
{{/DependencyDefinitions}}
return this;
{{!/isPush}}
{{/targetEndpointDefinition}}
}
{{/DependencyDefinitions}}
return this;
}
public {{javaType}} {{parentTypeName}}.get{{name}}() {
return get{{internalName}}();
}
public {{javaType}} {{parentTypeName}}.get{{name}}() {
return get{{internalName}}();
}
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