Skip to content

Commit

Permalink
Merge pull request #616 from bancika/next
Browse files Browse the repository at this point in the history
Next
  • Loading branch information
bancika authored Jan 3, 2021
2 parents 911d85c + 889d76c commit cccca4a
Show file tree
Hide file tree
Showing 38 changed files with 2,448 additions and 282 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public class BuildingBlockPackage {

private Map<String, List<IDIYComponent<?>>> block;
private String owner;

public BuildingBlockPackage() {
}

public BuildingBlockPackage(Map<String, List<IDIYComponent<?>>> blocks, String owner) {
super();
Expand Down
7 changes: 7 additions & 0 deletions diylc/diylc-core/src/org/diylc/common/INetlistProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ DIY Layout Creator (DIYLC).

import java.util.List;

import org.diylc.netlist.INetlistParser;
import org.diylc.netlist.Netlist;

/**
Expand All @@ -47,5 +48,11 @@ public interface INetlistProcessor {
*/
List<INetlistAnalyzer> getNetlistAnalyzers();

/**
* Finds all available {@link INetlistParser} implementations.
*
* @return
*/
List<INetlistParser> getNetlistParserDefinitions();

}
3 changes: 3 additions & 0 deletions diylc/diylc-core/src/org/diylc/common/VariantPackage.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public class VariantPackage {

private Map<String, List<Template>> variants;
private String owner;

public VariantPackage() {
}

public VariantPackage(Map<String, List<Template>> variants, String owner) {
super();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,5 @@ DIY Layout Creator (DIYLC).
*
* @return
*/
int sortOrder() default Integer.MAX_VALUE;
int sortOrder() default 100;
}
2 changes: 1 addition & 1 deletion diylc/diylc-core/src/org/diylc/core/measures/Voltage.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Voltage clone() throws CloneNotSupportedException {
return new Voltage(value, unit);
}

public static Voltage parseCapacitance(String value) {
public static Voltage parseVoltage(String value) {
value = value.replace("*", "");
for (VoltageUnit unit : VoltageUnit.values()) {
if (value.toLowerCase().endsWith(unit.toString().toLowerCase())) {
Expand Down
176 changes: 176 additions & 0 deletions diylc/diylc-core/src/org/diylc/netlist/AbstractNetlistParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/*
*
* DIY Layout Creator (DIYLC). Copyright (c) 2009-2018 held jointly by the individual authors.
*
* This file is part of DIYLC.
*
* DIYLC is free software: you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* DIYLC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with DIYLC. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
package org.diylc.netlist;

import java.awt.geom.Point2D;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.diylc.core.IDIYComponent;
import org.diylc.core.measures.Size;
import org.diylc.core.measures.SizeUnit;

public abstract class AbstractNetlistParser implements INetlistParser {

private static Size X_OFFSET = new Size(1.5d, SizeUnit.in);
private static Size Y_OFFSET = new Size(0.3d, SizeUnit.in);
private static int ROW_COUNT = 12;

public List<ParsedNetlistEntry> parseFile(String fileName, List<String> outputWarnings)
throws NetlistParseException {
String content;
try {
content = new String(Files.readAllBytes(Paths.get(fileName)));
} catch (IOException e) {
throw new NetlistParseException("Error reading file", e);
}

return parse(content, outputWarnings);
}

public List<ParsedNetlistEntry> parse(String content, List<String> outputWarnings)
throws NetlistParseException {
List<ParsedNetlistEntry> result = new ArrayList<ParsedNetlistEntry>();

Pattern pattern = Pattern.compile(getItemRegex(), Pattern.MULTILINE | Pattern.DOTALL);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
String type;
try {
type = matcher.group("Type").trim();
} catch (Exception e) {
outputWarnings.add("'Type' group not found.");
continue;
}

if (type == null) {
outputWarnings.add("'Type' group not found.");
continue;
}

Map<String, Object> valueMap = new HashMap<String, Object>();
List<Class<?>> typeCandidates;
try {
typeCandidates = getComponentTypeCandidates(type, valueMap);
if (typeCandidates == null) {
outputWarnings.add("Type mappings not found for " + type + ".");
continue;
}
} catch (Exception e) {
outputWarnings.add("Error parsing type mappings for " + type + ".");
continue;
}

for (String propertyName : getPropertyNames()) {
String valueStr = matcher.group(propertyName).trim();
try {
Object value = parseValue(type, propertyName, valueStr);
valueMap.put(propertyName, value);
} catch (Exception e) {
outputWarnings
.add("Error while parsing value for " + type + "." + propertyName + " = " + valueStr);
}
}

result.add(new ParsedNetlistEntry(typeCandidates, type, valueMap));
}

Collections.sort(result, new Comparator<ParsedNetlistEntry>() {

@Override
public int compare(ParsedNetlistEntry o1, ParsedNetlistEntry o2) {
int comp = o1.getRawType().compareToIgnoreCase(o2.getRawType());
if (comp == 0 && o1.getValues().containsKey("Name") && o2.getValues().containsKey("Name"))
comp = o1.getValues().get("Name").toString()
.compareToIgnoreCase(o2.getValues().get("Name").toString());
return comp;
}
});

return result;
}

public List<IDIYComponent<?>> generateComponents(List<ParsedNetlistComponent> components,
List<String> outputWarnings) {
double dx = 0;
double dy = 0;
int rowIdx = 0;
List<IDIYComponent<?>> result = new ArrayList<IDIYComponent<?>>();
for (int i = 0; i < components.size(); i++) {
ParsedNetlistComponent parsedComponent = components.get(i);
if (parsedComponent.getType() == null) {
outputWarnings.add("Type not selected for " + parsedComponent.getValues().get("Name"));
continue;
}
Class<?> clazz = parsedComponent.getType();
IDIYComponent<?> component;
try {
component = (IDIYComponent<?>) clazz.newInstance();
} catch (Exception e) {
outputWarnings.add(e.getMessage());
continue;
}
for (int j = 0; j < component.getControlPointCount(); j++) {
Point2D p = component.getControlPoint(j);
Point2D controlPoint = new Point2D.Double(p.getX() + dx, p.getY() + dy);
component.setControlPoint(controlPoint, j);
}

if (rowIdx + 1 == ROW_COUNT) {
dx += X_OFFSET.convertToPixels();
dy = 0;
rowIdx = 0;
} else {
dy += Y_OFFSET.convertToPixels();
rowIdx++;
}

Method[] methods = clazz.getMethods();
for (Map.Entry<String, Object> value : parsedComponent.getValues().entrySet()) {
try {
Optional<Method> first = Arrays.stream(methods)
.filter(x -> x.getName().equals("set" + value.getKey())).findFirst();
if (first.isPresent()) {
Method setter = first.get();
setter.invoke(component, value.getValue());
} else {
outputWarnings.add("No setter found for " + value.getKey());
}
} catch (Exception e) {
outputWarnings.add("Error setting " + value.getKey() + ": " + e.getMessage());
}
}
result.add(component);

}
return result;
}
}
66 changes: 66 additions & 0 deletions diylc/diylc-core/src/org/diylc/netlist/INetlistParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
*
* DIY Layout Creator (DIYLC). Copyright (c) 2009-2018 held jointly by the individual authors.
*
* This file is part of DIYLC.
*
* DIYLC is free software: you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* DIYLC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with DIYLC. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
package org.diylc.netlist;

import java.util.List;
import java.util.Map;

import org.diylc.core.IDIYComponent;

/*
*
* DIY Layout Creator (DIYLC). Copyright (c) 2009-2018 held jointly by the individual authors.
*
* This file is part of DIYLC.
*
* DIYLC is free software: you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* DIYLC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with DIYLC. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
public interface INetlistParser {

String getName();

String getFileExt();

String getItemRegex();

List<Class<?>> getComponentTypeCandidates(String type, Map<String, Object> additionalOutputs);

List<String> getPropertyNames();

Object parseValue(String type, String propertyName, String valueStr);

List<ParsedNetlistEntry> parseFile(String fileName, List<String> outputWarnings)
throws NetlistParseException;

List<ParsedNetlistEntry> parse(String content, List<String> outputWarnings)
throws NetlistParseException;

List<IDIYComponent<?>> generateComponents(List<ParsedNetlistComponent> components,
List<String> outputWarnings);
}
50 changes: 50 additions & 0 deletions diylc/diylc-core/src/org/diylc/netlist/NetlistParseException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
*
* DIY Layout Creator (DIYLC). Copyright (c) 2009-2018 held jointly by the individual authors.
*
* This file is part of DIYLC.
*
* DIYLC is free software: you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* DIYLC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with DIYLC. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
package org.diylc.netlist;

public class NetlistParseException extends Exception {

private static final long serialVersionUID = 1L;

public NetlistParseException() {
super();
// TODO Auto-generated constructor stub
}

public NetlistParseException(String message, Throwable cause, boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}

public NetlistParseException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}

public NetlistParseException(String message) {
super(message);
// TODO Auto-generated constructor stub
}

public NetlistParseException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
26 changes: 26 additions & 0 deletions diylc/diylc-core/src/org/diylc/netlist/ParsedNetlistComponent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.diylc.netlist;

import java.util.Map;

public class ParsedNetlistComponent {

public Class<?> type;
public Map<String, Object> values;

public ParsedNetlistComponent() {
}

public ParsedNetlistComponent(Class<?> type, Map<String, Object> values) {
super();
this.type = type;
this.values = values;
}

public Class<?> getType() {
return type;
}

public Map<String, Object> getValues() {
return values;
}
}
Loading

0 comments on commit cccca4a

Please sign in to comment.