Skip to content
Merged
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
18 changes: 17 additions & 1 deletion src/engines/julia/generators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -280,5 +280,21 @@ Remove module prefixes from types and functions
"""
removePrefix(arg::Any) = arg # Do not remove prefix in general
removePrefix(num::Number) = string(num)
removePrefix(type::Type) = split(string(type), '.')[end]
removePrefix(func::Function) = split(string(func), '.')[end]
removePrefix(T::Type) = split(string(T), '.')[end]
function removePrefix(T::DataType)
main_type_str = string(nameof(T)) # Strip leading module names and convert T to a string
param_types = T.parameters # Find parameterized types of T
if isempty(param_types) # If T is not parameterized further
return main_type_str # Stop-condition, return main name
else
# Recursively build a vector of strings for the parameter types
param_type_strs = []
for param_type in param_types
push!(param_type_strs, removePrefix(param_type)) # Recursive call
end
param_type_str = join(param_type_strs, ", ") # Join the constructed vector to a comma-separated string

return "$main_type_str{$param_type_str}" # Construct the parameterized type string
end
end
26 changes: 26 additions & 0 deletions test/engines/julia/test_generators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,34 @@ using ForneyLab
using LinearAlgebra: Diagonal
using ForneyLab: entropiesSourceCode, energiesSourceCode, freeEnergySourceCode, marginalTableSourceCode, inboundSourceCode, scheduleSourceCode, removePrefix, vagueSourceCode, initializationSourceCode, optimizeSourceCode, algorithmSourceCode, valueSourceCode, posteriorFactorSourceCode


# create custom module
module Foo
struct Baz
f::Int64
end
struct Bar{T}
f::T
end
struct Bak{T, K}
f::T
g::K
end
end

@testset "removePrefix" begin
# initial check
@test removePrefix(ForneyLab.SPGaussianMeanPrecisionOutNPP) == "SPGaussianMeanPrecisionOutNPP"

# initialize structures
baz = Foo.Baz(1)
bar = Foo.Bar(baz)
bak = Foo.Bak(bar, bar)

# check whether the removePrefix functions works as desired
@test removePrefix(typeof(baz)) == "Baz"
@test removePrefix(typeof(bar)) == "Bar{Baz}"
@test removePrefix(typeof(bak)) == "Bak{Bar{Baz}, Bar{Baz}}"
end

@testset "valueSourceCode" begin
Expand Down