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

Add extension points for more generic @constraint syntax #2051

Merged
merged 9 commits into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 9 additions & 0 deletions src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@ function parse_constraint(_error::Function, sense::Symbol, lhs, rhs)
(sense, vectorized) = _check_vectorized(sense)
vectorized, parse_one_operator_constraint(_error, vectorized, Val(sense), lhs, rhs)...
end
function parse_constraint(_error::Function, ::Val{:call}, args...)
dourouc05 marked this conversation as resolved.
Show resolved Hide resolved
return parse_constraint(_error, Val(args[1]), args[2:end]...)
end

function parse_constraint(_error::Function, sense::Val, F...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these two methods needed ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, they are not needed, I've just removed them.

sense_symbol = typeof(sense).parameters[1]
(sense, vectorized) = _check_vectorized(sense_symbol)
vectorized, parse_one_operator_constraint(_error, vectorized, Val(sense), F...)...
end

function parse_ternary_constraint(_error::Function, vectorized::Bool, lb, ::Union{Val{:(<=)}, Val{:(≤)}}, aff, rsign::Union{Val{:(<=)}, Val{:(≤)}}, ub)
newaff, parseaff = _MA.rewrite(aff)
Expand Down
16 changes: 15 additions & 1 deletion test/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,20 @@ function custom_expression_test(ModelType::Type{<:JuMP.AbstractModel})
end
end

function JuMP.parse_constraint_head(_error::Function, ::Val{:call}, ::Val{:f}, x)
return false, :(), :(build_constraint($_error, $(esc(x)), $(esc(CustomType()))))
end
function custom_function_test(ModelType::Type{<:JuMP.AbstractModel})
@testset "Custom function" begin
model = ModelType()
@variable(model, x)
@constraint(model, con_ref, f(x))
con = JuMP.constraint_object(con_ref)
@test jump_function(con) == x
@test moi_set(con) isa CustomSet
end
end

function macros_test(ModelType::Type{<:JuMP.AbstractModel}, VariableRefType::Type{<:JuMP.AbstractVariableRef})
@testset "build_constraint on variable" begin
m = ModelType()
Expand Down Expand Up @@ -357,8 +371,8 @@ function macros_test(ModelType::Type{<:JuMP.AbstractModel}, VariableRefType::Typ
end

build_constraint_keyword_test(ModelType)

custom_expression_test(ModelType)
custom_function_test(ModelType)
end

@testset "Macros for JuMP.Model" begin
Expand Down