Skip to content

Commit c060af4

Browse files
authored
[FileFormats] remove show method in favor of summary (#2510)
1 parent 902064f commit c060af4

File tree

15 files changed

+86
-83
lines changed

15 files changed

+86
-83
lines changed

docs/src/submodules/FileFormats/overview.md

Lines changed: 67 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -16,56 +16,84 @@ models using [`write_to_file`](@ref) and [`read_from_file`](@ref).
1616
You must read and write files to a [`FileFormats.Model`](@ref) object. Specific
1717
the file-type by passing a [`FileFormats.FileFormat`](@ref) enum. For example:
1818

19-
**The Conic Benchmark Format**
19+
### The Conic Benchmark Format
2020

2121
```jldoctest
2222
julia> model = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_CBF)
23-
A Conic Benchmark Format (CBF) model
23+
MOI.FileFormats.CBF.Model
24+
├ ObjectiveSense: FEASIBILITY_SENSE
25+
├ ObjectiveFunctionType: MOI.ScalarAffineFunction{Float64}
26+
├ NumberOfVariables: 0
27+
└ NumberOfConstraints: 0
2428
```
2529

26-
**The LP file format**
30+
### The LP file format
2731

2832
```jldoctest
2933
julia> model = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_LP)
30-
A .LP-file model
34+
MOI.FileFormats.LP.Model
35+
├ ObjectiveSense: FEASIBILITY_SENSE
36+
├ ObjectiveFunctionType: MOI.ScalarAffineFunction{Float64}
37+
├ NumberOfVariables: 0
38+
└ NumberOfConstraints: 0
3139
```
3240

33-
**The MathOptFormat file format**
41+
### The MathOptFormat file format
3442

3543
```jldoctest
3644
julia> model = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_MOF)
37-
A MathOptFormat Model
45+
MOI.FileFormats.MOF.Model
46+
├ ObjectiveSense: FEASIBILITY_SENSE
47+
├ ObjectiveFunctionType: MOI.ScalarAffineFunction{Float64}
48+
├ NumberOfVariables: 0
49+
└ NumberOfConstraints: 0
3850
```
3951

40-
**The MPS file format**
52+
### The MPS file format
4153

4254
```jldoctest
4355
julia> model = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_MPS)
44-
A Mathematical Programming System (MPS) model
56+
MOI.FileFormats.MPS.Model
57+
├ ObjectiveSense: FEASIBILITY_SENSE
58+
├ ObjectiveFunctionType: MOI.ScalarAffineFunction{Float64}
59+
├ NumberOfVariables: 0
60+
└ NumberOfConstraints: 0
4561
```
4662

47-
**The NL file format**
63+
### The NL file format
4864

4965
```jldoctest
5066
julia> model = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_NL)
51-
An AMPL (.nl) model
67+
MOI.FileFormats.NL.Model
68+
├ ObjectiveSense: unknown
69+
├ ObjectiveFunctionType: unknown
70+
├ NumberOfVariables: unknown
71+
└ NumberOfConstraints: unknown
5272
```
5373

54-
**The REW file format**
74+
### The REW file format
5575

5676
```jldoctest
5777
julia> model = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_REW)
58-
A Mathematical Programming System (MPS) model
78+
MOI.FileFormats.MPS.Model
79+
├ ObjectiveSense: FEASIBILITY_SENSE
80+
├ ObjectiveFunctionType: MOI.ScalarAffineFunction{Float64}
81+
├ NumberOfVariables: 0
82+
└ NumberOfConstraints: 0
5983
```
6084

6185
Note that the REW format is identical to the MPS file format, except that all
6286
names are replaced with generic identifiers.
6387

64-
**The SDPA file format**
88+
### The SDPA file format
6589

6690
```jldoctest
6791
julia> model = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_SDPA)
68-
A SemiDefinite Programming Algorithm Format (SDPA) model
92+
MOI.FileFormats.SDPA.Model
93+
├ ObjectiveSense: FEASIBILITY_SENSE
94+
├ ObjectiveFunctionType: MOI.ScalarAffineFunction{Float64}
95+
├ NumberOfVariables: 0
96+
└ NumberOfConstraints: 0
6997
```
7098

7199
## Write to file
@@ -75,11 +103,9 @@ use:
75103
```jldoctest fileformats
76104
julia> src = MOI.Utilities.Model{Float64}();
77105
78-
julia> MOI.add_variable(src)
79-
MOI.VariableIndex(1)
106+
julia> MOI.add_variable(src);
80107
81-
julia> dest = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_MOF)
82-
A MathOptFormat Model
108+
julia> dest = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_MOF);
83109
84110
julia> MOI.copy_to(dest, src)
85111
MathOptInterface.Utilities.IndexMap with 1 entry:
@@ -110,8 +136,7 @@ julia> print(read("file.mof.json", String))
110136

111137
To read a MathOptFormat file, use:
112138
```jldoctest fileformats
113-
julia> dest = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_MOF)
114-
A MathOptFormat Model
139+
julia> dest = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_MOF);
115140
116141
julia> MOI.read_from_file(dest, "file.mof.json")
117142
@@ -131,29 +156,14 @@ guess the format from the file extension. For example:
131156
```jldoctest fileformats
132157
julia> src = MOI.Utilities.Model{Float64}();
133158
134-
julia> dest = MOI.FileFormats.Model(filename = "file.cbf.gz")
135-
A Conic Benchmark Format (CBF) model
159+
julia> dest = MOI.FileFormats.Model(filename = "file.cbf.gz");
136160
137161
julia> MOI.copy_to(dest, src)
138162
MathOptInterface.Utilities.IndexMap()
139163
140164
julia> MOI.write_to_file(dest, "file.cbf.gz")
141165
142-
julia> src_2 = MOI.FileFormats.Model(filename = "file.cbf.gz")
143-
A Conic Benchmark Format (CBF) model
144-
145-
julia> src = MOI.Utilities.Model{Float64}();
146-
147-
julia> dest = MOI.FileFormats.Model(filename = "file.cbf.gz")
148-
A Conic Benchmark Format (CBF) model
149-
150-
julia> MOI.copy_to(dest, src)
151-
MathOptInterface.Utilities.IndexMap()
152-
153-
julia> MOI.write_to_file(dest, "file.cbf.gz")
154-
155-
julia> src_2 = MOI.FileFormats.Model(filename = "file.cbf.gz")
156-
A Conic Benchmark Format (CBF) model
166+
julia> src_2 = MOI.FileFormats.Model(filename = "file.cbf.gz");
157167
158168
julia> MOI.read_from_file(src_2, "file.cbf.gz")
159169
@@ -167,14 +177,22 @@ filename.
167177
In some cases `src` may contain constraints that are not supported by the file
168178
format (for example, the CBF format supports integer variables but not binary).
169179
If so, copy `src` to a bridged model using [`Bridges.full_bridge_optimizer`](@ref):
170-
```julia
171-
src = MOI.Utilities.Model{Float64}()
172-
x = MOI.add_variable(model)
173-
MOI.add_constraint(model, x, MOI.ZeroOne())
174-
dest = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_CBF)
175-
bridged = MOI.Bridges.full_bridge_optimizer(dest, Float64)
176-
MOI.copy_to(bridged, src)
177-
MOI.write_to_file(dest, "my_model.cbf")
180+
```jldoctest
181+
julia> src = MOI.Utilities.Model{Float64}();
182+
183+
julia> x = MOI.add_variable(src);
184+
185+
julia> MOI.add_constraint(src, x, MOI.ZeroOne());
186+
187+
julia> dest = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_CBF);
188+
189+
julia> bridged = MOI.Bridges.full_bridge_optimizer(dest, Float64);
190+
191+
julia> MOI.copy_to(bridged, src);
192+
193+
julia> MOI.write_to_file(dest, "my_model.cbf")
194+
195+
julia> rm("my_model.cbf") # Clean up after ourselves.
178196
```
179197
!!! note
180198
Even after bridging, it may still not be possible to write the model to file
@@ -188,8 +206,7 @@ read and write directly from `IO` streams using `Base.write` and `Base.read!`:
188206
```jldoctest
189207
julia> src = MOI.Utilities.Model{Float64}();
190208
191-
julia> dest = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_MPS)
192-
A Mathematical Programming System (MPS) model
209+
julia> dest = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_MPS);
193210
194211
julia> MOI.copy_to(dest, src)
195212
MathOptInterface.Utilities.IndexMap()
@@ -200,8 +217,7 @@ julia> write(io, dest)
200217
201218
julia> seekstart(io);
202219
203-
julia> src_2 = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_MPS)
204-
A Mathematical Programming System (MPS) model
220+
julia> src_2 = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_MPS);
205221
206222
julia> read!(io, src_2);
207223
```
@@ -218,14 +234,12 @@ pass the `use_nlp_block = false` keyword argument to the `Model` constructor:
218234
julia> model = MOI.FileFormats.Model(;
219235
format = MOI.FileFormats.FORMAT_MOF,
220236
use_nlp_block = false,
221-
)
222-
A MathOptFormat Model
237+
);
223238
224239
julia> model = MOI.FileFormats.Model(;
225240
format = MOI.FileFormats.FORMAT_NL,
226241
use_nlp_block = false,
227-
)
228-
An AMPL (.nl) model
242+
);
229243
```
230244

231245
## Validating MOF files

docs/src/tutorials/implementing.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,10 @@ interface.
298298
isn't clear, let us know and we will improve the docstrings.
299299
It is also very helpful to look at an existing wrapper for a similar solver.
300300

301-
You should also implement `Base.show(::IO, ::Optimizer)` to print a nice string
302-
when someone prints your model. For example
301+
You should also implement `Base.summary(::IO, ::Optimizer)` to print a nice
302+
string when someone shows your model. For example
303303
```julia
304-
function Base.show(io::IO, model::Optimizer)
304+
function Base.summary(io::IO, model::Optimizer)
305305
return print(io, "NewSolver with the pointer $(model.ptr)")
306306
end
307307
```

src/FileFormats/CBF/CBF.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Create an empty instance of `FileFormats.CBF.Model`.
6161
"""
6262
Model(; kwargs...) = Model{Float64}()
6363

64-
Base.show(io::IO, ::Model) = print(io, "A Conic Benchmark Format (CBF) model")
64+
Base.summary(io::IO, ::Model) = print(io, "MOI.FileFormats.CBF.Model")
6565

6666
include("read.jl")
6767
include("write.jl")

src/FileFormats/LP/LP.jl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,7 @@ function Model(; maximum_length::Int = 255, warn::Bool = false)
9191
return model
9292
end
9393

94-
function Base.show(io::IO, ::Model)
95-
print(io, "A .LP-file model")
96-
return
97-
end
94+
Base.summary(io::IO, ::Model) = print(io, "MOI.FileFormats.LP.Model")
9895

9996
# ==============================================================================
10097
#

src/FileFormats/MOF/MOF.jl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,7 @@ function Model(;
157157
return model
158158
end
159159

160-
function Base.show(io::IO, ::Model)
161-
print(io, "A MathOptFormat Model")
162-
return
163-
end
160+
Base.summary(io::IO, ::Model) = print(io, "MOI.FileFormats.MOF.Model")
164161

165162
include("read.jl")
166163
include("write.jl")

src/FileFormats/MPS/MPS.jl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,7 @@ function Model(;
142142
return model
143143
end
144144

145-
function Base.show(io::IO, ::Model)
146-
print(io, "A Mathematical Programming System (MPS) model")
147-
return
148-
end
145+
Base.summary(io::IO, ::Model) = print(io, "MOI.FileFormats.MPS.Model")
149146

150147
@enum(VType, VTYPE_CONTINUOUS, VTYPE_INTEGER, VTYPE_BINARY)
151148

src/FileFormats/NL/NL.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ mutable struct Model <: MOI.ModelLike
164164
end
165165
end
166166

167-
Base.show(io::IO, ::Model) = print(io, "An AMPL (.nl) model")
167+
Base.summary(io::IO, ::Model) = print(io, "MOI.FileFormats.NL.Model")
168168

169169
MOI.get(model::Model, ::MOI.SolverName) = "AmplNLWriter"
170170

src/FileFormats/SDPA/SDPA.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,7 @@ function Model(; number_type::Type = Float64)
127127
return model
128128
end
129129

130-
function Base.show(io::IO, ::Model)
131-
return print(io, "A SemiDefinite Programming Algorithm Format (SDPA) model")
132-
end
130+
Base.summary(io::IO, ::Model) = print(io, "MOI.FileFormats.SDPA.Model")
133131

134132
# ==============================================================================
135133
#

src/MathOptInterface.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ end
3030

3131
function Base.show(io::IO, model::ModelLike)
3232
offset = Base.get(io, :offset, "")
33-
Utilities.print_with_acronym(io, "$(typeof(model))\n")
33+
Utilities.print_with_acronym(io, summary(model))
34+
println(io)
3435
# ObjectiveSense
3536
sense = _try_get(model, ObjectiveSense(), "unknown")
3637
println(io, offset, "├ ObjectiveSense: $sense")

test/FileFormats/CBF/CBF.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function _test_read(filename::String, model_string::String)
8787
end
8888

8989
function test_show()
90-
@test sprint(show, CBF.Model()) == "A Conic Benchmark Format (CBF) model"
90+
@test sprint(summary, CBF.Model()) == "MOI.FileFormats.CBF.Model"
9191
return
9292
end
9393

0 commit comments

Comments
 (0)