Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename _error to error_fn #3600

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 8 additions & 8 deletions docs/src/developers/extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,15 @@ julia> const MutableArithmetics = JuMP._MA;
julia> model = Model(); @variable(model, x);

julia> function JuMP.parse_constraint_head(
_error::Function,
error_fn::Function,
::Val{:≔},
lhs,
rhs,
)
println("Rewriting ≔ as ==")
new_lhs, parse_code = MutableArithmetics.rewrite(lhs)
build_code = :(
build_constraint($(_error), $(new_lhs), MOI.EqualTo($(rhs)))
build_constraint($(error_fn), $(new_lhs), MOI.EqualTo($(rhs)))
)
return false, parse_code, build_code
end
Expand All @@ -227,7 +227,7 @@ julia> const MutableArithmetics = JuMP._MA;
julia> model = Model(); @variable(model, x);

julia> function JuMP.parse_constraint_call(
_error::Function,
error_fn::Function,
is_vectorized::Bool,
::Val{:my_equal_to},
lhs,
Expand All @@ -236,10 +236,10 @@ julia> function JuMP.parse_constraint_call(
println("Rewriting my_equal_to to ==")
new_lhs, parse_code = MutableArithmetics.rewrite(lhs)
build_code = if is_vectorized
:(build_constraint($(_error), $(new_lhs), MOI.EqualTo($(rhs)))
:(build_constraint($(error_fn), $(new_lhs), MOI.EqualTo($(rhs)))
)
else
:(build_constraint.($(_error), $(new_lhs), MOI.EqualTo($(rhs))))
:(build_constraint.($(error_fn), $(new_lhs), MOI.EqualTo($(rhs))))
end
return parse_code, build_code
end
Expand Down Expand Up @@ -281,14 +281,14 @@ julia> model = Model(); @variable(model, x);
julia> struct MyConstrType end

julia> function JuMP.build_constraint(
_error::Function,
error_fn::Function,
f::JuMP.GenericAffExpr,
set::MOI.EqualTo,
extra::Type{MyConstrType};
d = 0,
)
new_set = MOI.LessThan(set.value + d)
return JuMP.build_constraint(_error, f, new_set)
return JuMP.build_constraint(error_fn, f, new_set)
end

julia> @constraint(model, my_con, x == 0, MyConstrType, d = 2)
Expand Down Expand Up @@ -322,7 +322,7 @@ julia> struct MyConstraint{S} <: AbstractConstraint
end

julia> function JuMP.build_constraint(
_error::Function,
error_fn::Function,
f::AffExpr,
set::MOI.AbstractScalarSet,
extra::MyTag,
Expand Down
27 changes: 16 additions & 11 deletions src/Containers/macro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ function _expr_is_splat(expr)
return false
end

function _parse_index_sets(_error::Function, index_vars, index_sets, arg::Expr)
function _parse_index_sets(
error_fn::Function,
index_vars,
index_sets,
arg::Expr,
)
index_var, index_set = gensym(), esc(arg)
if isexpr(arg, :kw, 2) || isexpr(arg, :(=), 2)
# Handle [i=S] and x[i=S]
Expand All @@ -77,7 +82,7 @@ function _parse_index_sets(_error::Function, index_vars, index_sets, arg::Expr)
index_var, index_set = arg.args[2], esc(arg.args[3])
end
if index_var in index_vars
_error(
error_fn(
"The index $(index_var) appears more than once. The " *
"index associated with each set must be unique.",
)
Expand Down Expand Up @@ -108,7 +113,7 @@ Takes an `Expr` that specifies the container, e.g.,
3. `condition`: Expr containing any conditional imposed on indexing, or `:()`
if none is present
"""
function _parse_ref_sets(_error::Function, expr::Expr)
function _parse_ref_sets(error_fn::Function, expr::Expr)
c = copy(expr)
index_vars, index_sets, condition = Any[], Any[], :()
# `:(t[i, j; k])` is a `:ref`, while `:(t[i; j])` is a `:typed_vcat`. In
Expand All @@ -120,7 +125,7 @@ function _parse_ref_sets(_error::Function, expr::Expr)
# An expression like `t[i; k]` or `[i; k]`. The filtering condition is
# the second argument.
if length(c.args) > 2
_error(
error_fn(
"Unsupported syntax $c: There can be at most one filtering " *
"condition, which is separated from the indices by a single " *
"`;`.",
Expand All @@ -136,7 +141,7 @@ function _parse_ref_sets(_error::Function, expr::Expr)
if isexpr(c.args[1], :parameters)
parameters = popfirst!(c.args)
if length(parameters.args) != 1
_error(
error_fn(
"Unsupported syntax $c: There can be at most one " *
"filtering condition, which is separated from the " *
"indices by a single `;`.",
Expand All @@ -146,7 +151,7 @@ function _parse_ref_sets(_error::Function, expr::Expr)
end
end
for arg in c.args
_parse_index_sets(_error, index_vars, index_sets, arg)
_parse_index_sets(error_fn, index_vars, index_sets, arg)
end
return index_vars, index_sets, condition
end
Expand Down Expand Up @@ -180,7 +185,7 @@ function _has_dependent_sets(index_vars::Vector{Any}, index_sets::Vector{Any})
end

"""
build_ref_sets(_error::Function, expr)
build_ref_sets(error_fn::Function, expr)

Helper function for macros to construct container objects.

Expand All @@ -190,7 +195,7 @@ Helper function for macros to construct container objects.

## Arguments

* `_error`: a function that takes a `String` and throws an error, potentially
* `error_fn`: a function that takes a `String` and throws an error, potentially
annotating the input string with extra information such as from which macro
it was thrown from. Use `error` if you do not want a modified error message.
* `expr`: an `Expr` that specifies the container, e.g.,
Expand All @@ -208,10 +213,10 @@ Helper function for macros to construct container objects.

See [`container_code`](@ref) for a worked example.
"""
function build_ref_sets(_error::Function, expr)
index_vars, index_sets, condition = _parse_ref_sets(_error, expr)
function build_ref_sets(error_fn::Function, expr)
index_vars, index_sets, condition = _parse_ref_sets(error_fn, expr)
if any(_expr_is_splat, index_sets)
_error(
error_fn(
"cannot use splatting operator `...` in the definition of an " *
"index set.",
)
Expand Down
2 changes: 1 addition & 1 deletion src/constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ In order to automatically add the `CustomBridge` to any model to
which an `F`-in-`CustomSet` is added, add the following method:
```julia
function JuMP.build_constraint(
_error::Function,
error_fn::Function,
func::AbstractJuMPScalar,
set::CustomSet,
)
Expand Down
30 changes: 16 additions & 14 deletions src/indicator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# JuMP can be extended.

function _build_indicator_constraint(
_error::Function,
error_fn::Function,
variable::AbstractVariableRef,
constraint::ScalarConstraint,
::Type{MOI.Indicator{A}},
Expand All @@ -17,12 +17,12 @@ function _build_indicator_constraint(
end

function _build_indicator_constraint(
_error::Function,
error_fn::Function,
lhs::F,
::ScalarConstraint,
::Type{<:MOI.Indicator},
) where {F}
return _error(
return error_fn(
"unable to build indicator constraint with the left-hand side term " *
"`($lhs)::$F`. The left-hand side must be a binary decision variable.",
)
Expand All @@ -32,10 +32,10 @@ function _indicator_variable_set(::Function, variable::Symbol)
return variable, MOI.Indicator{MOI.ACTIVATE_ON_ONE}
end

function _indicator_variable_set(_error::Function, expr::Expr)
function _indicator_variable_set(error_fn::Function, expr::Expr)
if expr.args[1] == :¬ || expr.args[1] == :!
if length(expr.args) != 2
_error(
error_fn(
"Invalid binary variable expression `$(expr)` for indicator constraint.",
)
end
Expand All @@ -45,35 +45,37 @@ function _indicator_variable_set(_error::Function, expr::Expr)
end
end

function parse_constraint_head(_error::Function, ::Val{:(-->)}, lhs, rhs)
code, call = parse_constraint_call(_error, false, Val(:(=>)), lhs, rhs)
function parse_constraint_head(error_fn::Function, ::Val{:(-->)}, lhs, rhs)
code, call = parse_constraint_call(error_fn, false, Val(:(=>)), lhs, rhs)
return false, code, call
end

function parse_constraint_call(
_error::Function,
error_fn::Function,
vectorized::Bool,
::Union{Val{:(=>)},Val{:⇒}},
lhs,
rhs,
)
variable, S = _indicator_variable_set(_error, lhs)
variable, S = _indicator_variable_set(error_fn, lhs)
if !isexpr(rhs, :braces) || length(rhs.args) != 1
_error(
error_fn(
"Invalid right-hand side `$(rhs)` of indicator constraint. Expected constraint surrounded by `{` and `}`.",
)
end
rhs_vectorized, rhs_parsecode, rhs_build_call =
parse_constraint(_error, rhs.args[1])
parse_constraint(error_fn, rhs.args[1])
if vectorized != rhs_vectorized
_error("Inconsistent use of `.` in symbols to indicate vectorization.")
error_fn(
"Inconsistent use of `.` in symbols to indicate vectorization.",
)
end
f, lhs_parse_code = _rewrite_expression(variable)
push!(rhs_parsecode.args, lhs_parse_code)
build_call = if vectorized
:(_build_indicator_constraint.($_error, $f, $rhs_build_call, $S))
:(_build_indicator_constraint.($error_fn, $f, $rhs_build_call, $S))
else
:(_build_indicator_constraint($_error, $f, $rhs_build_call, $S))
:(_build_indicator_constraint($error_fn, $f, $rhs_build_call, $S))
end
return rhs_parsecode, build_call
end
Expand Down
Loading
Loading