Skip to content

Commit 97e6008

Browse files
committed
add -> submit
1 parent a53659c commit 97e6008

File tree

2 files changed

+59
-68
lines changed

2 files changed

+59
-68
lines changed

src/attributes.jl

Lines changed: 53 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -75,24 +75,53 @@ operation_name(err::SetAttributeNotAllowed) = "Setting attribute $(err.attr)"
7575
message(err::SetAttributeNotAllowed) = err.message
7676

7777
"""
78-
struct AddAttributeNotAllowed{AttrType} <: NotAllowedError
79-
attr::AttrType
78+
AbstractSubmittable
79+
80+
Abstract supertype for objects that can be submitted to the model.
81+
"""
82+
abstract type AbstractSubmittable end
83+
84+
# This allows to use submittable in broadcast calls without the need to embed
85+
# it in a `Ref`.
86+
Base.broadcastable(sub::AbstractSubmittable) = Ref(sub)
87+
88+
"""
89+
struct UnsupportedSubmittable{SubmitType} <: UnsupportedError
90+
sub::SubmitType
91+
message::String
92+
end
93+
94+
An error indicating that the submittable `sub` is not supported by the model,
95+
i.e. that [`supports`](@ref) returns `false`.
96+
"""
97+
struct UnsupportedSubmittable{SubmitType<:AbstractSubmittable} <: UnsupportedError
98+
sub::SubmitType
99+
message::String
100+
end
101+
102+
"""
103+
struct SubmitNotAllowed{SubmitTyp<:AbstractSubmittable} <: NotAllowedError
104+
sub::SubmitType
80105
message::String # Human-friendly explanation why the attribute cannot be set
81106
end
82107
83-
An error indicating that the attribute `attr` is supported (see
108+
An error indicating that the submittable `sub` is supported (see
84109
[`supports`](@ref)) but cannot be added for some reason (see the error string).
85110
"""
86-
struct AddAttributeNotAllowed{AttrType<:AnyAttribute} <: NotAllowedError
87-
attr::AttrType
88-
message::String # Human-friendly explanation why the attribute cannot be set
111+
struct SubmitNotAllowed{SubmitType<:AbstractSubmittable} <: NotAllowedError
112+
sub::SubmitType
113+
message::String # Human-friendly explanation why the attribute cannot be set
89114
end
90-
AddAttributeNotAllowed(attr::AnyAttribute) = AddAttributeNotAllowed(attr, "")
115+
SubmitNotAllowed(sub::AbstractSubmittable) = SubmitNotAllowed(sub, "")
91116

92-
operation_name(err::AddAttributeNotAllowed) = "Adding attribute $(err.attr)"
93-
message(err::AddAttributeNotAllowed) = err.message
117+
operation_name(err::SubmitNotAllowed) = "Submitting $(err.sub)"
118+
message(err::SubmitNotAllowed) = err.message
94119

