Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 44 additions & 10 deletions src/attributes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -295,29 +295,63 @@ end
function get(model::ModelLike, attr::AnyAttribute, args...)
return get_fallback(model, attr, args...)
end
function get_fallback(model::ModelLike, attr::AnyAttribute, args...)

function get_fallback(
model::ModelLike,
attr::Union{AbstractModelAttribute,AbstractOptimizerAttribute},
)
return throw(
ArgumentError(
"ModelLike of type $(typeof(model)) does not support accessing the attribute $attr",
"$(typeof(model)) does not support getting the attribute $(attr).",
),
)
end

"""
get!(output, model::ModelLike, args...)
function get_fallback(
model::ModelLike,
attr::AbstractVariableAttribute,
::VariableIndex,
)
return throw(
ArgumentError(
"$(typeof(model)) does not support getting the attribute $(attr).",
),
)
end

An in-place version of `get`.
The signature matches that of `get` except that the the result is placed in the vector `output`.
"""
function get! end
function get!(output, model::ModelLike, attr::AnyAttribute, args...)
function get_fallback(
model::ModelLike,
attr::AbstractConstraintAttribute,
::ConstraintIndex,
)
return throw(
ArgumentError(
"$(typeof(model)) does not support getting the attribute $(attr).",
),
)
end

function get_fallback(::ModelLike, attr::AnyAttribute, args...)
return throw(
ArgumentError(
"ModelLike of type $(typeof(model)) does not support accessing the attribute $attr",
"Unable to get attribute $(attr): invalid arguments $(args).",
),
)
end

"""
get!(output, model::ModelLike, args...)

An in-place version of [`get`](@ref).

The signature matches that of [`get`](@ref) except that the the result is placed
in the vector `output`.
"""
function get!(output, model::ModelLike, attr::AnyAttribute, args...)
output .= get(model, attr, args...)
return
end

"""
set(optimizer::AbstractOptimizer, attr::AbstractOptimizerAttribute, value)

Expand Down
30 changes: 30 additions & 0 deletions test/attributes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,36 @@ function test_no_constraint_name()
)
end

function test_get_fallback()
model = DummyModelWithAdd()
x = MOI.add_variable(model)
c = MOI.add_constraint(model, x, MOI.EqualTo(0.0))
err = ArgumentError(
"$(typeof(model)) does not support getting the attribute " *
"$(MOI.SolveTimeSec()).",
)
@test_throws(err, MOI.get(model, MOI.SolveTimeSec()))
output = Ref{Cdouble}()
@test_throws(err, MOI.get!(output, model, MOI.SolveTimeSec()))
errv = ArgumentError(
"$(typeof(model)) does not support getting the attribute " *
"$(MOI.VariablePrimal()).",
)
@test_throws(errv, MOI.get(model, MOI.VariablePrimal(), x))
errc = ArgumentError(
"$(typeof(model)) does not support getting the attribute " *
"$(MOI.ConstraintPrimal()).",
)
@test_throws(errc, MOI.get(model, MOI.ConstraintPrimal(), c))
@test_throws(
ArgumentError(
"Unable to get attribute $(MOI.VariablePrimal()): invalid " *
"arguments $((c,)).",
),
MOI.get(model, MOI.VariablePrimal(), c),
)
end

function test_ConstraintBasisStatus_fallback()
model = DummyModelWithAdd()
c = MOI.ConstraintIndex{MOI.SingleVariable,MOI.EqualTo{Float64}}(1)
Expand Down