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

Handling of free events

parent e17a12be
Pipeline #86414 failed with stage
in 2 minutes and 26 seconds
......@@ -21,7 +21,6 @@ import de.monticore.lang.monticar.generator.cpp.instruction.EventConnectInstruct
import de.monticore.lang.monticar.generator.cpp.instruction.ExecuteDynamicConnects;
import de.se_rwth.commons.logging.Log;
import javax.swing.plaf.metal.MetalTheme;
import java.util.*;
public class EventConverter {
......@@ -41,6 +40,8 @@ public class EventConverter {
if(event.isDynamicPortConnectionEvent()) {
// generateCondition = generateDynamicConnectEvent(event, componentSymbol, executeMethod, bluePrint);
generateCondition = EventDynamicConnectConverter.generateDynamicConnectEvent(event, componentSymbol, executeMethod, bluePrint);
}else if(event.isDynamicPortFreeEvent()){
generateCondition = generateFreeEvent(event, executeMethod, bluePrint);
}else{
Log.info("Create connectors for: "+event.getFullName(), "EventConverter");
......@@ -82,6 +83,10 @@ public class EventConverter {
return number > 0;
}
protected static boolean generateFreeEvent(EMADynamicEventHandlerInstanceSymbol event, Method executeMethod, BluePrint bluePrint){
return true;
}
public static void generatePVCNextMethod(BluePrintCPP bluePrint){
Method next = new Method("next", "void");
next.setPublic(false);
......@@ -133,9 +138,6 @@ public class EventConverter {
}
//<editor-fold desc="Generate event condition">
protected static String generateEventCondition(EventExpressionSymbol expression, EMAComponentInstanceSymbol componentSymbol, BluePrintCPP bluePrint){
......@@ -147,6 +149,8 @@ public class EventConverter {
return generateEventConditionEventPortValueSymbol((EventPortExpressionValueSymbol) expression, componentSymbol, bluePrint);
}else if(expression instanceof EventPortExpressionConnectSymbol){
return generateEventConditionEventPortConnectSymbol((EventPortExpressionConnectSymbol) expression, componentSymbol, bluePrint);
}else if(expression instanceof EventPortExpressionFreeSymbol){
return "( /*"+((EventPortExpressionFreeSymbol) expression).getPortName()+"::free*/ true)";
}
if(expression instanceof EventBooleanExpressionSymbol){
......
......@@ -3,6 +3,7 @@ package de.monticore.lang.monticar.generator.cpp.converter;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.cncModel.EMAPortSymbol;
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.monticar.generator.*;
......@@ -11,9 +12,10 @@ import de.monticore.lang.monticar.generator.cpp.GeneralHelperMethods;
import de.monticore.lang.monticar.generator.cpp.instruction.EventConnectInstructionCPP;
import de.monticore.lang.monticar.generator.cpp.instruction.ExecuteDynamicConnects;
import de.se_rwth.commons.logging.Log;
import javafx.util.Pair;
import java.util.*;
import java.util.stream.Collectors;
public class EventDynamicConnectConverter {
......@@ -75,7 +77,7 @@ public class EventDynamicConnectConverter {
//finish everything that needs to be set
//
body.getInstructions().add(1, new TargetCodeInstruction("int* _connected_idxs = (int *)calloc("+free_method_index_counter+", sizeof(int));\n"));
generateEndOfFreeMethod(event, free);
generateEndOfFreeMethod(event, free, componentSymbol);
String varName = "__event_connects_"+convertName(event.getName());
if(!bluePrint.getVariable(varName).isPresent()){
......@@ -167,7 +169,7 @@ public class EventDynamicConnectConverter {
return method;
}
protected static void generateEndOfFreeMethod(EMADynamicEventHandlerInstanceSymbol event, Method free){
protected static void generateEndOfFreeMethod(EMADynamicEventHandlerInstanceSymbol event, Method free, EMAComponentInstanceSymbol componentSymbol){
String name = String.format(FREE_CONNECTIONTRACARRAY, convertName(event.getName()));
free.addInstruction(new TargetCodeInstruction(
......@@ -178,6 +180,23 @@ public class EventDynamicConnectConverter {
}
free.addInstruction(new TargetCodeInstruction("free(_connected_idxs);\n"));
// free.addInstruction(new TargetCodeInstruction("//----------------------------------------------------------------------------\n"));
List<String> names = new ArrayList<>();
event.getCondition().getConnectPortNames(names);
for(EMADynamicEventHandlerInstanceSymbol freeEvent : getFreeEvents(componentSymbol)){
List<String> freeNames = new ArrayList<>();
freeEvent.getCondition().getFreePortNames(freeNames);
freeNames.removeAll(names);
if(freeNames.isEmpty()){
// free event
free.addInstruction(new TargetCodeInstruction("if("+EventConnectInstructionCPP.getEventNameCPP(freeEvent.getName())+"){\n"));
generateConnectsForFreeEventBody(freeEvent, free);
free.addInstruction(new TargetCodeInstruction("}\n"));
}
}
// free.addInstruction(new TargetCodeInstruction("//----------------------------------------------------------------------------\n"));
free.addInstruction(new TargetCodeInstruction("}\n}\n"));
}
......@@ -322,38 +341,11 @@ public class EventDynamicConnectConverter {
protected static void generateConnects(EMADynamicEventHandlerInstanceSymbol event, Method body, BluePrintCPP bluePrint, Method executeMethod, Method free){
for(EMADynamicConnectorInstanceSymbol connector : event.getConnectorsDynamic()){
Optional<String> before = Optional.empty();
String sourceName = connector.getSource();
String targetName = connector.getTarget();
Optional<String> before = generateConnectsBefore(connector);
String sourceName = generateConnectsSource(connector);
String targetName = generateConnectsTarget(connector);
EMAPortInstanceSymbol target = connector.getTargetPort();
if(connector.isDynamicSourceNewPort()) {
sourceName = generateConnectNameForNewPort(sourceName, connector.getSourceComponentName(), connector.getSourcePortName());
}else if(connector.isDynamicSourceNewComponent()){
sourceName = generateConnectNameForNewInstance(connector.getSourceComponentName().get(), connector.getSourcePortName());
}else{
sourceName = GeneralHelperMethods.getTargetLanguageVariableInstanceName(sourceName);
}
if(connector.isDynamicTargetNewPort()) {
targetName = generateConnectNameForNewPort(targetName, connector.getTargetComponentName(), connector.getTargetPortName());
before = connector.getTargetComponentName();
if (before.isPresent()) {
before = Optional.of(GeneralHelperMethods.getTargetLanguageVariableInstanceName(before.get()));
}
}else if(connector.isDynamicTargetNewComponent()){
targetName = generateConnectNameForNewInstance(connector.getTargetComponentName().get(), connector.getTargetPortName());
String inst = EMAPortSymbol.getNameWithoutArrayBracketPart(connector.getTargetComponentName().get());
before = Optional.of(String.format("%s["+DYNINSTANCEID+"]", inst, inst));
}else{
targetName = GeneralHelperMethods.getTargetLanguageVariableInstanceName(targetName);
before = connector.getTargetComponentName();
if(before.isPresent()){
before = Optional.of(GeneralHelperMethods.getTargetLanguageVariableInstanceName(before.get()));
}
}
Optional<VariableType> vt = TypeConverter.getVariableTypeForMontiCarTypeName(target.getTypeReference().getName());
generateEventDynamicConnectVector(vt.get().getTypeNameTargetLanguage(), bluePrint);
......@@ -374,6 +366,63 @@ public class EventDynamicConnectConverter {
}
}
protected static String generateConnectsSource(EMADynamicConnectorInstanceSymbol connector){
String sourceName = connector.getSource();
if(connector.isDynamicSourceNewPort()) {
sourceName = generateConnectNameForNewPort(sourceName, connector.getSourceComponentName(), connector.getSourcePortName());
}else if(connector.isDynamicSourceNewComponent()){
sourceName = generateConnectNameForNewInstance(connector.getSourceComponentName().get(), connector.getSourcePortName());
}else{
sourceName = GeneralHelperMethods.getTargetLanguageVariableInstanceName(sourceName);
}
return sourceName;
}
protected static String generateConnectsTarget(EMADynamicConnectorInstanceSymbol connector){
String targetName = connector.getTarget();
if(connector.isDynamicTargetNewPort()) {
targetName = generateConnectNameForNewPort(targetName, connector.getTargetComponentName(), connector.getTargetPortName());
// before = connector.getTargetComponentName();
// if (before.isPresent()) {
// before = Optional.of(GeneralHelperMethods.getTargetLanguageVariableInstanceName(before.get()));
// }
}else if(connector.isDynamicTargetNewComponent()){
targetName = generateConnectNameForNewInstance(connector.getTargetComponentName().get(), connector.getTargetPortName());
// String inst = EMAPortSymbol.getNameWithoutArrayBracketPart(connector.getTargetComponentName().get());
// before = Optional.of(String.format("%s["+DYNINSTANCEID+"]", inst, inst));
}else{
targetName = GeneralHelperMethods.getTargetLanguageVariableInstanceName(targetName);
// before = connector.getTargetComponentName();
// if(before.isPresent()){
// before = Optional.of(GeneralHelperMethods.getTargetLanguageVariableInstanceName(before.get()));
// }
}
return targetName;
}
protected static Optional<String> generateConnectsBefore(EMADynamicConnectorInstanceSymbol connector){
Optional<String> before = Optional.empty();
// String targetName = connector.getTarget();
if(connector.isDynamicTargetNewPort()) {
// targetName = generateConnectNameForNewPort(targetName, connector.getTargetComponentName(), connector.getTargetPortName());
before = connector.getTargetComponentName();
if (before.isPresent()) {
before = Optional.of(GeneralHelperMethods.getTargetLanguageVariableInstanceName(before.get()));
}
}else if(connector.isDynamicTargetNewComponent()){
// targetName = generateConnectNameForNewInstance(connector.getTargetComponentName().get(), connector.getTargetPortName());
String inst = EMAPortSymbol.getNameWithoutArrayBracketPart(connector.getTargetComponentName().get());
before = Optional.of(String.format("%s["+DYNINSTANCEID+"]", inst, inst));
}else{
// targetName = GeneralHelperMethods.getTargetLanguageVariableInstanceName(targetName);
before = connector.getTargetComponentName();
if(before.isPresent()){
before = Optional.of(GeneralHelperMethods.getTargetLanguageVariableInstanceName(before.get()));
}
}
return before;
}
protected static void generateEventDynamicConnectVector(String typeName, BluePrint bluePrint){
Optional<Method> execDynConnects = bluePrint.getMethod("executeDynamicConnects");
......@@ -426,7 +475,23 @@ public class EventDynamicConnectConverter {
}
protected static void generateConnectsForFreeEventBody(EMADynamicEventHandlerInstanceSymbol event, Method free){
for(EMADynamicConnectorInstanceSymbol connector : event.getConnectorsDynamic()){
String sourceName = generateConnectsSource(connector);
String targetName = generateConnectsTarget(connector);
free.addInstruction(new TargetCodeInstruction(targetName+" = "+sourceName+";\n"));
}
}
protected static String convertName(String name){
return name.replace("[", "_").replace("]", "_");
}
protected static Collection<EMADynamicEventHandlerInstanceSymbol> getFreeEvents(EMAComponentInstanceSymbol component){
if(component instanceof EMADynamicComponentInstanceSymbol) {
return ((EMADynamicComponentInstanceSymbol)component).getEventHandlers().stream().filter(e -> e.isDynamicPortFreeEvent()).collect(Collectors.toList());
}
return new ArrayList<>();
}
}
package de.monticore.lang.monticar.generator.dynamics;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstanceSymbol;
import de.monticore.lang.monticar.generator.AbstractSymtabTest;
import de.monticore.lang.monticar.generator.cpp.GeneratorCPP;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
import de.se_rwth.commons.logging.Log;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import java.io.File;
import java.io.IOException;
import java.util.List;
import static org.junit.Assert.assertNotNull;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class DynamicsPortFreeTest extends AbstractSymtabTest {
protected String path(){
return "src/test/resources/dynamics";
}
@BeforeClass
public static void setUp() {
// ensure an empty log
Log.getFindings().clear();
Log.enableFailQuick(false);
}
@Test
public void Test_00_BasicFree() throws IOException {
test("free.free", "./target/generated-sources-cpp/dynamics-free/test00");
}
@Test
public void Test_01_SetFree() throws IOException {
test("free.freeSetOnValue", "./target/generated-sources-cpp/dynamics-free/test01");
}
@Test
public void Test_02_OutFree() throws IOException {
test("free.outerFree", "./target/generated-sources-cpp/dynamics-free/test02");
}
@Test
public void Test_03_OutFree2() throws IOException {
test("free.outerFree2", "./target/generated-sources-cpp/dynamics-free/test03");
}
protected void test(String instName, String target){
try {
TaggingResolver symtab = createSymTabAndTaggingResolver(path());
EMAComponentInstanceSymbol componentSymbol = symtab.<EMAComponentInstanceSymbol>resolve(instName, EMAComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentSymbol);
GeneratorCPP generatorCPP = new GeneratorCPP();
generatorCPP.useArmadilloBackend();
generatorCPP.setGenerationTargetPath(target);
List<File> files = generatorCPP.generateFiles(symtab, componentSymbol, symtab);
}catch (IOException ex){
ex.printStackTrace();
Log.error(ex.getMessage());
}
}
}
package free;
component Free{
ports
dynamic in Q a[1:7],
dynamic out Q b[1:7],
in Q c,
out Q d;
@ a::connect && b::connect {
connect a[?] -> b[?];
}
@ b::free {
connect 42 -> b[?];
connect c -> d;
}
}
\ No newline at end of file
package free;
component FreeSetOnValue{
ports
in B setter,
dynamic in Q a[1:7],
dynamic out Q b[1:7];
@ a::connect && b::connect {
connect a[?] -> b[?];
}
@ b::free && setter::value(true) {
connect 42 -> b[?];
}
}
\ No newline at end of file
package free;
dynamic component OuterFree{
ports
dynamic in Q a[1:7],
dynamic out Q b[1:7];
instance Free f;
@ a::connect && b::connect {
connect a[?] -> f.a[?];
connect f.b[?] -> b[?];
}
@ b::free {
connect 123.456 -> f.a[?];
connect f.b[?] -> b[?];
}
}
\ No newline at end of file
package free;
dynamic component OuterFree2{
ports
dynamic in Q a[1:7],
dynamic out Q b[1:7];
component Inner {
ports
in Q x,
out Q y;
connect x -> y;
}
instance Free f;
instance Inner g[0:5];
@ a::connect && b::connect {
connect a[?] -> g[?].x;
connect g[?].y -> f.a[?];
connect f.b[?] -> b[?];
}
@ b::free {
connect 1024 -> g[?].x;
connect 123.456 -> f.a[?];
connect f.b[?] -> b[?];
}
}
\ No newline at end of file
Supports Markdown
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