/** * Close the file. */ public final void close() { try { this.reader.close(); } catch (final IOException e) { throw new PersistError(e); } }
/** * Parse an int from a name-value collection of params. * @param params The name-value pairs. * @param name The name to parse. * @return The parsed int value. */ public static int parseInt(final Map<String, String> params, final String name) { String value = null; try { value = params.get(name); if (value == null) { throw new PersistError("Missing property: " + name); } return Integer.parseInt(value); } catch (final NumberFormatException ex) { throw new PersistError("Field: " + name + ", " + "invalid integer: " + value); } }
/** * Parse a boolean from a name-value collection of params. * @param params The name-value pairs. * @param name The name to parse. * @return The parsed boolean value. */ public static boolean parseBoolean(final Map<String, String> params, final String name) { String value = null; try { value = params.get(name); if (value == null) { throw new PersistError("Missing property: " + name); } return value.trim().toLowerCase().charAt(0) == 't'; } catch (final NumberFormatException ex) { throw new PersistError("Field: " + name + ", " + "invalid integer: " + value); } }
/** * Parse an activation function from a string. * @param params The params. * @param name The name of the param to parse. * @return The parsed activation function. */ public static ActivationFunction parseActivationFunction( final Map<String, String> params, final String name) { String value = null; try { value = params.get(name); if (value == null) { throw new PersistError("Missing property: " + name); } return parseActivationFunction(value); } catch (final Exception ex) { throw new PersistError(ex); } }
/** * Parse a double from a name-value collection of params. * @param params The name-value pairs. * @param name The name to parse. * @return The parsed double value. */ public static double parseDouble(final Map<String, String> params, final String name) { String value = null; try { value = params.get(name); if (value == null) { throw new PersistError("Missing property: " + name); } return CSVFormat.EG_FORMAT.parse(value); } catch (final NumberFormatException ex) { throw new PersistError("Field: " + name + ", " + "invalid integer: " + value); } }
/** * Parse an int array from a name-value collection of params. * @param params The name-value pairs. * @param name The name to parse. * @return The parsed int array value. */ public static int[] parseIntArray(final Map<String, String> params, final String name) { String value = null; try { value = params.get(name); if (value == null) { throw new PersistError("Missing property: " + name); } return NumberList.fromListInt(CSVFormat.EG_FORMAT, value); } catch (final NumberFormatException ex) { throw new PersistError("Field: " + name + ", " + "invalid integer: " + value); } }
/** * Read a line from the input stream. * @param is The input stream. * @return The line read. */ private static String readLine(final InputStream is) { try { final StringBuilder result = new StringBuilder(); char ch; do { final int b = is.read(); if (b == -1) { return result.toString(); } ch = (char) b; if ((ch != 13) && (ch != 10)) { result.append(ch); } } while (ch != 10); return result.toString(); } catch (final IOException ex) { throw new PersistError(ex); } }
/** * Parse a double array from a name-value collection of params. * @param params The name-value pairs. * @param name The name to parse. * @return The parsed double array value. */ public double[] parseDoubleArray(final Map<String, String> params, final String name) { String value = null; try { value = params.get(name); if (value == null) { throw new PersistError("Missing property: " + name); } if( value.startsWith("##") ) { int i = Integer.parseInt(value.substring(2)); return this.largeArrays.get(i); } else { return NumberList.fromList(CSVFormat.EG_FORMAT, value); } } catch (final NumberFormatException ex) { throw new PersistError("Field: " + name + ", " + "invalid integer: " + value); } }
/** * Parse an activation function from a value. * @param value The value. * @return The activation function. */ public static ActivationFunction parseActivationFunction(String value) { ActivationFunction af = null; final String[] cols = value.split("\\|"); final String afName = "org.encog.engine.network.activation." + cols[0]; try { final Class<?> clazz = Class.forName(afName); af = (ActivationFunction) clazz.newInstance(); } catch (final ClassNotFoundException e) { throw new PersistError(e); } catch (final InstantiationException e) { throw new PersistError(e); } catch (final IllegalAccessException e) { throw new PersistError(e); } for (int i = 0; i < af.getParamNames().length; i++) { af.setParam(i, CSVFormat.EG_FORMAT.parse(cols[i + 1])); } return af; }
/** * Get the type of an Encog object in an EG file, without the * need to read the entire file. * @param name The filename to read. * @return The type. */ public String getEncogType(final String name) { BufferedReader br = null; try { final File path = new File(this.parent, name); br = new BufferedReader(new FileReader(path)); final String header = br.readLine(); if( header==null ) { throw new PersistError("Invalid file, can't find header"); } final String[] params = header.split(","); return params[1]; } catch (final IOException ex) { throw new PersistError(ex); } finally { if( br!=null ) { try { br.close(); } catch (IOException e) { EncogLogging.log(e); } } } }
/** * Save the specified object. * @param filename The filename to save to. * @param obj The Object to save. */ public static void saveObject(final File filename, final Object obj) { FileOutputStream fos = null; try { fos = new FileOutputStream(filename); EncogDirectoryPersistence.saveObject(fos, obj); } catch (final IOException ex) { throw new PersistError(ex); } finally { try { if (fos != null) { fos.close(); } } catch (final IOException e) { EncogLogging.log(e); } } }
/** * Save the specified object. * @param os The output stream to write to. * @param obj The object to save. */ public static void saveObject(final OutputStream os, final Object obj) { try { final EncogPersistor p = PersistorRegistry.getInstance() .getPersistor(obj.getClass()); if (p == null) { throw new PersistError("Do not know how to persist object: " + obj.getClass().getSimpleName()); } os.flush(); final PrintWriter pw = new PrintWriter(os); final Date now = new Date(); pw.println("encog," + obj.getClass().getSimpleName() + ",java," + Encog.VERSION + "," + p.getFileVersion() + "," + now.getTime()); pw.flush(); p.save(os, obj); } catch (final IOException ex) { throw new PersistError(ex); } }
/** * Load an object from an input stream. * @param is The input stream to read from. * @return The loaded object. */ public static Object loadObject(final InputStream is) { final String header = EncogDirectoryPersistence.readLine(is); final String[] params = header.split(","); if (!"encog".equals(params[0])) { throw new PersistError("Not a valid EG file."); } final String name = params[1]; final EncogPersistor p = PersistorRegistry.getInstance().getPersistor( name); if (p == null) { throw new PersistError("Do not know how to read the object: " + name); } if (p.getFileVersion() < Integer.parseInt(params[4])) { throw new PersistError( "The file you are trying to read is from a later version of Encog. Please upgrade Encog to read this file."); } return p.read(is); }
/** * Load the specified object. * @param file The file to load. * @return The loaded object. */ public static Object loadObject(final File file) { FileInputStream fis = null; try { fis = new FileInputStream(file); final Object result = EncogDirectoryPersistence.loadObject(fis); return result; } catch (final IOException ex) { throw new PersistError(ex); } finally { if (fis != null) { try { fis.close(); } catch (final IOException e) { EncogLogging.log(e); } } } }
String s = line.substring(1).trim(); if (!s.endsWith("]")) { throw new PersistError("Invalid section: " + line); } else { if (this.currentSectionName.length() < 1) { throw new PersistError( "Can't begin subsection when a section has not yet been defined: " + line); throw new PersistError("Can't begin subsection " + line + ", while we are still in the section: " } else { if (this.currentSectionName.length() < 1) { throw new PersistError( "Unknown command before first section: " + line); return this.section; } catch (final IOException ex) { throw new PersistError(ex);
/** * Parse a matrix from a name-value collection of params. * @param params The name-value pairs. * @param name The name to parse. * @return The parsed matrix value. */ public static Matrix parseMatrix(final Map<String, String> params, final String name) { if (!params.containsKey(name)) { throw new PersistError("Missing property: " + name); } final String line = params.get(name); final double[] d = NumberList.fromList(CSVFormat.EG_FORMAT, line); final int rows = (int) d[0]; final int cols = (int) d[1]; final Matrix result = new Matrix(rows, cols); int index = 2; for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { result.set(r, c, d[index++]); } } return result; }
af = (ActivationFunction) clazz.newInstance(); } catch (final ClassNotFoundException e) { throw new PersistError(e); } catch (final InstantiationException e) { throw new PersistError(e); } catch (final IllegalAccessException e) { throw new PersistError(e);
af = (ActivationFunction) clazz.newInstance(); } catch (final ClassNotFoundException e) { throw new PersistError(e); } catch (final InstantiationException e) { throw new PersistError(e); } catch (final IllegalAccessException e) { throw new PersistError(e);
af = (ActivationFunction) clazz.newInstance(); } catch (final ClassNotFoundException e) { throw new PersistError(e); } catch (final InstantiationException e) { throw new PersistError(e); } catch (final IllegalAccessException e) { throw new PersistError(e); rbf = (RadialBasisFunction) clazz.newInstance(); } catch (final ClassNotFoundException e) { throw new PersistError(e); } catch (final InstantiationException e) { throw new PersistError(e); } catch (final IllegalAccessException e) { throw new PersistError(e);
af = (ActivationFunction) clazz.newInstance(); } catch (final ClassNotFoundException e) { throw new PersistError(e); } catch (final InstantiationException e) { throw new PersistError(e); } catch (final IllegalAccessException e) { throw new PersistError(e);