diff --git a/src/main/jastadd/Printing.jrag b/src/main/jastadd/Printing.jrag index 4d070f82a05b8acfc01028abbfe06a50b265f5c0..de093552e1cde9451c3032ae16dbbc80e6fee333 100644 --- a/src/main/jastadd/Printing.jrag +++ b/src/main/jastadd/Printing.jrag @@ -1,14 +1,14 @@ 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); diff --git a/src/main/jastadd/backend/MustacheNodesToYAML.jrag b/src/main/jastadd/backend/MustacheNodesToYAML.jrag index bdd032ad9ee27002413746ae3e3f12135c0d4e5c..b87dcdd049614fcddae916e58ce326c6bbff892c 100644 --- a/src/main/jastadd/backend/MustacheNodesToYAML.jrag +++ b/src/main/jastadd/backend/MustacheNodesToYAML.jrag @@ -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; } } diff --git a/src/main/java/org/jastadd/ragconnect/compiler/Compiler.java b/src/main/java/org/jastadd/ragconnect/compiler/Compiler.java index 12fc212b9b10be507fffa6b56bc4a6816082e87a..711169ec2ea7d9c21b48dc52c5a7d2cf4cc8b427 100644 --- a/src/main/java/org/jastadd/ragconnect/compiler/Compiler.java +++ b/src/main/java/org/jastadd/ragconnect/compiler/Compiler.java @@ -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) { diff --git a/src/main/resources/dependencyDefinition.mustache b/src/main/resources/dependencyDefinition.mustache index b8d74ffe8f8aa5aaa8979682e4442215af1974e8..0ab8d55242816a7d3ada0491c8c7db6dcd70ee42 100644 --- a/src/main/resources/dependencyDefinition.mustache +++ b/src/main/resources/dependencyDefinition.mustache @@ -1,3 +1,3 @@ - public void {{targetParentTypeName}}.{{dependencyMethod}}({{sourceParentTypeName}} source) { - add{{internalRelationPrefix}}Source(source); - } +public void {{targetParentTypeName}}.{{dependencyMethod}}({{sourceParentTypeName}} source) { + add{{internalRelationPrefix}}Source(source); +} diff --git a/src/main/resources/handleUri.mustache b/src/main/resources/handleUri.mustache index a9187f8c610b258b9da28e86da818e13b57d29d5..1a20e7c4df2a4c54ccba4c825fff55ca89a51dec 100644 --- a/src/main/resources/handleUri.mustache +++ b/src/main/resources/handleUri.mustache @@ -1,11 +1,11 @@ - 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; +} diff --git a/src/main/resources/mappingDefinition.mustache b/src/main/resources/mappingDefinition.mustache index 920c5a610b0ce0cff1687122c97b6e18ce63bf7f..fe26e949b7a8e446bf97947e1632624741a83726 100644 --- a/src/main/resources/mappingDefinition.mustache +++ b/src/main/resources/mappingDefinition.mustache @@ -1,3 +1,3 @@ - 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}} +} diff --git a/src/main/resources/ragconnect.mustache b/src/main/resources/ragconnect.mustache index 5b12e3fae20555a7fe9055b226ca0cb96bcff8f5..a39510d73854301efb1f05fe7f9c1fd476916ef7 100644 --- a/src/main/resources/ragconnect.mustache +++ b/src/main/resources/ragconnect.mustache @@ -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}} } diff --git a/src/main/resources/receiveDefinition.mustache b/src/main/resources/receiveDefinition.mustache index 62e2bf6faf441fe7c6d700bb8b4daf6168196c3e..1dd0e0ddbfbfd2acf03536ff41a2d740043eb34b 100644 --- a/src/main/resources/receiveDefinition.mustache +++ b/src/main/resources/receiveDefinition.mustache @@ -1,23 +1,23 @@ - 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; } +} diff --git a/src/main/resources/sendDefinition.mustache b/src/main/resources/sendDefinition.mustache index 517baab18824ead12ebca5f1d22901b3a960b0ba..fac4ecd37967b8047c9101b58fab273527979ab2 100644 --- a/src/main/resources/sendDefinition.mustache +++ b/src/main/resources/sendDefinition.mustache @@ -1,51 +1,51 @@ - 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(); +} diff --git a/src/main/resources/tokenComponent.mustache b/src/main/resources/tokenComponent.mustache index 7de943a37edd9358fec19e53575629a44e976cff..e78efb96b846bd7aa814723472032299a70c7de3 100644 --- a/src/main/resources/tokenComponent.mustache +++ b/src/main/resources/tokenComponent.mustache @@ -1,19 +1,19 @@ - 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}}(); +}