Skip to content

Commit

Permalink
removed _is_valid_input, since the usage could be misleading, and _as…
Browse files Browse the repository at this point in the history
…sert_valid_input covers all necessary usecases
  • Loading branch information
Uwe Hernandez Acosta committed Oct 5, 2023
1 parent 63c5019 commit 2a37a0f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 42 deletions.
42 changes: 5 additions & 37 deletions src/interfaces/setup_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ quantity depends on is kept constant.
_assert_valid_input(stp::AbstractComputationSetup, input)
```
which should throw and an exception subtyped from [`AbstractInvalidInputException`](@ref) if the `input` is not valid for the computation of the associated quantity (see [`_is_valid_input`](@ref) and [`_assert_valid_input`](@ref) for more details).
which should throw and an exception subtyped from [`AbstractInvalidInputException`](@ref) if the `input` is not valid for the computation of the associated quantity ([`_assert_valid_input`](@ref) for more details).
The default implementation does nothing, i.e. every input is valid by default. Provide a custom implementation if a different behavior is required.
## Actual computation
Expand Down Expand Up @@ -92,51 +92,19 @@ Interface function, which asserts that the given `input` is valid, and throws an
!!! note "default implementation"
The generic fallback uses the boolian value returned by `_is_valid_input(stp,input)` to check for validity,
i.e. if the returned value is `true` the assert does not trigger and nothing happens, but if the returned value is `false`,
an error with a generic error message will be thrown (see [`_is_valid_input`](@ref) for details).
To customize the error message, or use a custom assertion mechanism, just add your own implementation of
By default, every input is assumed to be valid. Therefore, this functions does nothing.
To customize this behavior add your own implementation of
```Julia
_assert_valid_input(stp::YourCustomSetup,input)
```
which should throw an [`InvalidInputError`](@ref) if the input is invalid.
Despite the presence of a custom `_assert_valid_input`, it is highly recommended to also implement `_is_valid_input` for `CustomSetup`, because it might be used outside of the assert function.
which should throw an exception, which is subtype of [`AbstractInvalidInputError`](@ref). One may also use the concrete implementation [`InvalidInputError`](@ref) if the input is invalid, instead of writing a custom exception type.
"""
@inline function _assert_valid_input(stp::AbstractComputationSetup, input)
return nothing
end

"""
_is_valid_input(stp::AbstractComputationSetup, input::Any)
Interface function, which returns true if the constraints of the `input` associated with the quantity of `stp` are met.
This function is called to validate the input of [`compute`](@ref) before calling [`_compute`](@ref).
!!! note "Default implementation"
Since no input validation is equivalent to every input being valid, this function returns `true` by default.
This behavior can be overwritten if actual validation is necessary.
An assert version of this function is given by [`_assert_valid_input`](@ref), which directly uses the output of this function.
"""
@inline function _is_valid_input(stp::AbstractComputationSetup, input)
try
_assert_valid_input(stp, input)
catch e
if isa(e, AbstractInvalidInputException)
return false
end
@warn "The function _assert_valid_input throws an Exception, which is not an InvalidInputError! The Exception thrown is: "
throw(e)
end
return true
end

"""
function _post_processing(stp::AbstractComputationSetup, input::Any, result::Any)
Expand All @@ -161,7 +129,7 @@ Interface function that returns the value of the associated quantity evaluated o
!!! note "unsafe implementation"
This function must be implemented for any subtype of [`AbstractComputationSetup`]. It should not do any input validation or post processing (see [`_is_valid_input`](@ref) and [`_post_processing`](@ref)), as those two are performed while calling
This function must be implemented for any subtype of [`AbstractComputationSetup`]. It should not do any input validation or post processing (see [`_assert_valid_input`](@ref) and [`_post_processing`](@ref)), as those two are performed while calling
the safe version of this function [`compute`](@ref).
"""
Expand Down
5 changes: 0 additions & 5 deletions test/interfaces/setup_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ QEDprocesses._post_processing(::TestSetupCustomPostProcessingFAIL, x, y) =

rnd_input = rand(RNG)
rnd_output = rand(RNG)
@test QEDprocesses._is_valid_input(stp, rnd_input)
@test QEDprocesses._post_processing(stp, rnd_input, rnd_output) == rnd_output
@test isapprox(
QEDprocesses._compute(stp, rnd_input),
Expand All @@ -103,8 +102,6 @@ QEDprocesses._post_processing(::TestSetupCustomPostProcessingFAIL, x, y) =
@testset "custom input validation" begin
stp = TestSetupCustomAssertValidInput()
rnd_input = rand(RNG)
@test QEDprocesses._is_valid_input(stp, _groundtruth_input_validation(rnd_input))
@test !QEDprocesses._is_valid_input(stp, !_groundtruth_input_validation(rnd_input))
@test QEDprocesses._assert_valid_input(stp, rnd_input) == nothing
@test_throws TestException QEDprocesses._assert_valid_input(
stp,
Expand Down Expand Up @@ -133,8 +130,6 @@ QEDprocesses._post_processing(::TestSetupCustomPostProcessingFAIL, x, y) =
rnd_input = rand(RNG)
rnd_output = rand(RNG)

@test QEDprocesses._is_valid_input(stp, _groundtruth_input_validation(rnd_input))
@test !QEDprocesses._is_valid_input(stp, !_groundtruth_input_validation(rnd_input))
@test_throws TestException() compute(stp, _transform_to_invalid(rnd_input))
@test isapprox(
QEDprocesses._post_processing(stp, rnd_input, rnd_output),
Expand Down

0 comments on commit 2a37a0f

Please sign in to comment.