From 63951d2bcabf709b8ea4edeae79084ab5e4ab117 Mon Sep 17 00:00:00 2001 From: Nandi Date: Mon, 25 Nov 2024 20:44:07 +0100 Subject: [PATCH] Modified WireConsistencyChecker to stop merging of Wires if they are connected to a Pin --- .../digital/draw/elements/Circuit.java | 10 ++-- .../draw/elements/WireConsistencyChecker.java | 20 +++++++- .../elements/WireConsistencyCheckerTest.java | 47 +++++++++++++++++-- .../custom/CustomShapeDescriptionTest.java | 14 ++++-- 4 files changed, 76 insertions(+), 15 deletions(-) diff --git a/src/main/java/de/neemann/digital/draw/elements/Circuit.java b/src/main/java/de/neemann/digital/draw/elements/Circuit.java index 5f3cd7b93..25e0f6a95 100644 --- a/src/main/java/de/neemann/digital/draw/elements/Circuit.java +++ b/src/main/java/de/neemann/digital/draw/elements/Circuit.java @@ -356,7 +356,7 @@ public Circuit add(Wire newWire) { return null; wires.add(newWire); - WireConsistencyChecker checker = new WireConsistencyChecker(wires); + WireConsistencyChecker checker = new WireConsistencyChecker(wires, visualElements); wires = checker.check(); dotsPresent = false; @@ -371,7 +371,7 @@ public Circuit add(Wire newWire) { */ public Circuit add(ArrayList newWires) { wires.addAll(newWires); - WireConsistencyChecker checker = new WireConsistencyChecker(wires); + WireConsistencyChecker checker = new WireConsistencyChecker(wires, visualElements); wires = checker.check(); dotsPresent = false; @@ -382,7 +382,7 @@ public Circuit add(ArrayList newWires) { * Called if elements are moved */ public void elementsMoved() { - WireConsistencyChecker checker = new WireConsistencyChecker(wires); + WireConsistencyChecker checker = new WireConsistencyChecker(wires, visualElements); wires = checker.check(); dotsPresent = false; @@ -627,7 +627,7 @@ public void delete(Vector min, Vector max) { } if (wireDeleted) { - WireConsistencyChecker checker = new WireConsistencyChecker(wires); + WireConsistencyChecker checker = new WireConsistencyChecker(wires, visualElements); wires = checker.check(); } @@ -650,7 +650,7 @@ public void delete(VisualElement partToDelete) { */ public void delete(Wire wireToDelete) { if (wires.remove(wireToDelete)) { - WireConsistencyChecker checker = new WireConsistencyChecker(wires); + WireConsistencyChecker checker = new WireConsistencyChecker(wires, visualElements); wires = checker.check(); dotsPresent = false; } diff --git a/src/main/java/de/neemann/digital/draw/elements/WireConsistencyChecker.java b/src/main/java/de/neemann/digital/draw/elements/WireConsistencyChecker.java index bfd5c2d5b..27f6ad7c6 100644 --- a/src/main/java/de/neemann/digital/draw/elements/WireConsistencyChecker.java +++ b/src/main/java/de/neemann/digital/draw/elements/WireConsistencyChecker.java @@ -16,14 +16,17 @@ */ public class WireConsistencyChecker { private ArrayList wires; + private ArrayList elements; /** * Creates a new instance * - * @param wires the wires to check + * @param wires the wires to check + * @param elements the VisualElements which may have a connection to the wires */ - public WireConsistencyChecker(ArrayList wires) { + public WireConsistencyChecker(ArrayList wires, ArrayList elements) { this.wires = wires; + this.elements = elements; } /** @@ -41,6 +44,11 @@ private ArrayList merge(ArrayList wires) { HashSet horiPoints = new HashSet<>(); HashSet vertPoints = new HashSet<>(); HashSet diagPoints = new HashSet<>(); + HashSet allElemPoints = new HashSet<>(); + for (VisualElement e : elements) + for (Pin p : e.getPins()) + allElemPoints.add(p.getPos()); + HashSet elemPoints = new HashSet<>(); ArrayList newWires = new ArrayList<>(); WireMerger hori = new WireMerger(Wire.Orientation.horizontal); @@ -68,10 +76,18 @@ private ArrayList merge(ArrayList wires) { } } + elemPoints.addAll(diagPoints); + elemPoints.addAll(vertPoints); + elemPoints.addAll(horiPoints); + elemPoints.retainAll(allElemPoints); + + hori.protectPoints(diagPoints); hori.protectPoints(vertPoints); + hori.protectPoints(elemPoints); vert.protectPoints(diagPoints); vert.protectPoints(horiPoints); + vert.protectPoints(elemPoints); hori.addTo(newWires); vert.addTo(newWires); diff --git a/src/test/java/de/neemann/digital/draw/elements/WireConsistencyCheckerTest.java b/src/test/java/de/neemann/digital/draw/elements/WireConsistencyCheckerTest.java index 3f1c08ebb..76a051745 100644 --- a/src/test/java/de/neemann/digital/draw/elements/WireConsistencyCheckerTest.java +++ b/src/test/java/de/neemann/digital/draw/elements/WireConsistencyCheckerTest.java @@ -5,11 +5,18 @@ */ package de.neemann.digital.draw.elements; +import de.neemann.digital.core.element.Keys; +import de.neemann.digital.core.io.Out; import de.neemann.digital.draw.graphics.Vector; +import de.neemann.digital.draw.library.ElementLibrary; +import de.neemann.digital.draw.shapes.ShapeFactory; import junit.framework.TestCase; import java.util.ArrayList; +import static de.neemann.digital.draw.graphics.Vector.vec; +import static de.neemann.digital.draw.shapes.GenericShape.SIZE; + /** */ public class WireConsistencyCheckerTest extends TestCase { @@ -19,8 +26,9 @@ public void testCheck() throws Exception { wires.add(new Wire(new Vector(0, 0), new Vector(10, 0))); wires.add(new Wire(new Vector(10, 0), new Vector(10, 10))); wires.add(new Wire(new Vector(10, 0), new Vector(20, 0))); + ArrayList visualElements = new ArrayList<>(); - wires = new WireConsistencyChecker(wires).check(); + wires = new WireConsistencyChecker(wires, visualElements).check(); assertEquals(3, wires.size()); checkContains(wires, new Wire(new Vector(0, 0), new Vector(10, 0))); @@ -33,8 +41,9 @@ public void testCheck2() throws Exception { wires.add(new Wire(new Vector(0, 0), new Vector(10, 0))); wires.add(new Wire(new Vector(10, 0), new Vector(10, 10))); wires.add(new Wire(new Vector(0, 0), new Vector(20, 0))); + ArrayList visualElements = new ArrayList<>(); - wires = new WireConsistencyChecker(wires).check(); + wires = new WireConsistencyChecker(wires, visualElements).check(); assertEquals(3, wires.size()); checkContains(wires, new Wire(new Vector(0, 0), new Vector(10, 0))); @@ -48,8 +57,9 @@ public void testCheck3() throws Exception { wires.add(new Wire(new Vector(10, 0), new Vector(20, 0))); wires.add(new Wire(new Vector(10, 0), new Vector(10, 10))); wires.add(new Wire(new Vector(10, 0), new Vector(10, -10))); + ArrayList visualElements = new ArrayList<>(); - wires = new WireConsistencyChecker(wires).check(); + wires = new WireConsistencyChecker(wires, visualElements).check(); assertEquals(4, wires.size()); checkContains(wires, new Wire(new Vector(0, 0), new Vector(10, 0))); @@ -62,14 +72,43 @@ public void testCheck4() throws Exception { ArrayList wires = new ArrayList<>(); wires.add(new Wire(new Vector(0, 10), new Vector(20, 10))); wires.add(new Wire(new Vector(10, 0), new Vector(10, 20))); + ArrayList visualElements = new ArrayList<>(); - wires = new WireConsistencyChecker(wires).check(); + wires = new WireConsistencyChecker(wires, visualElements).check(); assertEquals(2, wires.size()); checkContains(wires, new Wire(new Vector(0, 10), new Vector(20, 10))); checkContains(wires, new Wire(new Vector(10, 0), new Vector(10, 20))); } + public void testCheck5() throws Exception { + ArrayList wires = new ArrayList<>(); + wires.add(new Wire(new Vector(0, 10), new Vector(0, 20))); + wires.add(new Wire(new Vector(0, 20), new Vector(0, 30))); + ArrayList visualElements = new ArrayList<>(); + visualElements.add(new VisualElement(new VisualElement(Out.DESCRIPTION.getName()).setPos(vec(0, 20)) + .setAttribute(Keys.LABEL, "out").setShapeFactory(new ShapeFactory(new ElementLibrary())))); + + wires = new WireConsistencyChecker(wires, visualElements).check(); + + assertEquals(2, wires.size()); + checkContains(wires, new Wire(new Vector(0, 10), new Vector(0, 20))); + checkContains(wires, new Wire(new Vector(0, 20), new Vector(0, 30))); + } + + public void testCheck6() throws Exception { + ArrayList wires = new ArrayList<>(); + wires.add(new Wire(new Vector(0, 10), new Vector(0, 30))); + ArrayList visualElements = new ArrayList<>(); + visualElements.add(new VisualElement(new VisualElement(Out.DESCRIPTION.getName()).setPos(vec(0, 20)) + .setAttribute(Keys.LABEL, "out").setShapeFactory(new ShapeFactory(new ElementLibrary())))); + + wires = new WireConsistencyChecker(wires, visualElements).check(); + + assertEquals(1, wires.size()); + checkContains(wires, new Wire(new Vector(0, 10), new Vector(0, 30))); + } + public static void checkContains(ArrayList wires, Wire wire) { for (Wire w : wires) if (wire.equalsContent(wire)) diff --git a/src/test/java/de/neemann/digital/draw/shapes/custom/CustomShapeDescriptionTest.java b/src/test/java/de/neemann/digital/draw/shapes/custom/CustomShapeDescriptionTest.java index c40e423ce..1dce115d6 100644 --- a/src/test/java/de/neemann/digital/draw/shapes/custom/CustomShapeDescriptionTest.java +++ b/src/test/java/de/neemann/digital/draw/shapes/custom/CustomShapeDescriptionTest.java @@ -13,6 +13,8 @@ import de.neemann.digital.draw.elements.PinException; import de.neemann.digital.draw.elements.VisualElement; import de.neemann.digital.draw.elements.Wire; +import de.neemann.digital.draw.library.ElementLibrary; +import de.neemann.digital.draw.shapes.ShapeFactory; import de.neemann.digital.draw.shapes.custom.svg.SvgException; import de.neemann.digital.draw.shapes.custom.svg.SvgImporter; import junit.framework.TestCase; @@ -37,8 +39,10 @@ public void setUp() throws IOException, SvgException { public void testCheckCompatibilityOk() throws PinException { Circuit circuit = new Circuit() - .add(new VisualElement(In.DESCRIPTION.getName()).setPos(vec(0, 0)).setAttribute(Keys.LABEL, "in")) - .add(new VisualElement(Out.DESCRIPTION.getName()).setPos(vec(20, 0)).setAttribute(Keys.LABEL, "out")) + .add(new VisualElement(In.DESCRIPTION.getName()).setPos(vec(0, 0)).setAttribute(Keys.LABEL, "in") + .setShapeFactory(new ShapeFactory(new ElementLibrary()))) + .add(new VisualElement(Out.DESCRIPTION.getName()).setPos(vec(20, 0)).setAttribute(Keys.LABEL, "out") + .setShapeFactory(new ShapeFactory(new ElementLibrary()))) .add(new Wire(vec(0, 0), vec(20, 0))); custom.checkCompatibility(circuit); @@ -46,8 +50,10 @@ public void testCheckCompatibilityOk() throws PinException { public void testCheckCompatibilityClock() throws PinException { Circuit circuit = new Circuit() - .add(new VisualElement(Clock.DESCRIPTION.getName()).setPos(vec(0, 0)).setAttribute(Keys.LABEL, "in")) - .add(new VisualElement(Out.DESCRIPTION.getName()).setPos(vec(20, 0)).setAttribute(Keys.LABEL, "out")) + .add(new VisualElement(Clock.DESCRIPTION.getName()).setPos(vec(0, 0)).setAttribute(Keys.LABEL, "in") + .setShapeFactory(new ShapeFactory(new ElementLibrary()))) + .add(new VisualElement(Out.DESCRIPTION.getName()).setPos(vec(20, 0)).setAttribute(Keys.LABEL, "out") + .setShapeFactory(new ShapeFactory(new ElementLibrary()))) .add(new Wire(vec(0, 0), vec(20, 0))); custom.checkCompatibility(circuit);