Commit 3bb6d274 authored by Alexander David Hellwig's avatar Alexander David Hellwig
Browse files

getRosType is now static, needs packageName as parameter

parent 06ac62eb
......@@ -19,7 +19,7 @@ import java.util.stream.Collectors;
public class GeneratorRosMsg {
private String path;
private String packageName;
private String currentPackageName;
private Set<String> alreadyGenerated = new HashSet<>();
......@@ -30,16 +30,16 @@ public class GeneratorRosMsg {
this.path = path;
}
this.packageName = packageName;
this.currentPackageName = packageName;
}
public List<File> generate(MCTypeReference<? extends MCTypeSymbol> typeReference) throws IOException {
//is typeReference basic type or struct?
if (getRosType(typeReference) != null) {
if (getRosType(currentPackageName, typeReference) != null) {
MCTypeSymbol type = typeReference.getReferencedSymbol();
List<File> res = new ArrayList<>();
if (type instanceof StructSymbol) {
res.addAll(generateStruct((StructSymbol) type));
res.addAll(generateStruct(currentPackageName, (StructSymbol) type));
}
return res;
}
......@@ -48,7 +48,7 @@ public class GeneratorRosMsg {
return new ArrayList<>();
}
public List<File> generateStruct(StructSymbol structSymbol) throws IOException {
public List<File> generateStruct(String packageName, StructSymbol structSymbol) throws IOException {
if (alreadyGenerated.contains(packageName + "/" + getTargetName(structSymbol))) {
Log.debug("Struct " + structSymbol + " was already generated!", "structGeneration");
return new ArrayList<>();
......@@ -59,7 +59,7 @@ public class GeneratorRosMsg {
String definition = structSymbol.getStructFieldDefinitions().stream()
.filter(sfds -> sfds.getType().existsReferencedSymbol())
.map(sfds -> getInMsgRosType(sfds.getType().getReferencedSymbol()) + " " + sfds.getName())
.map(sfds -> getInMsgRosType(currentPackageName, sfds.getType().getReferencedSymbol()) + " " + sfds.getName())
.collect(Collectors.joining("\n"));
File f = new File(path + "/" + getTargetName(structSymbol) + ".msg");
......@@ -70,7 +70,7 @@ public class GeneratorRosMsg {
if (sfds.getType().existsReferencedSymbol()) {
if (sfds.getType().getReferencedSymbol() instanceof StructSymbol) {
StructSymbol referencedSymbol = (StructSymbol) sfds.getType().getReferencedSymbol();
generateStruct(referencedSymbol);
generateStruct(packageName, referencedSymbol);
}
}
}
......@@ -78,22 +78,22 @@ public class GeneratorRosMsg {
return files;
}
public RosMsg createMsgForStruct(StructSymbol structSymbol) {
RosMsg res = new RosMsg(getFullTargetName(structSymbol));
public static RosMsg createMsgForStruct(String packageName, StructSymbol structSymbol) {
RosMsg res = new RosMsg(getFullTargetName(packageName, structSymbol));
structSymbol.getStructFieldDefinitions().stream()
.filter(sfds -> sfds.getType().existsReferencedSymbol())
.forEach(sfds -> {
MCTypeSymbol referencedSymbol = sfds.getType().getReferencedSymbol();
if (referencedSymbol instanceof StructSymbol) {
res.addField(new RosField(sfds.getName(), createMsgForStruct((StructSymbol) referencedSymbol)));
res.addField(new RosField(sfds.getName(), createMsgForStruct(packageName, (StructSymbol) referencedSymbol)));
} else {
res.addField(new RosField(sfds.getName(), new RosType(getInMsgRosType(referencedSymbol))));
res.addField(new RosField(sfds.getName(), new RosType(getInMsgRosType(packageName, referencedSymbol))));
}
});
return res;
}
private String getInMsgRosType(MCTypeSymbol referencedSymbol) {
private static String getInMsgRosType(String packageName, MCTypeSymbol referencedSymbol) {
if (referencedSymbol.isKindOf(MontiCarTypeSymbol.KIND)) {
MontiCarTypeSymbol mcastTypeSymbol = (MontiCarTypeSymbol) referencedSymbol;
if (mcastTypeSymbol.getName().equals("Q")) {
......@@ -109,9 +109,6 @@ public class GeneratorRosMsg {
if (referencedSymbol instanceof StructSymbol) {
StructSymbol structSymbol = (StructSymbol) referencedSymbol;
if (packageName == null)
Log.error("Target must be set! Use GeneratorRosMsg::setTarget!");
return packageName + "/" + getTargetName(structSymbol);
}
......@@ -119,7 +116,7 @@ public class GeneratorRosMsg {
return null;
}
public RosMsg getRosType(MCTypeReference<? extends MCTypeSymbol> typeReference) {
public static RosMsg getRosType(String packageName, MCTypeReference<? extends MCTypeSymbol> typeReference) {
MCTypeSymbol type = typeReference.getReferencedSymbol();
if(type.isKindOf(MCASTTypeSymbol.KIND)){
MCASTTypeSymbol mcastTypeSymbol = (MCASTTypeSymbol) type;
......@@ -142,20 +139,17 @@ public class GeneratorRosMsg {
if (type instanceof StructSymbol) {
StructSymbol structSymbol = (StructSymbol) type;
return createMsgForStruct(structSymbol);
return createMsgForStruct(packageName, structSymbol);
}
Log.error("Case not handled! MCTypeReference " + typeReference);
return null;
}
private String getTargetName(StructSymbol structSymbol) {
private static String getTargetName(StructSymbol structSymbol) {
return structSymbol.getFullName().replace(".", "_");
}
private String getFullTargetName(StructSymbol structSymbol) {
if (packageName == null)
Log.error("Target must be set! Use GeneratorRosMsg::setTarget!");
private static String getFullTargetName(String packageName, StructSymbol structSymbol) {
return packageName + "/" + getTargetName(structSymbol);
}
}
......@@ -20,8 +20,6 @@ public class BasicTypesTest extends AbstractSymtabTest {
ExpandedComponentInstanceSymbol component = symtab.<ExpandedComponentInstanceSymbol>resolve("tests.basicTypesComp", ExpandedComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(component);
GeneratorRosMsg generatorRosMsg = new GeneratorRosMsg();
PortSymbol inQ = component.getPort("inQ").orElse(null);
PortSymbol inZ = component.getPort("inZ").orElse(null);
PortSymbol inB = component.getPort("inB").orElse(null);
......@@ -30,9 +28,10 @@ public class BasicTypesTest extends AbstractSymtabTest {
assertNotNull(inZ);
assertNotNull(inB);
RosMsg msgInQ = generatorRosMsg.getRosType(inQ.getTypeReference());
RosMsg msgInZ = generatorRosMsg.getRosType(inZ.getTypeReference());
RosMsg msgInB = generatorRosMsg.getRosType(inB.getTypeReference());
String packageName = "basic";
RosMsg msgInQ = GeneratorRosMsg.getRosType(packageName, inQ.getTypeReference());
RosMsg msgInZ = GeneratorRosMsg.getRosType(packageName, inZ.getTypeReference());
RosMsg msgInB = GeneratorRosMsg.getRosType(packageName, inB.getTypeReference());
assertNotNull(msgInQ);
assertNotNull(msgInZ);
......@@ -59,7 +58,8 @@ public class BasicTypesTest extends AbstractSymtabTest {
assertNotNull(component);
GeneratorRosMsg generatorRosMsg = new GeneratorRosMsg();
generatorRosMsg.setTarget("target/generated-sources-rosmsg/basic", "basic");
String packageName = "basic";
generatorRosMsg.setTarget("target/generated-sources-rosmsg/basic", packageName);
PortSymbol in1 = component.getPort("in1").orElse(null);
PortSymbol out1 = component.getPort("out1").orElse(null);
......@@ -67,13 +67,13 @@ public class BasicTypesTest extends AbstractSymtabTest {
assertNotNull(in1);
assertNotNull(out1);
RosMsg msgIn1 = generatorRosMsg.getRosType(in1.getTypeReference());
RosMsg msgOut1 = generatorRosMsg.getRosType(out1.getTypeReference());
RosMsg msgIn1 = GeneratorRosMsg.getRosType(packageName, in1.getTypeReference());
RosMsg msgOut1 = GeneratorRosMsg.getRosType(packageName, out1.getTypeReference());
assertEquals(msgIn1, msgOut1);
assertEquals(msgIn1.getName(), "basic/structs_BasicStruct");
assertTrue(Objects.equals(msgIn1, getBasicStruct("basic")));
assertTrue(Objects.equals(msgIn1, getBasicStruct(packageName)));
List<File> files = generatorRosMsg.generate(in1.getTypeReference());
testFilesAreEqual(files, "basicStruct/");
......@@ -88,16 +88,17 @@ public class BasicTypesTest extends AbstractSymtabTest {
assertNotNull(component);
GeneratorRosMsg generatorRosMsg = new GeneratorRosMsg();
generatorRosMsg.setTarget("target/generated-sources-rosmsg/nested", "nested");
String packageName = "nested";
generatorRosMsg.setTarget("target/generated-sources-rosmsg/nested", packageName);
PortSymbol inNested = component.getPort("inNested").orElse(null);
assertNotNull(inNested);
RosMsg rosMsg = generatorRosMsg.getRosType(inNested.getTypeReference());
RosMsg rosMsg = GeneratorRosMsg.getRosType(packageName, inNested.getTypeReference());
assertEquals(rosMsg.getName(), "nested/structs_NestedStruct");
assertEquals(rosMsg, getNestedStruct("nested"));
assertEquals(rosMsg, getNestedStruct(packageName));
List<File> files = generatorRosMsg.generate(inNested.getTypeReference());
......@@ -112,13 +113,14 @@ public class BasicTypesTest extends AbstractSymtabTest {
assertNotNull(component);
GeneratorRosMsg generatorRosMsg = new GeneratorRosMsg();
generatorRosMsg.setTarget("target/generated-sources-rosmsg/multinested", "multinested");
String packageName = "multinested";
generatorRosMsg.setTarget("target/generated-sources-rosmsg/multinested", packageName);
PortSymbol inMultiNested = component.getPort("inMultiNested").orElse(null);
assertNotNull(inMultiNested);
assertEquals(generatorRosMsg.getRosType(inMultiNested.getTypeReference()), getMultinestedStruct("multinested"));
assertEquals(GeneratorRosMsg.getRosType(packageName, inMultiNested.getTypeReference()), getMultinestedStruct(packageName));
List<File> files = generatorRosMsg.generate(inMultiNested.getTypeReference());
testFilesAreEqual(files, "multinestedStruct/");
......
Supports Markdown
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