Skip to content

Commit

Permalink
Move most of the code to MathOptFormat.
Browse files Browse the repository at this point in the history
  • Loading branch information
dourouc05 committed Nov 26, 2019
1 parent b82f70c commit 6991b86
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 77 deletions.
3 changes: 0 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ version = "0.20.0"

[deps]
Calculus = "49dc2e85-a5d0-5ad3-a950-438e2897f1b9"
CodecBzip2 = "523fee87-0ab8-5b00-afb7-3ecf72e48cfd"
CodecXz = "ba30903b-d9e8-5048-a5ec-d1f5b0d4b47b"
CodecZlib = "944b1d66-785c-5afd-91f1-9de20f533193"
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
3 changes: 0 additions & 3 deletions src/JuMP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ import MathOptInterface
const MOI = MathOptInterface
const MOIU = MOI.Utilities
using MathOptFormat
import CodecBzip2
import CodecXz
import CodecZlib

import Calculus
import DataStructures.OrderedDict
Expand Down
79 changes: 8 additions & 71 deletions src/file_formats.jl
Original file line number Diff line number Diff line change
@@ -1,72 +1,6 @@
"""
List of accepted export formats. `AUTOMATIC_FILE_FORMAT` corresponds to
a detection from the file name.
"""
@enum(FileFormat, CBF, LP, MOF, MPS, AUTOMATIC_FILE_FORMAT)

function _filename_to_format(filename::String)
return if endswith(filename, ".mof.json.gz") || endswith(filename, ".mof.json")
MOF
elseif endswith(filename, ".cbf.gz") || endswith(filename, ".cbf")
CBF
elseif endswith(filename, ".mps.gz") || endswith(filename, ".mps")
MPS
elseif endswith(filename, ".lp.gz") || endswith(filename, ".lp")
LP
else
error("File type of $(filename) not recognized by JuMP.")
end
end

"""
List of accepted export compression formats. `AUTOMATIC_FILE_COMPRESSION`
corresponds to a detection from the file name.
"""
@enum(FileCompression, NO_FILE_COMPRESSION, BZIP2, GZIP, XZ, AUTOMATIC_FILE_COMPRESSION)

function _filename_to_compression(filename::String)
return if endswith(filename, ".bz2")
BZIP2
elseif endswith(filename, ".gz")
GZIP
elseif endswith(filename, ".xz")
XZ
else
NO_FILE_COMPRESSION
end
end

function _open(f::Function, filename::String, mode::String; compression::FileCompression=AUTOMATIC_FILE_COMPRESSION)
if compression == AUTOMATIC_FILE_COMPRESSION
compression = _filename_to_compression(filename)
end

if compression == BZIP2 || compression == GZIP || compression == XZ
stream = if compression == BZIP2
(mode == "r") ? CodecBzip2.Bzip2DecompressorStream : CodecBzip2.Bzip2CompressorStream
elseif compression == GZIP
(mode == "r") ? CodecZlib.GzipDecompressorStream : CodecZlib.GzipCompressorStream
else # compression == XZ
(mode == "r") ? CodecXz.XzDecompressorStream : CodecXz.XzCompressorStream
end
return open(f, stream, filename, mode)
else
return open(f, filename, mode)
end
end

function write_to_file(model::Model, io::IO, format::FileFormat; kwargs...)
dest = if format == CBF
MathOptFormat.CBF.Model(; kwargs...)
elseif format == LP
MathOptFormat.LP.Model(; kwargs...)
elseif format == MOF
MathOptFormat.MOF.Model(; kwargs...)
elseif format == MPS
MathOptFormat.MPS.Model(; kwargs...)
else
error("When passing an IO object to write_to_file, the file format cannot be guessed from the file name.")
end
function write_to_file(model::Model, io::IO, format::MathOptFormat.FileFormat; kwargs...)
@assert format != AUTOMATIC_FILE_FORMAT
dest = MathOptFormat._file_formats[format]()
MOI.copy_to(dest, backend(model))
MOI.write_to_file(dest, io)
return
Expand All @@ -82,12 +16,15 @@ are given by the enum [`FileCompression`](@ref).
For keyword options, see [MathOptFormat.jl](https://github.com/odow/MathOptFormat.jl).
"""
function write_to_file(model::Model, filename::String; format::FileFormat=AUTOMATIC_FILE_FORMAT, compression::FileCompression=AUTOMATIC_FILE_COMPRESSION, kwargs...)
function write_to_file(model::Model, filename::String;
format::MathOptFormat.FileFormat=MathOptFormat.AUTOMATIC_FILE_FORMAT,
compression::MathOptFormat.FileCompression=AUTOMATIC_FILE_COMPRESSION,
kwargs...)
if format == AUTOMATIC_FILE_FORMAT
format = _filename_to_format(filename)
end

_open(filename, "w", compression=compression) do io
MathOptFormat.gzip_open(filename, "w", compression=compression) do io
write_to_file(model, io, format; kwargs...)
end
return
Expand Down

0 comments on commit 6991b86

Please sign in to comment.