Commit f6279b78 authored by Nils Kaminski's avatar Nils Kaminski
Browse files

Working events for port basic value / sequence (equals a value)

parent 07f4a991
Pipeline #79633 failed
package de.monticore.lang.monticar.generator;
public class EventConnectInstruction extends ConnectInstruction {
protected String eventName;
public EventConnectInstruction(String name) {
super();
eventName = name;
}
public EventConnectInstruction(String name, Variable variable1, Variable variable2) {
super(variable1, variable2);
eventName = name;
}
public EventConnectInstruction(String name, Variable variable1, boolean useThis1, Variable variable2, boolean useThis2) {
super(variable1, useThis1, variable2, useThis2);
eventName = name;
}
public String getEventName() {
return eventName;
}
public void setEventName(String eventName) {
this.eventName = eventName;
}
@Override
public String getTargetLanguageInstruction() {
return null;
}
@Override
public boolean isTargetCodeInstruction() {
return false;
}
@Override
public boolean isExecuteInstruction() {
return false;
}
}
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);
}
}
......@@ -19,13 +19,9 @@ import java.util.*;
*/
public class ExecutionOrderFixer {
public static void fixExecutionOrder(TaggingResolver taggingResolver, BluePrintCPP bluePrintCPP, GeneratorCPP generatorCPP) {
Optional<Method> optMethod = bluePrintCPP.getMethod("execute_inner");
Method method = null;
if(optMethod.isPresent()){
method = optMethod.get();
}else {
method = bluePrintCPP.getMethod("execute").get();
}
Method method = bluePrintCPP.getMethod("execute").get();
Map<String, List<Instruction>> map = new HashMap<>();
......@@ -48,6 +44,9 @@ public class ExecutionOrderFixer {
newList.addAll(joinInstructions);
newList.addAll(otherInstructions);
fixNextInstruction(newList, bluePrintCPP);
method.setInstructions(newList);
}
......@@ -61,6 +60,12 @@ public class ExecutionOrderFixer {
}
}
public static void fixNextInstruction(List<Instruction> newList, BluePrintCPP bluePrintCPP){
if(bluePrintCPP.getMethod("next").isPresent()){
newList.add(0, new TargetCodeInstruction("next();\n"));
}
}
public static boolean listContainsExecuteInstruction(List<Instruction> list, String name) {
for (Instruction instruction : list) {
if (instruction.isExecuteInstruction()) {
......@@ -72,7 +77,6 @@ public class ExecutionOrderFixer {
return false;
}
public static Instruction getExecuteInstruction(String nameToAdd, BluePrintCPP bluePrintCPP, List<EMAComponentInstanceSymbol> threadableComponents, GeneratorCPP generatorCPP) {
boolean canBeThreaded = false;
if (generatorCPP.useThreadingOptimizations())
......@@ -108,6 +112,9 @@ public class ExecutionOrderFixer {
otherInstructions.add(instruction);
}
} else {
if(instruction.isTargetCodeInstruction() && instruction.getTargetLanguageInstruction().equals("next();\n")){
continue;
}
otherInstructions.add(instruction);
}
}
......
......@@ -160,7 +160,8 @@ public class GeneratorCPP implements Generator {
if(componentInstanceSymbol instanceof EMADynamicComponentInstanceSymbol){
//TODO: add Events Value Helper
fileContents.add(EventPortValueCheck.getEventPortValueCheckFileContent());
if(!((EMADynamicComponentInstanceSymbol) componentInstanceSymbol).getEventHandlers().isEmpty())
fileContents.add(EventPortValueCheck.getEventPortValueCheckFileContent());
}
if (shouldGenerateMainClass()) {
......
......@@ -111,6 +111,12 @@ public class LanguageUnitCPP extends LanguageUnit {
resultString += "#include \"" + v.getVariableType().getIncludeName() + ".h\"\n";
}
}
if(v instanceof VariablePortValueChecker){
if(!alreadyGeneratedIncludes.contains("PortValueCheck")){
alreadyGeneratedIncludes.add("PortValueCheck");
resultString += "#include \"PortValueCheck.h\"\n";
}
}
}
for (String string : bluePrint.getAdditionalIncludeStrings())
......@@ -182,10 +188,16 @@ public class LanguageUnitCPP extends LanguageUnit {
}
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";
if(v instanceof VariablePortValueChecker){
return ((VariablePortValueChecker)v).getTypeTargetLanguageFormat() +" "+ v.getNameTargetLanguageFormat()+";\n";
}else {
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){
......
package de.monticore.lang.monticar.generator.cpp;
import de.monticore.lang.monticar.generator.Method;
import de.monticore.lang.monticar.generator.TargetCodeInstruction;
import de.monticore.lang.monticar.generator.Variable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class VariablePortValueChecker extends Variable {
protected static int PVCCOUNTER =0;
protected int idPVC = 0;
protected List<VariablePortValueCheckerTestSymbol> testsInitList = new ArrayList<>();
public VariablePortValueChecker(String name){
super();
this.setName(name);
this.idPVC = (PVCCOUNTER++);
this.setPublic(false);
}
public int getIdPVC() {
return idPVC;
}
@Override
public String getNameTargetLanguageFormat() {
return "__pvc_"+this.idPVC+"_"+GeneralHelperMethods.getTargetLanguageVariableInstanceName(this.getName());
//return name.replaceAll("\\[", "_").replaceAll("\\]", "_");
}
public String getTypeTargetLanguageFormat(){
return "PortValueCheck<"+this.getVariableType().getTypeNameTargetLanguage()+","+this.testsInitList.size()+">";
}
public void addInitInstructionsToMethod(Method init){
TargetCodeInstruction ti = new TargetCodeInstruction(getNameTargetLanguageFormat()+".setPortReference(&"+this.getName()+");\n" );
init.addInstruction(ti);
int id = 0;
for (VariablePortValueCheckerTestSymbol test : this.testsInitList){
String s = getNameTargetLanguageFormat()+".";
if(test.testType == VariablePortValueCheckerTestSymbolType.EqualsTest){
s += "setEqualsTest("+id+", "+test.value+");\n";
}else{
continue;
}
init.addInstruction(new TargetCodeInstruction(s));
++id;
}
}
public TargetCodeInstruction nextValueInstruction(){
return new TargetCodeInstruction(this.getNameTargetLanguageFormat()+".next();\n");
}
public void addTestSymbol(VariablePortValueCheckerTestSymbol symbol){
testsInitList.add(symbol);
}
public void addTestSymbol_Equals(String value){
addTestSymbol(
new VariablePortValueCheckerTestSymbol(value, VariablePortValueCheckerTestSymbolType.EqualsTest)
);
}
//<editor-fold desc="VariablePortValueCheckerTestSymbol">
public enum VariablePortValueCheckerTestSymbolType{
EqualsTest
}
public class VariablePortValueCheckerTestSymbol{
public String value;
public VariablePortValueCheckerTestSymbolType testType;
public VariablePortValueCheckerTestSymbol(String value, VariablePortValueCheckerTestSymbolType testType){
this.value = value;
this.testType = testType;
}
}
//</editor-fold>
}
......@@ -39,19 +39,19 @@ public class ComponentConverter {
// String lastNameWithoutArrayPart = "";
String lastNameWithoutArrayPart = "";
for (EMAComponentInstanceSymbol instanceSymbol : componentSymbol.getSubComponents()) {
// 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) {
// }
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));
}
......@@ -63,15 +63,20 @@ public class ComponentConverter {
}*/
//create arrays from variables that only differ at the end by _number_
BluePrintFixer.fixBluePrintVariableArrays(bluePrint);
MathInformationFilter.filterStaticInformation(componentSymbol, bluePrint, mathStatementsSymbol, generatorCPP, includeStrings);
generateInitMethod(componentSymbol, bluePrint, generatorCPP, includeStrings);
//generate execute method
ComponentConverterMethodGeneration.generateExecuteMethod(componentSymbol, bluePrint, mathStatementsSymbol, generatorCPP, includeStrings);
Method execute = ComponentConverterMethodGeneration.generateExecuteMethod(componentSymbol, bluePrint, mathStatementsSymbol, generatorCPP, includeStrings);
Method init = generateInitMethod(componentSymbol, bluePrint, generatorCPP, includeStrings);
bluePrint.addMethod(init);
bluePrint.addMethod(execute);
EventConverter.generatePVCNextMethod(bluePrint);
return bluePrint;
}
......@@ -104,9 +109,9 @@ public class ComponentConverter {
}
}
public static void generateInitMethod(EMAComponentInstanceSymbol componentSymbol, BluePrintCPP bluePrint, GeneratorCPP generatorCPP, List<String> includeStrings) {
public static Method generateInitMethod(EMAComponentInstanceSymbol componentSymbol, BluePrintCPP bluePrint, GeneratorCPP generatorCPP, List<String> includeStrings) {
Method method = new Method("init", "void");
bluePrint.addMethod(method);
// bluePrint.addMethod(method);
for (Variable v : bluePrint.getMathInformationRegister().getVariables()) {
String oldName = v.getName();
if (v.isArray()) {
......@@ -125,13 +130,17 @@ public class ComponentConverter {
for (Variable v : bluePrint.getVariables()) {
Log.info("Variable: " + v.getName(), "initBluePrintCreate:");
if (v.isInputVariable() && !v.isConstantVariable()) {
//method.addParameter(v);
//Instruction instruction = new ConnectInstructionCPP(v, true, v, false);
//method.addInstruction(instruction);
} else if (v.isConstantVariable()) {
Instruction instruction = new ConstantConnectInstructionCPP(v, v);
method.addInstruction(instruction);
if(v instanceof VariablePortValueChecker){
((VariablePortValueChecker)v).addInitInstructionsToMethod(method);
}else {
if (v.isInputVariable() && !v.isConstantVariable()) {
//method.addParameter(v);
//Instruction instruction = new ConnectInstructionCPP(v, true, v, false);
//method.addInstruction(instruction);
} else if (v.isConstantVariable()) {
Instruction instruction = new ConstantConnectInstructionCPP(v, v);
method.addInstruction(instruction);
}
}
}
......@@ -151,6 +160,7 @@ public class ComponentConverter {
TargetCodeInstruction instruction = new TargetCodeInstruction(result);
method.addInstruction(instruction);
}
return method;
}
public static void generateInitStaticVariablePart(Method method, Variable v, BluePrintCPP bluePrint) {
......
......@@ -3,13 +3,11 @@ 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.*;
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;
......@@ -28,28 +26,39 @@ public class ComponentConverterMethodGeneration {
public static int currentGenerationIndex = 0;
public static EMAComponentInstanceSymbol currentComponentSymbol = null;
public static void generateExecuteMethod(EMAComponentInstanceSymbol componentSymbol, BluePrintCPP bluePrint, MathStatementsSymbol mathStatementsSymbol, GeneratorCPP generatorCPP, List<String> includeStrings) {
public static Method generateExecuteMethod(EMAComponentInstanceSymbol componentSymbol, BluePrintCPP bluePrint, MathStatementsSymbol mathStatementsSymbol, GeneratorCPP generatorCPP, List<String> includeStrings) {
currentComponentSymbol = componentSymbol;
/*
if(componentSymbol instanceof EMADynamicComponentInstanceSymbol){
EMADynamicComponentInstanceSymbol dynComp = (EMADynamicComponentInstanceSymbol)componentSymbol;
if(dynComp.getEventHandlers().size() > 0){
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);
EventHandlerMethodsGenerator.generateInputsOfEventHandlers(componentSymbol, bluePrint,execute);
if (!inner.getInstructions().isEmpty()){
execute.addInstruction(new TargetCodeInstruction("//--------------------\n"));
execute.addInstruction(new TargetCodeInstruction("execute_inner();\n"));
execute.addInstruction(new TargetCodeInstruction("//--------------------\n"));
bluePrint.addMethod(inner);
}
EventHandlerMethodsGenerator.generateOutputsOfEventHandlers(componentSymbol, bluePrint,execute);
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);
*/
Method exMethod = new Method("execute", "void");
generateExecuteMethodInner(exMethod,componentSymbol, bluePrint, mathStatementsSymbol, generatorCPP, includeStrings);
// bluePrint.addMethod(exMethod);
return exMethod;
}
protected static void generateExecuteMethodInner(Method method, EMAComponentInstanceSymbol componentSymbol, BluePrintCPP bluePrint, MathStatementsSymbol mathStatementsSymbol, GeneratorCPP generatorCPP, List<String> includeStrings){
......@@ -58,13 +67,15 @@ public class ComponentConverterMethodGeneration {
Collection<EMAConnectorInstanceSymbol> connectors = componentSymbol.getConnectorInstances();
generateConnectors(connectors, bluePrint, method);
EventConverter.generateEventConnectors(method, componentSymbol, bluePrint, mathStatementsSymbol,generatorCPP, includeStrings);
if (mathStatementsSymbol != null) {
handleMathStatementGeneration(method, bluePrint, mathStatementsSymbol, generatorCPP, includeStrings);
}
bluePrint.addMethod(method);
}
protected static void generateConnectors(Collection<EMAConnectorInstanceSymbol> connectors, BluePrintCPP bluePrint, Method method){
public 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:");
......
package de.monticore.lang.monticar.generator.cpp.converter;
import alice.tuprolog.Var;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAPortInstanceSymbol;
import de.monticore.lang.embeddedmontiarcdynamic.embeddedmontiarcdynamic._symboltable.instanceStructure.EMADynamicComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarcdynamic.embeddedmontiarcdynamic._symboltable.instanceStructure.EMADynamicConnectorInstanceSymbol;
import de.monticore.lang.embeddedmontiarcdynamic.embeddedmontiarcdynamic._symboltable.instanceStructure.EMADynamicEventHandlerInstanceSymbol;
import de.monticore.lang.embeddedmontiarcdynamic.event._symboltable.expression.EventBracketExpressionSymbol;
import de.monticore.lang.embeddedmontiarcdynamic.event._symboltable.expression.EventExpressionSymbol;
import de.monticore.lang.embeddedmontiarcdynamic.event._symboltable.expression.EventLogicalOperationExpressionSymbol;
import de.monticore.lang.embeddedmontiarcdynamic.event._symboltable.expression.EventPortExpressionValueSymbol;
import de.monticore.lang.embeddedmontiarcdynamic.event._symboltable.expression.portvalueexpressionvalues.PortValueInputSymbol;
import de.monticore.lang.embeddedmontiarcdynamic.event._symboltable.expression.portvalueexpressionvalues.PortValueSymbol;
import de.monticore.lang.embeddedmontiarcdynamic.event._symboltable.expression.portvalueexpressionvalues.PortValuesArraySymbol;
import de.monticore.lang.math._symboltable.MathStatementsSymbol;
import de.monticore.lang.monticar.generator.Instruction;
import de.monticore.lang.monticar.generator.Method;
import de.monticore.lang.monticar.generator.TargetCodeInstruction;
import de.monticore.lang.monticar.generator.Variable;
import de.monticore.lang.monticar.generator.cpp.BluePrintCPP;
import de.monticore.lang.monticar.generator.cpp.GeneratorCPP;
import de.monticore.lang.monticar.generator.cpp.VariablePortValueChecker;
import de.monticore.lang.monticar.generator.cpp.instruction.ConnectInstructionCPP;
import de.monticore.lang.monticar.generator.cpp.instruction.EventConnectInstructionCPP;
import de.se_rwth.commons.logging.Log;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
public class EventConverter {
public static void generateEventConnectors(Method method, EMAComponentInstanceSymbol componentSymbol, BluePrintCPP bluePrint, MathStatementsSymbol mathStatementsSymbol, GeneratorCPP generatorCPP, List<String> includeStrings){
if(!(componentSymbol instanceof EMADynamicComponentInstanceSymbol)){
return;
}
EMADynamicComponentInstanceSymbol dynComponent = (EMADynamicComponentInstanceSymbol) componentSymbol;
for(EMADynamicEventHandlerInstanceSymbol event : dynComponent.getEventHandlers()){
Log.info("Create connectors for: "+event.getFullName(), "EventConverter");
int number = 0;
for(EMADynamicConnectorInstanceSymbol connector : event.getConnectorsDynamic()){
if(connector.isDynamicSourceNewComponent() || connector.isDynamicSourceNewPort() || connector.isDynamicTargetNewComponent() || connector.isDynamicTargetNewPort()){
continue;
}
generateEventConnector(event.getFullName(), connector, bluePrint, method);
++number;
}
if (number > 0) {
//generate event condition method
Method condition = new Method(EventConnectInstructionCPP.getEventNameCPP(event.getFullName()), "bool");
condition.setPublic(false);
String conditionExpression = "return ";
conditionExpression += generateEventCondition(event.getCondition(), componentSymbol, bluePrint);
conditionExpression += ";\n";
condition.addInstruction(new TargetCodeInstruction(conditionExpression));
bluePrint.addMethod(condition);
}
}
}