示例#1
0
        /// <summary>
        /// Writes a <seealso cref="BaseModel"/> to disk. Occurring errors are printed to the console
        /// to inform the user.
        /// </summary>
        /// <param name="modelName"> type of the model, name is used in error messages. </param>
        /// <param name="modelFile"> output file of the model </param>
        /// <param name="model"> the model itself which should be written to disk </param>
        public static void writeModel(string modelName, Jfile modelFile, BaseModel model)
        {
            CmdLineUtil.checkOutputFile(modelName + " model", modelFile);

            Console.Error.Write("Writing " + modelName + " model ... ");

            long beginModelWritingTime = DateTime.Now.Ticks;

            FileOutputStream modelOut = null;

            try
            {
                modelOut = new FileOutputStream(modelFile);
                model.serialize(modelOut);
            }
            catch (IOException e)
            {
                Console.Error.WriteLine("failed");
                throw new TerminateToolException(-1, "Error during writing model file '" + modelFile + "'", e);
            }
            finally
            {
                if (modelOut != null)
                {
                    try
                    {
                        modelOut.close();
                    }
                    catch (IOException e)
                    {
                        Console.Error.WriteLine("Failed to properly close model file '" + modelFile + "': " + e.Message);
                    }
                }
            }

            long modelWritingDuration = DateTime.Now.Ticks - beginModelWritingTime;

            //System.Error.printf("done (%.3fs)\n", modelWritingDuration / 1000d);

            Console.Error.WriteLine();

            Console.Error.WriteLine("Wrote " + modelName + " model to");
            Console.Error.WriteLine("path: " + modelFile.AbsolutePath);

            Console.Error.WriteLine();
        }
示例#2
0
        public virtual T load(Jfile modelFile)
        {
            long beginModelLoadingTime = DateTimeHelperClass.CurrentUnixTimeMillis();

            CmdLineUtil.checkInputFile(modelName + " model", modelFile);

            Console.Error.Write("Loading " + modelName + " model ... ");

            InputStream modelIn = new BufferedInputStream(CmdLineUtil.openInFile(modelFile), CmdLineUtil.IO_BUFFER_SIZE);

            T model;

            try
            {
                model = loadModel(modelIn);
            }
            catch (InvalidFormatException e)
            {
                Console.Error.WriteLine("failed");
                throw new TerminateToolException(-1, "Model has invalid format", e);
            }
            catch (IOException e)
            {
                Console.Error.WriteLine("failed");
                throw new TerminateToolException(-1, "IO error while loading model file '" + modelFile + "'", e);
            }
            finally
            {
                // will not be null because openInFile would
                // terminate in this case
                try
                {
                    modelIn.close();
                }
                catch (IOException)
                {
                    // sorry that this can fail
                }
            }

            long modelLoadingDuration = DateTimeHelperClass.CurrentUnixTimeMillis() - beginModelLoadingTime;

            System.err.printf("done (%.3fs)\n", modelLoadingDuration / 1000d);

            return(model);
        }