diff --git a/src/function_data.jl b/src/function_data.jl index 3d7fa8645..874be0098 100644 --- a/src/function_data.jl +++ b/src/function_data.jl @@ -41,6 +41,11 @@ function transform_array_for_hdf( return transform_array_for_hdf(transfd_data) end +function Base.show(io::IO, ::MIME"text/plain", fd::LinearFunctionData) + get(io, :compact, false)::Bool || print(io, "$(typeof(fd)) representing function ") + print(io, "f(x) = $(fd.proportional_term) x + $(fd.constant_term)") +end + """ Structure to represent the underlying data of quadratic functions. Principally used for the representation of cost functions @@ -94,6 +99,14 @@ function _validate_piecewise_x(x_coords::Vector) end end +function Base.show(io::IO, ::MIME"text/plain", fd::QuadraticFunctionData) + get(io, :compact, false)::Bool || print(io, "$(typeof(fd)) representing function ") + print( + io, + "f(x) = $(fd.quadratic_term) x^2 + $(fd.proportional_term) x + $(fd.constant_term)", + ) +end + """ Structure to represent piecewise linear data as a series of points: two points define one segment, three points define two segments, etc. The curve starts at the first point given, @@ -191,6 +204,18 @@ function transform_array_for_hdf( return transform_array_for_hdf(transfd_data) end +function Base.show(io::IO, ::MIME"text/plain", fd::PiecewiseLinearData) + if get(io, :compact, false)::Bool + print(io, "piecewise linear ") + else + print(io, "$(typeof(fd)) representing piecewise linear function ") + end + print(io, "y = f(x) connecting points:") + for point in fd.points + print(io, "\n $point") + end +end + """ Structure to represent a step function as a series of endpoint x-coordinates and segment y-coordinates: two x-coordinates and one y-coordinate defines a single segment, three @@ -274,6 +299,15 @@ function transform_array_for_hdf( return transform_array_for_hdf(transfd_data) end +function Base.show(io::IO, ::MIME"text/plain", fd::PiecewiseStepData) + get(io, :compact, false)::Bool || + print(io, "$(typeof(fd)) representing step (piecewise constant) function ") + print(io, "f(x) =") + for (y, x1, x2) in zip(fd.y_coords, fd.x_coords[1:(end - 1)], fd.x_coords[2:end]) + print(io, "\n $y for x in [$x1, $x2)") + end +end + """ Calculates the x-length of each segment of a piecewise curve. """ diff --git a/test/test_function_data.jl b/test/test_function_data.jl index 9f93e1c1b..d58b2cb00 100644 --- a/test/test_function_data.jl +++ b/test/test_function_data.jl @@ -160,7 +160,7 @@ end end end -@testset "Test equality with NaN" begin +@testset "Test FunctionData equality with NaN" begin examples_1 = get_more_test_function_data() examples_2 = get_more_test_function_data() @@ -179,3 +179,33 @@ end @test hash(examples_1[(my_type, false)]) != hash(examples_2[(my_type, true)]) end end + +@testset "Test FunctionData printing" begin + repr_answers = [ + "InfrastructureSystems.LinearFunctionData(5.0, 1.0)", + "InfrastructureSystems.QuadraticFunctionData(2.0, 3.0, 4.0)", + "InfrastructureSystems.PiecewiseLinearData(@NamedTuple{x::Float64, y::Float64}[(x = 1.0, y = 1.0), (x = 3.0, y = 5.0), (x = 5.0, y = 10.0)])", + "InfrastructureSystems.PiecewiseStepData([1.0, 3.0, 5.0], [2.0, 2.5])", + ] + plain_answers = [ + "InfrastructureSystems.LinearFunctionData representing function f(x) = 5.0 x + 1.0", + "InfrastructureSystems.QuadraticFunctionData representing function f(x) = 2.0 x^2 + 3.0 x + 4.0", + "InfrastructureSystems.PiecewiseLinearData representing piecewise linear function y = f(x) connecting points:\n (x = 1.0, y = 1.0)\n (x = 3.0, y = 5.0)\n (x = 5.0, y = 10.0)", + "InfrastructureSystems.PiecewiseStepData representing step (piecewise constant) function f(x) =\n 2.0 for x in [1.0, 3.0)\n 2.5 for x in [3.0, 5.0)", + ] + compact_plain_answers = [ + "f(x) = 5.0 x + 1.0", + "f(x) = 2.0 x^2 + 3.0 x + 4.0", + "piecewise linear y = f(x) connecting points:\n (x = 1.0, y = 1.0)\n (x = 3.0, y = 5.0)\n (x = 5.0, y = 10.0)", + "f(x) =\n 2.0 for x in [1.0, 3.0)\n 2.5 for x in [3.0, 5.0)", + ] + + for (fd, repr_ans, plain_ans, compact_plain_ans) in + zip(get_test_function_data(), repr_answers, plain_answers, compact_plain_answers) + @test sprint(show, fd) == repr(fd) == repr_ans + @test sprint(show, "text/plain", fd) == + sprint(show, "text/plain", fd; context = :compact => false) == plain_ans + @test sprint(show, "text/plain", fd; context = :compact => true) == + compact_plain_ans + end +end diff --git a/test/test_printing.jl b/test/test_printing.jl index 80e90ee1f..c9caf7920 100644 --- a/test/test_printing.jl +++ b/test/test_printing.jl @@ -6,7 +6,7 @@ ) io = IOBuffer() for mime in ("text/plain", "text/html") - show(io, "text/plain", sys) + show(io, mime, sys) text = String(take!(io)) @test occursin("TestComponent", text) @test occursin("time_series_type", text)