refactoring towards net merge fix

This commit is contained in:
rusefillc 2021-01-09 19:08:31 -05:00
parent f786d51cd9
commit aa0725cf3a
3 changed files with 51 additions and 29 deletions

View File

@ -1,14 +1,18 @@
package com.rusefi.pcb; package com.rusefi.pcb;
import com.rusefi.pcb.nodes.NetNode;
import com.rusefi.pcb.nodes.PcbNode;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import static com.rusefi.pcb.nodes.PcbNode.TOKEN_NET;
public class Networks { public class Networks {
/** /**
* Net name > Net Id * Net name > Net Id
*/ */
private final Map<String, Integer> networks = new HashMap<>(); private final Map<String, Integer> networks = new HashMap<>();
private final Map<Integer, String> nameById = new HashMap<>();
/** /**
* @param name current name * @param name current name
@ -41,17 +45,38 @@ public class Networks {
private void registerNet(String name) { private void registerNet(String name) {
networks.put(name, networks.size()); networks.put(name, networks.size());
nameById.put(networks.get(name), name);
} }
public int getId(String localName) { public int getId(String localName) {
if (localName == null)
throw new NullPointerException("localName");
Integer value = networks.get(localName); Integer value = networks.get(localName);
if (value == null) if (value == null)
throw new NullPointerException("No id for " + localName); throw new NullPointerException("No id for " + localName);
return value; return value;
} }
public String getNameById(int networkId) { public BoardState registerAdditionalBoard(PcbNode source) {
return nameById.get(networkId);
BoardState result = new BoardState();
for (NetNode net : source.<NetNode>iterate(TOKEN_NET)) {
String netId = net.getChild(0);
String netName = net.getChild(1); // todo: nicer method?
String newNameInCombinedBoard = registerNetworkIfPcbSpecific(netName);
result.netNameInLocalToNetNameInCombined.put(netName, newNameInCombinedBoard);
result.netIdMapping.put(netId, getId(newNameInCombinedBoard));
}
return result;
}
public static class BoardState {
/**
* original local net ID (as string) > new net ID
*/
public Map<String, Integer> netIdMapping = new HashMap<>();
public Map<String, String> netNameInLocalToNetNameInCombined = new HashMap<>();
} }
} }

View File

@ -3,9 +3,7 @@ package com.rusefi.pcb;
import com.rusefi.pcb.nodes.*; import com.rusefi.pcb.nodes.*;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import static com.rusefi.pcb.nodes.PcbNode.*; import static com.rusefi.pcb.nodes.PcbNode.*;
@ -85,17 +83,8 @@ public class PcbMergeTool {
} }
static void mergePcb(PcbNode destNode, PcbNode source, Networks networks) { static void mergePcb(PcbNode destNode, PcbNode source, Networks networks) {
/**
* original local net ID (as string) > new net ID
*/
Map<String, Integer> netIdMapping = new HashMap<>();
for (NetNode net : source.<NetNode>iterate(TOKEN_NET)) { Networks.BoardState state = networks.registerAdditionalBoard(source);
String netId = net.getChild(0);
String netName = net.getChild(1); // todo: nicer method?
String newName = networks.registerNetworkIfPcbSpecific(netName);
netIdMapping.put(netId, networks.getId(newName));
}
List<PcbNode> zones = source.iterate(TOKEN_ZONE); List<PcbNode> zones = source.iterate(TOKEN_ZONE);
log("Processing " + zones.size() + " zone(s)"); log("Processing " + zones.size() + " zone(s)");
@ -132,7 +121,7 @@ public class PcbMergeTool {
for (PcbNode pad : module.iterate(TOKEN_PAD)) { for (PcbNode pad : module.iterate(TOKEN_PAD)) {
if (!pad.hasChild(TOKEN_NET)) if (!pad.hasChild(TOKEN_NET))
continue; continue;
fixNetId(networks, netIdMapping, pad); fixNetId(networks, pad, state);
// PcbNode net = pad.find("net"); // PcbNode net = pad.find("net");
// String localName = netNameMapping.get(net.getChild(1)); // String localName = netNameMapping.get(net.getChild(1));
// net.setString(1, localName); // net.setString(1, localName);
@ -146,7 +135,7 @@ public class PcbMergeTool {
for (PcbNode segment : segments) { for (PcbNode segment : segments) {
// if (!segment.hasChild(TOKEN_NET)) // if (!segment.hasChild(TOKEN_NET))
// continue; // continue;
fixNetId(networks, netIdMapping, segment); fixNetId(networks, segment, state);
destNode.addChild(segment); destNode.addChild(segment);
} }
@ -154,7 +143,7 @@ public class PcbMergeTool {
List<PcbNode> pads = source.iterate(TOKEN_PAD); List<PcbNode> pads = source.iterate(TOKEN_PAD);
log("Processing " + pads.size() + " pads"); log("Processing " + pads.size() + " pads");
for (PcbNode pad : pads) { for (PcbNode pad : pads) {
fixNetId(networks, netIdMapping, pad); fixNetId(networks, pad, state);
destNode.addChild(pad); destNode.addChild(pad);
} }
@ -162,7 +151,7 @@ public class PcbMergeTool {
List<PcbNode> vias = source.iterate(TOKEN_VIA); List<PcbNode> vias = source.iterate(TOKEN_VIA);
log("Processing " + vias.size() + " vias"); log("Processing " + vias.size() + " vias");
for (PcbNode via : vias) { for (PcbNode via : vias) {
fixNetId(networks, netIdMapping, via); fixNetId(networks, via, state);
destNode.addChild(via); destNode.addChild(via);
} }
@ -191,22 +180,25 @@ public class PcbMergeTool {
return false; return false;
} }
private static void fixNetId(Networks networks, Map<String, Integer> netIdMapping, PcbNode node) { private static void fixNetId(Networks networks, PcbNode node, Networks.BoardState state) {
NetNode net = node.find(TOKEN_NET); NetNode net = node.find(TOKEN_NET);
String originalId = net.id; String globalName;
Integer currentNetId = netIdMapping.get(originalId); if (state.netNameInLocalToNetNameInCombined.containsKey(net.nodeName)) {
String globalName = networks.getNameById(currentNetId); globalName = state.netNameInLocalToNetNameInCombined.get(net.nodeName);
} else {
globalName = networks.registerNetworkIfPcbSpecific(net.nodeName);
}
if (ChangesModel.getInstance().NET_MERGE_REQUESTS.containsKey(globalName)) { if (ChangesModel.getInstance().NET_MERGE_REQUESTS.containsKey(globalName)) {
String newName = ChangesModel.getInstance().NET_MERGE_REQUESTS.get(globalName); String newName = ChangesModel.getInstance().NET_MERGE_REQUESTS.get(globalName);
log("Will merge " + globalName + " into " + newName + ". ID was " + currentNetId); log("Will merge " + globalName + " into " + newName);
currentNetId = networks.getId(newName);
log("New ID: " + currentNetId);
globalName = newName; globalName = newName;
net.setName(newName);
} }
net.setInt(0, currentNetId); net.setInt(0, networks.getId(globalName));
if (net.getName() != null) if (net.getName() != null)
net.setString(1, globalName); net.setName(globalName);
} }
public static void log(String s) { public static void log(String s) {

View File

@ -32,4 +32,9 @@ public class NetNode extends PcbNode {
"id='" + id + '\'' + "id='" + id + '\'' +
'}'; '}';
} }
public void setName(String newName) {
this.name = newName;
setString(1, newName);
}
} }