wiring analysis improvements

This commit is contained in:
Branislav Stojkovic
2019-02-17 09:31:42 +01:00
parent 88275e95e2
commit ec7244042d
7 changed files with 104 additions and 24 deletions
Binary file not shown.
@@ -22,9 +22,14 @@
package org.diylc.netlist;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.diylc.core.IDIYComponent;
public abstract class AbstractNetlistSummarizer {
public AbstractNetlistSummarizer() {
@@ -44,6 +49,19 @@ public abstract class AbstractNetlistSummarizer {
return res;
}
protected List<Node> find(Set<String> typeNames, String nodeName, Netlist netlist) {
List<Node> res = new ArrayList<Node>();
for (Group g : netlist.getGroups()) {
for (Node n : g.getNodes()) {
if (typeNames.contains(n.getComponent().getClass().getCanonicalName()) &&
(nodeName == null || n.getDisplayName().equalsIgnoreCase(nodeName))) {
res.add(n);
}
}
}
return res;
}
protected List<Node> find(Set<String> typeNames, String nodeName, Netlist netlist, List<Node> inGroupWith) {
List<Node> res = new ArrayList<Node>();
for (Group g : netlist.getGroups()) {
@@ -70,15 +88,58 @@ public abstract class AbstractNetlistSummarizer {
return res;
}
protected Netlist eliminate(Netlist netlist, Set<Node> nodes) {
protected List<Node> find(Set<String> typeNames, String nodeName, Netlist netlist, Collection<IDIYComponent<?>> belongTo) {
List<Node> res = new ArrayList<Node>();
for (Group g : netlist.getGroups()) {
for (Node n : g.getNodes()) {
if ((belongTo == null || belongTo.contains(n.getComponent())) && typeNames.contains(n.getComponent().getClass().getCanonicalName()) &&
(nodeName == null || n.getDisplayName().equalsIgnoreCase(nodeName))) {
res.add(n);
}
}
}
return res;
}
protected boolean allMatch(Collection<Node> nodes, String name) {
for (Node n : nodes)
if (!n.getDisplayName().equalsIgnoreCase(name))
return false;
return true;
}
protected boolean allComponentsMatch(Collection<Node> nodes, Set<IDIYComponent<?>> components) {
Set<IDIYComponent<?>> extracted = extractComponents(nodes);
return extracted.containsAll(components);
}
protected Set<IDIYComponent<?>> extractComponents(Collection<Node> nodes) {
Set<IDIYComponent<?>> components = new HashSet<IDIYComponent<?>>();
for (Node n : nodes)
components.add(n.getComponent());
return components;
}
protected List<String> extractNames(Collection<IDIYComponent<?>> components) {
List<String> names = new ArrayList<String>();
for (IDIYComponent<?> c : components)
names.add(c.getName() + " " + c.getValueForDisplay());
Collections.sort(names);
return names;
}
protected Netlist simplify(Netlist netlist, Set<Node> nodesToMerge, Collection<Node> nodesToPurge) {
Netlist res = new Netlist();
res.getSwitchSetup().addAll(netlist.getSwitchSetup());
Group merged = new Group();
// merge
for (Group g : netlist.getGroups()) {
boolean needsMerging = false;
for (Node n : nodes) {
for (Node n : nodesToMerge) {
if (g.getNodes().contains(n)) {
needsMerging = true;
break;
@@ -86,17 +147,25 @@ public abstract class AbstractNetlistSummarizer {
}
if (needsMerging) {
for (Node n : g.getNodes()) {
if (!nodes.contains(n))
if (!nodesToMerge.contains(n))
merged.getNodes().add(n);
}
} else {
res.getGroups().add(g);
res.getGroups().add(g.clone());
}
}
if (!merged.getNodes().isEmpty())
res.getGroups().add(merged);
// purge
for (Group g : res.getGroups()) {
g.getNodes().removeAll(nodesToPurge);
}
System.out.println("Before:\n" + netlist);
System.out.println("After:\n" + res);
return res;
}
@@ -35,6 +35,7 @@ import org.diylc.components.guitar.P90Pickup;
import org.diylc.components.guitar.PBassPickup;
import org.diylc.components.guitar.SingleCoilPickup;
import org.diylc.components.passive.PotentiometerPanel;
import org.diylc.core.IDIYComponent;
public class GuitarDiagramSummarizer extends AbstractNetlistSummarizer implements INetlistSummarizer {
@@ -74,32 +75,35 @@ public class GuitarDiagramSummarizer extends AbstractNetlistSummarizer implement
private Summary summarize(Netlist netlist, Node preferredOutput) {
List<String> notes = new ArrayList<String>();
List<Node> jackTipNodes = find(JACK_TYPES, "Tip", netlist, null);
List<Node> jackSleeveNodes = find(JACK_TYPES, "Sleeve", netlist, null);
List<Node> jackTipNodes = find(JACK_TYPES, "Tip", netlist);
List<Node> jackSleeveNodes = find(JACK_TYPES, "Sleeve", netlist);
netlist = findAndEliminateVolumePot(netlist, jackTipNodes, jackSleeveNodes, notes);
netlist = findAndEliminateVolumeControls(netlist, jackTipNodes, jackSleeveNodes, notes);
return new Summary(netlist, notes);
}
private Netlist findAndEliminateVolumePot(Netlist netlist, List<Node> jackTipGroups, List<Node> jackSleeveGroups, List<String> notes) {
private Netlist findAndEliminateVolumeControls(Netlist netlist, List<Node> jackTipGroups, List<Node> jackSleeveGroups, List<String> notes) {
List<Node> potTipNodes = find(POT_TYPES, null, netlist, jackTipGroups);
List<Node> potSleeveNodes = find(POT_TYPES, null, netlist, jackSleeveGroups);
if (!potTipNodes.isEmpty() && !potSleeveNodes.isEmpty()) {
if (potTipNodes.size() == 1 && potTipNodes.get(0).getDisplayName().equals("2") &&
potSleeveNodes.size() == 1 && potSleeveNodes.get(0).getDisplayName().equals("3")) {
notes.add("Volume pot detected: " + potTipNodes.get(0).getComponent().getName());
if (!potTipNodes.isEmpty() && !potSleeveNodes.isEmpty()) {
Set<IDIYComponent<?>> pots = extractComponents(potTipNodes);
List<Node> potInputNodes = find(POT_TYPES, "1", netlist, pots);
if (allMatch(potTipNodes, "2") && allMatch(potSleeveNodes, "3") && allComponentsMatch(potInputNodes, pots)) {
List<String> potNames = extractNames(pots);
notes.add("Detected volume control(s): " + potNames);
Set<Node> toEliminate = new HashSet<Node>(potTipNodes);
toEliminate.addAll(potSleeveNodes);
return eliminate(netlist, toEliminate);
} else if (potTipNodes.size() > 1 || potSleeveNodes.size() > 1) {
notes.add("Detected multiple volume pots, test inconclusive.");
Set<Node> toMerge = new HashSet<Node>(potTipNodes);
toMerge.addAll(potInputNodes);
return simplify(netlist, toMerge, potSleeveNodes);
} else {
notes.add("Detected incorrectly wired volume control(s).");
return netlist;
}
}
notes.add("No volume pot detected.");
notes.add("No volume controls detected.");
return netlist;
}
}