package Neuro; import java.util.HashMap; /** * The NeuronalNetwork it self, it owns the root element of the Tree and a * HashMap with all neurons. * Use addNeuron and addInput to add new elements to the * Network. value returns the output for the root element. */ public class NeuronalNetwork { private HashMap neurons = null; public NeuronalElement root = null; public NeuronalNetwork() throws NeuroException { // create the root element and add it to the hashmap try { neurons = new HashMap(); root = new Neuron( 1 ); neurons.put( new Integer( 1 ), root ); } catch( NeuroException e ) { throw new NeuroException("unexptected exception: " + e.getMessage() ); } } private Neuron getNeuron( int number ) throws NeuroException { Integer key = new Integer( number ); if ( neurons.containsKey( key ) ) { return (Neuron) neurons.get( key ); } else throw new NeuroException( "neuron " + number + " does not exist" ); } public void addNeuron( int number, int signum, int output ) throws NeuroException { Neuron parent = getNeuron( output ); Integer key = new Integer( number ); if ( !neurons.containsKey( key ) ) { Neuron neuron = new Neuron( number ); neurons.put( key, neuron ); parent.addSubelement( neuron, signum ); } else throw new NeuroException( "Neuron " + number + " is already defined" ); } public void addInput( int value, int signum, int output ) throws NeuroException { Neuron parent = getNeuron( output ); parent.addSubelement( new Input( value ), signum ); } public int value() throws NeuroException { return root.value(); } }