Improve terminal node naming within building blocks to ensure no duplicates by adding a name of the nested component as a prefix

This commit is contained in:
Bane Stojkovic
2026-09-14 13:04:03 +02:00
parent 69bb77d307
commit 51698ef8af
5 changed files with 314 additions and 29 deletions
@@ -24,17 +24,20 @@ package org.diylc.components.composite;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.util.Arrays;
import java.util.UUID;
import org.diylc.common.Percentage;
import org.diylc.core.IDIYComponent;
import org.diylc.netlist.Node;
import org.diylc.serialization.ProjectFileManager;
import org.diylc.testcomponents.OnePointTestComponent;
import org.diylc.testcomponents.TwoPointTestComponent;
import org.junit.Test;
@@ -55,6 +58,32 @@ public class CompositeComponentTest {
return composite;
}
private CompositeComponent customBoard(IDIYComponent<?>... children) {
CompositeComponent composite = new CompositeComponent();
composite.setName("BLK1");
composite.initBlockName("Custom Board");
composite.getChildComponents().addAll(Arrays.asList(children));
return composite;
}
private OnePointTestComponent pad(String name, String nodeName) {
return new OnePointTestComponent(name, nodeName, new Point2D.Double(0, 0));
}
// Stands in for a blank board: its corner points keep the default "1" and "2" node names but
// are not sticky.
private TwoPointTestComponent blankBoard() {
TwoPointTestComponent board = new TwoPointTestComponent("Board1", new Point2D.Double(0, 0),
new Point2D.Double(100, 100));
board.setSticky(0, false);
board.setSticky(1, false);
return board;
}
private TwoPointTestComponent twoPoint(String name) {
return new TwoPointTestComponent(name, new Point2D.Double(0, 0), new Point2D.Double(10, 0));
}
@Test
public void controlPointCountIsSumOverChildren() {
CompositeComponent composite = twoChildComposite();
@@ -103,18 +132,94 @@ public class CompositeComponentTest {
}
@Test
public void nodeNamingProducesHierarchicalNetlistLabels() {
// Design decision D3: Node.toString() should read "<composite>.<child>.<local point name>"
public void clashingPinsOfMultiPointChildrenAreQualifiedWithTheChildName() {
CompositeComponent composite = twoChildComposite();
Node r1FirstPoint = new Node(composite, 0);
Node r2FirstPoint = new Node(composite, 2);
Node r2SecondPoint = new Node(composite, 3);
assertEquals("BLK1.1", r1FirstPoint.toString());
assertEquals("BLK1.1", r2FirstPoint.toString());
assertEquals("R1.1", composite.getControlPointNodeName(0));
assertEquals("R1.2", composite.getControlPointNodeName(1));
assertEquals("BLK1.R2.1", new Node(composite, 2).toString());
// local point index must reset per child, not keep growing with the flat composite index
assertEquals("BLK1.2", r2SecondPoint.toString());
assertEquals("BLK1.R2.2", new Node(composite, 3).toString());
}
@Test
public void uniquePadNamesPassThroughWithoutThePadName() {
CompositeComponent composite =
customBoard(blankBoard(), pad("Pad3", "VCC"), pad("Pad7", "SDA"));
assertEquals("VCC", composite.getControlPointNodeName(2));
assertEquals("SDA", composite.getControlPointNodeName(3));
assertEquals("BLK1.VCC", new Node(composite, 2).toString());
}
@Test
public void nonStickyPointsDoNotForceARename() {
CompositeComponent composite = customBoard(blankBoard(), pad("Pad1", "1"));
assertEquals("1", composite.getControlPointNodeName(2));
}
@Test
public void padsSharingANameGetNumericSuffixesInSaveOrder() {
CompositeComponent composite = customBoard(blankBoard(), pad("Pad1", "GND"),
pad("Pad2", "VCC"), pad("Pad3", "GND"), pad("Pad4", "GND"));
assertEquals("GND_1", composite.getControlPointNodeName(2));
assertEquals("VCC", composite.getControlPointNodeName(3));
assertEquals("GND_2", composite.getControlPointNodeName(4));
assertEquals("GND_3", composite.getControlPointNodeName(5));
}
@Test
public void suffixesSkipNamesThatAreAlreadyTaken() {
CompositeComponent composite =
customBoard(pad("Pad1", "GND"), pad("Pad2", "GND_1"), pad("Pad3", "GND"));
assertEquals("GND_2", composite.getControlPointNodeName(0));
assertEquals("GND_1", composite.getControlPointNodeName(1));
assertEquals("GND_3", composite.getControlPointNodeName(2));
}
@Test
public void padClashingWithAPinOfAMultiPointChildKeepsItsName() {
CompositeComponent composite = customBoard(pad("Pad1", "1"), twoPoint("R1"));
assertEquals("1", composite.getControlPointNodeName(0));
assertEquals("R1.1", composite.getControlPointNodeName(1));
assertEquals("2", composite.getControlPointNodeName(2));
}
@Test
public void childrenSharingANameAreStillDistinguished() {
// renaming in the property editor does not enforce uniqueness, so a block can hold two "R1"s
CompositeComponent composite = customBoard(twoPoint("R1"), twoPoint("R1"));
assertEquals("R1.1_1", composite.getControlPointNodeName(0));
assertEquals("R1.2_1", composite.getControlPointNodeName(1));
assertEquals("R1.1_2", composite.getControlPointNodeName(2));
assertEquals("R1.2_2", composite.getControlPointNodeName(3));
}
@Test
public void unnamedPointsAreNotTerminals() {
CompositeComponent composite =
customBoard(pad("Pad1", null), pad("Pad2", null), pad("Pad3", "VCC"));
assertNull(composite.getControlPointNodeName(0));
assertNull(composite.getControlPointNodeName(1));
assertEquals("VCC", composite.getControlPointNodeName(2));
}
@Test
public void instancesOfTheSameBlockResolveIdenticalNames() throws Exception {
CompositeComponent original = customBoard(blankBoard(), pad("Pad1", "GND"),
pad("Pad2", "GND"), twoPoint("R1"), twoPoint("R2"));
CompositeComponent clone = (CompositeComponent) original.clone();
for (int i = 0; i < original.getControlPointCount(); i++) {
assertEquals(original.getControlPointNodeName(i), clone.getControlPointNodeName(i));
}
}
@Test
@@ -204,7 +309,7 @@ public class CompositeComponentTest {
assertEquals(original.getBlockName(), reloaded.getBlockName());
for (int i = 0; i < original.getControlPointCount(); i++)
assertEquals(original.getControlPoint(i), reloaded.getControlPoint(i));
assertEquals("BLK1.2", new Node(reloaded, 3).toString());
assertEquals("BLK1.R2.2", new Node(reloaded, 3).toString());
}
@Test
@@ -0,0 +1,107 @@
/*
DIY Layout Creator (DIYLC).
Copyright (c) 2009-2025 held jointly by the individual authors.
This file is part of DIYLC.
DIYLC is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DIYLC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with DIYLC. If not, see <http://www.gnu.org/licenses/>.
*/
package org.diylc.testcomponents;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import org.diylc.components.AbstractComponent;
import org.diylc.core.ComponentState;
import org.diylc.core.IDIYComponent;
import org.diylc.core.IDrawingObserver;
import org.diylc.core.Project;
import org.diylc.core.VisibilityPolicy;
import org.diylc.core.annotations.ComponentDescriptor;
/**
* Minimal single-point, sticky component with a settable node name, standing in for a solder pad
* or turret when testing how {@code CompositeComponent} resolves the terminal names of a custom
* board. A {@code null} node name marks the point as not being a netlist node, like a pad whose
* Node Name is left blank.
*/
@ComponentDescriptor(name = "One Point Test", category = "Test", author = "Test",
description = "test", instanceNamePrefix = "OP", zOrder = IDIYComponent.COMPONENT)
public class OnePointTestComponent extends AbstractComponent<Void> {
private static final long serialVersionUID = 1L;
private Point2D point;
private String nodeName;
public OnePointTestComponent() {
this("", null, new Point2D.Double(0, 0));
}
public OnePointTestComponent(String name, String nodeName, Point2D point) {
setName(name);
this.nodeName = nodeName;
this.point = point;
}
@Override
public int getControlPointCount() {
return 1;
}
@Override
public Point2D getControlPoint(int index) {
return point;
}
@Override
public void setControlPoint(Point2D point, int index) {
this.point = point;
}
@Override
public boolean isControlPointSticky(int index) {
return true;
}
@Override
public VisibilityPolicy getControlPointVisibilityPolicy(int index) {
return VisibilityPolicy.ALWAYS;
}
@Override
public String getControlPointNodeName(int index) {
return nodeName;
}
@Override
public Void getValue() {
return null;
}
@Override
public void setValue(Void value) {
}
@Override
public void draw(Graphics2D g2d, ComponentState componentState, boolean outlineMode,
Project project, IDrawingObserver drawingObserver) {
}
@Override
public void drawIcon(Graphics2D g2d, int width, int height) {
}
}