Refactoring: extracted fixRosConnectionSymbols to RosHelper

parent 92581f02
package de.monticore.lang.monticar.generator.middleware;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ConnectorSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ExpandedComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.PortSymbol;
import de.monticore.lang.embeddedmontiarc.tagging.MiddlewareSymbol;
import de.monticore.lang.embeddedmontiarc.tagging.RosConnectionSymbol;
import de.monticore.lang.monticar.generator.FileContent;
import de.monticore.lang.monticar.generator.middleware.helpers.ClusterHelper;
import de.monticore.lang.monticar.generator.middleware.helpers.FileHelper;
import de.monticore.lang.monticar.generator.middleware.helpers.RosHelper;
import de.monticore.lang.monticar.generator.middleware.helpers.TemplateHelper;
import de.monticore.lang.monticar.generator.middleware.impls.GeneratorImpl;
import de.monticore.lang.monticar.generator.rosmsg.GeneratorRosMsg;
import de.monticore.lang.monticar.generator.rosmsg.RosMsg;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
import de.se_rwth.commons.logging.Log;
import org.apache.commons.io.FileUtils;
import java.io.File;
......@@ -79,83 +73,7 @@ public class DistributedTargetGenerator extends CMakeGenerator {
}
private void fixComponentInstance(ExpandedComponentInstanceSymbol componentInstanceSymbol) {
fixRosTopics(componentInstanceSymbol);
}
private void fixRosTopics(ExpandedComponentInstanceSymbol componentInstanceSymbol) {
componentInstanceSymbol.getConnectors().stream()
.filter(connectorSymbol -> connectorSymbol.getSourcePort().isRosPort() && connectorSymbol.getTargetPort().isRosPort())
.forEach(connectorSymbol -> {
if (Objects.equals(connectorSymbol.getSourcePort().getComponentInstance().orElse(null), componentInstanceSymbol)) {
//In port of supercomp
inferRosConnectionIfPossible(connectorSymbol.getSourcePort(), connectorSymbol.getTargetPort());
generateRosConnectionIfPossible(connectorSymbol);
} else if (Objects.equals(connectorSymbol.getTargetPort().getComponentInstance().orElse(null), componentInstanceSymbol)) {
//out port of supercomp
inferRosConnectionIfPossible(connectorSymbol.getTargetPort(), connectorSymbol.getSourcePort());
generateRosConnectionIfPossible(connectorSymbol);
} else {
//In between subcomps
generateRosConnectionIfPossible(connectorSymbol);
}
});
}
private void generateRosConnectionIfPossible(ConnectorSymbol connectorSymbol) {
MiddlewareSymbol sourceTag = connectorSymbol.getSourcePort().getMiddlewareSymbol().orElse(null);
MiddlewareSymbol targetTag = connectorSymbol.getTargetPort().getMiddlewareSymbol().orElse(null);
if (sourceTag == null || targetTag == null || !sourceTag.isKindOf(RosConnectionSymbol.KIND) || !targetTag.isKindOf(RosConnectionSymbol.KIND)) {
Log.debug("Both sourcePort and targetPort need to have a RosConnectionSymbol", "RosConnectionSymbol");
return;
}
RosConnectionSymbol rosConnectionA = (RosConnectionSymbol) sourceTag;
RosConnectionSymbol rosConnectionB = (RosConnectionSymbol) targetTag;
RosConnectionSymbol emptyRosConnection = new RosConnectionSymbol();
if (!rosConnectionA.equals(emptyRosConnection) || !rosConnectionB.equals(emptyRosConnection)) {
Log.debug("Will not override rosConnections that are not empty!", "RosConnectionSymbol");
return;
}
//target port name is unique: each in port can only have one connection!
String topicName = connectorSymbol.getTargetPort().getFullName().replace(".", "_");
RosMsg rosTypeA = GeneratorRosMsg.getRosType("struct_msgs", connectorSymbol.getTargetPort().getTypeReference());
RosMsg rosTypeB = GeneratorRosMsg.getRosType("struct_msgs", connectorSymbol.getSourcePort().getTypeReference());
if (!rosTypeA.equals(rosTypeB)) {
Log.error("topicType mismatch! "
+ connectorSymbol.getSourcePort().getFullName() + " has " + rosTypeB + " and "
+ connectorSymbol.getTargetPort().getFullName() + " has " + rosTypeA);
return;
}
if (rosTypeA.getFields().size() == 1) {
connectorSymbol.getSourcePort().setMiddlewareSymbol(new RosConnectionSymbol(topicName, rosTypeB.getName(), rosTypeB.getFields().get(0).getName()));
connectorSymbol.getTargetPort().setMiddlewareSymbol(new RosConnectionSymbol(topicName, rosTypeA.getName(), rosTypeA.getFields().get(0).getName()));
} else {
connectorSymbol.getSourcePort().setMiddlewareSymbol(new RosConnectionSymbol(topicName, rosTypeB.getName()));
connectorSymbol.getTargetPort().setMiddlewareSymbol(new RosConnectionSymbol(topicName, rosTypeA.getName()));
}
}
private void inferRosConnectionIfPossible(PortSymbol sourcePort, PortSymbol targetPort) {
MiddlewareSymbol sourceTag = sourcePort.getMiddlewareSymbol().orElse(null);
MiddlewareSymbol targetTag = targetPort.getMiddlewareSymbol().orElse(null);
if (sourceTag == null || targetTag == null || !sourceTag.isKindOf(RosConnectionSymbol.KIND) || !targetTag.isKindOf(RosConnectionSymbol.KIND)) {
Log.debug("Both sourcePort and targetPort need to have a RosConnectionSymbol", "RosConnectionSymbol");
return;
}
RosConnectionSymbol sourceRosConnection = (RosConnectionSymbol) sourceTag;
RosConnectionSymbol targetRosConnection = (RosConnectionSymbol) targetTag;
if (sourceRosConnection.getTopicName().isPresent() && !targetRosConnection.getTopicName().isPresent())
targetRosConnection.setTopicName(sourceRosConnection.getTopicName().get());
if (sourceRosConnection.getTopicType().isPresent() && !targetRosConnection.getTopicType().isPresent())
targetRosConnection.setTopicType(sourceRosConnection.getTopicType().get());
if (sourceRosConnection.getMsgField().isPresent() && !targetRosConnection.getMsgField().isPresent())
targetRosConnection.setMsgField(sourceRosConnection.getMsgField().get());
RosHelper.fixRosConnectionSymbols(componentInstanceSymbol);
}
@Override
......
package de.monticore.lang.monticar.generator.middleware.helpers;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ConnectorSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ExpandedComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.PortSymbol;
import de.monticore.lang.embeddedmontiarc.tagging.MiddlewareSymbol;
import de.monticore.lang.embeddedmontiarc.tagging.RosConnectionSymbol;
import de.monticore.lang.monticar.generator.rosmsg.GeneratorRosMsg;
import de.monticore.lang.monticar.generator.rosmsg.RosMsg;
import de.se_rwth.commons.logging.Log;
import java.util.Objects;
public class RosHelper {
private RosHelper() {
}
public static void fixRosConnectionSymbols(ExpandedComponentInstanceSymbol componentInstanceSymbol) {
componentInstanceSymbol.getConnectors().stream()
.filter(connectorSymbol -> connectorSymbol.getSourcePort().isRosPort() && connectorSymbol.getTargetPort().isRosPort())
.forEach(connectorSymbol -> {
if (Objects.equals(connectorSymbol.getSourcePort().getComponentInstance().orElse(null), componentInstanceSymbol)) {
//In port of supercomp
inferRosConnectionIfPossible(connectorSymbol.getSourcePort(), connectorSymbol.getTargetPort());
generateRosConnectionIfPossible(connectorSymbol);
} else if (Objects.equals(connectorSymbol.getTargetPort().getComponentInstance().orElse(null), componentInstanceSymbol)) {
//out port of supercomp
inferRosConnectionIfPossible(connectorSymbol.getTargetPort(), connectorSymbol.getSourcePort());
generateRosConnectionIfPossible(connectorSymbol);
} else {
//In between subcomps
generateRosConnectionIfPossible(connectorSymbol);
}
});
}
private static void generateRosConnectionIfPossible(ConnectorSymbol connectorSymbol) {
MiddlewareSymbol sourceTag = connectorSymbol.getSourcePort().getMiddlewareSymbol().orElse(null);
MiddlewareSymbol targetTag = connectorSymbol.getTargetPort().getMiddlewareSymbol().orElse(null);
if (sourceTag == null || targetTag == null || !sourceTag.isKindOf(RosConnectionSymbol.KIND) || !targetTag.isKindOf(RosConnectionSymbol.KIND)) {
Log.debug("Both sourcePort and targetPort need to have a RosConnectionSymbol", "RosConnectionSymbol");
return;
}
RosConnectionSymbol rosConnectionA = (RosConnectionSymbol) sourceTag;
RosConnectionSymbol rosConnectionB = (RosConnectionSymbol) targetTag;
RosConnectionSymbol emptyRosConnection = new RosConnectionSymbol();
if (!rosConnectionA.equals(emptyRosConnection) || !rosConnectionB.equals(emptyRosConnection)) {
Log.debug("Will not override rosConnections that are not empty!", "RosConnectionSymbol");
return;
}
//target port name is unique: each in port can only have one connection!
String topicName = connectorSymbol.getTargetPort().getFullName().replace(".", "_");
RosMsg rosTypeA = GeneratorRosMsg.getRosType("struct_msgs", connectorSymbol.getTargetPort().getTypeReference());
RosMsg rosTypeB = GeneratorRosMsg.getRosType("struct_msgs", connectorSymbol.getSourcePort().getTypeReference());
if (!rosTypeA.equals(rosTypeB)) {
Log.error("topicType mismatch! "
+ connectorSymbol.getSourcePort().getFullName() + " has " + rosTypeB + " and "
+ connectorSymbol.getTargetPort().getFullName() + " has " + rosTypeA);
return;
}
if (rosTypeA.getFields().size() == 1) {
connectorSymbol.getSourcePort().setMiddlewareSymbol(new RosConnectionSymbol(topicName, rosTypeB.getName(), rosTypeB.getFields().get(0).getName()));
connectorSymbol.getTargetPort().setMiddlewareSymbol(new RosConnectionSymbol(topicName, rosTypeA.getName(), rosTypeA.getFields().get(0).getName()));
} else {
connectorSymbol.getSourcePort().setMiddlewareSymbol(new RosConnectionSymbol(topicName, rosTypeB.getName()));
connectorSymbol.getTargetPort().setMiddlewareSymbol(new RosConnectionSymbol(topicName, rosTypeA.getName()));
}
}
private static void inferRosConnectionIfPossible(PortSymbol sourcePort, PortSymbol targetPort) {
MiddlewareSymbol sourceTag = sourcePort.getMiddlewareSymbol().orElse(null);
MiddlewareSymbol targetTag = targetPort.getMiddlewareSymbol().orElse(null);
if (sourceTag == null || targetTag == null || !sourceTag.isKindOf(RosConnectionSymbol.KIND) || !targetTag.isKindOf(RosConnectionSymbol.KIND)) {
Log.debug("Both sourcePort and targetPort need to have a RosConnectionSymbol", "RosConnectionSymbol");
return;
}
RosConnectionSymbol sourceRosConnection = (RosConnectionSymbol) sourceTag;
RosConnectionSymbol targetRosConnection = (RosConnectionSymbol) targetTag;
if (sourceRosConnection.getTopicName().isPresent() && !targetRosConnection.getTopicName().isPresent())
targetRosConnection.setTopicName(sourceRosConnection.getTopicName().get());
if (sourceRosConnection.getTopicType().isPresent() && !targetRosConnection.getTopicType().isPresent())
targetRosConnection.setTopicType(sourceRosConnection.getTopicType().get());
if (sourceRosConnection.getMsgField().isPresent() && !targetRosConnection.getMsgField().isPresent())
targetRosConnection.setMsgField(sourceRosConnection.getMsgField().get());
}
}
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