Skip to content

Commit

Permalink
wrap dispatch moved and FunctionModel docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
mauro-milella committed Oct 25, 2024
1 parent fed7981 commit 4fcd733
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 26 deletions.
21 changes: 20 additions & 1 deletion src/types/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ and enclose a *tree* of `AbstractModel`s (with `LeafModel`s at the leaves).
- `isopen(m::AbstractModel)::Bool`
- `apply(m::AbstractModel, i::AbstractInterpretation; kwargs...)`
# Utility functions (requiring a walk of the tree)
# Utility functions
- `outcometype(m::AbstractModel)`
- `outputtype(m::AbstractModel)`
- `info(m::AbstractModel, key)`
- `info!(m::AbstractModel, key)`
- `hasinfo(m::AbstractModel, key)`
- `wrap(m::AbstractModel)`
# Examples
TODO:
Expand Down Expand Up @@ -204,6 +205,22 @@ See also [`AbstractModel`](@ref), [`info`](@ref).
"""
hasinfo(m::AbstractModel, key) = haskey(info(m), key)

"""
wrap(o::Any)::AbstractModel
This function wraps anything into an AbstractModel.
The default behavior is the following:
- when called on an `AbstractModel`, the model is simply returned
(no wrapping is performed);
- Function`s and `FunctionWrapper`s are wrapped into a [`FunctionModel`](@ref);
- every other object is wrapped into a `ConstantModel`.
See also [`AbstractModel`](@ref), [`ConstantModel`](@ref), [`FunctionModel`](@ref),
[`LeafModel`](@ref).
"""
wrap(o::Any, FM::Type{<:AbstractModel}) = convert(FM, wrap(o))
wrap(m::AbstractModel) = m

############################################################################################
##################################### LeafModel ############################################
############################################################################################
Expand Down Expand Up @@ -232,3 +249,5 @@ true
See also [`AbstractModel`](@ref), [`ConstantModel`](@ref), [`FunctionModel`](@ref).
"""
abstract type LeafModel{O} <: AbstractModel{O} end

LeafModel(o) = wrap(o)
45 changes: 20 additions & 25 deletions src/utils/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ apply(
kwargs...
) = Fill(outcome(m), ninstances(d))

wrap(o::O) where {O} = convert(ConstantModel{O}, o)

convert(::Type{ConstantModel{O}}, o::O) where {O} = ConstantModel{O}(o)
convert(::Type{<:AbstractModel{F}}, m::ConstantModel) where {F} = ConstantModel{F}(m)

Expand All @@ -92,13 +94,15 @@ convert(::Type{<:AbstractModel{F}}, m::ConstantModel) where {F} = ConstantModel{
info::NamedTuple
end
A `FunctionModel` is a `LeafModel` that applies a native Julia `Function`
in order to compute the outcome. Over efficiency concerns, it is mandatory to make explicit
the output type `O` by wrapping the `Function` into an object of type
`FunctionWrapper{O}`
(see [FunctionWrappers](https://github.com/yuyichao/FunctionWrappers.jl).
A `FunctionModel` is a `LeafModel` that applies a native Julia `Function` in order to
compute the outcome.
See also [`ConstantModel`](@ref), [`LeafModel`](@ref).
!!! warning
Over efficiency concerns, it is mandatory to make explicit the output type `O` by
wrapping the `Function` into an object of type `FunctionWrapper{O}`
(see [FunctionWrappers](https://github.com/yuyichao/FunctionWrappers.jl).
See also [`LeafModel`](@ref).
"""
struct FunctionModel{O} <: LeafModel{O}
f::FunctionWrapper{O}
Expand Down Expand Up @@ -149,8 +153,18 @@ struct FunctionModel{O} <: LeafModel{O}
end
end

"""
f(m::FunctionModel)
Getter for the `FunctionWrapper` within `m`.
See also [`FunctionModel`](@ref),
[FunctionWrappers](https://github.com/yuyichao/FunctionWrappers.jl).
"""
f(m::FunctionModel) = m.f

isopen(::FunctionModel) = false

function apply(
m::FunctionModel,
i::AbstractInterpretation;
Expand Down Expand Up @@ -181,27 +195,8 @@ end

convert(::Type{<:AbstractModel{F}}, m::FunctionModel) where {F} = FunctionModel{F}(m)

"""
wrap(o::Any)::AbstractModel
This function wraps anything into an AbstractModel.
The default behavior is the following:
- when called on an `AbstractModel`, the model is
simply returned (no wrapping is performed);
- `Function`s and `FunctionWrapper`s are wrapped into a `FunctionModel`;
- every other object is wrapped into a `ConstantModel`.
See also
[`ConstantModel`](@ref), [`FunctionModel`](@ref), [`LeafModel`](@ref).
"""
wrap(o::Any, FM::Type{<:AbstractModel}) = convert(FM, wrap(o))
wrap(m::AbstractModel) = m
wrap(o::Function) = FunctionModel(o)
wrap(o::FunctionWrapper{O}) where {O} = FunctionModel{O}(o)
wrap(o::O) where {O} = convert(ConstantModel{O}, o)

# Helper
LeafModel(o) = wrap(o)

############################################################################################
############################################################################################
Expand Down

0 comments on commit 4fcd733

Please sign in to comment.