95120
"""
121+
supports(model::ModelLike, sub::AbstractSubmittable)::Bool
122+
123+
Return a `Bool` indicating whether `model` supports the submittable `sub`.
124+
96125
supports(model::ModelLike, attr::AbstractOptimizerAttribute)::Bool
97126
98127
Return a `Bool` indicating whether `model` supports the optimizer attribute
@@ -120,14 +149,15 @@ Return a `Bool` indicating whether `model` supports the constraint attribute
120149
`copy_to(model, src)` cannot be performed in case `attr` is in the
121150
[`ListOfConstraintAttributesSet`](@ref) of `src`.
122151
123-
For all four methods, if the attribute is only not supported in specific
152+
For all five methods, if the attribute is only not supported in specific
124153
circumstances, it should still return `true`.
125154
126155
Note that `supports` is only defined for attributes for which
127156
[`is_copyable`](@ref) returns `true` as other attributes do not appear in the
128157
list of attributes set obtained by `ListOf...AttributesSet`.
129158
"""
130159
function supports end
160+
supports(::ModelLike, ::AbstractSubmittable) = false
131161
function supports(::ModelLike, attr::Union{AbstractModelAttribute,
132162
AbstractOptimizerAttribute})
133163
if !is_copyable(attr)
@@ -346,61 +376,22 @@ function throw_set_error_fallback(model::ModelLike,
346376
end
347377

348378
"""
349-
add(optimizer::AbstractOptimizer, attr::AbstractOptimizerAttribute,
350-
value)::AttributeIndex{typeof(attr)}
351-
352-
Add `value` to the attribute `attr` of the optimizer `optimizer`.
379+
submit(optimizer::AbstractOptimizer, sub::AbstractSubmittable,
380+
value)::Union{Nothing, SubmittedIndex{typeof(sub)}}
353381
354-
add(model::ModelLike, attr::AbstractModelAttribute,
355-
value)::AttributeIndex{typeof(attr)}
382+
Submit `value` to the submittable `sub` of the optimizer `optimizer`.
356383
357-
Add `value` to the attribute `attr` of the model `model`.
358-
359-
add(model::ModelLike, attr::AbstractVariableAttribute, v::VariableIndex,
360-
value)::AttributeIndex{typeof(attr)}
361-
362-
Add `value` to the attribute `attr` of variable `v` in model `model`.
363-
364-
add(model::ModelLike, attr::AbstractVariableAttribute,
365-
v::Vector{VariableIndex},
366-
vector_of_values)::Vector{AttributeIndex{typeof(attr)}}
367-
368-
Add a value respectively to the attribute `attr` of each variable in the
369-
collection `v` in model `model`.
370-
371-
set(model::ModelLike, attr::AbstractConstraintAttribute, c::ConstraintIndex,
372-
value)
373-
374-
Add a value to the attribute `attr` of constraint `c` in model `model`.
375-
376-
set(model::ModelLike, attr::AbstractConstraintAttribute,
377-
c::Vector{ConstraintIndex{F,S}}, vector_of_values)
378-
379-
Add a value respectively to the attribute `attr` of each constraint in the
380-
collection `c` in model `model`.
381-
382-
An [`UnsupportedAttribute`](@ref) error is thrown if `model` does not support
383-
the attribute `attr` (see [`supports`](@ref)) and a
384-
[`SetAttributeNotAllowed`](@ref) error is thrown if it supports the attribute
385-
`attr` but it cannot be set.
386-
""" # TODO add an example once we have an attribute which can be added, e.g. Lazy constraint
387-
function add end
388-
# See note with get
389-
function add(model::ModelLike,
390-
attr::Union{AbstractVariableAttribute,
391-
AbstractConstraintAttribute},
392-
idxs::Vector, vector_of_values::Vector)
393-
if length(idxs) != length(vector_of_values)
394-
throw(DimensionMismatch("Number of indices ($(length(idxs))) does " *
395-
"not match the number of values " *
396-
"($(length(vector_of_values))) added to `$attr`."))
384+
An [`UnsupportedSubmittable`](@ref) error is thrown if `model` does not support
385+
the attribute `attr` (see [`supports`](@ref)) and a [`SubmitNotAllowed`](@ref)
386+
error is thrown if it supports the submittable `sub` but it cannot be submitted.
387+
""" # TODO add an example once we have an attribute which can be submitted, e.g. Lazy constraint
388+
function submit end
389+
function submit(model::ModelLike, sub::AbstractSubmittable, args...)
390+
if supports(model, sub)
391+
throw(SubmitNotAllowed(sub))
392+
else
393+
throw(UnsupportedSubmittable(sub))
397394
end
398-
return add.(model, attr, idxs, vector_of_values)
399-
end
400-
401-
function add(model::ModelLike, attr::AnyAttribute, args...)
402-
throw_set_error_fallback(model, attr, args...;
403-
error_if_supported = AddAttributeNotAllowed(attr))
404395
end
405396

406397
"""

src/indextypes.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,20 @@ end
3636
Base.hash(v::VariableIndex, h::UInt) = hash(v.value, h)
3737

3838
"""
39-
AttributeIndex{AttrType}
39+
SubmittedIndex{SubmitType}
4040
41-
A type-safe wrapper for `Int64` for use in referencing elements added for
42-
attribute of type `AttrType`.
41+
A type-safe wrapper for `Int64` for use in referencing elements submitted for
42+
submittable of type `SubmitType`.
4343
"""
44-
struct AttributeIndex{AttrType}
45-
attr::AttrType
44+
struct SubmittedIndex{SubmitType}
45+
attr::SubmitType
4646
value::Int64
4747
end
4848

4949
# No need to define isequal because the default matches our implementation of
5050
# hash.
5151

52-
const Index = Union{ConstraintIndex, VariableIndex, AttributeIndex}
52+
const Index = Union{ConstraintIndex, VariableIndex, SubmittedIndex}
5353

5454
"""
5555
struct InvalidIndex{IndexType<:Index} <: Exception

0 commit comments

Comments
 (0)