Skip to content

Commit

Permalink
Merge 67d8fc0 into aff74fb
Browse files Browse the repository at this point in the history
  • Loading branch information
dourouc05 committed May 6, 2020
2 parents aff74fb + 67d8fc0 commit d548bea
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
20 changes: 19 additions & 1 deletion 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...)
return parse_constraint(_error, Val(args[1]), args[2:end]...)
end

function parse_constraint(_error::Function, sense::Val, F...)
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 Expand Up @@ -237,7 +246,16 @@ function parse_constraint_head(_error::Function, ::Val, args...)
_unknown_constraint_expr(_error)
end
function parse_constraint(_error::Function, args...)
_unknown_constraint_expr(_error)
# Define this as the last fallback: either this is a function call that may
# be overridden by extensions, or a syntax that is not recognised.
# Multiple dispatch does not work here, due to ambiguity with:
# parse_constraint(_error::Function, lb, lsign::Symbol, aff, rsign::Symbol, ub)
if args[1] isa Symbol
(sense, vectorized) = _check_vectorized(args[1])
vectorized, parse_one_operator_constraint(_error, vectorized, Val(sense), args[2:end]...)...
else
_unknown_constraint_expr(_error)
end
end

# Generic fallback.
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_one_operator_constraint(_error::Function, ::Bool, ::Val{:f}, x)
return :(), :(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

0 comments on commit d548bea

Please sign in to comment.