Commit 26a99f71 authored by Nils Kaminski's avatar Nils Kaminski
Browse files

Start of Event Handling + public private pattern for variables & methods

parent 63b5352c
Pipeline #79050 failed
......@@ -7,11 +7,14 @@ import java.util.List;
* @author Sascha Schneiders
*/
public class Method {
String name;
String typeName;
List<Variable> parameters = new ArrayList<>();
List<Instruction> instructions = new ArrayList<>();
protected boolean isPublic = true;
public Method() {
}
......@@ -88,4 +91,12 @@ public class Method {
args += parameters.get(size - 1).getNameTargetLanguageFormat();
return String.format("%s(%s)", name, args);
}
public boolean isPublic() {
return isPublic;
}
public void setPublic(boolean aPublic) {
isPublic = aPublic;
}
}
......@@ -28,6 +28,8 @@ public class Variable {
int arraySize = 1;
boolean isArray = false;
boolean isPublic = true;
boolean isDynamic = false;
......@@ -260,4 +262,12 @@ public class Variable {
public void setDynamic(boolean dynamic) {
isDynamic = dynamic;
}
public boolean isPublic() {
return isPublic;
}
public void setPublic(boolean aPublic) {
isPublic = aPublic;
}
}
......@@ -66,7 +66,7 @@ public class BluePrintFixer {
variable.setTypeNameTargetLanguage("bool");
variable.setIsConstantVariable(true);
variable.setConstantValue(s);
variable.setPublic(false);
bluePrint.getMathInformationRegister().addVariable(variable);
......
package de.monticore.lang.monticar.generator.cpp.Dynamics;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.cncModel.EMAComponentSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarcdynamic.embeddedmontiarcdynamic._symboltable.instanceStructure.EMADynamicComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarcdynamic.embeddedmontiarcdynamic._symboltable.instanceStructure.EMADynamicEventHandlerInstanceSymbol;
import de.monticore.lang.monticar.generator.BluePrint;
import de.monticore.lang.monticar.generator.Method;
import de.monticore.lang.monticar.generator.TargetCodeInstruction;
import de.monticore.lang.monticar.generator.cpp.BluePrintCPP;
import de.monticore.lang.monticar.generator.cpp.GeneratorCPP;
import java.util.Collection;
import java.util.List;
public class EventHandlerMethodsGenerator {
public static final String EventhandlerIN = "eventhandler_in";
public static final String EventhandlerOUT = "eventhandler_out";
public static final String EventHandleMethodExtIN = "_in";
public static final String EventHandleMethodExtOUT = "_out";
public static final String EventHandleMethodExtCOND = "_condition";
public static void generateMethods(EMADynamicComponentInstanceSymbol componentSymbol, BluePrintCPP bluePrint){
Collection<EMADynamicEventHandlerInstanceSymbol> eventHandlers = componentSymbol.getEventHandlers();
if(eventHandlers.size() == 0){
return;
}
Method inGather = new Method(EventhandlerIN, "void");
inGather.setPublic(false);
Method outGather = new Method(EventhandlerOUT, "void");
outGather.setPublic(false);
for (EMADynamicEventHandlerInstanceSymbol eh : eventHandlers){
Method evCond = new Method(eh.getName()+EventHandleMethodExtCOND, "inline bool");
evCond.setPublic(false);
Method evIn = new Method(eh.getName()+EventHandleMethodExtIN, "inline void");
evIn.setPublic(false);
Method evOut = new Method(eh.getName()+EventHandleMethodExtIN, "inline void");
evOut.setPublic(false);
}
bluePrint.addMethod(inGather);
bluePrint.addMethod(outGather);
}
public static void generateInputsOfEventHandlers(EMAComponentInstanceSymbol componentSymbol, BluePrintCPP bluePrint, Method executeMethod){
if(!(componentSymbol instanceof EMADynamicComponentInstanceSymbol)){
return;
}
EMADynamicComponentInstanceSymbol dynComp = (EMADynamicComponentInstanceSymbol)componentSymbol;
//TODO: better check
Collection<EMADynamicEventHandlerInstanceSymbol> eventHandlers = dynComp.getEventHandlers();
if(eventHandlers.size() == 0){
return;
}
Method inGather = new Method(EventhandlerIN, "void");
inGather.setPublic(false);
executeMethod.addInstruction(new TargetCodeInstruction(EventhandlerIN+"();\n"));
bluePrint.addMethod(inGather);
}
}
package de.monticore.lang.monticar.generator.cpp.Dynamics;
import de.monticore.lang.monticar.generator.FileContent;
import de.monticore.lang.monticar.generator.cpp.ArmadilloHelperSource;
import de.monticore.lang.monticar.generator.cpp.template.AllTemplates;
import freemarker.template.Template;
/**
*
*/
public class EventPortValueCheck {
public static String fileName = "PortValueCheck";
public static FileContent getEventPortValueCheckFileContent() {
FileContent fileContent = new FileContent();
fileContent.setFileName(fileName + ".h");
String fileContentString = AllTemplates.generateDynamicEventsPortValueCheck();
fileContent.setFileContent(fileContentString);
return fileContent;
}
}
......@@ -12,17 +12,21 @@ import de.monticore.lang.monticar.generator.order.ImplementExecutionOrder;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
import de.se_rwth.commons.logging.Log;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
* @author Sascha Schneiders
*/
public class ExecutionOrderFixer {
public static void fixExecutionOrder(TaggingResolver taggingResolver, BluePrintCPP bluePrintCPP, GeneratorCPP generatorCPP) {
Method method = bluePrintCPP.getMethod("execute").get();
Optional<Method> optMethod = bluePrintCPP.getMethod("execute_inner");
Method method = null;
if(optMethod.isPresent()){
method = optMethod.get();
}else {
method = bluePrintCPP.getMethod("execute").get();
}
Map<String, List<Instruction>> map = new HashMap<>();
List<EMAComponentInstanceSymbol> threadableSubComponents = bluePrintCPP.getOriginalSymbol().getIndependentSubComponents();
......
package de.monticore.lang.monticar.generator.cpp;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarcdynamic.embeddedmontiarcdynamic._symboltable.instanceStructure.EMADynamicComponentInstanceSymbol;
import de.monticore.lang.math._symboltable.MathStatementsSymbol;
import de.monticore.lang.monticar.generator.*;
import de.monticore.lang.monticar.generator.cmake.CMakeConfig;
import de.monticore.lang.monticar.generator.cmake.CMakeFindModule;
import de.monticore.lang.monticar.generator.cpp.Dynamics.EventPortValueCheck;
import de.monticore.lang.monticar.generator.cpp.converter.MathConverter;
import de.monticore.lang.monticar.generator.cpp.converter.TypeConverter;
import de.monticore.lang.monticar.generator.cpp.template.AllTemplates;
......@@ -130,6 +132,9 @@ public class GeneratorCPP implements Generator {
} else {
//setGenerateMainClass(true);
}
currentFileContentList = fileContents;
fileContents.add(new FileContent(generateString(taggingResolver, componentInstanceSymbol, symtab), componentInstanceSymbol));
String lastNameWithoutArrayPart = "";
......@@ -153,6 +158,11 @@ public class GeneratorCPP implements Generator {
if (MathConverter.curBackend.getBackendName().equals("ArmadilloBackend"))
fileContents.add(ArmadilloHelper.getArmadilloHelperFileContent());
if(componentInstanceSymbol instanceof EMADynamicComponentInstanceSymbol){
//TODO: add Events Value Helper
fileContents.add(EventPortValueCheck.getEventPortValueCheckFileContent());
}
if (shouldGenerateMainClass()) {
//fileContents.add(getMainClassFileContent(componentInstanceSymbol, fileContents.get(0)));
} else if (shouldGenerateSimulatorInterface()) {
......
......@@ -19,6 +19,7 @@
package de.monticore.lang.monticar.generator.cpp;
import alice.tuprolog.Var;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstanceSymbol;
import de.monticore.lang.math._symboltable.MathStatementsSymbol;
import de.monticore.lang.monticar.generator.*;
......@@ -134,50 +135,39 @@ public class LanguageUnitCPP extends LanguageUnit {
//const variables
for (String constString : bluePrint.getConsts())
resultString += constString;
resultString += "public:\n";
//input variable
//private variables
for (Variable v : bluePrint.getVariables()) {
if (!v.isArray())
resultString += v.getVariableType().getTypeNameTargetLanguage() + " " + v.getNameTargetLanguageFormat() + ";\n";
else
resultString += v.getVariableType().getTypeNameTargetLanguage() + " " + v.getNameTargetLanguageFormat() + "[" + v.getArraySize() + "]" + ";\n";
if (v.isPublic()) {
continue;
}
resultString += generateHeaderGenerateVariable(v);
}
//generate methods
// //private methods
for (Method method : bluePrint.getMethods()) {
int counter = 0;
resultString += method.getReturnTypeName() + " " + method.getName() + "(";
for (Variable param : method.getParameters()) {
if (counter == 0) {
++counter;
resultString += param.getVariableType().getTypeNameTargetLanguage() + " " + param.getNameTargetLanguageFormat();
} else {
resultString += ", " + param.getVariableType().getTypeNameTargetLanguage() + " " + param.getNameTargetLanguageFormat();
}
if (param.isArray())
resultString += "[" + param.getArraySize() + "]";
if(method.isPublic()){
continue;
}
resultString += ")\n";//TODO add semicolon when using source files
//method body start
resultString += "{\n";
resultString += generateMethod(method);
}
for (Instruction instruction : method.getInstructions()) {
if (instruction instanceof ConnectInstructionCPP) {
ConnectInstructionCPP connectInstructionCPP = (ConnectInstructionCPP) instruction;
Log.info("v1: " + connectInstructionCPP.getVariable1().getName() + "v2: " + connectInstructionCPP.getVariable2().getName(), "Instruction:");
} else if (instruction instanceof ExecuteInstruction) {
ExecuteInstruction executeInstruction = (ExecuteInstruction) instruction;
resultString += "public:\n";
}
Log.info(resultString, "beforRes:");
resultString += instruction.getTargetLanguageInstruction();
Log.info(resultString, "afterRes:");
//input variable
for (Variable v : bluePrint.getVariables()) {
if(!v.isPublic()){
continue;
}
resultString += generateHeaderGenerateVariable(v);
}
//method body end
resultString += "}\n";
//generate methods
for (Method method : bluePrint.getMethods()) {
if(!method.isPublic()){
continue;
}
resultString += generateMethod(method);
}
......@@ -191,5 +181,48 @@ public class LanguageUnitCPP extends LanguageUnit {
return resultString;
}
protected String generateHeaderGenerateVariable(Variable v){
if (!v.isArray())
return v.getVariableType().getTypeNameTargetLanguage() + " " + v.getNameTargetLanguageFormat() + ";\n";
else
return v.getVariableType().getTypeNameTargetLanguage() + " " + v.getNameTargetLanguageFormat() + "[" + v.getArraySize() + "]" + ";\n";
}
protected String generateMethod(Method method){
int counter = 0;
String resultString = method.getReturnTypeName() + " " + method.getName() + "(";
for (Variable param : method.getParameters()) {
if (counter == 0) {
++counter;
resultString += param.getVariableType().getTypeNameTargetLanguage() + " " + param.getNameTargetLanguageFormat();
} else {
resultString += ", " + param.getVariableType().getTypeNameTargetLanguage() + " " + param.getNameTargetLanguageFormat();
}
if (param.isArray())
resultString += "[" + param.getArraySize() + "]";
}
resultString += ")\n";//TODO add semicolon when using source files
//method body start
resultString += "{\n";
for (Instruction instruction : method.getInstructions()) {
if (instruction instanceof ConnectInstructionCPP) {
ConnectInstructionCPP connectInstructionCPP = (ConnectInstructionCPP) instruction;
Log.info("v1: " + connectInstructionCPP.getVariable1().getName() + "v2: " + connectInstructionCPP.getVariable2().getName(), "Instruction:");
} else if (instruction instanceof ExecuteInstruction) {
ExecuteInstruction executeInstruction = (ExecuteInstruction) instruction;
}
Log.info(resultString, "beforRes:");
resultString += instruction.getTargetLanguageInstruction();
Log.info(resultString, "afterRes:");
}
//method body end
resultString += "}\n";
return resultString;
}
}
......@@ -4,6 +4,7 @@ import de.monticore.expressionsbasis._ast.ASTExpression;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAPortInstanceSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc.types.EMAVariable;
import de.monticore.lang.embeddedmontiarcdynamic.embeddedmontiarcdynamic._symboltable.instanceStructure.EMADynamicComponentInstanceSymbol;
import de.monticore.lang.math._symboltable.MathStatementsSymbol;
import de.monticore.lang.math._symboltable.expression.MathExpressionSymbol;
import de.monticore.lang.math._symboltable.matrix.MathMatrixArithmeticValueSymbol;
......@@ -36,21 +37,32 @@ public class ComponentConverter {
addVariables(componentSymbol, bluePrint);
String lastNameWithoutArrayPart = "";
// String lastNameWithoutArrayPart = "";
for (EMAComponentInstanceSymbol instanceSymbol : componentSymbol.getSubComponents()) {
int arrayBracketIndex = instanceSymbol.getName().indexOf("[");
boolean generateComponentInstance = true;
if (arrayBracketIndex != -1) {
generateComponentInstance = !instanceSymbol.getName().substring(0, arrayBracketIndex).equals(lastNameWithoutArrayPart);
lastNameWithoutArrayPart = instanceSymbol.getName().substring(0, arrayBracketIndex);
Log.info(lastNameWithoutArrayPart, "Without:");
Log.info(generateComponentInstance + "", "Bool:");
}
if (generateComponentInstance) {
}
// Unused: (?)
// int arrayBracketIndex = instanceSymbol.getName().indexOf("[");
// boolean generateComponentInstance = true;
// if (arrayBracketIndex != -1) {
// generateComponentInstance = !instanceSymbol.getName().substring(0, arrayBracketIndex).equals(lastNameWithoutArrayPart);
// lastNameWithoutArrayPart = instanceSymbol.getName().substring(0, arrayBracketIndex);
// Log.info(lastNameWithoutArrayPart, "Without:");
// Log.info(generateComponentInstance + "", "Bool:");
// }
// if (generateComponentInstance) {
// }
bluePrint.addVariable(ComponentInstanceConverter.convertComponentInstanceSymbolToVariable(instanceSymbol, componentSymbol));
}
/*if(componentSymbol instanceof EMADynamicComponentInstanceSymbol){
EMADynamicComponentInstanceSymbol dynComp = (EMADynamicComponentInstanceSymbol)componentSymbol;
if(dynComp.getEventHandlers().size() > 0){
EventHandlerMethodsGenerator.generateMethods(dynComp, bluePrint, generatorCPP, includeStrings);
}
}*/
//create arrays from variables that only differ at the end by _number_
BluePrintFixer.fixBluePrintVariableArrays(bluePrint);
MathInformationFilter.filterStaticInformation(componentSymbol, bluePrint, mathStatementsSymbol, generatorCPP, includeStrings);
......@@ -207,10 +219,7 @@ public class ComponentConverter {
componentSymbol, List<String> includeStrings, GeneratorCPP generatorCPP) {
return convertComponentSymbolToBluePrint(componentSymbol, null, includeStrings, generatorCPP);
}
public static void fixMathFunctions(MathExpressionSymbol mathExpressionSymbol, BluePrintCPP bluePrintCPP) {
MathFunctionFixer.fixMathFunctions(mathExpressionSymbol, bluePrintCPP);
}
}
......@@ -3,14 +3,13 @@ package de.monticore.lang.monticar.generator.cpp.converter;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAConnectorInstanceSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAPortInstanceSymbol;
import de.monticore.lang.embeddedmontiarcdynamic.embeddedmontiarcdynamic._symboltable.instanceStructure.EMADynamicComponentInstanceSymbol;
import de.monticore.lang.math._symboltable.MathStatementsSymbol;
import de.monticore.lang.math._symboltable.expression.*;
import de.monticore.lang.math._symboltable.matrix.*;
import de.monticore.lang.monticar.generator.Instruction;
import de.monticore.lang.monticar.generator.Method;
import de.monticore.lang.monticar.generator.TargetCodeMathInstruction;
import de.monticore.lang.monticar.generator.Variable;
import de.monticore.lang.monticar.generator.*;
import de.monticore.lang.monticar.generator.cpp.BluePrintCPP;
import de.monticore.lang.monticar.generator.cpp.Dynamics.EventHandlerMethodsGenerator;
import de.monticore.lang.monticar.generator.cpp.GeneratorCPP;
import de.monticore.lang.monticar.generator.cpp.MathFunctionFixer;
import de.monticore.lang.monticar.generator.cpp.instruction.ConnectInstructionCPP;
......@@ -19,6 +18,7 @@ import de.monticore.lang.monticar.generator.optimization.MathOptimizer;
import de.se_rwth.commons.logging.Log;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
......@@ -29,9 +29,43 @@ public class ComponentConverterMethodGeneration {
public static EMAComponentInstanceSymbol currentComponentSymbol = null;
public static void generateExecuteMethod(EMAComponentInstanceSymbol componentSymbol, BluePrintCPP bluePrint, MathStatementsSymbol mathStatementsSymbol, GeneratorCPP generatorCPP, List<String> includeStrings) {
Method method = new Method("execute", "void");
currentComponentSymbol = componentSymbol;
for (EMAConnectorInstanceSymbol connector : componentSymbol.getConnectorInstances()) {
if(componentSymbol instanceof EMADynamicComponentInstanceSymbol){
EMADynamicComponentInstanceSymbol dynComp = (EMADynamicComponentInstanceSymbol)componentSymbol;
if(dynComp.getEventHandlers().size() > 0){
Method execute = new Method("execute", "void");
Method inner = new Method("execute_inner", "void");
inner.setPublic(false);
generateExecuteMethodInner(inner,componentSymbol, bluePrint, mathStatementsSymbol, generatorCPP, includeStrings);
execute.addInstruction(new TargetCodeInstruction("\n//--------------------\n"));
execute.addInstruction(new TargetCodeInstruction("execute_inner();\n"));
execute.addInstruction(new TargetCodeInstruction("//--------------------\n\n"));
bluePrint.addMethod(execute);
return;
}
}
generateExecuteMethodInner(new Method("execute", "void"),componentSymbol, bluePrint, mathStatementsSymbol, generatorCPP, includeStrings);
}
protected static void generateExecuteMethodInner(Method method, EMAComponentInstanceSymbol componentSymbol, BluePrintCPP bluePrint, MathStatementsSymbol mathStatementsSymbol, GeneratorCPP generatorCPP, List<String> includeStrings){
Collection<EMAConnectorInstanceSymbol> connectors = componentSymbol.getConnectorInstances();
generateConnectors(connectors, bluePrint, method);
if (mathStatementsSymbol != null) {
handleMathStatementGeneration(method, bluePrint, mathStatementsSymbol, generatorCPP, includeStrings);
}
bluePrint.addMethod(method);
}
protected static void generateConnectors(Collection<EMAConnectorInstanceSymbol> connectors, BluePrintCPP bluePrint, Method method){
for (EMAConnectorInstanceSymbol connector : connectors) {
if (!connector.isConstant()) {
Log.info("source:" + connector.getSource() + " target:" + connector.getTarget(), "Port info:");
Variable v1 = PortConverter.getVariableForPortSymbol(connector, connector.getSource(), bluePrint);
......@@ -65,10 +99,6 @@ public class ComponentConverterMethodGeneration {
}
}
}
if (mathStatementsSymbol != null) {
handleMathStatementGeneration(method, bluePrint, mathStatementsSymbol, generatorCPP, includeStrings);
}
bluePrint.addMethod(method);
}
private static List<MathExpressionSymbol> visitedMathExpressionSymbols = new ArrayList<>();
......
......@@ -26,6 +26,8 @@ public final class AllTemplates {
private static final Template AUTOPILOT_ADAPTER;
private static final Template SERVER_WRAPPER;
private static final Template DYNAMICS_EVENT_PortValueCheker;
static {
Configuration conf = new Configuration(Configuration.VERSION_2_3_23);
conf.setDefaultEncoding("UTF-8");
......@@ -40,6 +42,7 @@ public final class AllTemplates {
ENUM = conf.getTemplate("/type/Enum.ftl");
AUTOPILOT_ADAPTER = conf.getTemplate("/autopilotadapter/AutopilotAdapter.ftl");
SERVER_WRAPPER = conf.getTemplate("/serverwrapper/ServerWrapper.ftl");
DYNAMICS_EVENT_PortValueCheker = conf.getTemplate("/dynamics/events_port_value_check_h.ftl");
} catch (IOException e) {
String msg = "could not load templates";
Log.error(msg, e);
......@@ -77,10 +80,27 @@ public final class AllTemplates {
return generate(SERVER_WRAPPER, viewModel);
}
public static String generateDynamicEventsPortValueCheck(){
return generateWithoutData(DYNAMICS_EVENT_PortValueCheker);
}
private static String generate(Template template, ViewModelBase viewModelBase) {
return generate(template, TemplateHelper.getDataForTemplate(viewModelBase));
}
private static String generateWithoutData(Template template){
Log.errorIfNull(template);
StringWriter sw = new StringWriter();
try{
template.process(new Object(), sw);
} catch (TemplateException | IOException e) {
Log.error("template generation failed, template: " + template.getName(), e);
}
return sw.toString();
}
private static String generate(Template template, Object dataForTemplate) {
Log.errorIfNull(template);
Log.errorIfNull(dataForTemplate);
......
#ifndef PortValueCheck_h
#define PortValueCheck_h
#include <iostream>
using namespace std;
template <typename T>
class TestsInterface {
public:
virtual bool check(){
return false;
}
};
template <typename T>
class EqualsTest: public TestsInterface<T>{
protected:
T value;
T* refValue;