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

WIP: More mustache templates.

parent 7d6a79a5
Pipeline #7145 failed with stages
in 1 minute and 51 seconds
TypeComponentMustache ;
rel TypeComponentMustache.TypeComponent -> TypeComponent ;
//TypeComponentMustache ;
//rel TypeComponentMustache.TypeComponent -> TypeComponent ;
MRos2Rag ::= ReadDefinition:MReadDefinition* WriteDefinition:MWriteDefinition* MappingDefinition:MMappingDefinition* DependencyDefinition:MDependencyDefinition* TypeComponent:MTypeComponent*;
MUpdateDefinition ::= <FirstInputVarName>;
MReadDefinition : MUpdateDefinition;
MWriteDefinition : MUpdateDefinition;
MMappingDefinition;
MDependencyDefinition;
MTypeComponent;
MUpdateDefinition.InnerMappingDefinition* -> MInnerMappingDefinition;
MInnerMappingDefinition.MappingDefinition -> MappingDefinition;
MReadDefinition.ReadFromMqttDefinition -> ReadFromMqttDefinition;
MWriteDefinition.WriteToMqttDefinition -> WriteToMqttDefinition;
MMappingDefinition.MappingDefinition -> MappingDefinition;
MDependencyDefinition.DependencyDefinition -> DependencyDefinition;
MTypeComponent.TypeComponent -> TypeComponent;
MTypeComponent.DependencyDefinition* -> MDependencyDefinition;
......@@ -49,6 +49,10 @@ aspect Navigation {
syn WriteToMqttDefinition UpdateDefinition.asWriteToMqttDefinition() = null;
eq WriteToMqttDefinition.asWriteToMqttDefinition() = this;
// --- asReadFromMqttDefinition ---
syn ReadFromMqttDefinition UpdateDefinition.asReadFromMqttDefinition() = null;
eq ReadFromMqttDefinition.asReadFromMqttDefinition() = this;
// --- targetUpdateDefinition ---
syn WriteToMqttDefinition DependencyDefinition.targetUpdateDefinition() {
// resolve definition in here, as we do not need resolveMethod in any other place (yet)
......
......@@ -21,6 +21,154 @@ aspect GenerationUtils {
}
}
/* Open questions
- Should all string constants be defined on the normal AST, or on the special mustache AST?
*/
aspect AttributesForMustache {
// --- MRos2Rag ---
// --- MUpdateDefinition ---
syn String MUpdateDefinition.preemptiveExpectedValue();
syn String MUpdateDefinition.preemptiveReturn();
syn String MUpdateDefinition.mqttUpdaterAttribute();
syn UpdateDefinition MUpdateDefinition.updateDef();
eq MUpdateDefinition.getMInnerMappingDefinition(int i).isLast() = i == numInnerMappingDefinition();
eq MUpdateDefinition.getInnerMappingDefinition().resultVarPrefix() = resultVarPrefix();
eq MUpdateDefinition.getInnerMappingDefinition(int i).inputVarName() = i == 0 ? getFirstInputVarName() : resultVarPrefix() + getMInnerMappingDefinition(i - 1).getMappingDefinition().methodName();
syn String MUpdateDefinition.connectMethod() = updateDef().connectMethod();
syn TokenComponent MUpdateDefinition.token() = updateDef().getToken();
syn boolean MUpdateDefinition.alwaysApply() = updateDef().getAlwaysApply();
syn String MUpdateDefinition.resultVarPrefix() = "result"; // we do not need "_" here, because methodName begins with one
syn String MUpdateDefinition.parentTypeName() = token().containingTypeDecl().getName();
syn String MUpdateDefinition.TokenName() = token().getName();
syn MInnerMappingDefinition MUpdateDefinition.lastDefinition() = getInnerMappingDefinition(numInnerMappingDefinition() - 1);
syn String MUpdateDefinition.lastDefinitionType() = lastDefinition().ToType();
syn String MUpdateDefinition.lastDefinitionName() = lastDefinition().methodName();
syn String MUpdateDefinition.condition() {
String lastVarName = resultVarPrefix() + lastDefinitionDefinitionName();
if (lastDefinition().getMappingDefinition().getToType().isArray()) {
return "java.util.Arrays.equals(" + preemptiveExpectedValue() + ", " + lastVarName + ")";
}
if (token().isPrimitiveType() && lastDefinition().getMappingDefinition().getToType().isPrimitiveType()) {
return preemptiveExpectedValue() + " == " + lastVarName;
}
if (lastDefinition().getMappingDefinition().isDefaultMappingDefinition()) {
return preemptiveExpectedValue() + " != null && " + preemptiveExpectedValue() + ".equals(" + lastVarName ")";
}
return preemptiveExpectedValue() + " != null ? " + preemptiveExpectedValue() + ".equals(" + lastVarName ") : " + inputVariableName + " == null";
}
// --- MInnerMappingDefinition ---
inh boolean MInnerMappingDefinition.isLast();
inh String MInnerMappingDefinition.resultVarPrefix();
syn String MInnerMappingDefinition.ToType() = getMappingDefinition().getToType().prettyPrint();
syn String MInnerMappingDefinition.methodName() = getMappingDefinition().methodName();
inh String MInnerMappingDefinition.inputVarName();
// --- MReadDefinition ---
eq MReadDefinition.connectMethod() = getReadFromMqttDefinition().connectMethod();
eq MReadDefinition.preemptiveExpectedValue() = "get" + TokenName() + "()";
eq MReadDefinition.preemptiveReturn() = "return;";
eq MReadDefinition.updateDef() = getReadFromMqttDefinition();
syn String MReadDefinition.mqttUpdaterAttribute() = getReadFromMqttDefinition().mqttUpdaterAttribute;
syn String MReadDefinition.lastResult() = getReadFromMqttDefinition();
// --- MWriteDefinition ---
eq MWriteDefinition.preemptiveExpectedValue() = lastValue();
eq MWriteDefinition.preemptiveReturn() = "return false;";
eq MWriteDefinition.updateDef() = getWriteToMqttDefinition();
syn String MWriteDefinition.writeTopic() = getWriteToMqttDefinition().writeTopic();
syn String MWriteDefinition.lastValue() = getWriteToMqttDefinition().lastValue();
syn String MWriteDefinition.updateMethod() = getWriteToMqttDefinition().updateMethod();
syn String MWriteDefinition.writeMethod() = getWriteToMqttDefinition().writeMethod();
syn String MWriteDefinition.tokenResetMethod() = getWriteToMqttDefinition().tokenResetMethod();
// --- MMappingDefinition ---
syn String MMappingDefinition.toType() = getMappingDefinition().getToType().prettyPrint();
syn String MMappingDefinition.methodName() = getMappingDefinition().methodName();
syn String MMappingDefinition.fromType() = getMappingDefinition().getFromType().prettyPrint();
syn String MMappingDefinition.fromVariableName() = getMappingDefinition().getFromVariableName();
syn String MMappingDefinition.content() = getMappingDefinition().getContent();
// --- MDependencyDefinition ---
syn String MDependencyDefinition.targetParentTypeName() = getDependencyDefinition().getTarget().containingTypeDecl().getName();
syn String MDependencyDefinition.dependencyMethod() = getDependencyDefinition().dependencyMethod();
syn String MDependencyDefinition.sourceParentTypeName() = getDependencyDefinition().getSource().containingTypeDecl().getName();
syn String MDependencyDefinition.internalRelationPrefix() = getDependencyDefinition().internalRelationPrefix();
syn nta MUpdateDefinition MDependencyDefinition.targetUpdateDefinition() {
return wrap(getDependencyDefinition().targetUpdateDefinition());
}
// --- MTypeComponent ---
syn String MTypeComponent.parentTypeName() = getTypeComponent().containingTypeDecl().getName();
syn String MTypeComponent.name() = getTypeComponent().getName();
syn String MTypeComponent.javaType() = getTypeComponent().getJavaTypeUse().prettyPrint();
syn String MTypeComponent.internalName() = getTypeComponent().internalName();
// --- wrap ---
syn lazy MRos2Rag Ros2Rag.wrap() {
MRos2Rag result = new MRos2Rag();
for (UpdateDefinition def : getUpdateDefinitionList()) {
if (def.isWriteToMqttDefinition()) {
result.addWriteDefinition(def.asWriteToMqttDefinition.wrap());
} else {
result.addReadDefinition(def.asReadFromMqttDefinition().wrap());
}
}
for (MappingDefinition def : allMappingDefinitions()) {
result.addMappingDefinition(def.wrap());
}
for (DependencyDefinition def : getDependencyDefinitionList()) {
result.addDependencyDefinition(def.wrap());
}
for (TokenComponent token : getProgram().allTokenComponents()) {
result.addTokenComponent(token.wrap());
}
return result;
}
//MInnerMappingDefinition.MappingDefinition -> MappingDefinition;
private void MUpdateDefinition.addInnerMappings() {
for (MappingDefinition def : updateDefinition().effectiveMappings()) {
addInnerMappingDefinition(def.wrap());
}
}
syn lazy MReadDefinition ReadFromMqttDefinition.wrap() {
MReadDefinition result = new MReadDefinition();
result.setReadFromMqttDefinition(this);
result.addInnerMappings();
return result;
}
syn lazy MWriteDefinition WriteToMqttDefinition.wrap() {
MWriteDefinition result = new MWriteDefinition();
result.setWriteToMqttDefinition(this);
result.addInnerMappings();
return result;
}
syn lazy MMappingDefinition MappingDefinition.wrap() {
MMappingDefinition result = new MMappingDefinition();
result.setMappingDefinition(this);
return result;
}
syn lazy MDependencyDefinition DependencyDefinition.wrap() {
MDependencyDefinition result = new MDependencyDefinition();
result.setDependencyDefinition(this);
return result;
}
syn lazy MTypeComponent TypeComponent.wrap() {
MTypeComponent result = new MTypeComponent();
result.setTypeComponent(this);
for (DependencyDefinition def : getDependencySourceDefinitionList()) {
result.addDependencyDefinition(def.wrap());
}
return result;
}
}
aspect AspectGeneration {
// naming convention attributes
syn String TokenComponent.internalName() = getDependencySourceDefinitionList().isEmpty() ? externalName() : "_internal_" + getName();
......
public void {{targetParentTypeName}}.{{dependencyMethod}}({{sourceParentTypeName}} source) {
add{{internalRelationPrefix}}Source(source);
}
{{lastDefinitionToType}} {{resultVarPrefix}}{{lastDefinitionName}};
try {
{{#InnerMappingDefinitions}}
{{^isLast}}{{ToType}} {{/isLast}}{{resultVarPrefix}}{{methodName}} = {{methodName}}({{inputVarName}});{{!inputVarName has to be computed beforehand}}
{{/InnerMappingDefinitions}}
} catch (Exception e) {
e.printStackTrace();
{{preemptiveReturn}}
}
{{^alwaysApply}}
if ({{condition}}) {
{{preemptiveReturn}}
}
{{/alwaysApply}}
protected static {{toType}} ASTNode.{{methodName}}({{fromType}} {{fromVariableName}}) throws Exception {
{{content}}{{!maybe print line by line to get better indentation}}
}
public void {{parentTypeName}}.{{connectMethod}}(String topic) {
{{mqttUpdaterAttribute}}().newConnection(topic, message -> {
{{> mappingApplication}}
{{#loggingEnabledForReads}}
System.out.println("[Read] " + topic + " -> {{TokenName}} = " + {{lastResult}});{{!lastResult has to be a new attribute}}
{{/loggingEnabledForReads}}
set{{TokenName}}({{lastResult}});
});
}
aspect ROS2RAG {
{{#ReadDefinitions}}
{{> readDefinition}}
{{/ReadDefinitions}}
{{#WriteDefinitions}}
{{> writeDefinition}}
{{/WriteDefinitions}}
{{#MappingDefinitions}}
{{> mappingDefinition}}
{{/MappingDefinitions}}
{{#DependencyDefinitions}}
{{> dependencyDefinition}}
{{/DependencyDefinitions}}
{{#TokenComponents}}
{{> tokenComponent}}
{{/TokenComponents}}
}
public {{parentTypeName}} {{parentTypeName}}.set{{name}}({{javaType}} value) {
set{{internalName}}(value);
{{#DependencyDefinitions}}
for ({{targetParentType}} target : get{{internalRelationPrefix}}TargetList) {
{{#targetUpdateDefinition}}
if (target.{{updateMethod}}()) {
target.{{writeMethod}}();
}
{{/targetUpdateDefinition}}
}
{{/DependencyDefinitions}}
return this;
}
public {{javaType}} {{parentTypeName}}.get{{name}}() {
return get{{internalName}}();
}
private String {{parentTypeName}}.{{writeTopic}} = null;
private byte[] {{parentTypeName}}.{{lastValue}} = null;
public void {{parentTypeName}}.{{connectMethod}}(String topic, boolean writeCurrentValue) {
{{writeTopic}} = topic;
{{updateMethod}}();
if (writeCurrentValue) {
{{writeMethod}}();
}
}
protected boolean {{parentTypeName}}.{{updateMethod}}() {
{{tokenResetMethod}}();
{{> mappingApplication}}
{{lastValue}} = {{lastResult}};
return true;
}
protected void {{parentTypeName}}.{{writeMethod}}() {
{{#loggingEnabledForWrites}}
System.out.println("[Write] {{TokenName}} = " + get{{TokenName}}() + " -> {{writeTopic}});
{{/loggingEnabledForWrites}}
{{mqttUpdateAttribute}}().publish({{writeTopic}}, {{lastValue}});
}
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