Added GeneratorImpl::willAccept to make sure no empty Adapters are generated + tests

parent ea799751
......@@ -27,11 +27,11 @@ public class CMakeGenerator extends StarBridgeGenerator {
}
List<File> res = super.generate(componentInstanceSymbol, taggingResolver);
res.add(generateCMake(generationTargetPath));
res.add(generateCMake(generationTargetPath, componentInstanceSymbol));
return res;
}
private File generateCMake(String targetPath) throws IOException {
private File generateCMake(String targetPath, ExpandedComponentInstanceSymbol componentInstanceSymbol) throws IOException {
FileContent fileContent = new FileContent();
fileContent.setFileName("CMakeLists.txt");
StringBuilder content = new StringBuilder();
......@@ -41,6 +41,7 @@ public class CMakeGenerator extends StarBridgeGenerator {
content.append("set (CMAKE_CXX_STANDARD 11)\n");
getGeneratorImpls().stream()
.filter(gen -> gen.willAccept(componentInstanceSymbol))
.map(this::getImplSubdir)
.forEach(subdir -> content.append("add_subdirectory(" + subdir + ")\n"));
......
......@@ -19,8 +19,7 @@ import java.util.*;
public class DistributedTargetGenerator extends CMakeGenerator {
private RosMsgImpl rosMsgImpl;
public DistributedTargetGenerator(String generationTargetPath) {
setGenerationTargetPath(generationTargetPath);
public DistributedTargetGenerator() {
rosMsgImpl = new RosMsgImpl("rosmsg");
//this.add(rosMsgImpl,"rosmsg/");
}
......@@ -40,7 +39,7 @@ public class DistributedTargetGenerator extends CMakeGenerator {
.flatMap(comp -> comp.getPorts().stream())
.allMatch(p -> p.getMiddlewareSymbol().isPresent());
if (allSubsMwOnly) {
if (subComps.size() > 0 && allSubsMwOnly) {
subComps.forEach(comp ->
generatorMap.put(comp, createFullGenerator(comp.getFullName().replace(".", "_")))
);
......
......@@ -25,7 +25,7 @@ public class MiddlewareGenerator extends CMakeGenerator {
files.add(FileHelper.generateFile(generationTargetPath + subdir, generateIAdapter(componentInstanceSymbol)));
files.add(FileHelper.generateFile(generationTargetPath + subdir, generateCoordinator(componentInstanceSymbol, files)));
files.add(FileHelper.generateFile(generationTargetPath + subdir, generateCMakeList(componentInstanceSymbol, files)));
files.add(FileHelper.generateFile(generationTargetPath + subdir, generateCoordinatorCMakeList(componentInstanceSymbol, files)));
return files;
}
......@@ -70,7 +70,7 @@ public class MiddlewareGenerator extends CMakeGenerator {
}
private FileContent generateCMakeList(ExpandedComponentInstanceSymbol componentInstanceSymbol, List<File> files) {
private FileContent generateCoordinatorCMakeList(ExpandedComponentInstanceSymbol componentInstanceSymbol, List<File> files) {
FileContent res = new FileContent();
String name = NameHelper.getComponentNameTargetLanguage(componentInstanceSymbol.getFullName());
......@@ -80,7 +80,7 @@ public class MiddlewareGenerator extends CMakeGenerator {
.map(fn -> fn.substring(0, fn.length() - 2))
.collect(Collectors.joining(" "));
String content = TemplateHelper.cmakeListsTemplate
String content = TemplateHelper.coordinatorCmakeListsTemplate
.replace("${targets}", targets)
.replace("${compName}", name);
......
......@@ -37,19 +37,21 @@ public class StarBridgeGenerator implements GeneratorImpl {
public List<File> generate(ExpandedComponentInstanceSymbol componentInstanceSymbol, TaggingResolver taggingResolver) throws IOException {
List<File> result = new ArrayList<>();
generatorImpls.forEach((key, value) -> {
String fullTargetPath = generationTargetPath;
if (value != null)
fullTargetPath = fullTargetPath + value;
if (key.willAccept(componentInstanceSymbol)) {
String fullTargetPath = generationTargetPath;
if (value != null)
fullTargetPath = fullTargetPath + value;
if (!fullTargetPath.endsWith("/"))
fullTargetPath = fullTargetPath + "/";
if (!fullTargetPath.endsWith("/"))
fullTargetPath = fullTargetPath + "/";
key.setGenerationTargetPath(fullTargetPath);
key.setGenerationTargetPath(fullTargetPath);
try {
result.addAll(key.generate(componentInstanceSymbol, taggingResolver));
} catch (IOException e) {
Log.error("IOException occurred!", e);
try {
result.addAll(key.generate(componentInstanceSymbol, taggingResolver));
} catch (IOException e) {
Log.error("IOException occurred!", e);
}
}
});
......
package de.monticore.lang.monticar.generator.middleware.impls;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ExpandedComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.PortSymbol;
import de.monticore.lang.monticar.generator.FileContent;
import de.monticore.lang.monticar.generator.middleware.helpers.FileHelper;
import de.monticore.lang.monticar.generator.middleware.helpers.TemplateHelper;
......@@ -11,6 +12,7 @@ import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class DummyMiddlewareGenImpl implements GeneratorImpl {
......@@ -42,6 +44,15 @@ public class DummyMiddlewareGenImpl implements GeneratorImpl {
this.generationTargetPath = path;
}
@Override
public boolean willAccept(ExpandedComponentInstanceSymbol componentInstanceSymbol) {
return componentInstanceSymbol.getPorts().stream()
.map(PortSymbol::getMiddlewareSymbol)
.filter(Optional::isPresent)
.map(Optional::get)
.filter(mws -> mws.isKindOf(DummyMiddlewareSymbol.KIND))
.count() > 0;
}
private FileContent generateCMake(ExpandedComponentInstanceSymbol componentInstanceSymbol) {
FileContent res = new FileContent();
......
package de.monticore.lang.monticar.generator.middleware.impls;
import de.monticore.lang.embeddedmontiarc.tagging.MiddlewareSymbol;
import de.monticore.lang.tagging._symboltable.TagKind;
public class DummyMiddlewareSymbol extends MiddlewareSymbol {
public static final DummyMiddlewareKind KIND = DummyMiddlewareKind.INSTANCE;
public DummyMiddlewareSymbol() {
super(KIND);
}
private static class DummyMiddlewareKind extends TagKind {
public static final DummyMiddlewareKind INSTANCE = new DummyMiddlewareKind();
protected DummyMiddlewareKind() {
}
}
}
......@@ -16,4 +16,8 @@ public interface GeneratorImpl {
default void setGenerationTargetPath(String path) {
}
default boolean willAccept(ExpandedComponentInstanceSymbol componentInstanceSymbol) {
return true;
}
}
package de.monticore.lang.monticar.generator.middleware.impls;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ExpandedComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.PortSymbol;
import de.monticore.lang.monticar.generator.roscpp.GeneratorRosCpp;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
......@@ -24,4 +25,11 @@ public class RosCppGenImpl implements GeneratorImpl {
public void setGenerationTargetPath(String path) {
this.generationTargetPath = path;
}
@Override
public boolean willAccept(ExpandedComponentInstanceSymbol componentInstanceSymbol) {
return componentInstanceSymbol.getPorts().stream()
.filter(PortSymbol::isRosPort)
.count() > 0;
}
}
package de.monticore.lang.monticar.generator.middleware.impls;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ExpandedComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.PortSymbol;
import de.monticore.lang.monticar.generator.rosmsg.GeneratorRosMsg;
import de.monticore.lang.monticar.generator.rosmsg.RosMsg;
import de.monticore.lang.monticar.ts.MCTypeSymbol;
......@@ -44,4 +45,11 @@ public class RosMsgImpl implements GeneratorImpl {
public RosMsg getRosType(MCTypeReference<? extends MCTypeSymbol> typeReference) {
return generatorRosMsg.getRosType(typeReference);
}
@Override
public boolean willAccept(ExpandedComponentInstanceSymbol componentInstanceSymbol) {
return componentInstanceSymbol.getPorts().stream()
.filter(PortSymbol::isRosPort)
.count() > 0;
}
}
package de.monticore.lang.monticar.generator.middleware;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ExpandedComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.PortSymbol;
import de.monticore.lang.embeddedmontiarc.tagging.RosConnectionSymbol;
import de.monticore.lang.embeddedmontiarc.tagging.RosToEmamTagSchema;
import de.monticore.lang.monticar.generator.middleware.impls.CPPGenImpl;
import de.monticore.lang.monticar.generator.middleware.impls.DummyMiddlewareGenImpl;
import de.monticore.lang.monticar.generator.middleware.impls.DummyMiddlewareSymbol;
import de.monticore.lang.monticar.generator.middleware.impls.RosCppGenImpl;
import de.monticore.lang.monticar.generator.roscpp.helper.TagHelper;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class GenerationTest extends AbstractSymtabTest {
......@@ -82,11 +89,70 @@ public class GenerationTest extends AbstractSymtabTest {
TagHelper.resolveTags(taggingResolver, componentInstanceSymbol);
DistributedTargetGenerator distributedTargetGenerator = new DistributedTargetGenerator("./target/generated-sources-cmake/distributed/src/");
DistributedTargetGenerator distributedTargetGenerator = new DistributedTargetGenerator();
distributedTargetGenerator.setGenerationTargetPath("./target/generated-sources-cmake/distributed/src/");
distributedTargetGenerator.add(new CPPGenImpl(), "cpp");
distributedTargetGenerator.add(new RosCppGenImpl(), "roscpp");
distributedTargetGenerator.generate(componentInstanceSymbol, taggingResolver);
}
@Test
public void testMutliMwGenerateAll() throws IOException {
testMutliMw("allMw", true, true);
}
@Test
public void testMutliMwGenerateSome() throws IOException {
testMutliMw("someMw", true, false);
}
@Test
public void testMutliMwGenerateNone() throws IOException {
testMutliMw("noneMw", false, false);
}
public void testMutliMw(String relPath, boolean genRosAdapter, boolean genDummyAdapter) throws IOException {
TaggingResolver taggingResolver = createSymTabAndTaggingResolver("src/test/resources/");
//Don't load tags, will be set manually
//RosToEmamTagSchema.registerTagTypes(taggingResolver);
ExpandedComponentInstanceSymbol componentInstanceSymbol = taggingResolver.<ExpandedComponentInstanceSymbol>resolve("tests.a.addComp", ExpandedComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentInstanceSymbol);
PortSymbol in1 = componentInstanceSymbol.getPort("in1").orElse(null);
assertNotNull(in1);
PortSymbol in2 = componentInstanceSymbol.getPort("in2").orElse(null);
assertNotNull(in2);
PortSymbol out1 = componentInstanceSymbol.getPort("out1").orElse(null);
assertNotNull(out1);
if (genRosAdapter) {
in1.setMiddlewareSymbol(new RosConnectionSymbol("/test", "std_msgs/Float64", "data"));
in2.setMiddlewareSymbol(new RosConnectionSymbol("/test2", "std_msgs/Float64", "data"));
}
if (genDummyAdapter) {
out1.setMiddlewareSymbol(new DummyMiddlewareSymbol());
}
DistributedTargetGenerator distributedTargetGenerator = new DistributedTargetGenerator();
distributedTargetGenerator.setGenerationTargetPath("./target/generated-sources-cmake/" + relPath + "/src/");
distributedTargetGenerator.add(new CPPGenImpl(), "cpp");
distributedTargetGenerator.add(new RosCppGenImpl(), "roscpp");
distributedTargetGenerator.add(new DummyMiddlewareGenImpl(), "dummy");
List<File> files = distributedTargetGenerator.generate(componentInstanceSymbol, taggingResolver);
List<String> fileNames = files.stream()
.map(File::getName)
.collect(Collectors.toList());
assertEquals(fileNames.contains("RosAdapter_tests_a_addComp.h"), genRosAdapter);
assertEquals(fileNames.contains("DummyAdapter_tests_a_addComp.h"), genDummyAdapter);
}
}
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