/** * Write the state of this recommender engine to the given file so * that it can be recreated later using another DAOFactory. This uses * default object serialization so if the factory has a PicoContainer or * session bindings containing non-serializable types, this will fail. * * @param file The file to write the rec engine to. * @param compressed Whether to compress the output file. * @throws IOException if there is an error serializing the engine. * @see #write(OutputStream) */ public void write(@Nonnull File file, CompressionMode compressed) throws IOException { try (OutputStream out = new FileOutputStream(file); OutputStream zout = compressed.getEffectiveCompressionMode(file.getName()).wrapOutput(out)) { write(zout); } }
/** * Open a file for input, optionally compressed. * * @param file The file to open. * @param charset The character set to use. * @param compression Whether to compress the file. * @return A reader opened on the file. * @throws IOException if there is an error opening the file. */ public static Writer openOutput(File file, Charset charset, CompressionMode compression) throws IOException { CompressionMode effComp = compression.getEffectiveCompressionMode(file.getName()); OutputStream ostream = new FileOutputStream(file); try { OutputStream wrapped = effComp.wrapOutput(ostream); return new OutputStreamWriter(wrapped, charset); } catch (IOException ex) { try { ostream.close(); } catch (IOException ex2) { ex.addSuppressed(ex2); } throw ex; } }
@Override public void execute(Namespace opts) throws LenskitCommandException { Context ctx = new Context(opts); LenskitConfiguration dataConfig = ctx.input.getConfiguration(); LenskitRecommenderEngineBuilder builder = LenskitRecommenderEngine.newBuilder(); try { for (LenskitConfiguration config: ctx.environment.loadConfigurations(ctx.getConfigFiles())) { builder.addConfiguration(config); } } catch (IOException e) { throw new LenskitCommandException("error loading LensKit configuration", e); } builder.addConfiguration(dataConfig, ModelDisposition.EXCLUDED); Stopwatch timer = Stopwatch.createStarted(); LenskitRecommenderEngine engine = builder.build(ctx.input.getDAO()); timer.stop(); logger.info("built model in {}", timer); File output = ctx.getOutputFile(); CompressionMode comp = CompressionMode.autodetect(output); logger.info("writing model to {}", output); try (OutputStream raw = new FileOutputStream(output); OutputStream stream = comp.wrapOutput(raw)) { engine.write(stream); } catch (IOException e) { throw new LenskitCommandException("could not write output file", e); } }
/** * Write the state of this recommender engine to the given file so * that it can be recreated later using another DAOFactory. This uses * default object serialization so if the factory has a PicoContainer or * session bindings containing non-serializable types, this will fail. * * @param file The file to write the rec engine to. * @param compressed Whether to compress the output file. * @throws IOException if there is an error serializing the engine. * @see #write(OutputStream) */ public void write(@Nonnull File file, CompressionMode compressed) throws IOException { try (OutputStream out = new FileOutputStream(file); OutputStream zout = compressed.getEffectiveCompressionMode(file.getName()).wrapOutput(out)) { write(zout); } }
/** * Open a file for input, optionally compressed. * * @param file The file to open. * @param charset The character set to use. * @param compression Whether to compress the file. * @return A reader opened on the file. * @throws IOException if there is an error opening the file. */ public static Writer openOutput(File file, Charset charset, CompressionMode compression) throws IOException { CompressionMode effComp = compression.getEffectiveCompressionMode(file.getName()); OutputStream ostream = new FileOutputStream(file); try { OutputStream wrapped = effComp.wrapOutput(ostream); return new OutputStreamWriter(wrapped, charset); } catch (IOException ex) { try { ostream.close(); } catch (IOException ex2) { ex.addSuppressed(ex2); } throw ex; } }