/* * File: NeuralNetwork.java * Author: Justin Basilico * Course: PO CS 152: Neural Networks * Assignment: Final Project * Updated: 2001.12.18 * Created: 2001.12.03 * * Description: * This file contains the NeuralNetwork abstract class, which implements * common functionality among neural networks. * * Copyright: Justin Basilico (2001). */ import java.io.*; /** * NeuralNetwork abstract class * * This abstract class implements the common functionality among different * types of neural networks. Currently it just implements methods for reading * and writing NeuralNetwork objects from and to a file using the Java * Serialization framework. Any implementations of neural networks should * extend this class. No Objects of this class can be constructed because it * is abstract, but sub-classes of this class need to call the default * constructor for the NeuralNetwork. * * @author Justin Basilico * @version 2001.12.18 * @see java.io.Serializable */ public abstract class NeuralNetwork extends Object implements Serializable { /** * NeuralNetwork default constructor * * Creates new NeuralNetwork. Since the NeuralNetwork has no internal * data, the constructor does nothing. */ public NeuralNetwork() { } /** * readNetworkFromFile * * This method takes a String file name and if such a file exists, it * attempts to read a NeuralNetwork object from that file. If the file * does not exist or does not contain a valid NeuralNetwork object, it * returns null. Otherwise, it returns the NeuralNetwork object from that * file. * * It outputs error messages to System.err along with messages when it * starts reading in the network to System.out. * * @param fileName The String file name to read a NeuralNetwork object in * from. * @return The NeuralNetwork object stored in the given file, or null if * no such file exists or if there is an error reading in the * file. */ public static NeuralNetwork readNetworkFromFile( String fileName) // File to read network from. { if ( !(new File(fileName)).exists() ) // No such file exists. return null; try { // Open a file input stream. FileInputStream fis = new FileInputStream(fileName); // This reads in Objects using the Serialization interface. ObjectInputStream in = new ObjectInputStream(fis); // Try to read in the network. System.out.print("Reading network from file " + fileName + "..."); NeuralNetwork network = (NeuralNetwork) in.readObject(); // Done, close the input stream. in.close(); System.out.println(" Done."); return network; } catch ( ClassNotFoundException cnfe ) { // Error: Serialization problem reading the network. Return null. System.err.println("Serialization error when loading file: " + cnfe); return null; } catch ( IOException ioe ) { // Error: IOException reading in the network. Return null. System.err.println("Error reading network from file: " + ioe); return null; } } /** * writeNetworkToFile * * This method takes a String file name and writes this NeuralNetwork * object into the specified file, overwriting any other information that * was once there. If it successfully writes the network to the given * file, it returns true. If there is an error, it outputs the error * message to System.err and returns false. * * @param fileName The String file name to write this NeuralNetwork * object too. * @return True if the file is successfully written to the specified file * name. False otherwise. */ public boolean writeNetworkToFile( String fileName) // File to write network to. { try { // Open a file output stream. FileOutputStream fos = new FileOutputStream(fileName); // Writes out the Object using the Serialization interface. ObjectOutputStream out = new ObjectOutputStream(fos); // Write the network to the file. System.out.print("Saving network to file " + fileName + "..."); out.writeObject(this); // Done reading the file. out.close(); System.out.println(" Done."); return true; } catch ( IOException ioe ) { // Error: IOException writing the network. Return false. System.err.println("Error saving network to file :" + ioe); return false; } } }