Commit e5284824 authored by Markus Georg Bendel's avatar Markus Georg Bendel

Add basic generator structure and Compile Adapter in CI

parent a3e3f601
stages:
- linux
- project
- adapter
BranchJobLinux:
stage: linux
BranchJobProject:
stage: project
image: registry.git.rwth-aachen.de/monticore/embeddedmontiarc/generators/emam2someip
script:
- mvn clean install -s settings.xml
except:
- master
BranchJobAdapter:
stage: adapter
image: registry.git.rwth-aachen.de/monticore/embeddedmontiarc/generators/emam2someip
script:
- cd someip_adapter
- cmake .
- make
except:
- master
......@@ -51,7 +51,7 @@
<artifactId>embedded-montiarc</artifactId>
<!-- or newer version -->
<version>0.1.10-SNAPSHOT</version>
<dependency>
</dependency>
<dependency>
<groupId>de.se_rwth.commons</groupId>
......
package de.monticore.lang.monticar.generator.someip;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAPortInstanceSymbol;
//import de.monticore.lang.embeddedmontiarc.tagging.middleware.someip.SomeIPConnectionSymbol;
import de.monticore.lang.monticar.generator.someip.template.SomeIPAdapterModel;
import de.monticore.lang.monticar.generator.someip.template.SomeIPTemplates;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class GeneratorSomeIP {
List<File> generateSomeIPAdapter(EMAComponentInstanceSymbol component) {
List<File> files = new ArrayList<>();
// Get info about the ports from the component
Collection<EMAPortInstanceSymbol> ports = component.getPortInstanceList();
// Create and fill model
SomeIPAdapterModel model = new SomeIPAdapterModel(component.getFullName());
model.addPorts(ports);
//Generate files and write to project
String content = SomeIPTemplates.generateSomeIPAdapter(model);
File file = new File("./target/generated-sources/ports.txt");
files.add(file);
FileWriter fr = null;
try {
fr = new FileWriter(file);
fr.write(content);
} catch (IOException e) {
e.printStackTrace();
} finally {
//Close resources
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return files;
}
}
package de.monticore.lang.monticar.generator.someip.template;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAPortInstanceSymbol;
import de.monticore.lang.embeddedmontiarc.tagging.middleware.MiddlewareSymbol;
import de.monticore.lang.embeddedmontiarc.tagging.middleware.someip.SomeIPConnectionSymbol;
import de.monticore.lang.embeddedmontiarc.tagging.middleware.someip.SomeIPConnectionSymbol.SomeIPConnectionKind;
import java.util.*;
// Used to fill .ftl files
public class SomeIPAdapterModel {
private String compName;
private List<String> ports = new ArrayList<>();
public SomeIPAdapterModel(String compName) {
this.compName = compName;
}
public String getCompName() {
return compName;
}
// Parse through component to find information about its ports
public void addPorts(Collection<EMAPortInstanceSymbol> ports)
{
for (EMAPortInstanceSymbol port : ports)
{
String kind = port.isIncoming()?"incoming":"outgoing";
String symbolKind = "unknown symbol";
Optional<MiddlewareSymbol> symbol = port.getMiddlewareSymbol();
if(symbol.isPresent() && symbol.get().isKindOf(SomeIPConnectionKind.INSTANCE))
{
SomeIPConnectionSymbol sym = (SomeIPConnectionSymbol) symbol.get();
String topicName = sym.getTopicName().isPresent()?sym.getTopicName().get():"unknown";
symbolKind = "someip, topic: "+topicName;
}
this.ports.add(port.getName()+" : "+kind+" ("+symbolKind+")");
}
}
public List<String> getPorts()
{
return this.ports;
}
}
package de.monticore.lang.monticar.generator.someip.template;
import de.se_rwth.commons.logging.Log;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
public class SomeIPTemplates {
private static final Template SOMEIP_PRETTYPRINT;
// Loading .ftl files
static {
Configuration conf = new Configuration(Configuration.VERSION_2_3_23);
conf.setDefaultEncoding("UTF-8");
conf.setTemplateExceptionHandler(TemplateExceptionHandler.DEBUG_HANDLER);
conf.setLogTemplateExceptions(false);
conf.setClassForTemplateLoading(SomeIPTemplates.class, "");
try {
SOMEIP_PRETTYPRINT = conf.getTemplate("PrettyPrint.ftl");
} catch (IOException e) {
String msg = "could not load template";
Log.error(msg, e);
throw new RuntimeException(msg, e);
}
}
public static String generateSomeIPAdapter(SomeIPAdapterModel model) {
HashMap<String, Object> data = new HashMap<>();
data.put("model", model);
return generate(SOMEIP_PrettyPrint, data);
}
@SuppressWarnings("rawtypes")
private static String generate(Template template, Map dataForTemplate) {
Log.errorIfNull(template);
Log.errorIfNull(dataForTemplate);
StringWriter sw = new StringWriter();
try {
template.process(dataForTemplate, sw);
} catch (TemplateException | IOException e) {
Log.error("template generation failed, template: " + template.getName(), e);
}
return sw.toString();
}
}
package emam2someipgroup;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
<#--@formatter:off-->
Component name: ${model.getCompName()}
Ports:
<#list model.getPorts() as gen>
${gen}
</#list>
package de.monticore.lang.monticar.generator.someip;
/*
* Copyright (c) 2015 RWTH Aachen. All rights reserved.
*
* http://www.se-rwth.de/
*/
import de.monticore.ModelingLanguageFamily;
import de.monticore.io.paths.ModelPath;
import de.monticore.lang.embeddedmontiarc.LogConfig;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarcmath._symboltable.EmbeddedMontiArcMathLanguage;
import de.monticore.lang.embeddedmontiarc.tagging.middleware.ros.RosToEmamTagSchema;
import de.monticore.lang.monticar.Utils;
import de.monticore.lang.monticar.enumlang._symboltable.EnumLangLanguage;
import de.monticore.lang.monticar.streamunits._symboltable.StreamUnitsLanguage;
import de.monticore.lang.monticar.struct._symboltable.StructLanguage;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
import de.monticore.symboltable.GlobalScope;
import de.monticore.symboltable.Scope;
import org.junit.Assert;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static org.junit.Assert.assertTrue;
/**
* Common methods for symboltable tests
*/
public class AbstractSymtabTest {
public AbstractSymtabTest() {
}
public static TaggingResolver createSymTabAndTaggingResolver(String... modelPath) {
Scope scope = createSymTab(modelPath);
TaggingResolver taggingResolver = new TaggingResolver(scope, Arrays.asList(modelPath));
RosToEmamTagSchema.registerTagTypes(taggingResolver);
return taggingResolver;
}
public static Scope createSymTab(String... modelPath) {
ModelingLanguageFamily fam = new ModelingLanguageFamily();
EmbeddedMontiArcMathLanguage montiArcLanguage = new EmbeddedMontiArcMathLanguage();
fam.addModelingLanguage(montiArcLanguage);
fam.addModelingLanguage(new StreamUnitsLanguage());
fam.addModelingLanguage(new StructLanguage());
fam.addModelingLanguage(new EnumLangLanguage());
ModelPath mp = new ModelPath(new Path[0]);
String[] var4 = modelPath;
int var5 = modelPath.length;
for(int var6 = 0; var6 < var5; ++var6) {
String m = var4[var6];
mp.addEntry(Paths.get(m));
}
LogConfig.init();
GlobalScope scope = new GlobalScope(mp, fam);
Utils.addBuiltInTypes(scope);
return scope;
}
public static void testFilesAreEqual(List<File> files, String restPath) {
assertTrue(files.size() > 0);
for (File f : files) {
File fileTarget = new File("./src/test/resources/results/" + restPath + f.getName());
// System.out.println("" + fileTarget.exists() + "Exists:");
// System.out.println(f.getName() + " " + fileTarget.getName() + "Comparing:");
assertTrue(areBothFilesEqual(f, fileTarget));
}
}
public static void testFilesAreEqual(List<File> files, String relResultPath, String basePath) {
String resultPath = "src/test/resources/results/";
for (File f : files) {
File tmpFile = new File(basePath);
String relativePath = f.getAbsolutePath().replace(tmpFile.getAbsolutePath(), "");
File fileTarget = new File(resultPath + relResultPath + relativePath);
assertTrue(areBothFilesEqual(f, fileTarget));
}
}
public static boolean areBothFilesEqual(File file1, File file2) {
if (!file1.exists()) {
Assert.fail("file does not exist: " + file1.getAbsolutePath());
return false;
}
if (!file2.exists()) {
Assert.fail("file does not exist: " + file2.getAbsolutePath());
return false;
}
List<String> lines1;
List<String> lines2;
try {
lines1 = Files.readAllLines(file1.toPath());
lines2 = Files.readAllLines(file2.toPath());
} catch (IOException e) {
e.printStackTrace();
Assert.fail("IO error: " + e.getMessage());
return false;
}
lines1 = discardEmptyLines(lines1);
lines1 = discardCommentLines(lines1);
lines2 = discardEmptyLines(lines2);
lines2 = discardCommentLines(lines2);
if (lines1.size() != lines2.size()) {
Assert.fail(
"files have different number of lines: "
+ file1.getAbsolutePath()
+ " has " + lines1.size()
+ " lines and " + file2.getAbsolutePath() + " has " + lines2.size() + " lines"
);
return false;
}
int len = lines1.size();
for (int i = 0; i < len; i++) {
String l1 = lines1.get(i);
String l2 = lines2.get(i);
Assert.assertEquals("files differ in " + i + " line: "
+ file1.getAbsolutePath()
+ " has " + l1
+ " and " + file2.getAbsolutePath() + " has " + l2,
l1,
l2
);
}
return true;
}
private static List<String> discardEmptyLines(List<String> lines) {
return lines.stream()
.map(String::trim)
.filter(l -> !l.isEmpty())
.collect(Collectors.toList());
}
private static List<String> discardCommentLines(List<String> lines) {
return lines.stream()
.map(String::trim)
.filter(l -> !l.startsWith("//"))
.collect(Collectors.toList());
}
}
package de.monticore.lang.monticar.generator.someip;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarc.tagging.middleware.ros.RosToEmamTagSchema;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
import de.monticore.lang.embeddedmontiarc.tagging.middleware.someip.SomeIPConnectionSymbol;
import static org.junit.Assert.assertNotNull;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.junit.Test;
public class PrettyPrintGenerationTest extends AbstractSymtabTest {
@Test
public void testPrettyPrint() throws IOException {
TaggingResolver taggingResolver = createSymTabAndTaggingResolver("src/test/resources/");
// Ros schema is used for now
RosToEmamTagSchema.registerTagTypes(taggingResolver);
// Create component instance and run the generator
EMAComponentInstanceSymbol componentInstanceSymbol = taggingResolver.<EMAComponentInstanceSymbol>resolve("tests.a.compA", EMAComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentInstanceSymbol);
GeneratorSomeIP generatorSomeIP = new GeneratorSomeIP();
// Connect component's ports to topics
componentInstanceSymbol.getPortInstance("portA").orElse(null).setMiddlewareSymbol(new SomeIPConnectionSymbol(1,2,3));
List<File> files = generatorSomeIP.generatePrettyPrint(componentInstanceSymbol);
//testFilesAreEqual(files, "");
}
}
package emam2someipgroup;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
/**
* Unit test for simple App.
*/
public class AppTest
{
/**
* Rigorous Test :-)
*/
@Test
public void shouldAnswerWithTrue()
{
assertTrue( true );
}
}
Component name: tests.a.compA
Ports:
portA : incoming (mqtt, topic: /clock)
portB : incoming (unknown symbol)
portC : outgoing (unknown symbol)
portD : outgoing (unknown symbol)
package tests.a;
component CompA{
port in Q portA;
port in Q portB;
port out Q portC;
port out Q portD;
implementation Math{
portA = portD;
}
}
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