Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop merging of Wires if they are connected to a Pin #1364

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/main/java/de/neemann/digital/draw/elements/Circuit.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -371,7 +371,7 @@ public Circuit add(Wire newWire) {
*/
public Circuit add(ArrayList<Wire> newWires) {
wires.addAll(newWires);
WireConsistencyChecker checker = new WireConsistencyChecker(wires);
WireConsistencyChecker checker = new WireConsistencyChecker(wires, visualElements);
wires = checker.check();

dotsPresent = false;
Expand All @@ -382,7 +382,7 @@ public Circuit add(ArrayList<Wire> 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;
Expand Down Expand Up @@ -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();
}

Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
*/
public class WireConsistencyChecker {
private ArrayList<Wire> wires;
private ArrayList<VisualElement> 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<Wire> wires) {
public WireConsistencyChecker(ArrayList<Wire> wires, ArrayList<VisualElement> elements) {
this.wires = wires;
this.elements = elements;
}

/**
Expand All @@ -41,6 +44,11 @@ private ArrayList<Wire> merge(ArrayList<Wire> wires) {
HashSet<Vector> horiPoints = new HashSet<>();
HashSet<Vector> vertPoints = new HashSet<>();
HashSet<Vector> diagPoints = new HashSet<>();
HashSet<Vector> allElemPoints = new HashSet<>();
for (VisualElement e : elements)
for (Pin p : e.getPins())
allElemPoints.add(p.getPos());
HashSet<Vector> elemPoints = new HashSet<>();

ArrayList<Wire> newWires = new ArrayList<>();
WireMerger hori = new WireMerger(Wire.Orientation.horizontal);
Expand Down Expand Up @@ -68,10 +76,18 @@ private ArrayList<Wire> merge(ArrayList<Wire> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<VisualElement> 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)));
Expand All @@ -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<VisualElement> 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)));
Expand All @@ -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<VisualElement> 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)));
Expand All @@ -62,14 +72,43 @@ public void testCheck4() throws Exception {
ArrayList<Wire> 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<VisualElement> 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<Wire> 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<VisualElement> 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<Wire> wires = new ArrayList<>();
wires.add(new Wire(new Vector(0, 10), new Vector(0, 30)));
ArrayList<VisualElement> 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<Wire> wires, Wire wire) {
for (Wire w : wires)
if (wire.equalsContent(wire))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,17 +39,21 @@ 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);
}

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);
Expand Down