Improved comparison logic of the 'Compare With..' tool

This commit is contained in:
Bane Stojkovic
2026-07-17 16:38:41 +02:00
parent 896b6e5f69
commit d8a2538b35
3 changed files with 195 additions and 336 deletions
@@ -22,8 +22,10 @@
package org.diylc.plugins.compare.service;
import org.diylc.appframework.miscutils.InMemoryConfigurationManager;
import org.diylc.common.DrawOption;
import org.diylc.common.DummyView;
import org.diylc.common.IPlugInPort;
import org.diylc.core.IDIYComponent;
import org.diylc.netlist.Group;
import org.diylc.netlist.Netlist;
import org.diylc.netlist.Node;
@@ -33,12 +35,14 @@ import org.diylc.plugins.compare.model.ComponentDiff;
import org.diylc.plugins.compare.model.CompareResults;
import org.diylc.presenter.Presenter;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
@@ -53,6 +57,11 @@ public class CompareService {
public CompareResults compareWith(File file) {
Presenter presenter = new Presenter(new DummyView(), InMemoryConfigurationManager.getInstance());
presenter.loadProjectFromFile(file.getAbsolutePath());
// Render in-memory to populate componentAreaMap,
// which is required for getContinuityAreas() used by extractNetlists()
renderInMemory(presenter);
try {
List<Netlist> otherNetlists = presenter.extractNetlists(false);
List<Netlist> thisNetlists = plugInPort.extractNetlists(false);
@@ -65,284 +74,92 @@ public class CompareService {
}
}
public CompareResults compare(Netlist netlist1, Netlist netlist2) {
// Get all groups from both netlists
Set<Group> groups1 = netlist1.getGroups();
Set<Group> groups2 = netlist2.getGroups();
// Build lists of differences
List<ConnectionDiff> connectionDiffs = new ArrayList<>();
Set<ComponentDiff> componentDiffs = new HashSet<>();
Set<String> addedComponentNames = new HashSet<>();
// Helper to get a unique string for a node
java.util.function.Function<Node, String> nodeKey = node ->
node.getComponent().getName() + ":" + node.getComponent().getControlPointNodeName(node.getPointIndex());
// Get all component names from both netlists for existence check
Set<String> netlist1Components = netlist1.getComponents().stream()
.map(comp -> comp.getName())
.collect(Collectors.toSet());
Set<String> netlist2Components = netlist2.getComponents().stream()
.map(comp -> comp.getName())
.collect(Collectors.toSet());
// For each group in netlist1, check if it exists in netlist2
for (Group group1 : groups1) {
boolean groupFound = false;
for (Group group2 : groups2) {
if (areGroupsEqual(group1, group2)) {
groupFound = true;
break;
}
}
if (!groupFound) {
// If group not found, check each component
List<Node> nodes = new ArrayList<>(group1.getNodes());
for (int i = 0; i < nodes.size(); i++) {
Node node = nodes.get(i);
String componentName = node.getComponent().getName();
// Check if this component exists in any group in netlist2
boolean componentExistsInNetlist2 = false;
for (Group group2 : groups2) {
for (Node node2 : group2.getNodes()) {
if (node2.getComponent().getName().equals(componentName)) {
componentExistsInNetlist2 = true;
break;
}
}
if (componentExistsInNetlist2) break;
}
// Add component difference if it doesn't exist in any group in netlist2 and hasn't been added yet
if (!componentExistsInNetlist2 && addedComponentNames.add(componentName)) {
componentDiffs.add(new ComponentDiff(componentName, true));
}
// Add connection differences for each pair of nodes
for (int j = i + 1; j < nodes.size(); j++) {
Node otherNode = nodes.get(j);
connectionDiffs.add(new ConnectionDiff(
node.getComponent().getName(),
node.getComponent().getControlPointNodeName(node.getPointIndex()),
otherNode.getComponent().getName(),
otherNode.getComponent().getControlPointNodeName(otherNode.getPointIndex()),
true));
}
}
}
}
// For each group in netlist2, check if it exists in netlist1
for (Group group2 : groups2) {
boolean groupFound = false;
for (Group group1 : groups1) {
if (areGroupsEqual(group1, group2)) {
groupFound = true;
break;
}
}
if (!groupFound) {
// If group not found, check each component
List<Node> nodes = new ArrayList<>(group2.getNodes());
for (int i = 0; i < nodes.size(); i++) {
Node node = nodes.get(i);
String componentName = node.getComponent().getName();
// Check if this component exists in any group in netlist1
boolean componentExistsInNetlist1 = false;
for (Group group1 : groups1) {
for (Node node1 : group1.getNodes()) {
if (node1.getComponent().getName().equals(componentName)) {
componentExistsInNetlist1 = true;
break;
}
}
if (componentExistsInNetlist1) break;
}
// Add component difference if it doesn't exist in any group in netlist1 and hasn't been added yet
if (!componentExistsInNetlist1 && addedComponentNames.add(componentName)) {
componentDiffs.add(new ComponentDiff(componentName, false));
}
// Add connection differences for each pair of nodes
for (int j = i + 1; j < nodes.size(); j++) {
Node otherNode = nodes.get(j);
connectionDiffs.add(new ConnectionDiff(
node.getComponent().getName(),
node.getComponent().getControlPointNodeName(node.getPointIndex()),
otherNode.getComponent().getName(),
otherNode.getComponent().getControlPointNodeName(otherNode.getPointIndex()),
false));
}
}
}
}
// Try to resolve differences by flipping non-polarized components
if (!connectionDiffs.isEmpty()) {
// Find all non-polarized components that have nodes in different groups
Map<String, List<Group>> componentGroups = new HashMap<>();
for (Group group : groups1) {
for (Node node : group.getNodes()) {
if (!node.getComponent().isPolarized()) {
componentGroups.computeIfAbsent(node.getComponent().getName(), k -> new ArrayList<>())
.add(group);
}
}
}
// Try swapping nodes for each non-polarized component that appears in multiple groups
for (Map.Entry<String, List<Group>> entry : componentGroups.entrySet()) {
if (entry.getValue().size() >= 2) {
String componentName = entry.getKey();
List<Group> groupsWithComponent = entry.getValue();
// Get the nodes from the first two groups
Group group1 = groupsWithComponent.get(0);
Group group2 = groupsWithComponent.get(1);
Node node1 = group1.getNodes().stream()
.filter(n -> n.getComponent().getName().equals(componentName))
.findFirst()
.get();
Node node2 = group2.getNodes().stream()
.filter(n -> n.getComponent().getName().equals(componentName))
.findFirst()
.get();
// Create new groups with swapped nodes
Group newGroup1 = new Group();
Group newGroup2 = new Group();
// Copy all nodes except the ones we're swapping
for (Node n : group1.getNodes()) {
if (!n.getComponent().getName().equals(componentName)) {
newGroup1.getNodes().add(n);
}
}
for (Node n : group2.getNodes()) {
if (!n.getComponent().getName().equals(componentName)) {
newGroup2.getNodes().add(n);
}
}
// Add the swapped nodes
newGroup1.getNodes().add(node2);
newGroup2.getNodes().add(node1);
// Create a new set of groups with the swapped nodes
Set<Group> newGroups1 = new HashSet<>(groups1);
newGroups1.remove(group1);
newGroups1.remove(group2);
newGroups1.add(newGroup1);
newGroups1.add(newGroup2);
// Check if this resolves ALL differences
List<ConnectionDiff> newDiffs = new ArrayList<>();
Set<ComponentDiff> newComponentDiffs = new HashSet<>();
// Check groups from netlist1 against netlist2
for (Group g1 : newGroups1) {
boolean groupFound = false;
for (Group g2 : groups2) {
if (areGroupsEqual(g1, g2)) {
groupFound = true;
break;
}
}
if (!groupFound) {
// Add connection differences for this group
List<Node> nodes = new ArrayList<>(g1.getNodes());
for (int i = 0; i < nodes.size(); i++) {
Node node = nodes.get(i);
for (int j = i + 1; j < nodes.size(); j++) {
Node otherNode = nodes.get(j);
newDiffs.add(new ConnectionDiff(
node.getComponent().getName(),
node.getComponent().getControlPointNodeName(node.getPointIndex()),
otherNode.getComponent().getName(),
otherNode.getComponent().getControlPointNodeName(otherNode.getPointIndex()),
true));
}
}
}
}
// Check groups from netlist2 against netlist1
for (Group g2 : groups2) {
boolean groupFound = false;
for (Group g1 : newGroups1) {
if (areGroupsEqual(g1, g2)) {
groupFound = true;
break;
}
}
if (!groupFound) {
// Add connection differences for this group
List<Node> nodes = new ArrayList<>(g2.getNodes());
for (int i = 0; i < nodes.size(); i++) {
Node node = nodes.get(i);
for (int j = i + 1; j < nodes.size(); j++) {
Node otherNode = nodes.get(j);
newDiffs.add(new ConnectionDiff(
node.getComponent().getName(),
node.getComponent().getControlPointNodeName(node.getPointIndex()),
otherNode.getComponent().getName(),
otherNode.getComponent().getControlPointNodeName(otherNode.getPointIndex()),
false));
}
}
}
}
// If this swap resolved all differences, use the new groups
if (newDiffs.isEmpty() && newComponentDiffs.isEmpty()) {
groups1 = newGroups1;
connectionDiffs = new ArrayList<>();
componentDiffs = new HashSet<>();
break;
}
}
}
}
if (connectionDiffs.isEmpty() && componentDiffs.isEmpty()) {
return new CompareResults(true, List.of(), List.of());
}
return new CompareResults(false, connectionDiffs, new ArrayList<>(componentDiffs));
private void renderInMemory(Presenter presenter) {
Dimension d = presenter.getCanvasDimensions(false, false);
BufferedImage img = new BufferedImage(
Math.max(1, d.width), Math.max(1, d.height), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
presenter.draw(g2d, EnumSet.noneOf(DrawOption.class), null, null, null, null);
g2d.dispose();
}
private boolean areGroupsEqual(Group group1, Group group2) {
if (group1.getNodes().size() != group2.getNodes().size()) {
return false;
public CompareResults compare(Netlist netlist1, Netlist netlist2) {
// Step 1: Compare components
Set<String> currentComponents = netlist1.getComponents().stream()
.map(IDIYComponent::getName)
.collect(Collectors.toSet());
Set<String> targetComponents = netlist2.getComponents().stream()
.map(IDIYComponent::getName)
.collect(Collectors.toSet());
List<ComponentDiff> componentDiffs = new ArrayList<>();
for (String name : currentComponents) {
if (!targetComponents.contains(name)) {
componentDiffs.add(new ComponentDiff(name, true));
}
}
for (String name : targetComponents) {
if (!currentComponents.contains(name)) {
componentDiffs.add(new ComponentDiff(name, false));
}
}
// Helper to get a unique string for a node
java.util.function.Function<Node, String> nodeKey = node ->
node.getComponent().getName() + ":" + node.getComponent().getControlPointNodeName(node.getPointIndex());
// Step 2: Compare connections
// A connection is a pair of nodes within the same group.
// We represent each connection as a normalized string so order doesn't matter.
Set<String> currentConnections = extractConnections(netlist1);
Set<String> targetConnections = extractConnections(netlist2);
// For polarized components, we need to check exact node order
List<String> group1Keys = group1.getNodes().stream()
.map(nodeKey)
.collect(Collectors.toList());
List<String> group2Keys = group2.getNodes().stream()
.map(nodeKey)
.collect(Collectors.toList());
// If any component in the group is polarized, we need exact order match
boolean hasPolarizedComponent = group1.getNodes().stream()
.anyMatch(node -> node.getComponent().isPolarized()) ||
group2.getNodes().stream()
.anyMatch(node -> node.getComponent().isPolarized());
if (hasPolarizedComponent) {
return group1Keys.equals(group2Keys);
List<ConnectionDiff> connectionDiffs = new ArrayList<>();
for (String conn : currentConnections) {
if (!targetConnections.contains(conn)) {
String[] parts = conn.split("\\|");
if (targetComponents.contains(parts[0]) && targetComponents.contains(parts[2])) {
connectionDiffs.add(new ConnectionDiff(parts[0], parts[1], parts[2], parts[3], true));
}
}
}
for (String conn : targetConnections) {
if (!currentConnections.contains(conn)) {
String[] parts = conn.split("\\|");
if (currentComponents.contains(parts[0]) && currentComponents.contains(parts[2])) {
connectionDiffs.add(new ConnectionDiff(parts[0], parts[1], parts[2], parts[3], false));
}
}
}
// For non-polarized components, we can use unordered comparison
return new HashSet<>(group1Keys).equals(new HashSet<>(group2Keys));
boolean matches = componentDiffs.isEmpty() && connectionDiffs.isEmpty();
return new CompareResults(matches, connectionDiffs, componentDiffs);
}
/**
* Extracts all connections from a netlist. Each connection is a pair of nodes
* that belong to the same group, represented as a normalized string key.
*/
private Set<String> extractConnections(Netlist netlist) {
Set<String> connections = new HashSet<>();
for (Group group : netlist.getGroups()) {
List<Node> nodes = new ArrayList<>(group.getNodes());
for (int i = 0; i < nodes.size(); i++) {
for (int j = i + 1; j < nodes.size(); j++) {
connections.add(connectionKey(nodes.get(i), nodes.get(j)));
}
}
}
return connections;
}
/**
* Creates a normalized string key for a connection between two nodes.
* The key is ordered alphabetically so that the same connection always
* produces the same key regardless of the order of the two nodes.
*/
private String connectionKey(Node a, Node b) {
String keyA = a.getComponent().getName() + "|" + a.getComponent().getControlPointNodeName(a.getPointIndex());
String keyB = b.getComponent().getName() + "|" + b.getComponent().getControlPointNodeName(b.getPointIndex());
if (keyA.compareTo(keyB) <= 0) {
return keyA + "|" + keyB;
}
return keyB + "|" + keyA;
}
}
@@ -360,5 +360,21 @@
</changes>
<url>https://github.com/bancika/diy-layout-creator/releases</url>
</org.diylc.appframework.update.Version>
<org.diylc.appframework.update.Version>
<versionNumber>
<major>6</major>
<minor>2</minor>
<build>0</build>
</versionNumber>
<releaseDate>2026-07-30 00:00:00.000 CET</releaseDate>
<name></name>
<changes>
<org.diylc.appframework.update.Change>
<changeType>IMPROVEMENT</changeType>
<description>Improved comparison logic of the 'Compare With..' tool</description>
</org.diylc.appframework.update.Change>
</changes>
<url>https://github.com/bancika/diy-layout-creator/releases</url>
</org.diylc.appframework.update.Version>
</a>
</java.util.Arrays_-ArrayList>
@@ -48,33 +48,34 @@ public class CompareServiceTest {
@Test
public void testCompare_MissingConnectionsInSecondNetlist() {
Map<String, IDIYComponent<?>> componentMap = new HashMap<>();
// Group will have 3 nodes
Netlist netlist1 = createNetlist(componentMap, "Component1", "Node1", "Component2", "Node2", "Component3", "Node3");
Netlist netlist2 = createNetlist(componentMap, "Component1", "Node1"); // Missing group with 2 nodes
Map<String, IDIYComponent<?>> componentMap1 = new HashMap<>();
Netlist netlist1 = createNetlist(componentMap1, "Component1", "Node1", "Component2", "Node2", "Component3", "Node3");
Map<String, IDIYComponent<?>> componentMap2 = new HashMap<>();
Netlist netlist2 = createNetlist(componentMap2, "Component1", "Node1"); // Missing components 2 & 3
CompareResults results = compareService.compare(netlist1, netlist2);
assertTrue(!results.matches());
// 2 components missing = 2 component diffs
assertEquals(2, results.componentDiffs().size());
// 3 nodes = 3 connections (1-2, 1-3, 2-3)
assertEquals(3, results.connectionDiffs().size());
// Connections involving missing components are filtered out, so 0 connection diffs
assertEquals(0, results.connectionDiffs().size());
}
@Test
public void testCompare_MissingConnectionsInFirstNetlist() {
Map<String, IDIYComponent<?>> componentMap = new HashMap<>();
// Group will have 3 nodes
Netlist netlist1 = createNetlist(componentMap, "Component1", "Node1");
Netlist netlist2 = createNetlist(componentMap, "Component1", "Node1", "Component2", "Node2", "Component3", "Node3");
Map<String, IDIYComponent<?>> componentMap1 = new HashMap<>();
Netlist netlist1 = createNetlist(componentMap1, "Component1", "Node1");
Map<String, IDIYComponent<?>> componentMap2 = new HashMap<>();
Netlist netlist2 = createNetlist(componentMap2, "Component1", "Node1", "Component2", "Node2", "Component3", "Node3");
CompareResults results = compareService.compare(netlist1, netlist2);
assertTrue(!results.matches());
// 2 components missing = 2 component diffs
assertEquals(2, results.componentDiffs().size());
assertEquals(3, results.connectionDiffs().size());
// Connections involving missing components are filtered out, so 0 connection diffs
assertEquals(0, results.connectionDiffs().size());
}
@Test
@@ -89,8 +90,8 @@ public class CompareServiceTest {
assertTrue(!results.matches());
// 4 components missing (2 in each netlist) = 4 component diffs
assertEquals(4, results.componentDiffs().size());
// 2 connections in each netlist = 2 connection diffs
assertEquals(2, results.connectionDiffs().size());
// Connections involving missing components are filtered out, so 0 connection diffs
assertEquals(0, results.connectionDiffs().size());
// Verify differences from first netlist
List<ComponentDiff> firstNetlistComponentDiffs = results.componentDiffs().stream()
@@ -109,13 +110,15 @@ public class CompareServiceTest {
public void testCompare_MultipleGroups_Identical() {
Map<String, IDIYComponent<?>> componentMap = new HashMap<>();
// Create first netlist with two groups
Netlist netlist1 = new Netlist(new ArrayList<>());
netlist1.add(createGroup(componentMap, "Component1", "Node1", "Component2", "Node2"));
netlist1.add(createGroup(componentMap, "Component3", "Node3", "Component4", "Node4"));
// Create groups first so componentMap is populated
Group g1a = createGroup(componentMap, "Component1", "Node1", "Component2", "Node2");
Group g1b = createGroup(componentMap, "Component3", "Node3", "Component4", "Node4");
Netlist netlist1 = new Netlist(new ArrayList<>(componentMap.values()));
netlist1.add(g1a);
netlist1.add(g1b);
// Create second netlist with same groups
Netlist netlist2 = new Netlist(new ArrayList<>());
Netlist netlist2 = new Netlist(new ArrayList<>(componentMap.values()));
netlist2.add(createGroup(componentMap, "Component1", "Node1", "Component2", "Node2"));
netlist2.add(createGroup(componentMap, "Component3", "Node3", "Component4", "Node4"));
@@ -130,13 +133,16 @@ public class CompareServiceTest {
public void testCompare_MultipleGroups_DifferentOrder() {
Map<String, IDIYComponent<?>> componentMap = new HashMap<>();
// Create first netlist with two groups
Netlist netlist1 = new Netlist(new ArrayList<>());
netlist1.add(createGroup(componentMap, "Component1", "Node1", "Component2", "Node2"));
netlist1.add(createGroup(componentMap, "Component3", "Node3", "Component4", "Node4"));
// Create groups first so componentMap is populated
Group g1a = createGroup(componentMap, "Component1", "Node1", "Component2", "Node2");
Group g1b = createGroup(componentMap, "Component3", "Node3", "Component4", "Node4");
Netlist netlist1 = new Netlist(new ArrayList<>(componentMap.values()));
netlist1.add(g1a);
netlist1.add(g1b);
// Create second netlist with same groups but in different order
Netlist netlist2 = new Netlist(new ArrayList<>());
// Same groups but in different order
Netlist netlist2 = new Netlist(new ArrayList<>(componentMap.values()));
netlist2.add(createGroup(componentMap, "Component3", "Node3", "Component4", "Node4"));
netlist2.add(createGroup(componentMap, "Component1", "Node1", "Component2", "Node2"));
@@ -149,33 +155,45 @@ public class CompareServiceTest {
@Test
public void testCompare_MultipleGroups_ExtraGroup() {
Map<String, IDIYComponent<?>> componentMap = new HashMap<>();
// Each group has 2 nodes
Netlist netlist1 = new Netlist(new ArrayList<>());
netlist1.add(createGroup(componentMap, "Component1", "Node1", "Component2", "Node2"));
netlist1.add(createGroup(componentMap, "Component3", "Node3", "Component4", "Node4"));
Netlist netlist2 = new Netlist(new ArrayList<>());
netlist2.add(createGroup(componentMap, "Component1", "Node1", "Component2", "Node2"));
netlist2.add(createGroup(componentMap, "Component3", "Node3", "Component4", "Node4"));
netlist2.add(createGroup(componentMap, "Component5", "Node5", "Component6", "Node6"));
Map<String, IDIYComponent<?>> componentMap1 = new HashMap<>();
// Create shared groups first
Group shared1 = createGroup(componentMap1, "Component1", "Node1", "Component2", "Node2");
Group shared2 = createGroup(componentMap1, "Component3", "Node3", "Component4", "Node4");
Netlist netlist1 = new Netlist(new ArrayList<>(componentMap1.values()));
netlist1.add(shared1);
netlist1.add(shared2);
Map<String, IDIYComponent<?>> componentMap2 = new HashMap<>();
Group shared2_1 = createGroup(componentMap2, "Component1", "Node1", "Component2", "Node2");
Group shared2_2 = createGroup(componentMap2, "Component3", "Node3", "Component4", "Node4");
Group extra = createGroup(componentMap2, "Component5", "Node5", "Component6", "Node6");
Netlist netlist2 = new Netlist(new ArrayList<>(componentMap2.values()));
netlist2.add(shared2_1);
netlist2.add(shared2_2);
netlist2.add(extra);
CompareResults results = compareService.compare(netlist1, netlist2);
assertTrue(!results.matches());
// 2 components in extra group = 2 component diffs
assertEquals(2, results.componentDiffs().size());
// 1 connection in extra group = 1 connection diff
assertEquals(1, results.connectionDiffs().size());
// Connections to/from missing Component5/6 are filtered out, so 0 connection diffs
assertEquals(0, results.connectionDiffs().size());
}
@Test
public void testCompare_MultipleGroups_DifferentConnections() {
Map<String, IDIYComponent<?>> componentMap = new HashMap<>();
// Each group has 2 nodes
Netlist netlist1 = new Netlist(new ArrayList<>());
netlist1.add(createGroup(componentMap, "Component1", "Node1", "Component2", "Node2"));
netlist1.add(createGroup(componentMap, "Component3", "Node3", "Component4", "Node4"));
Netlist netlist2 = new Netlist(new ArrayList<>());
// Create groups first so componentMap is populated
Group g1a = createGroup(componentMap, "Component1", "Node1", "Component2", "Node2");
Group g1b = createGroup(componentMap, "Component3", "Node3", "Component4", "Node4");
Netlist netlist1 = new Netlist(new ArrayList<>(componentMap.values()));
netlist1.add(g1a);
netlist1.add(g1b);
Netlist netlist2 = new Netlist(new ArrayList<>(componentMap.values()));
netlist2.add(createGroup(componentMap, "Component1", "Node1", "Component3", "Node3"));
netlist2.add(createGroup(componentMap, "Component2", "Node2", "Component4", "Node4"));
@@ -191,13 +209,16 @@ public class CompareServiceTest {
public void testCompare_NonPolarizedComponent_SwappedNodes() {
Map<String, IDIYComponent<?>> componentMap = new HashMap<>();
// Create first netlist with non-polarized component in two groups
Netlist netlist1 = new Netlist(new ArrayList<>());
netlist1.add(createGroup(componentMap, "Resistor1", "Node1", "Component2", "Node2"));
netlist1.add(createGroup(componentMap, "Resistor1", "Node2", "Component3", "Node3"));
// Create groups first so componentMap is populated
Group g1a = createGroup(componentMap, "Resistor1", "Node1", "Component2", "Node2");
Group g1b = createGroup(componentMap, "Resistor1", "Node2", "Component3", "Node3");
Netlist netlist1 = new Netlist(new ArrayList<>(componentMap.values()));
netlist1.add(g1a);
netlist1.add(g1b);
// Create second netlist with same connections but different node order
Netlist netlist2 = new Netlist(new ArrayList<>());
// Same connections but different node order
Netlist netlist2 = new Netlist(new ArrayList<>(componentMap.values()));
netlist2.add(createGroup(componentMap, "Resistor1", "Node2", "Component2", "Node2"));
netlist2.add(createGroup(componentMap, "Resistor1", "Node1", "Component3", "Node3"));
@@ -214,9 +235,6 @@ public class CompareServiceTest {
@Test
public void testCompare_PolarizedComponent_SwappedNodes() {
// Create first netlist with polarized component in two groups
Netlist netlist1 = new Netlist(new ArrayList<>());
// Create diode for netlist1
IDIYComponent<?> diode1 = mock(IDIYComponent.class);
when(diode1.getName()).thenReturn("Diode1");
@@ -250,6 +268,9 @@ public class CompareServiceTest {
when(component3_1.toString()).thenReturn("Component3");
when(component3_1.isPolarized()).thenReturn(false);
// Create first netlist with polarized component in two groups
Netlist netlist1 = new Netlist(List.of(diode1, component2_1, component3_1));
// Create groups for netlist1
Group group1 = new Group();
Node node1 = new Node(diode1, 0); // Anode
@@ -265,9 +286,6 @@ public class CompareServiceTest {
group2.getNodes().add(node4);
netlist1.add(group2);
// Create second netlist with same connections but different node order
Netlist netlist2 = new Netlist(new ArrayList<>());
// Create diode for netlist2
IDIYComponent<?> diode2 = mock(IDIYComponent.class);
when(diode2.getName()).thenReturn("Diode1");
@@ -301,6 +319,9 @@ public class CompareServiceTest {
when(component3_2.toString()).thenReturn("Component3");
when(component3_2.isPolarized()).thenReturn(false);
// Create second netlist with same connections but different node order
Netlist netlist2 = new Netlist(List.of(diode2, component2_2, component3_2));
// Create groups for netlist2 with swapped nodes
Group group3 = new Group();
Node node5 = new Node(diode2, 1); // Cathode
@@ -447,14 +468,18 @@ public class CompareServiceTest {
public void testCompare_MixedPolarizedAndNonPolarized() {
Map<String, IDIYComponent<?>> componentMap = new HashMap<>();
// Create first netlist with both polarized and non-polarized components
Netlist netlist1 = new Netlist(new ArrayList<>());
netlist1.add(createGroup(componentMap, "Resistor1", "Node1", "Diode1", "Anode"));
netlist1.add(createGroup(componentMap, "Resistor1", "Node2", "Component3", "Node3"));
netlist1.add(createGroup(componentMap, "Diode1", "Cathode", "Component4", "Node4"));
// Create groups first so componentMap is populated
Group g1a = createGroup(componentMap, "Resistor1", "Node1", "Diode1", "Anode");
Group g1b = createGroup(componentMap, "Resistor1", "Node2", "Component3", "Node3");
Group g1c = createGroup(componentMap, "Diode1", "Cathode", "Component4", "Node4");
Netlist netlist1 = new Netlist(new ArrayList<>(componentMap.values()));
netlist1.add(g1a);
netlist1.add(g1b);
netlist1.add(g1c);
// Create second netlist with same connections but different node order for non-polarized
Netlist netlist2 = new Netlist(new ArrayList<>());
// Same connections but different node order for non-polarized
Netlist netlist2 = new Netlist(new ArrayList<>(componentMap.values()));
netlist2.add(createGroup(componentMap, "Resistor1", "Node2", "Diode1", "Anode"));
netlist2.add(createGroup(componentMap, "Resistor1", "Node1", "Component3", "Node3"));
netlist2.add(createGroup(componentMap, "Diode1", "Cathode", "Component4", "Node4"));
@@ -473,8 +498,9 @@ public class CompareServiceTest {
}
private Netlist createNetlist(Map<String, IDIYComponent<?>> componentMap, String... componentNodePairs) {
Netlist netlist = new Netlist(new ArrayList<>());
netlist.add(createGroup(componentMap, componentNodePairs));
Group group = createGroup(componentMap, componentNodePairs);
Netlist netlist = new Netlist(new ArrayList<>(componentMap.values()));
netlist.add(group);
return netlist;
}