Skip to content

fix bug - removePrefix() #150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 16, 2021
Merged

fix bug - removePrefix() #150

merged 7 commits into from
Mar 16, 2021

Conversation

bartvanerp
Copy link
Member

Allows for parameterized custom nodes with custom types.

Allows for parameterized custom nodes with custom types.
@bartvanerp bartvanerp added the bug Something isn't working label Jan 28, 2021
@bartvanerp bartvanerp linked an issue Jan 28, 2021 that may be closed by this pull request
@ThijsvdLaar
Copy link
Collaborator

Thanks for catching this. I started refactoring and came up with a more general recursive implementation:

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

This could be tested for a nested definition, like:

module Foo

struct Baz
    f::Int64
end

struct Bar{T}
    f::T
end

struct Bak{T, K}
    f::T
    g::K
end

end

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

removePrefix(typeof(baz))
removePrefix(typeof(bar))
removePrefix(typeof(bak))

To allow for the change in the removePrefix() function, some tests have been changed by changing `Message` to `ForneyLab.Message`.
@bartvanerp
Copy link
Member Author

Thanks for the suggestion Thijs. I changed the removePrefix() function to your suggestion. As a result from this change 6 tests failed, each having to do with the string comparison of terms involving Message. Due to the change of removePrefix(), the generated source code included strings with ForneyLab.Message instead of the original Message. To copy with this, I updated the tests. I am not sure whether these changes in the tests are desirable or whether changes to
removePrefix() are preferred.

@ThijsvdLaar
Copy link
Collaborator

Thanks for the implementation, I did not think about how this would affect the Message code generation. However, generating ForneyLab.Message seems rather superfluous to me, so I think it would be better to update the removePrefix() instead.

@bartvanerp
Copy link
Member Author

bartvanerp commented Mar 4, 2021

The proposed function originally had issues with the following:
The input argument type was converted from ::Type to ::DataType, which excluded the ::UnionAll type that is used for the inbound.dist_or_msg attribute.
Therefore the new function was not called, but instead the ::Any function was called which just return the input argument.

In order to solve this, the original function with input ::Type was added back to catch the ::UnionAll cases.

The latest commits revert the changes to the tests, add new tests and reintroduce the original function as a backup to the new function.

@ThijsvdLaar ThijsvdLaar self-requested a review March 16, 2021 15:54
@ThijsvdLaar ThijsvdLaar merged commit f5d4ac1 into master Mar 16, 2021
@ThijsvdLaar ThijsvdLaar deleted the debug_removePrefix branch September 14, 2021 13:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Free energy code generation using custom parameterized factor nodes
2 participants