Commit fe50b6f2 authored by Alexander David Hellwig's avatar Alexander David Hellwig
Browse files

Added support for Matrices of basic types

parent 300c226f
package de.monticore.lang.monticar.generator.rosmsg;
import de.monticore.lang.monticar.common2._ast.ASTCommonMatrixType;
import de.monticore.lang.monticar.generator.FileContent;
import de.monticore.lang.monticar.struct._symboltable.StructFieldDefinitionSymbol;
import de.monticore.lang.monticar.struct._symboltable.StructSymbol;
......@@ -132,21 +133,40 @@ public class GeneratorRosMsg {
public static RosMsg getRosType(String packageName, MCTypeReference<? extends MCTypeSymbol> typeReference) {
MCTypeSymbol type = typeReference.getReferencedSymbol();
if(type.isKindOf(MCASTTypeSymbol.KIND)){
if (type.isKindOf(MCASTTypeSymbol.KIND)) {
MCASTTypeSymbol mcastTypeSymbol = (MCASTTypeSymbol) type;
if(mcastTypeSymbol.getName().equals("Q")){
if (mcastTypeSymbol.getName().equals("Q")) {
RosMsg tmpMsg = new RosMsg("std_msgs/Float64");
tmpMsg.addField(new RosField("data", new RosType("float64")));
return tmpMsg;
}else if(mcastTypeSymbol.getName().equals("Z")){
} else if (mcastTypeSymbol.getName().equals("Z")) {
RosMsg tmpMsg = new RosMsg("std_msgs/Int32");
tmpMsg.addField(new RosField("data", new RosType("int32")));
return tmpMsg;
}else if(mcastTypeSymbol.getName().equals("B")){
} else if (mcastTypeSymbol.getName().equals("B")) {
RosMsg tmpMsg = new RosMsg("std_msgs/Bool");
tmpMsg.addField(new RosField("data", new RosType("bool")));
return tmpMsg;
}else{
} else if (mcastTypeSymbol.getName().equals("CommonMatrixType")) {
ASTCommonMatrixType matrixType = (ASTCommonMatrixType) mcastTypeSymbol.getAstType();
String tmpMsgName = "";
String tmpTypeName = "";
if (matrixType.getElementType().isIsRational()) {
tmpMsgName = "std_msgs/Float64MultiArray";
tmpTypeName = "float64";
} else if (matrixType.getElementType().isIsWholeNumberNumber()) {
tmpMsgName = "std_msgs/Int32MultiArray";
tmpTypeName = "int32";
} else if (matrixType.getElementType().isIsBoolean()) {
//TODO: BoolMultiArray?
tmpMsgName = "std_msgs/ByteMultiArray";
tmpTypeName = "byte";
} else {
Log.error("Matrix type not supported: " + matrixType);
}
//TODO: refactor
return getMultMatrixRosMsg(tmpMsgName, tmpTypeName);
} else {
Log.error("Case not handled! MCASTTypeSymbol " + mcastTypeSymbol.getName());
}
}
......@@ -159,6 +179,38 @@ public class GeneratorRosMsg {
return null;
}
private static RosMsg getMultMatrixRosMsg(String msgName, String tmpTypeName) {
RosMsg tmpMsg = new RosMsg(msgName);
//uint32 data_offset
RosMsg multiArrayLayout = new RosMsg("MultiArrayLayout");
multiArrayLayout.addField(new RosField("data_offset", new RosType("uint32")));
//MultiArrayDimension[] dim
RosMsg mutliArrayDimMsg = new RosMsg("MultiArrayDimension");
//string label
mutliArrayDimMsg.addField(new RosField("label", new RosType("string")));
//uint32 size
mutliArrayDimMsg.addField(new RosField("size", new RosType("uint32")));
//uint32 stride
mutliArrayDimMsg.addField(new RosField("stride", new RosType("uint32")));
RosField multiArrayDimField = new RosField("dim", mutliArrayDimMsg);
multiArrayDimField.setArray(true);
multiArrayLayout.addField(multiArrayDimField);
RosField fieldData = new RosField("data", new RosType(tmpTypeName));
fieldData.setArray(true);
RosField layoutField = new RosField("layout", multiArrayLayout);
tmpMsg.addField(fieldData);
tmpMsg.addField(layoutField);
return tmpMsg;
}
private static String getTargetName(StructSymbol structSymbol) {
return structSymbol.getFullName().replace(".", "_");
}
......
......@@ -5,6 +5,7 @@ import com.google.common.base.Objects;
public class RosField {
private String name;
private RosType type;
private boolean isArray;
public RosField(String name, RosType type) {
this.name = name;
......@@ -27,17 +28,26 @@ public class RosField {
this.type = type;
}
public boolean isArray() {
return isArray;
}
public void setArray(boolean array) {
isArray = array;
}
@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) &&
return isArray == rosField.isArray &&
Objects.equal(name, rosField.name) &&
Objects.equal(type, rosField.type);
}
@Override
public int hashCode() {
return Objects.hashCode(name, type);
return Objects.hashCode(name, type, isArray);
}
}
......@@ -2,13 +2,16 @@ package de.monticore.lang.monticar.generator.rosmsg;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ExpandedComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.PortSymbol;
import de.monticore.symboltable.CommonSymbol;
import de.monticore.symboltable.Scope;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import static org.junit.Assert.*;
......@@ -50,6 +53,21 @@ public class BasicTypesTest extends AbstractSymtabTest {
assertEquals(msgInB.getFields().get(0).getType().getName(), "bool");
}
@Test
public void testMatrixTypes() {
Scope symtab = createSymTab("src/test/resources/");
ExpandedComponentInstanceSymbol component = symtab.<ExpandedComponentInstanceSymbol>resolve("tests.matrixTypesComp", ExpandedComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(component);
Map<String, RosMsg> portToMsg = component.getPorts().stream()
.collect(Collectors.toMap(CommonSymbol::getName, p -> GeneratorRosMsg.getRosType("std_msgs", p.getTypeReference())));
assertTrue(portToMsg.get("in1").getName().equals("std_msgs/Float64MultiArray"));
assertTrue(portToMsg.get("in2").getName().equals("std_msgs/Float64MultiArray"));
assertTrue(portToMsg.get("out1").getName().equals("std_msgs/ByteMultiArray"));
assertTrue(portToMsg.get("out2").getName().equals("std_msgs/Int32MultiArray"));
}
@Test
public void testBasicStructComp() throws IOException {
......
package tests;
component MatrixTypesComp{
ports in Q^{3,1} in1,
in Q^{4,4} in2,
out B^{2,3,4} out1,
out Z^{2,3,4,5} out2;
}
\ No newline at end of file
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