diff --git a/src/modifications.jl b/src/modifications.jl index f82645da5f..b9ed5eb15a 100644 --- a/src/modifications.jl +++ b/src/modifications.jl @@ -1,6 +1,6 @@ """ struct ModifyConstraintNotAllowed{F<:AbstractFunction, S<:AbstractSet, - C<:AbstractFunctionModification} <: NotAllowedError + C<:AbstractFunctionModification} <: NotAllowedError constraint_index::ConstraintIndex{F, S} change::C message::String @@ -10,15 +10,18 @@ An error indicating that the constraint modification `change` cannot be applied to the constraint of index `ci`. """ struct ModifyConstraintNotAllowed{F<:AbstractFunction, S<:AbstractSet, - C<:AbstractFunctionModification} <: NotAllowedError + C<:AbstractFunctionModification} <: NotAllowedError constraint_index::ConstraintIndex{F, S} change::C message::String end -function ModifyConstraintNotAllowed(ci::ConstraintIndex{F, S}, - change::AbstractFunctionModification) where {F<:AbstractFunction, S<:AbstractSet} - ModifyConstraintNotAllowed{F, S, typeof(change)}(ci, change, "") +function ModifyConstraintNotAllowed( + ci::ConstraintIndex{F, S}, + change::AbstractFunctionModification, + message="") where {F<:AbstractFunction, S<:AbstractSet} + return ModifyConstraintNotAllowed{F, S, typeof(change)}(ci, change, message) end +throw_modify_not_allowed(ci::ConstraintIndex, args...) = throw(ModifyConstraintNotAllowed(ci, args...)) operation_name(err::ModifyConstraintNotAllowed{F, S}) where {F, S} = "Modifying the constraints $(err.constraint_index) with $(err.change)" @@ -36,8 +39,9 @@ struct ModifyObjectiveNotAllowed{C<:AbstractFunctionModification} <: NotAllowedE message::String end function ModifyObjectiveNotAllowed(change::AbstractFunctionModification) - ModifyObjectiveNotAllowed(change, "") + return ModifyObjectiveNotAllowed(change, "") end +throw_modify_not_allowed(::ObjectiveFunction, args...) = throw(ModifyObjectiveNotAllowed(args...)) operation_name(err::ModifyObjectiveNotAllowed) = "Modifying the objective function with $(err.change)" @@ -75,12 +79,12 @@ modify(model, ObjectiveFunction{ScalarAffineFunction{Float64}}(), ScalarConstant """ function modify end -function modify(model::ModelLike, ci::ConstraintIndex{F, S}, - change::AbstractFunctionModification) where {F, S} - throw(ModifyConstraintNotAllowed(ci, change)) +function modify(model::ModelLike, ci::ConstraintIndex, + change::AbstractFunctionModification) + throw_modify_not_allowed(ci, change) end -function modify(model::ModelLike, ::ObjectiveFunction, - change::AbstractFunctionModification) - throw(ModifyObjectiveNotAllowed(change)) +function modify(model::ModelLike, attr::ObjectiveFunction, + change::AbstractFunctionModification) + throw_modify_not_allowed(attr, change) end diff --git a/test/errors.jl b/test/errors.jl index 690ff279c0..fb008a11d4 100644 --- a/test/errors.jl +++ b/test/errors.jl @@ -1,3 +1,9 @@ +using Test +using MathOptInterface +const MOI = MathOptInterface + +include("dummy.jl") + @testset "Fallbacks for `set` methods" begin model = DummyModel() @@ -182,6 +188,32 @@ MOI.set(model, MOI.ConstraintSet(), ci, MOI.GreaterThan(1.0)) end end + + @testset "ModifyNotAllowed" begin + change = MOI.ScalarConstantChange(1.0) + @testset "Constraint" begin + err = MOI.ModifyConstraintNotAllowed(ci, change) + @test_throws err MOI.modify(model, ci, change) + @test sprint(showerror, err) == "MathOptInterface.ModifyConstraintNotAllowed{MathOptInterface.SingleVariable,MathOptInterface.EqualTo{Float64},MathOptInterface.ScalarConstantChange{Float64}}:" * + " Modifying the constraints MathOptInterface.ConstraintIndex{MathOptInterface.SingleVariable,MathOptInterface.EqualTo{Float64}}(1)" * + " with MathOptInterface.ScalarConstantChange{Float64}(1.0) cannot" * + " be performed. You may want to use a `CachingOptimizer` in" * + " `AUTOMATIC` mode or you may need to call `reset_optimizer`" * + " before doing this operation if the `CachingOptimizer` is in" * + " `MANUAL` mode." + end + @testset "Objective" begin + attr = MOI.ObjectiveFunction{MOI.SingleVariable}() + err = MOI.ModifyObjectiveNotAllowed(change) + @test_throws err MOI.modify(model, attr, change) + @test sprint(showerror, err) == "MathOptInterface.ModifyObjectiveNotAllowed{MathOptInterface.ScalarConstantChange{Float64}}:" * + " Modifying the objective function with MathOptInterface.ScalarConstantChange{Float64}(1.0)" * + " cannot be performed. You may want to use a `CachingOptimizer`" * + " in `AUTOMATIC` mode or you may need to call `reset_optimizer`" * + " before doing this operation if the `CachingOptimizer` is in" * + " `MANUAL` mode." + end + end end @testset "Error messages" begin