Commit dc267a56 authored by Georg Vinogradov's avatar Georg Vinogradov
Browse files

GeneratorMqtt.java now generates ports.txt from emam component

parent bdb75fc2
Pipeline #154958 passed with stage
in 1 minute and 16 seconds
package de.monticore.lang.monticar.generator.mqtt;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAPortInstanceSymbol;
import de.monticore.lang.monticar.generator.mqtt.template.MqttAdapterModel;
import de.monticore.lang.monticar.generator.mqtt.template.MqttTemplates;
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 GeneratorMqtt
......@@ -10,8 +17,38 @@ public class GeneratorMqtt
List<File> generateMqttAdapter(EMAComponentInstanceSymbol component)
{
return null;
List<File> files = new ArrayList<>();
// Get info about the ports from the component
Collection<EMAPortInstanceSymbol> ports = component.getPortInstanceList();
// Create and fill model
MqttAdapterModel model = new MqttAdapterModel(component.getFullName());
model.addPorts(ports);
//Generate files
String content = MqttTemplates.generateRosAdapter(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.mqtt.template;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAPortInstanceSymbol;
import java.util.*;
// Used to fill .ftl files
public class MqttAdapterModel {
private String compName;
private List<String> ports = new ArrayList<>();
public MqttAdapterModel(String compName)
{
this.compName = compName;
}
public String getCompName()
{
return compName;
}
public void addPorts(Collection<EMAPortInstanceSymbol> ports)
{
for (EMAPortInstanceSymbol port : ports)
{
String kind = port.isIncoming()?"incoming":"outgoing";
this.ports.add(port.getName()+" : "+kind);
}
}
public List<String> getPorts()
{
return this.ports;
}
}
......@@ -6,9 +6,12 @@ import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import de.se_rwth.commons.logging.Log;
@SuppressWarnings("unused")
public class MqttTemplates {
private static final Template PrettyPrint;
......@@ -28,4 +31,23 @@ public class MqttTemplates {
throw new RuntimeException(msg, e);
}
}
public static String generateRosAdapter(MqttAdapterModel model) {
HashMap<String, Object> data = new HashMap<>();
data.put("model", model);
return generate(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();
}
}
<#--@formatter:off-->
Component name: ${model.getCompName()}
Ports:
<#list model.getPorts() as gen>
${gen}
</#list>
\ No newline at end of file
package de.monticore.lang.monticar.generator.mqtt;
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 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 {
......@@ -21,20 +18,14 @@ public class PrettyPrintGenerationTest extends AbstractSymtabTest {
// 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);
/*
Optional<EMAPortInstanceSymbol> port = componentInstanceSymbol.getPortInstance("rosOut");
System.out.println("is the port incoming: "+port.get().isIncoming());
*/
GeneratorMqtt generatorMqtt = new GeneratorMqtt();
List<File> files = generatorMqtt.generateMqttAdapter(componentInstanceSymbol);
generatorMqtt.generateMqttAdapter(componentInstanceSymbol);
}
}
......@@ -4,7 +4,7 @@ component CompA{
port in Q portA;
port in Q portB;
port out Q portC;
port out Q PortD;
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