Commit 6cb1d4a7 authored by Alexander David Hellwig's avatar Alexander David Hellwig
Browse files

getRosType now returns RosMsg, a composite containing all fields and nested msgs

parent 7bdfb924
......@@ -78,6 +78,21 @@ public class GeneratorRosMsg {
return files;
}
public RosMsg createMsgForStruct(StructSymbol structSymbol) {
RosMsg res = new RosMsg(getFullTargetName(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)));
} else {
res.addField(new RosField(sfds.getName(), new RosType(getInMsgRosType(referencedSymbol))));
}
});
return res;
}
private String getInMsgRosType(MCTypeSymbol referencedSymbol) {
if (referencedSymbol.isKindOf(MontiCarTypeSymbol.KIND)) {
MontiCarTypeSymbol mcastTypeSymbol = (MontiCarTypeSymbol) referencedSymbol;
......@@ -103,16 +118,23 @@ public class GeneratorRosMsg {
Log.error("Case not handled! MCTypeReference " + referencedSymbol);
return null;
}
public String getRosType(MCTypeReference<? extends MCTypeSymbol> typeReference){
public RosMsg getRosType(MCTypeReference<? extends MCTypeSymbol> typeReference) {
MCTypeSymbol type = typeReference.getReferencedSymbol();
if(type.isKindOf(MCASTTypeSymbol.KIND)){
MCASTTypeSymbol mcastTypeSymbol = (MCASTTypeSymbol) type;
if(mcastTypeSymbol.getName().equals("Q")){
return "std_msgs/Float64";
RosMsg tmpMsg = new RosMsg("std_msgs/Float64");
tmpMsg.addField(new RosField("data", new RosType("float64")));
return tmpMsg;
}else if(mcastTypeSymbol.getName().equals("Z")){
return "std_msgs/Int32";
RosMsg tmpMsg = new RosMsg("std_msgs/Int32");
tmpMsg.addField(new RosField("data", new RosType("int32")));
return tmpMsg;
}else if(mcastTypeSymbol.getName().equals("B")){
return "std_msgs/Bool";
RosMsg tmpMsg = new RosMsg("std_msgs/Bool");
tmpMsg.addField(new RosField("data", new RosType("bool")));
return tmpMsg;
}else{
Log.error("Case not handled! MCASTTypeSymbol " + mcastTypeSymbol.getName());
}
......@@ -120,10 +142,7 @@ public class GeneratorRosMsg {
if (type instanceof StructSymbol) {
StructSymbol structSymbol = (StructSymbol) type;
if (packageName == null)
Log.error("Target must be set! Use GeneratorRosMsg::setTarget!");
return packageName + "/" + getTargetName(structSymbol);
return createMsgForStruct(structSymbol);
}
Log.error("Case not handled! MCTypeReference " + typeReference);
return null;
......@@ -133,4 +152,10 @@ public class GeneratorRosMsg {
return structSymbol.getFullName().replace(".", "_");
}
private String getFullTargetName(StructSymbol structSymbol) {
if (packageName == null)
Log.error("Target must be set! Use GeneratorRosMsg::setTarget!");
return packageName + "/" + getTargetName(structSymbol);
}
}
package de.monticore.lang.monticar.generator.rosmsg;
import com.google.common.base.Objects;
public class RosField {
private String name;
private RosType type;
public RosField(String name, RosType type) {
this.name = name;
this.type = type;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public RosType getType() {
return this.type;
}
public void setType(RosType type) {
this.type = type;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof RosField)) return false;
RosField rosField = (RosField) o;
return Objects.equal(name, rosField.name) &&
Objects.equal(type, rosField.type);
}
@Override
public int hashCode() {
return Objects.hashCode(name, type);
}
}
package de.monticore.lang.monticar.generator.rosmsg;
import java.util.List;
public class RosMsg extends RosType {
public RosMsg(String name) {
super(name);
}
public RosMsg(String name, RosField... fields) {
this(name);
for (RosField f : fields) {
this.addField(f);
}
}
public List<RosField> getFields() {
return fields;
}
public void addField(RosField field) {
fields.add(field);
}
}
package de.monticore.lang.monticar.generator.rosmsg;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class RosType {
private String name;
List<RosField> fields = new ArrayList<>();
public RosType(String name) {
this.name = name;
}
String getName() {
return name;
}
void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof RosType)) return false;
RosType rosType = (RosType) o;
boolean fieldsEqual = Objects.deepEquals(fields, ((RosType) o).fields);
return Objects.equals(name, rosType.name) && fieldsEqual;
}
}
......@@ -8,9 +8,9 @@ import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.*;
public class BasicTypesTest extends AbstractSymtabTest {
......@@ -30,9 +30,25 @@ public class BasicTypesTest extends AbstractSymtabTest {
assertNotNull(inZ);
assertNotNull(inB);
assertEquals(generatorRosMsg.getRosType(inQ.getTypeReference()),"std_msgs/Float64");
assertEquals(generatorRosMsg.getRosType(inZ.getTypeReference()),"std_msgs/Int32");
assertEquals(generatorRosMsg.getRosType(inB.getTypeReference()),"std_msgs/Bool");
RosMsg msgInQ = generatorRosMsg.getRosType(inQ.getTypeReference());
RosMsg msgInZ = generatorRosMsg.getRosType(inZ.getTypeReference());
RosMsg msgInB = generatorRosMsg.getRosType(inB.getTypeReference());
assertNotNull(msgInQ);
assertNotNull(msgInZ);
assertNotNull(msgInB);
assertEquals(msgInQ.getFields().size(), 1);
assertEquals(msgInZ.getFields().size(), 1);
assertEquals(msgInB.getFields().size(), 1);
assertEquals(msgInQ.getFields().get(0).getName(), "data");
assertEquals(msgInZ.getFields().get(0).getName(), "data");
assertEquals(msgInB.getFields().get(0).getName(), "data");
assertEquals(msgInQ.getFields().get(0).getType().getName(), "float64");
assertEquals(msgInZ.getFields().get(0).getType().getName(), "int32");
assertEquals(msgInB.getFields().get(0).getType().getName(), "bool");
}
@Test
......@@ -51,8 +67,13 @@ public class BasicTypesTest extends AbstractSymtabTest {
assertNotNull(in1);
assertNotNull(out1);
assertEquals(generatorRosMsg.getRosType(in1.getTypeReference()), "basic/structs_BasicStruct");
assertEquals(generatorRosMsg.getRosType(out1.getTypeReference()), "basic/structs_BasicStruct");
RosMsg msgIn1 = generatorRosMsg.getRosType(in1.getTypeReference());
RosMsg msgOut1 = generatorRosMsg.getRosType(out1.getTypeReference());
assertEquals(msgIn1, msgOut1);
assertEquals(msgIn1.getName(), "basic/structs_BasicStruct");
assertTrue(Objects.equals(msgIn1, getBasicStruct("basic")));
List<File> files = generatorRosMsg.generate(in1.getTypeReference());
testFilesAreEqual(files, "basicStruct/");
......@@ -73,7 +94,11 @@ public class BasicTypesTest extends AbstractSymtabTest {
assertNotNull(inNested);
assertEquals(generatorRosMsg.getRosType(inNested.getTypeReference()), "nested/structs_NestedStruct");
RosMsg rosMsg = generatorRosMsg.getRosType(inNested.getTypeReference());
assertEquals(rosMsg.getName(), "nested/structs_NestedStruct");
assertEquals(rosMsg, getNestedStruct("nested"));
List<File> files = generatorRosMsg.generate(inNested.getTypeReference());
testFilesAreEqual(files, "nestedStruct/");
......@@ -93,10 +118,41 @@ public class BasicTypesTest extends AbstractSymtabTest {
assertNotNull(inMultiNested);
assertEquals(generatorRosMsg.getRosType(inMultiNested.getTypeReference()), "multinested/structs_MultiNestedStruct");
assertEquals(generatorRosMsg.getRosType(inMultiNested.getTypeReference()), getMultinestedStruct("multinested"));
List<File> files = generatorRosMsg.generate(inMultiNested.getTypeReference());
testFilesAreEqual(files, "multinestedStruct/");
}
public RosMsg getBasicStruct(String packageName) {
RosMsg res = new RosMsg(packageName + "/structs_BasicStruct");
res.addField(new RosField("fieldQ1", new RosType("float64")));
res.addField(new RosField("fieldQ2", new RosType("float64")));
res.addField(new RosField("fieldZ1", new RosType("int32")));
res.addField(new RosField("fieldZ2", new RosType("int32")));
res.addField(new RosField("fieldB1", new RosType("bool")));
return res;
}
public RosMsg getNestedStruct(String packageName) {
RosMsg res = new RosMsg(packageName + "/structs_NestedStruct");
res.addField(new RosField("fieldNested1", getBasicStruct(packageName)));
res.addField(new RosField("fieldNested2", getBasicStruct(packageName)));
res.addField(new RosField("fieldQ", new RosType("float64")));
res.addField(new RosField("fieldB", new RosType("bool")));
res.addField(new RosField("fieldZ", new RosType("int32")));
return res;
}
public RosMsg getMultinestedStruct(String packageName) {
RosMsg res = new RosMsg(packageName + "/structs_MultiNestedStruct");
res.addField(new RosField("fieldMultiNested1", getNestedStruct(packageName)));
res.addField(new RosField("fieldMultiNested2", getNestedStruct(packageName)));
res.addField(new RosField("fieldNested", getBasicStruct(packageName)));
res.addField(new RosField("fieldQ", new RosType("float64")));
return res;
}
}
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