package Neuro; import java.io.StringReader; import java.io.IOException; import java.util.Vector; import java.util.Enumeration; import java.util.Stack; /** * Translates the terms ((E(0|1))|(N\d+)):=(+|-)N\d+ into approperate calls * to the NeuronalNetwork * @see NeuronalNetwork */ public class NeuronalNetworkReader { private static final int EOF = -1; public static final int UNKNOWN=0; public static final int NEURON=1; public static final int INPUT=2; private StringReader in = null; private int la = EOF; private Vector tmplist = null; public NeuronalNetworkReader() { tmplist = new Vector(); } private class TempListElement { public int type; public int value; public int signum; public int output; public boolean added; public TempListElement(int type, int value, int signum, int output) { this.type = type; this.value = value; this.signum = signum; this.output = output; this.added = false; } } /** creates a network from all the temporarilly stored terms */ public NeuronalNetwork createNetwork( ) throws NeuroException { NeuronalNetwork nn = new NeuronalNetwork(); Stack outputNeurons = new Stack(); outputNeurons.push( new Integer(1) ); int added=0; while ( !outputNeurons.isEmpty() ) { int outputNeuron = ((Integer)outputNeurons.pop()).intValue(); for( Enumeration e = tmplist.elements(); e.hasMoreElements(); ) { TempListElement t = (TempListElement)e.nextElement(); if ( t.output == outputNeuron && !t.added) { added++; if ( t.type == INPUT ) nn.addInput( t.value, t.signum, t.output ); else { nn.addNeuron( t.value, t.signum, t.output ); outputNeurons.push( new Integer( t.value ) ); } t.added = true; } } } if ( added != tmplist.size() ) throw new NeuroException("not all elements could be added to the neuronal network"); return nn; } /** reads an Integer from the input */ private int readInt() throws NeuroException, IOException { int ret = 0; if ( ( la != EOF ) && Character.isDigit( (char) la ) ) { // an integer can have more than 10 digits StringBuffer buf = new StringBuffer( 10 ); while ( ( la != EOF ) && Character.isDigit( (char) la ) ) { buf.append( (char) la ); la = in.read(); } ret = Integer.parseInt( buf.toString() ); } else throw new NeuroException( "number expected" ); return ret; } /** evals the term and temporarily stores the elements. */ public int readTerm( String term ) throws NeuroException { int type = UNKNOWN; in = new StringReader( term ); try { la = in.read(); int value = 0; int signum = 0; // E or N? if ( la == 'E' ) { type = INPUT; } else if ( la == 'N' ) { type = NEURON; } else throw new NeuroException( "must start with E or N" ); la = in.read(); // read 1. value value = readInt(); // check 1 value if ( type == NEURON ) { if ( value < 1 ) throw new NeuroException( "neuron number must be >= 1" ); } else if ( !( value == 0 || value == 1 ) ) throw new NeuroException( "only E0 and E1 allowd" ); // := if ( la == ':' ) { la = in.read(); if ( la == '=' ) la = in.read(); else throw new NeuroException( "= after : expected" ); } else throw new NeuroException( ":= exptected" ); // signum if ( la == '+' ) signum = 1; else if ( la == '-' ) signum = -1; else throw new NeuroException( "+ or - exptected" ); la = in.read(); // output neuron and add it to the network if ( la == 'N' ) { la = in.read(); int output = readInt(); if ( la != EOF ) throw new NeuroException( "input after end of term" ); tmplist.add( new TempListElement( type, value, signum, output )); } else throw new NeuroException( "neuron expected" ); } catch ( IOException e ) { throw new NeuroException( "error reading string" ); } catch ( NeuroException e ) { throw e; } finally { in = null; } return type; } }