Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
12 changes: 6 additions & 6 deletions src/FileFormats/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
create_unique_names(
model::MOI.ModelLike;
warn::Bool = false,
replacements::Vector{Function} = Function[],
replacements = (),
)

Rename variables in `model` to ensure that all variables and constraints have
a unique name. In addition, loop through `replacements` and replace names with
`f(name)`.
`f(name)`. The names are inferred to be `String`s if `replacements` is a tuple.

If `warn`, print a warning if a variable or constraint is renamed.
"""
function create_unique_names(
model::MOI.ModelLike;
warn::Bool = false,
replacements::Vector{Function} = Function[],
replacements = (),
)
create_unique_variable_names(model, warn, replacements)
create_unique_constraint_names(model, warn, replacements)
Expand Down Expand Up @@ -74,7 +74,7 @@ function create_generic_constraint_names(
return i
end

function _replace(s::String, replacements::Vector{Function})
function _replace(s::String, replacements)
for f in replacements
s = f(s)
end
Expand Down Expand Up @@ -170,7 +170,7 @@ end
function create_unique_constraint_names(
model::MOI.ModelLike,
warn::Bool,
replacements::Vector{Function},
replacements,
)
original_names = Set{String}()
for (F, S) in MOI.get(model, MOI.ListOfConstraintTypesPresent())
Expand All @@ -194,7 +194,7 @@ end
function create_unique_variable_names(
model::MOI.ModelLike,
warn::Bool,
replacements::Vector{Function},
replacements,
)
variables = MOI.get(model, MOI.ListOfVariableIndices())
# This is a list of all of the names currently in the model. We're going to
Expand Down
15 changes: 15 additions & 0 deletions test/FileFormats/test_FileFormats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,21 @@ function test_unique_names()
return
end

function test_unique_names_replacements()
replace_spaces = s -> replace(s, " " => "_")
@test (@inferred MOI.FileFormats._replace("a b", ())) == "a b"
@test (@inferred MOI.FileFormats._replace("a b", (replace_spaces,))) ==
"a_b"
# A vector of functions is still supported, without inference.
@test MOI.FileFormats._replace("a b", Function[replace_spaces]) == "a_b"
model = MOI.Utilities.Model{Float64}()
x = MOI.add_variables(model, 2)
MOI.set.(model, MOI.VariableName(), x, ["x a", "x_a"])
MOI.FileFormats.create_unique_names(model; replacements = (replace_spaces,))
@test MOI.get.(model, MOI.VariableName(), x) == ["x_a", "x_a_1"]
return
end

end

TestFileFormats.runtests()
11 changes: 11 additions & 0 deletions test/JuliaC/MyApp/src/MyApp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ function @main(args::Vector{String})::Cint
end
# The following commands are not traced by `test_juliac.jl`: the compiled
# binary must run them with the methods resolved at compile time.
if head == "--names"
return run_names(args)
end
return 1 # Incorrect head
end

Expand Down Expand Up @@ -54,4 +57,12 @@ function run_mathoptformat(args::Vector{String})::Cint
return _write(model, args)
end

# A second variable named `x`, which is renamed.
function run_names(args::Vector{String})::Cint
model, _ = _model()
y = MOI.add_variable(model)
MOI.set(model, MOI.VariableName(), y, "x")
return _write(model, args)
end

end # module MyApp
8 changes: 8 additions & 0 deletions test/JuliaC/test_juliac.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ function test_mathoptformat(output_dir)
return
end

function test_names(output_dir)
compiled, reference = _run(output_dir, "--names")
@test compiled == reference
object = JSON.parse(compiled)
@test object["variables"] == [Dict("name" => "x"), Dict("name" => "x_1")]
return
end

end # module TestJuliaC

TestJuliaC.runtests()
Loading