From 12141c22b7e60143bdcd2e96dadfccd78c2c8509 Mon Sep 17 00:00:00 2001 From: hneemann Date: Fri, 21 Jul 2017 16:28:30 +0200 Subject: [PATCH] simplified the origin book keeping --- .../digital/core/ExceptionWithOrigin.java | 19 ++--- .../digital/draw/elements/Circuit.java | 12 +++- .../digital/draw/elements/PinException.java | 1 + .../digital/draw/elements/VisualElement.java | 14 +--- .../digital/draw/library/CustomElement.java | 11 +-- .../digital/draw/library/ElementLibrary.java | 2 +- .../digital/draw/model/ModelCreator.java | 69 ++++++++----------- .../de/neemann/digital/draw/model/Net.java | 19 +++++ .../neemann/digital/draw/model/NetList.java | 3 + .../java/de/neemann/digital/gui/Main.java | 2 +- 10 files changed, 82 insertions(+), 70 deletions(-) diff --git a/src/main/java/de/neemann/digital/core/ExceptionWithOrigin.java b/src/main/java/de/neemann/digital/core/ExceptionWithOrigin.java index 58ac3fbec..f871e1398 100644 --- a/src/main/java/de/neemann/digital/core/ExceptionWithOrigin.java +++ b/src/main/java/de/neemann/digital/core/ExceptionWithOrigin.java @@ -9,8 +9,7 @@ import java.util.Set; * Created by hneemann on 16.06.17. */ public class ExceptionWithOrigin extends Exception { - private Set origin; - + private File origin; /** * Returns the file or the files that caused the given exception. @@ -22,9 +21,9 @@ public class ExceptionWithOrigin extends Exception { public static String getOriginOf(Throwable e) { while (e != null) { if (e instanceof ExceptionWithOrigin) { - String orig = ((ExceptionWithOrigin) e).getOriginStr(); - if (orig != null) - return orig; + String originStr = ((ExceptionWithOrigin) e).getOriginStr(); + if (originStr != null) + return originStr; } e = e.getCause(); } @@ -63,7 +62,9 @@ public class ExceptionWithOrigin extends Exception { * @return the origin of the error */ public Set getOrigin() { - return origin; + HashSet s = new HashSet<>(); + s.add(origin); + return s; } /** @@ -88,9 +89,9 @@ public class ExceptionWithOrigin extends Exception { * @param origin the file which had caused the exception */ public void setOrigin(File origin) { - if (this.origin == null && origin != null) { - this.origin = new HashSet<>(); - this.origin.add(origin); + if (origin != null) { + if (this.origin == null) + this.origin = origin; } } 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 09f5884dc..5b4e1c07e 100644 --- a/src/main/java/de/neemann/digital/draw/elements/Circuit.java +++ b/src/main/java/de/neemann/digital/draw/elements/Circuit.java @@ -57,6 +57,7 @@ public class Circuit { private transient boolean modified = false; private transient ArrayList recs; private transient ArrayList listeners; + private transient File origin; /** * Creates a proper configured XStream instance @@ -117,7 +118,7 @@ public class Circuit { e.setPos(e.getPos().mul(2)); circuit.version = 1; } - + circuit.origin = filename; return circuit; } } @@ -134,6 +135,7 @@ public class Circuit { out.write("\n"); xStream.marshal(this, new PrettyPrintWriter(out)); modified = false; + origin = filename; } } @@ -475,6 +477,7 @@ public class Circuit { /** * Sets this circuits state to not modified + * * @param modified the modified state */ public void setModified(boolean modified) { @@ -616,6 +619,13 @@ public class Circuit { recs.add(new CircRect(pos, size)); } + /** + * @return the file origin of this circuit, may be null + */ + public File getOrigin() { + return origin; + } + private static final class CircRect { private final Vector pos; private final Vector size; diff --git a/src/main/java/de/neemann/digital/draw/elements/PinException.java b/src/main/java/de/neemann/digital/draw/elements/PinException.java index e39104a1e..eb932bd80 100644 --- a/src/main/java/de/neemann/digital/draw/elements/PinException.java +++ b/src/main/java/de/neemann/digital/draw/elements/PinException.java @@ -32,6 +32,7 @@ public class PinException extends ExceptionWithOrigin { public PinException(String message, Net net) { super(message); this.net = net; + setOrigin(net.getOrigin()); } /** diff --git a/src/main/java/de/neemann/digital/draw/elements/VisualElement.java b/src/main/java/de/neemann/digital/draw/elements/VisualElement.java index 3eec0e8f2..b5e402a64 100644 --- a/src/main/java/de/neemann/digital/draw/elements/VisualElement.java +++ b/src/main/java/de/neemann/digital/draw/elements/VisualElement.java @@ -1,6 +1,5 @@ package de.neemann.digital.draw.elements; -import de.neemann.digital.core.Node; import de.neemann.digital.core.Observer; import de.neemann.digital.core.element.*; import de.neemann.digital.draw.graphics.*; @@ -385,18 +384,11 @@ public class VisualElement implements Drawable, Movable, AttributeListener { @Override public String toString() { - StringBuilder sb = new StringBuilder(elementName); String lab = elementAttributes.getCleanLabel(); if (lab != null && lab.length() > 0) - sb.append(" (").append(lab).append(")"); - - if (element != null && element instanceof Node) { - Node n = (Node) element; - if (n.getOrigin() != null) - sb.append("; ").append(n.getOrigin().getName()); - } - - return sb.toString(); + return elementName + " (" + lab + ")"; + else + return elementName; } /** 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 64f0f4815..27e3f004f 100644 --- a/src/main/java/de/neemann/digital/draw/library/CustomElement.java +++ b/src/main/java/de/neemann/digital/draw/library/CustomElement.java @@ -10,8 +10,6 @@ import de.neemann.digital.draw.model.ModelCreator; import de.neemann.digital.draw.model.NetList; import de.neemann.digital.lang.Lang; -import java.io.File; - /** * This class represents a custom, nested element. * So it is possible to use an element in the circuit witch is made from an @@ -24,7 +22,6 @@ public class CustomElement implements Element { private final Circuit circuit; private final ElementLibrary library; - private final File name; private NetList netList; /** @@ -32,12 +29,10 @@ public class CustomElement implements Element { * * @param circuit the inner circuit * @param library the library to use. - * @param name the name of the element */ - public CustomElement(Circuit circuit, ElementLibrary library, File name) { + public CustomElement(Circuit circuit, ElementLibrary library) { this.circuit = circuit; this.library = library; - this.name = name; } /** @@ -56,9 +51,9 @@ public class CustomElement implements Element { netList = new NetList(circuit); if (depth > MAX_DEPTH) - throw new NodeException(Lang.get("err_recursiveNestingAt_N0", name.getName())); + throw new NodeException(Lang.get("err_recursiveNestingAt_N0", circuit.getOrigin())); - return new ModelCreator(circuit, library, true, name, new NetList(netList), subName, depth); + return new ModelCreator(circuit, library, true, new NetList(netList), subName, depth); } @Override diff --git a/src/main/java/de/neemann/digital/draw/library/ElementLibrary.java b/src/main/java/de/neemann/digital/draw/library/ElementLibrary.java index a7b4e25e9..71f8054f8 100644 --- a/src/main/java/de/neemann/digital/draw/library/ElementLibrary.java +++ b/src/main/java/de/neemann/digital/draw/library/ElementLibrary.java @@ -416,7 +416,7 @@ public class ElementLibrary implements Iterable } ElementTypeDescriptionCustom description = new ElementTypeDescriptionCustom(file, - attributes -> new CustomElement(circuit, ElementLibrary.this, file), + attributes -> new CustomElement(circuit, ElementLibrary.this), circuit.getAttributes(), circuit.getInputNames()); description.setShortName(createShortName(file)); diff --git a/src/main/java/de/neemann/digital/draw/model/ModelCreator.java b/src/main/java/de/neemann/digital/draw/model/ModelCreator.java index d2aaaa7d0..dac62bfb2 100644 --- a/src/main/java/de/neemann/digital/draw/model/ModelCreator.java +++ b/src/main/java/de/neemann/digital/draw/model/ModelCreator.java @@ -18,7 +18,6 @@ import de.neemann.digital.draw.library.ElementNotFoundException; import de.neemann.digital.draw.shapes.Drawable; import de.neemann.digital.lang.Lang; -import java.io.File; import java.util.*; /** @@ -32,7 +31,6 @@ public class ModelCreator implements Iterable { private final NetList netList; private final ArrayList entries; private final HashMap ioMap; - private final File origin; /** * Creates the ModelDescription. @@ -61,7 +59,7 @@ public class ModelCreator implements Iterable { * @throws ElementNotFoundException ElementNotFoundException */ public ModelCreator(Circuit circuit, ElementLibrary library, boolean readAsCustom) throws PinException, NodeException, ElementNotFoundException { - this(circuit, library, readAsCustom, null, new NetList(circuit), "", 0); + this(circuit, library, readAsCustom, new NetList(circuit), "", 0); } /** @@ -70,7 +68,6 @@ public class ModelCreator implements Iterable { * @param circuit the circuit to use * @param library the library to use * @param isNestedCircuit if true the model is created for use as nested element - * @param origin 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 * @param depth recursion depth, used to detect a circuit which contains itself @@ -78,8 +75,7 @@ public class ModelCreator implements Iterable { * @throws NodeException NodeException * @throws ElementNotFoundException ElementNotFoundException */ - public ModelCreator(Circuit circuit, ElementLibrary library, boolean isNestedCircuit, File origin, NetList netList, String subName, int depth) throws PinException, NodeException, ElementNotFoundException { - this.origin = origin; + public ModelCreator(Circuit circuit, ElementLibrary library, boolean isNestedCircuit, NetList netList, String subName, int depth) throws PinException, NodeException, ElementNotFoundException { this.circuit = circuit; this.netList = netList; entries = new ArrayList<>(); @@ -103,7 +99,7 @@ public class ModelCreator implements Iterable { // sets the nodes origin to create better error messages if (element instanceof Node) - ((Node) element).setOrigin(origin); + ((Node) element).setOrigin(circuit.getOrigin()); // if handled as nested element, don't put pins in EntryList, but put the pins in a // separate map to connect it with the parent! @@ -112,11 +108,11 @@ public class ModelCreator implements Iterable { if (elementType == In.DESCRIPTION || elementType == Out.DESCRIPTION || elementType == Clock.DESCRIPTION) { String label = ve.getElementAttributes().getLabel(); if (label == null || label.length() == 0) - throw new PinException(Lang.get("err_pinWithoutName", origin)); + throw new PinException(Lang.get("err_pinWithoutName", circuit.getOrigin())); if (pins.size() != 1) - throw new PinException(Lang.get("err_N_isNotInputOrOutput", label, origin)); + throw new PinException(Lang.get("err_N_isNotInputOrOutput", label, circuit.getOrigin())); if (ioMap.containsKey(label)) - throw new PinException(Lang.get("err_duplicatePinLabel", label, origin)); + throw new PinException(Lang.get("err_duplicatePinLabel", label, circuit.getOrigin())); ioMap.put(label, pins.get(0)); isNotAIO = false; @@ -124,26 +120,26 @@ public class ModelCreator implements Iterable { } if (isNotAIO) - entries.add(new ModelEntry(element, pins, ve, elementType.getInputDescription(ve.getElementAttributes()), isNestedCircuit, origin)); + entries.add(new ModelEntry(element, pins, ve, elementType.getInputDescription(ve.getElementAttributes()), isNestedCircuit, circuit.getOrigin())); for (Pin p : pins) netList.add(p); } // connect all custom elements to the parents net - ArrayList cmdl = new ArrayList<>(); + ArrayList modelCreators = new ArrayList<>(); Iterator it = entries.iterator(); while (it.hasNext()) { ModelEntry me = it.next(); if (me.getElement() instanceof CustomElement) { // at first look for custom elements CustomElement ce = (CustomElement) me.getElement(); ModelCreator child = ce.getModelDescription(combineNames(subName, me.getVisualElement().getElementAttributes().getCleanLabel()), depth + 1); - cmdl.add(child); + modelCreators.add(child); HashMap netMatch = new HashMap<>(); for (Pin p : me.getPins()) { // connect the custom elements to the parents net - Net childNet = child.getNetOfIOandRemove(p.getName()); + Net childNet = child.getNetOfIOAndRemove(p.getName()); Net otherParentNet = netMatch.get(childNet); if (otherParentNet != null) { @@ -151,7 +147,7 @@ public class ModelCreator implements Iterable { // two nets in the parent are connected directly by the nested circuit // merge the nets in the parent! - // remove the childs inner pin which is already added to the other net + // remove the children's inner pin which is already added to the other net Pin insertedPin = child.getPinOfIO(p.getName()); otherParentNet.removePin(insertedPin); @@ -187,12 +183,12 @@ public class ModelCreator implements Iterable { it.remove(); } } - for (ModelCreator md : cmdl) { // put the elements of the custom element to the parent + for (ModelCreator md : modelCreators) { // put the elements of the custom element to the parent entries.addAll(md.entries); netList.add(md.netList); } } catch (PinException | NodeException e) { - e.setOrigin(origin); + e.setOrigin(circuit.getOrigin()); throw e; } } @@ -219,7 +215,7 @@ public class ModelCreator implements Iterable { return pin; } - private Net getNetOfIOandRemove(String name) throws PinException { + private Net getNetOfIOAndRemove(String name) throws PinException { Pin pin = getPinOfIO(name); Net netOfPin = netList.getNetOfPin(pin); if (netOfPin == null) @@ -231,7 +227,7 @@ public class ModelCreator implements Iterable { } /** - * Creates the model + * Creates the model. * * @param attachWires if true the wires are attached to the values * @return the model @@ -239,28 +235,23 @@ public class ModelCreator implements Iterable { * @throws NodeException NodeException */ public Model createModel(boolean attachWires) throws PinException, NodeException { - try { - Model m = new Model(); + Model m = new Model(); - for (Net n : netList) - n.interconnect(m, attachWires); + for (Net n : netList) + n.interconnect(m, attachWires); - for (ModelEntry e : entries) - e.applyInputs(); + for (ModelEntry e : entries) + e.applyInputs(); - for (ModelEntry e : entries) - e.getElement().registerNodes(m); + for (ModelEntry e : entries) + e.getElement().registerNodes(m); - for (ModelEntry e : entries) { - e.getElement().init(m); - e.getVisualElement().getShape().registerModel(this, m, e); - } - - return m; - } catch (PinException | NodeException e) { - e.setOrigin(origin); - throw e; + for (ModelEntry e : entries) { + e.getElement().init(m); + e.getVisualElement().getShape().registerModel(this, m, e); } + + return m; } /** @@ -306,11 +297,11 @@ public class ModelCreator implements Iterable { * @return the list */ public List getEntries(String elementName) { - List entr = new ArrayList<>(); + List entry = new ArrayList<>(); for (ModelEntry me : entries) if (me.getVisualElement().getElementName().endsWith(elementName)) - entr.add(me); - return entr; + entry.add(me); + return entry; } /** diff --git a/src/main/java/de/neemann/digital/draw/model/Net.java b/src/main/java/de/neemann/digital/draw/model/Net.java index a10e3112d..9b6044d2f 100644 --- a/src/main/java/de/neemann/digital/draw/model/Net.java +++ b/src/main/java/de/neemann/digital/draw/model/Net.java @@ -10,6 +10,7 @@ import de.neemann.digital.draw.elements.Wire; import de.neemann.digital.draw.graphics.Vector; import de.neemann.digital.lang.Lang; +import java.io.File; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; @@ -27,6 +28,7 @@ public class Net { private final ArrayList pins; private final ArrayList wires; private final HashSet labelSet; + private File origin; /** * Creates a copy of the given net @@ -38,6 +40,7 @@ public class Net { wires = null; // wires not needed pins = new ArrayList<>(toCopy.pins); // Pins are changed so create a deep copy labelSet = new HashSet<>(toCopy.labelSet); //ToDo copy necessary? + origin = toCopy.origin; } /** @@ -236,4 +239,20 @@ public class Net { else return labelSet + "/" + pins; } + + /** + * Sets the origin of this net + * + * @param origin the origin + */ + public void setOrigin(File origin) { + this.origin = origin; + } + + /** + * @return the origin of this net + */ + public File getOrigin() { + return origin; + } } diff --git a/src/main/java/de/neemann/digital/draw/model/NetList.java b/src/main/java/de/neemann/digital/draw/model/NetList.java index b5b9887ee..75cb51892 100644 --- a/src/main/java/de/neemann/digital/draw/model/NetList.java +++ b/src/main/java/de/neemann/digital/draw/model/NetList.java @@ -43,6 +43,9 @@ public class NetList implements Iterable { found.addLabel(label); } + for (Net n : netList) + n.setOrigin(circuit.getOrigin()); + mergeLabels(); } diff --git a/src/main/java/de/neemann/digital/gui/Main.java b/src/main/java/de/neemann/digital/gui/Main.java index 63071ddb6..d8c0f1568 100644 --- a/src/main/java/de/neemann/digital/gui/Main.java +++ b/src/main/java/de/neemann/digital/gui/Main.java @@ -316,7 +316,7 @@ public final class Main extends JFrame implements ClosingWindowListener.ConfirmS try { ElementLibrary.ElementTypeDescriptionCustom description = new ElementLibrary.ElementTypeDescriptionCustom(file, - attributes -> new CustomElement(circuit, library, filename), + attributes -> new CustomElement(circuit, library), circuit.getAttributes(), circuit.getInputNames()); description.setShortName(name); description.setDescription(circuit.getAttributes().get(Keys.DESCRIPTION));