From 50388bb8ec430d7ad5a82c23d03a33f5198d0c82 Mon Sep 17 00:00:00 2001 From: hneemann Date: Fri, 10 Jun 2016 20:36:28 +0200 Subject: [PATCH] Its possible to create unique names if a circuit is included more than once --- .../digital/draw/elements/Circuit.java | 1 - .../digital/draw/library/CustomElement.java | 7 ++--- .../digital/draw/model/ModelDescription.java | 27 ++++++++++++++++--- .../neemann/digital/gui/LibrarySelector.java | 2 -- 4 files changed, 27 insertions(+), 10 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 1e2691360..9f399324b 100644 --- a/src/main/java/de/neemann/digital/draw/elements/Circuit.java +++ b/src/main/java/de/neemann/digital/draw/elements/Circuit.java @@ -36,7 +36,6 @@ public class Circuit { static { ATTR_LIST.add(Keys.WIDTH); - ATTR_LIST.add(Keys.DESCRIPTION); } private int version = 1; diff --git a/src/main/java/de/neemann/digital/draw/library/CustomElement.java b/src/main/java/de/neemann/digital/draw/library/CustomElement.java index 2eb8a2881..ffed8eff2 100644 --- a/src/main/java/de/neemann/digital/draw/library/CustomElement.java +++ b/src/main/java/de/neemann/digital/draw/library/CustomElement.java @@ -38,17 +38,18 @@ public class CustomElement implements Element { /** * Gets a {@link ModelDescription} of this circuit. - * Every tim this method is called a new {@link ModelDescription} is created. + * Every time this method is called a new {@link ModelDescription} is created. * + * @param subName name of the circuit, used to name unique elements * @return the {@link ModelDescription} * @throws PinException PinException * @throws NodeException NodeException */ - public ModelDescription getModelDescription() throws PinException, NodeException { + public ModelDescription getModelDescription(String subName) throws PinException, NodeException { if (netList == null) netList = new NetList(circuit); - return new ModelDescription(circuit, library, true, name, new NetList(netList)); + return new ModelDescription(circuit, library, true, name, new NetList(netList), subName); } @Override diff --git a/src/main/java/de/neemann/digital/draw/model/ModelDescription.java b/src/main/java/de/neemann/digital/draw/model/ModelDescription.java index d1d4aca29..63130ade8 100644 --- a/src/main/java/de/neemann/digital/draw/model/ModelDescription.java +++ b/src/main/java/de/neemann/digital/draw/model/ModelDescription.java @@ -5,7 +5,9 @@ import de.neemann.digital.core.Node; import de.neemann.digital.core.NodeException; import de.neemann.digital.core.Observer; import de.neemann.digital.core.element.Element; +import de.neemann.digital.core.element.ElementAttributes; import de.neemann.digital.core.element.ElementTypeDescription; +import de.neemann.digital.core.element.Keys; import de.neemann.digital.core.io.In; import de.neemann.digital.core.io.Out; import de.neemann.digital.core.wiring.Clock; @@ -54,7 +56,7 @@ public class ModelDescription implements Iterable { * @throws NodeException NodeException */ public ModelDescription(Circuit circuit, ElementLibrary library, boolean readAsCustom) throws PinException, NodeException { - this(circuit, library, readAsCustom, "unknown", new NetList(circuit)); + this(circuit, library, readAsCustom, "unknown", new NetList(circuit), ""); } /** @@ -65,10 +67,11 @@ public class ModelDescription implements Iterable { * @param isNestedCircuit if true the model is created for use as nested element * @param fileName only used for better messages in exceptions * @param netList the NetList of the model. If known it is not necessary to create it. + * @param subName name of the circuit, used to name unique elements * @throws PinException PinException * @throws NodeException NodeException */ - public ModelDescription(Circuit circuit, ElementLibrary library, boolean isNestedCircuit, String fileName, NetList netList) throws PinException, NodeException { + public ModelDescription(Circuit circuit, ElementLibrary library, boolean isNestedCircuit, String fileName, NetList netList, String subName) throws PinException, NodeException { this.circuit = circuit; this.netList = netList; entries = new ArrayList<>(); @@ -80,7 +83,12 @@ public class ModelDescription implements Iterable { for (VisualElement ve : circuit.getElements()) { Pins pins = ve.getPins(); ElementTypeDescription elementType = library.getElementType(ve.getElementName()); - Element element = elementType.createElement(ve.getElementAttributes()); + ElementAttributes attr = ve.getElementAttributes(); + if (attr.getCleanLabel().contains("*")) { + attr = new ElementAttributes(attr); + attr.set(Keys.LABEL, attr.getCleanLabel().replace("*", subName)); + } + Element element = elementType.createElement(attr); ve.setElement(element); pins.bindOutputsToOutputPins(element.getOutputs()); @@ -117,7 +125,7 @@ public class ModelDescription implements Iterable { ModelEntry me = it.next(); if (me.getElement() instanceof CustomElement) { // at first look for custom elements CustomElement ce = (CustomElement) me.getElement(); - ModelDescription child = ce.getModelDescription(); + ModelDescription child = ce.getModelDescription(combineNames(subName, me.getVisualElement().getElementAttributes().getCleanLabel())); cmdl.add(child); for (Pin p : me.getPins()) { // connect the custom elements to the parents net @@ -141,6 +149,17 @@ public class ModelDescription implements Iterable { } } + private String combineNames(String s1, String s2) { + if (s1.length() > 0) { + if (s2.length() > 0) { + return s1 + "_" + s2; + } else + return s1; + } else { + return s2; + } + } + private void remove(Net childNet) { netList.remove(childNet); } diff --git a/src/main/java/de/neemann/digital/gui/LibrarySelector.java b/src/main/java/de/neemann/digital/gui/LibrarySelector.java index c082a8190..bb7603d23 100644 --- a/src/main/java/de/neemann/digital/gui/LibrarySelector.java +++ b/src/main/java/de/neemann/digital/gui/LibrarySelector.java @@ -264,8 +264,6 @@ public class LibrarySelector implements ElementNotFoundNotification { super(file.getPath().replace('\\', '/'), elementFactory, inputNames); this.file = file; this.attributes = attributes; - if (attributes.contains(Keys.DESCRIPTION)) - setDescription(attributes.get(Keys.DESCRIPTION)); setShortName(file.getName()); addAttribute(Keys.ROTATE); addAttribute(Keys.LABEL);