Commit 73f061c3 authored by Nils Kaminski's avatar Nils Kaminski
Browse files

Dynamic connect with initial value for incoming ports

parent 1aeca673
......@@ -50,7 +50,7 @@ public class EventDynamicConnectConverter {
List<String> newInstances = getNewInstances(event);
Map<String,List<String>> newPortsInstances = getNewPortsOfInstances(event);
Map<String, Map<String, Optional<EMADynamicConnectorInstanceSymbol>>> newPortsInstances = getNewPortsOfInstances(event);
generateConnectMethod(names, componentSymbol, bluePrint);
Method free = generateFreeMethod(names, event, bluePrint);
......@@ -110,12 +110,23 @@ public class EventDynamicConnectConverter {
Method method = new Method(name, "bool");
List<String> checks = new ArrayList<>();
List<Instruction> valueInits = new ArrayList<>();
for(String n : names ){
Variable v = new Variable();
v.setName(n+"_indexref");
v.setTypeNameTargetLanguage("int*");
method.addParameter(v);
Optional<EMAPortInstanceSymbol> port = componentSymbol.getIncomingPortInstance(n+"[1]");
if(port.isPresent()){
Variable v_initValue = new Variable();
v_initValue.setName(n+"_init");
v_initValue.setVariableType(TypeConverter.getVariableTypeForMontiCarTypeName(port.get().getTypeReference().getName(), v_initValue, port.get()).get());
method.addParameter(v_initValue);
valueInits.add(new TargetCodeInstruction(String.format("%1$s[*%1$s_indexref] = %1$s_init;\n", n)));
}
long counter = componentSymbol.getPortInstanceList().stream().filter(p->p.getNameWithoutArrayBracketPart().equals(n)).count();
String inst = String.format("*%s_indexref = dynamicconnect(%d, __%s_connected, &__%s_connect_request);\n", n, counter, n,n);
......@@ -129,6 +140,10 @@ public class EventDynamicConnectConverter {
method.addInstruction(new TargetCodeInstruction(
String.format("if(%s){return false;}\n", String.join(" || ", checks))
));
//add inits to method
valueInits.stream().forEach(vi -> method.addInstruction(vi));
method.addInstruction(new TargetCodeInstruction("return true;\n"));
bluePrint.addAdditionalIncludeString("DynamicHelper");
bluePrint.addMethod(method);
......@@ -227,29 +242,38 @@ public class EventDynamicConnectConverter {
return new ArrayList<>(insts);
}
protected static Map<String, List<String>> getNewPortsOfInstances(EMADynamicEventHandlerInstanceSymbol event){
Map<String,List<String>> newPorts = new HashMap<>();
protected static Map<String, Map<String, Optional<EMADynamicConnectorInstanceSymbol>>> getNewPortsOfInstances(EMADynamicEventHandlerInstanceSymbol event){
// Map<String,List<String>> newPorts = new HashMap<>();
Map<String, Map<String, Optional<EMADynamicConnectorInstanceSymbol>>> newPorts = new HashMap<>();
for(EMADynamicConnectorInstanceSymbol connector : event.getConnectorsDynamic()){
Optional<String> comp = connector.getSourceComponentName();
String p;
if( connector.isDynamicSourceNewPort() && comp.isPresent()){
if(!newPorts.containsKey(comp.get())){
newPorts.put(comp.get(), new ArrayList<>());
// newPorts.put(comp.get(), new ArrayList<>());
newPorts.put(comp.get(), new HashMap<>());
}
p = EMAPortSymbol.getNameWithoutArrayBracketPart(connector.getSourcePortName());
if(!newPorts.get(comp.get()).contains(p)){
newPorts.get(comp.get()).add(p);
// if(!newPorts.get(comp.get()).contains(p)){
// newPorts.get(comp.get()).add( p );
// }
if(!newPorts.get(comp.get()).containsKey(p)){
newPorts.get(comp.get()).put(p, Optional.empty());
}
}
comp = connector.getTargetComponentName();
if(connector.isDynamicTargetNewPort() && comp.isPresent()){
if(!newPorts.containsKey(comp.get())){
newPorts.put(comp.get(), new ArrayList<>());
// newPorts.put(comp.get(), new ArrayList<>());
newPorts.put(comp.get(), new HashMap<>());
}
p = EMAPortSymbol.getNameWithoutArrayBracketPart(connector.getTargetPortName());
if(!newPorts.get(comp.get()).contains(p)){
newPorts.get(comp.get()).add(p);
// if(!newPorts.get(comp.get()).contains(p)){
// newPorts.get(comp.get()).add(p);
// }
if(!newPorts.get(comp.get()).containsKey(p)){
newPorts.get(comp.get()).put(p, Optional.of(connector));
}
}
}
......@@ -292,34 +316,43 @@ public class EventDynamicConnectConverter {
}
}
protected static void generateHandleConnectRequestInInstances(Map<String, List<String>> newPorts, Method body, Method free){
for (Map.Entry<String,List<String>> entry : newPorts.entrySet()){
Collections.sort(entry.getValue());
String inst = entry.getKey()+".connect_"+String.join("_", entry.getValue())+"(";
String freeInst = entry.getKey()+".free_"+String.join("_", entry.getValue())+"(";
protected static void generateHandleConnectRequestInInstances(Map<String, Map<String, Optional<EMADynamicConnectorInstanceSymbol>>> newPorts, Method body, Method free){
for (Map.Entry<String, Map<String, Optional<EMADynamicConnectorInstanceSymbol>>> entry : newPorts.entrySet()){
List<String> portList = new ArrayList<>();
portList.addAll(entry.getValue().keySet());
Collections.sort(portList);
String inst = entry.getKey()+".connect_"+String.join("_", portList)+"(";
String freeInst = entry.getKey()+".free_"+String.join("_", portList)+"(";
String connectIdxs = "";
// for(String port : entry.getValue()){
for(int i = 0; i < entry.getValue().size(); ++i){
for(int i = 0; i < portList.size(); ++i){
body.addInstruction(new TargetCodeInstruction(String.format(
DYNPORTIDININSTANCEINIT, convertName(entry.getKey()), entry.getValue().get(i)
DYNPORTIDININSTANCEINIT, convertName(entry.getKey()), portList.get(i)
)));
inst = inst + "&"+String.format(DYNPORTIDININSTANCE, convertName(entry.getKey()), entry.getValue().get(i));
if(i < entry.getValue().size()-1){
inst = inst + "&"+String.format(DYNPORTIDININSTANCE, convertName(entry.getKey()), portList.get(i));
if(entry.getValue().get(portList.get(i)).isPresent()){
inst += ", "+generateConnectsSource(entry.getValue().get(portList.get(i)).get());
}
if(i < portList.size()-1){
inst += ", ";
}
free.addInstruction(new TargetCodeInstruction(String.format(FREE_DYNPORTIDININSTANCE,
convertName(entry.getKey()), entry.getValue().get(i), free_method_index_counter)));
convertName(entry.getKey()), portList.get(i), free_method_index_counter)));
freeInst += String.format(DYNPORTIDININSTANCE, convertName(entry.getKey()), entry.getValue().get(i));
if(i < entry.getValue().size()-1){
freeInst += String.format(DYNPORTIDININSTANCE, convertName(entry.getKey()), portList.get(i));
if(i < portList.size()-1){
freeInst += ", ";
}
connectIdxs += "_connected_idxs["+free_method_index_counter+"] = "+String.format(DYNPORTIDININSTANCE, convertName(entry.getKey()), entry.getValue().get(i))+";";
connectIdxs += "_connected_idxs["+free_method_index_counter+"] = "+String.format(DYNPORTIDININSTANCE, convertName(entry.getKey()), portList.get(i))+";";
free_method_index_counter++;
......@@ -429,7 +462,7 @@ public class EventDynamicConnectConverter {
if(!execDynConnects.isPresent()){
Method m = new Method("executeDynamicConnects", "void");
Variable p = new Variable();
p.setName("afterComponent");
p.setName("beforeComponent");
p.setTypeNameTargetLanguage("void*");
m.addParameter(p);
......@@ -446,7 +479,7 @@ public class EventDynamicConnectConverter {
bluePrint.addVariable(vdConnect);
execDynConnects.get().addInstruction(new TargetCodeInstruction(String.format(
"for (std::vector<connection<%s>>::iterator it = __dynamic_%s_connect.begin(); it < __dynamic_%s_connect.end(); ++it) {if(it->afterComponent == afterComponent){*(*it).target = *(*it).source;}}\n",
"for (std::vector<connection<%s>>::iterator it = __dynamic_%s_connect.begin(); it < __dynamic_%s_connect.end(); ++it) {if(it->beforeComponent == beforeComponent){*(*it).target = *(*it).source;}}\n",
typeName, typeName,typeName
)));
}
......@@ -474,7 +507,6 @@ public class EventDynamicConnectConverter {
}
protected static void generateConnectsForFreeEventBody(EMADynamicEventHandlerInstanceSymbol event, Method free){
for(EMADynamicConnectorInstanceSymbol connector : event.getConnectorsDynamic()){
......
......@@ -488,72 +488,6 @@ public class ImplementExecutionOrder {
Collection<EMAConnectorInstanceSymbol> result;
// if(inst instanceof EMADynamicComponentInstanceSymbol) {
// EMAPortInstanceSymbol port;
// Map<EMAPortInstanceSymbol, Collection<EMAConnectorInstanceSymbol>> map;
//
// if(instanceSourceConnectors.containsKey(inst)){
// map = instanceSourceConnectors.get(inst);
// }else{
// Map<EMAConnectorInstanceSymbol, EMAPortInstanceSymbol> sourceMap = new HashMap<>();
// map = new HashMap<>();
// for (EMAConnectorInstanceSymbol connector : ((EMADynamicComponentInstanceSymbol)inst).getConnectorInstancesAndEventConnectorInstances()) {
// if (connector instanceof EMADynamicConnectorInstanceSymbol) {
// EMADynamicConnectorInstanceSymbol d = (EMADynamicConnectorInstanceSymbol) connector;
//
// if (d.hasDynamicNew()) {
//// result.addAll(d.getAllPossibleConnectors());
// for(EMAConnectorInstanceSymbol con : d.getAllPossibleConnectors()){
////System.out.println(con.getSource());
// port = connectorSourcePort(inst, con);
// Collection<EMAConnectorInstanceSymbol> cons = map.getOrDefault(port, new ArrayList<>());
// cons.add(con);
// map.put(port, cons);
// sourceMap.put(con, port);
//
// }
// } else {
// port = connectorSourcePort(inst, connector);
// Collection<EMAConnectorInstanceSymbol> cons = map.getOrDefault(port, new ArrayList<>());
// cons.add(connector);
// map.put(port, cons);
// sourceMap.put(connector, port);
// }
// } else {
// port = connectorSourcePort(inst, connector);
// Collection<EMAConnectorInstanceSymbol> cons = map.getOrDefault(port, new ArrayList<>());
// cons.add(connector);
// map.put(port, cons);
// sourceMap.put(connector, port);
// }
// }
//
// instanceConnectors.put(inst, sourceMap);
// instanceSourceConnectors.put(inst, map);
// }
//
// result = new ArrayList<>();
//
// for(EMAPortInstanceSymbol p : sources){
// if(map.containsKey(p)){
// result.addAll(map.get(p));
// }
// }
//
// System.out.println("=========================================================================");
// System.out.println(result);
//// return result;
// }
// System.out.println("--------------------------------------------------------------------");
//
// result = getAllConnectors(inst).stream()
// .filter(c -> sources.contains(connectorSourcePort(inst, c)))
// .collect(Collectors.toList());
//
// System.out.println(result);
//
// return result;
return getAllConnectors(inst).stream()
.filter(c -> sources.contains(connectorSourcePort(inst, c)))
......
......@@ -65,6 +65,11 @@ public class DynamicPortConnectionGenerationTest extends AbstractSymtabTest {
test("portRequest.portRequest5", "./target/generated-sources-cpp/dynamics/port-connect/test05");
}
@Test
public void Test_06_PortRequest6() throws IOException {
test("portRequest.portRequest6", "./target/generated-sources-cpp/dynamics/port-connect/test06");
}
protected void test(String instName, String target){
try {
TaggingResolver symtab = createSymTabAndTaggingResolver(path());
......
......@@ -18,8 +18,13 @@ dynamic component Test1{
connect a[1] -> pt[1].in1;
connect pt[1].out1 -> b[1];
@ a::connect && b::connect {
@ a[1]::value(false) && a::connect && b::connect {
connect a[?] -> pt[?].in1;
connect pt[?].out1 -> b[?];
}
@ a[1]::value(true) && a::connect && b::connect {
connect a[?] -> pt[?].in1;
connect pt[?].out1 -> b[?];
}
}
\ No newline at end of file
......@@ -2,8 +2,8 @@ package instanceRequest;
dynamic component TestBig{
port
dynamic in B a[0:128],
dynamic out B b[0:128];
dynamic in B a[0:1024],
dynamic out B b[0:1024];
component PassThrough {
ports
......@@ -13,7 +13,7 @@ dynamic component TestBig{
connect in1 -> out1;
}
instance PassThrough pt[0:128];
instance PassThrough pt[0:1024];
@ a::connect && b::connect {
connect a[?] -> pt[?].in1;
......
package portRequest;
dynamic component PortRequest6 {
dynamic component PassThroughPortRequest {
ports
dynamic in B x [1:8],
dynamic out B y [1:8];
@ x::connect && y::connect {
connect x[?] -> y[?];
}
}
ports
dynamic in B a[1:8],
dynamic out B b[1:8];
instance PassThroughPortRequest ptpp;
@ b::connect && a::connect {
connect ptpp.y[?] -> b[?];
connect a[?] -> ptpp.x[?];
}
}
\ No newline at end of file
......@@ -6,10 +6,13 @@ component Not {
out B b,
out B c;
connect false -> c;
@ a::value(false){
connect true -> b;
@ p.b::value([false, true, false, true]){
instance Bla bla;
connect bla.b -> b;
}
@ a::value(true){
......
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