Download: project files
These files contain an implementation of simple circuits. There is
a Circuit
class that contains a list of circuit
elements (devices) and a list of connections (nodes). The class Device
is an abstract class. There three concrete
classes based on Device
: Resistor
, VoltageSource
and CurrentSource
.
This program tests this object-oriented structure consisting of Node objects and Device objects arranged in a Circuit object. This version allows us to parse simple netlist files.
The program was originally done in C++ (See C++ version.
netlist3.java
Node.java
Device.java
PassiveAnalog.java
Circuit.java
netlist3.java
01: // netlist3.java 02: 03: import java.io.*; 04: 05: public class netlist3 { 06: 07: static public void main(String args[]) 08: { 09: String filename; 10: FileInputStream instream; 11: BufferedReader in; 12: 13: if (args.length < 1) { 14: System.err.println("usage: java netlist3 source"); 15: System.exit(0); 16: } 17: try { 18: filename = args[0]; 19: instream = new FileInputStream(filename); 20: in = new BufferedReader(new InputStreamReader(instream)); 21: Circuit ckt = new Circuit(); 22: ckt.read(in); 23: in.close(); 24: System.out.println("\nDevice list:\n"); 25: ckt.list_devices(); 26: ckt.build_node_list(); 27: System.out.println("\nNode list:\n"); 28: ckt.list_nodes(); 29: } catch (IOException e) { 30: System.out.println( e); 31: } 32: } 33: } 34: 35:
Node.java
01: import java.util.ArrayList; 02: 03: public class Node 04: { 05: String name; 06: int index; 07: ArrayList<Device> devices; 08: Node(String str) { 09: name = str; 10: index = 0; 11: devices = new ArrayList<Device>(); 12: } 13: 14: public String toString() 15: { 16: String str = name; 17: for (Device d: devices) str += "\t" + d.name; 18: return str; 19: } 20: } 21: 22: 23:
Device.java
01: import java.util.ArrayList; 02: 03: public abstract class Device { 04: String name; 05: String [] nodes; 06: int [] ndx; 07: double value; 08: 09: Device(int n) 10: { 11: nodes = new String[n]; 12: ndx = new int[n]; 13: value = 0.0; 14: } 15: 16: String getType() { return "device"; } 17: String getCode() { return "device_code"; } 18: 19: public void parse(String tok[]) 20: { 21: name = tok[1]; 22: int j, n, ntokens; 23: ntokens = tok.length; 24: n = nodes.length; 25: if (ntokens < n+2) { 26: System.out.printf("%d tokens %d needed\n",ntokens,n+2); 27: System.err.println("Input error - device has insufficient nodes"); 28: } 29: for (j=0; j<n; j++) nodes[j] = tok[2+j]; 30: n += 2; 31: if (ntokens<=n) return; 32: // logic may not have a value, but it might have an orientation 33: value = parseValue(tok[n]); 34: } 35: 36: public String toString() 37: { 38: String str = getType() + " " + getCode() + " " + name; 39: for (String s: nodes) str += " " + s; 40: str += " " + value; 41: return str; 42: } 43: 44: static public double parseValue(String str) 45: { 46: final String suffix = "afpnumkKMGT"; 47: final double scales[] = {1e-18, 1e-15, 1e-12, 1e-9, 1e-6, 1e-3, 1e3, 1e3, 1e6, 1e9, 1e12}; 48: char [] chars = str.toCharArray(); 49: int n = chars.length; 50: while (n>0) { 51: if (!Character.isLetter(chars[n-1])) break; 52: n--; 53: } 54: double v; 55: try { 56: if (n<chars.length) { 57: v = Double.parseDouble(str.substring(0,n)); 58: //System.out.printf("value %g suffix %s\n",v,str.substring(n)); 59: n = suffix.indexOf(chars[n]); 60: if (n<0) return v; 61: else v *= scales[n]; 62: } 63: else v = Double.parseDouble(str); 64: } 65: catch (NumberFormatException e) { 66: System.out.println(e); 67: v = 0.0; 68: } 69: return v; 70: } 71: }
PassiveAnalog.java
01: public class PassiveAnalog { 02: 03: 04: static Device getNewDevice(String type) 05: { 06: if (type.equals(Resistor.code)) return new Resistor(); 07: if (type.equals(VoltageSource.code)) return new VoltageSource(); 08: return null; 09: } 10: } 11: 12: class Resistor extends Device { 13: final static String code = "R"; 14: Resistor() { 15: super(2); 16: //code = "R"; 17: //type = "resistor"; 18: } 19: String getType() { return "resistor"; } 20: String getCode() { return Resistor.code; } 21: } 22: 23: class VoltageSource extends Device 24: { 25: final static String code = "V"; 26: VoltageSource() { 27: super(2); 28: } 29: String getType() { return "voltage source"; } 30: String getCode() { return VoltageSource.code; } 31: } 32: 33: class CurrentSource extends Device 34: { 35: final static String code = "I"; 36: CurrentSource() { super(2); } 37: String getType() { return "current source"; } 38: String getCode() { return CurrentSource.code; } 39: } 40:
Circuit.java
01: import java.io.*; 02: import java.util.ArrayList; 03: 04: public class Circuit 05: { 06: String name; 07: Node ref; 08: ArrayList<Node> nodes; 09: ArrayList<Device> devices; 10: 11: Circuit() 12: { 13: ref = new Node("0"); 14: devices = new ArrayList<Device>(); 15: nodes = new ArrayList<Node>(); 16: } 17: 18: public void read(BufferedReader inr) { 19: LineNumberReader in = new LineNumberReader(inr); 20: boolean done = false; 21: while (!done) { 22: int n = in.getLineNumber(); 23: try { 24: String inp = in.readLine(); 25: if (inp==null) break; 26: System.out.println("Line " + n + ": " + inp); 27: String [] str = inp.split("%"); // terminate at comment character 28: String [] tokens = str[0].split("\\s+"); 29: if (tokens.length>1) { 30: Device d = null; 31: d = PassiveAnalog.getNewDevice(tokens[0]); 32: if (d!=null) { 33: d.parse(tokens); 34: devices.add(d); 35: } 36: else System.out.println("Unrecognized device: " + tokens[0]); 37: } 38: } 39: catch (IOException e) { 40: System.out.println(e); 41: } 42: } 43: System.out.println(); 44: } 45: 46: int add_node(String str) 47: { 48: int i; 49: Node node; 50: int ncount = nodes.size(); 51: for (i=0; i<ncount; i++) { 52: node = nodes.get(i); 53: if (node.name.equals(str)) return i; 54: } 55: nodes.add(new Node(str)); 56: return ncount; 57: } 58: 59: void build_node_list() 60: { 61: int i, j; 62: Device dev; 63: int ndevices = devices.size(); 64: for (i=0; i<ndevices; i++) { 65: dev = devices.get(i); 66: int nodecount = dev.nodes.length; 67: for (j=0; j<nodecount; j++) dev.ndx[j] = add_node(dev.nodes[j]); 68: } 69: } 70: 71: void list_nodes() 72: { 73: for (Node node: nodes) System.out.println(node); 74: /* 75: int i; 76: int ncount = nodes.size(); 77: for (i=0; i<ncount; i++) { 78: Node node = nodes.get(i); 79: System.out.printf("%d\t %s\n",i,node); 80: } 81: */ 82: } 83: 84: 85: void list_devices() 86: { 87: for (Device d: devices) System.out.println(d); 88: } 89: }
Test with command
java netlist3 test.net
The results are
Line 0: % This is a test of circuit file parser Line 1: R R1 N_2 N_1 3k Line 2: R R2 N_2 N_3 4MEG Line 3: V V1 N_1 N_3 5 Line 4: R R3 N_3 0 2 Device list: resistor R R1 N_2 N_1 3000.0 resistor R R2 N_2 N_3 4000000.0 voltage source V V1 N_1 N_3 5.0 resistor R R3 N_3 0 2.0 Node list: N_2 N_1 N_3 0
Maintained by John Loomis, updated Sat Mar 22 10:49:03 2008