Its possible to create unique names if a circuit is included more than once

This commit is contained in:
hneemann 2016-06-10 20:36:28 +02:00
parent 43dbcc4be7
commit 50388bb8ec
4 changed files with 27 additions and 10 deletions

View File

@ -36,7 +36,6 @@ public class Circuit {
static {
ATTR_LIST.add(Keys.WIDTH);
ATTR_LIST.add(Keys.DESCRIPTION);
}
private int version = 1;

View File

@ -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

View File

@ -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<ModelEntry> {
* @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<ModelEntry> {
* @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<ModelEntry> {
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> {
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<ModelEntry> {
}
}
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);
}

View File

@ -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);