From 1bf674ce9b446075c2803f075c8508156f4f5554 Mon Sep 17 00:00:00 2001 From: Reuben Gardos Reid <5456207+ReubenJ@users.noreply.github.com> Date: Fri, 15 Nov 2024 17:10:55 +0100 Subject: [PATCH 1/7] Add pirated methods from `HerbGrammar` --- Project.toml | 6 +++++- src/HerbCore.jl | 4 +++- src/grammar.jl | 8 ++++++++ src/rulenode.jl | 19 +++++++++++++++++++ 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 4c4453f..8fcea03 100644 --- a/Project.toml +++ b/Project.toml @@ -1,9 +1,13 @@ name = "HerbCore" uuid = "2b23ba43-8213-43cb-b5ea-38c12b45bd45" authors = ["Jaap de Jong ", "Nicolae Filat ", "Tilman Hinnerichs ", "Sebastijan Dumancic "] -version = "0.3.2" +version = "0.3.3" + +[deps] +AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" [compat] +AbstractTrees = "0.4.5" julia = "^1.8" [extras] diff --git a/src/HerbCore.jl b/src/HerbCore.jl index 844cac5..1f7b8f8 100644 --- a/src/HerbCore.jl +++ b/src/HerbCore.jl @@ -1,8 +1,10 @@ module HerbCore +using AbstractTrees + +include("grammar.jl") include("rulenode.jl") include("constraint.jl") -include("grammar.jl") export AbstractRuleNode, diff --git a/src/grammar.jl b/src/grammar.jl index 383dd79..bf1794b 100644 --- a/src/grammar.jl +++ b/src/grammar.jl @@ -19,3 +19,11 @@ If the grammar is non-probabilistic, the list can be `nothing`. For concrete types, see `ContextSensitiveGrammar` within the `HerbGrammar` module. """ abstract type AbstractGrammar end + +function Base.show(io::IO, grammar::AbstractGrammar) + for i in eachindex(grammar.rules) + println(io, i, ": ", grammar.types[i], " = ", grammar.rules[i]) + end +end + +Base.getindex(grammar::AbstractGrammar, typ::Symbol) = grammar.bytype[typ] diff --git a/src/rulenode.jl b/src/rulenode.jl index 0f02447..5d0c166 100644 --- a/src/rulenode.jl +++ b/src/rulenode.jl @@ -16,6 +16,10 @@ Expression trees consist of [`RuleNode`](@ref)s and [`AbstractHole`](@ref)s. """ abstract type AbstractRuleNode end +# Interface to AbstractTrees.jl +AbstractTrees.children(node::AbstractRuleNode) = get_children(node) +AbstractTrees.nodevalue(node::AbstractRuleNode) = get_rule(node) + """ RuleNode <: AbstractRuleNode @@ -36,6 +40,16 @@ mutable struct RuleNode <: AbstractRuleNode children::Vector{AbstractRuleNode} end +function RuleNode(ind::Int, grammar::AbstractGrammar) + RuleNode( + ind, nothing, [Hole(get_domain(grammar, type)) for type in grammar.childtypes[ind]]) +end + +function RuleNode(ind::Int, _val::Any, grammar::AbstractGrammar) + RuleNode( + ind, _val, [Hole(get_domain(grammar, type)) for type in grammar.childtypes[ind]]) +end + """ AbstractHole <: AbstractRuleNode @@ -64,6 +78,11 @@ mutable struct UniformHole <: AbstractUniformHole children::Vector{AbstractRuleNode} end +function UniformHole(domain::BitVector, grammar::AbstractGrammar) + UniformHole(domain, + [Hole(get_domain(grammar, type)) for type in grammar.childtypes[findfirst(domain)]]) +end + """ Hole <: AbstractHole From f6cea02e1c5839493610b9eac6a74696dd059112 Mon Sep 17 00:00:00 2001 From: Reuben Gardos Reid <5456207+ReubenJ@users.noreply.github.com> Date: Tue, 26 Nov 2024 19:15:11 +0300 Subject: [PATCH 2/7] Remove empty-child RuleNode and UniformHole constructors They are better-suited as functions in `HerbGrammar` --- src/rulenode.jl | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/src/rulenode.jl b/src/rulenode.jl index 5d0c166..b376ace 100644 --- a/src/rulenode.jl +++ b/src/rulenode.jl @@ -40,16 +40,6 @@ mutable struct RuleNode <: AbstractRuleNode children::Vector{AbstractRuleNode} end -function RuleNode(ind::Int, grammar::AbstractGrammar) - RuleNode( - ind, nothing, [Hole(get_domain(grammar, type)) for type in grammar.childtypes[ind]]) -end - -function RuleNode(ind::Int, _val::Any, grammar::AbstractGrammar) - RuleNode( - ind, _val, [Hole(get_domain(grammar, type)) for type in grammar.childtypes[ind]]) -end - """ AbstractHole <: AbstractRuleNode @@ -78,11 +68,6 @@ mutable struct UniformHole <: AbstractUniformHole children::Vector{AbstractRuleNode} end -function UniformHole(domain::BitVector, grammar::AbstractGrammar) - UniformHole(domain, - [Hole(get_domain(grammar, type)) for type in grammar.childtypes[findfirst(domain)]]) -end - """ Hole <: AbstractHole From e3a5874e5d12af5612fc824a3ca9c9843ac769c3 Mon Sep 17 00:00:00 2001 From: Reuben Gardos Reid <5456207+ReubenJ@users.noreply.github.com> Date: Tue, 26 Nov 2024 19:17:10 +0300 Subject: [PATCH 3/7] Add tests --- Project.toml | 6 ------ src/HerbCore.jl | 4 ++++ test/Project.toml | 3 +++ test/runtests.jl | 10 ++++++++++ 4 files changed, 17 insertions(+), 6 deletions(-) create mode 100644 test/Project.toml diff --git a/Project.toml b/Project.toml index 8fcea03..0fa0d58 100644 --- a/Project.toml +++ b/Project.toml @@ -9,9 +9,3 @@ AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" [compat] AbstractTrees = "0.4.5" julia = "^1.8" - -[extras] -Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" - -[targets] -test = ["Test"] diff --git a/src/HerbCore.jl b/src/HerbCore.jl index 1f7b8f8..941f168 100644 --- a/src/HerbCore.jl +++ b/src/HerbCore.jl @@ -32,4 +32,8 @@ export have_same_shape, AbstractConstraint, AbstractGrammar +# AbstractTrees interface +children, +nodevalue + end # module HerbCore diff --git a/test/Project.toml b/test/Project.toml new file mode 100644 index 0000000..aa04d0c --- /dev/null +++ b/test/Project.toml @@ -0,0 +1,3 @@ +[deps] +AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/test/runtests.jl b/test/runtests.jl index 06d0c01..2c2b8eb 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,7 +1,17 @@ +using AbstractTrees: children, nodevalue, treeheight using HerbCore using Test @testset "HerbCore.jl" verbose=true begin + @testset "AbstractTrees Interface" begin + @show typeof(nodevalue(RuleNode(1))) + @test nodevalue(RuleNode(1)) == 1 + @test isempty(children(RuleNode(1))) + @test length(children(RuleNode(1, [RuleNode(2), RuleNode(2)]))) == 2 + @test treeheight(RuleNode(1)) == 0 + @test treeheight(RuleNode(1, [RuleNode(2), RuleNode(2)])) == 1 + end + @testset "RuleNode tests" begin @testset "Equality tests" begin @test RuleNode(1) == RuleNode(1) From 3b208c72368073fdcd7a8743f5d2c4a85db3ad26 Mon Sep 17 00:00:00 2001 From: Reuben Gardos Reid <5456207+ReubenJ@users.noreply.github.com> Date: Tue, 26 Nov 2024 20:09:12 +0300 Subject: [PATCH 4/7] Test grammar `Base.show` --- test/Project.toml | 1 + test/runtests.jl | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/test/Project.toml b/test/Project.toml index aa04d0c..7084a9f 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -1,3 +1,4 @@ [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" +HerbGrammar = "4ef9e186-2fe5-4b24-8de7-9f7291f24af7" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/test/runtests.jl b/test/runtests.jl index 2c2b8eb..367bbfe 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,5 +1,6 @@ using AbstractTrees: children, nodevalue, treeheight using HerbCore +using HerbGrammar: @csgrammar using Test @testset "HerbCore.jl" verbose=true begin @@ -345,4 +346,14 @@ using Test "12{14,2{4{hole[Bool[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]}},2{4{6}}}" end end + + @testset "Grammar" begin + g = @csgrammar begin + A = 1 + end + + io = IOBuffer() + Base.show(io, g) + @test String(take!(io)) == "1: A = 1\n" + end end From e8e24ac5e8baca995399383da2f1adc2dc38dca5 Mon Sep 17 00:00:00 2001 From: Reuben Gardos Reid <5456207+ReubenJ@users.noreply.github.com> Date: Wed, 27 Nov 2024 08:22:57 +0300 Subject: [PATCH 5/7] Remove test dependency on `HerbGrammar` --- test/Project.toml | 1 - test/runtests.jl | 12 ++++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/test/Project.toml b/test/Project.toml index 7084a9f..aa04d0c 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -1,4 +1,3 @@ [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" -HerbGrammar = "4ef9e186-2fe5-4b24-8de7-9f7291f24af7" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/test/runtests.jl b/test/runtests.jl index 367bbfe..e6e563b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,11 +1,9 @@ using AbstractTrees: children, nodevalue, treeheight using HerbCore -using HerbGrammar: @csgrammar using Test @testset "HerbCore.jl" verbose=true begin @testset "AbstractTrees Interface" begin - @show typeof(nodevalue(RuleNode(1))) @test nodevalue(RuleNode(1)) == 1 @test isempty(children(RuleNode(1))) @test length(children(RuleNode(1, [RuleNode(2), RuleNode(2)]))) == 2 @@ -348,10 +346,16 @@ using Test end @testset "Grammar" begin - g = @csgrammar begin - A = 1 + struct ExGrammar <: AbstractGrammar + rules::Vector{Any} + types::Vector{Symbol} + # ... + # only partially implementing the AbstractGrammar interface + # to test the Base.show end + g = ExGrammar([1], [:A]) + io = IOBuffer() Base.show(io, g) @test String(take!(io)) == "1: A = 1\n" From d8232aa1dd70b23cc1430dc41a2f5ece75377052 Mon Sep 17 00:00:00 2001 From: Reuben Gardos Reid <5456207+ReubenJ@users.noreply.github.com> Date: Wed, 27 Nov 2024 09:20:06 +0300 Subject: [PATCH 6/7] Add `Base.getindex` test --- test/runtests.jl | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index e6e563b..b08f2a8 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -349,15 +349,22 @@ using Test struct ExGrammar <: AbstractGrammar rules::Vector{Any} types::Vector{Symbol} + bytype::Dict{Symbol, Vector{Int}} # ... # only partially implementing the AbstractGrammar interface # to test the Base.show end - g = ExGrammar([1], [:A]) + g = ExGrammar([1], [:A], Dict([:A => [1]])) - io = IOBuffer() - Base.show(io, g) - @test String(take!(io)) == "1: A = 1\n" + @testset "show" begin + io = IOBuffer() + Base.show(io, g) + @test String(take!(io)) == "1: A = 1\n" + end + + @testset "get_index" begin + @test g[:A] == [1] + end end end From e3189be46a07abb401124357bf7002c6f8205b5c Mon Sep 17 00:00:00 2001 From: Reuben Gardos Reid <5456207+ReubenJ@users.noreply.github.com> Date: Wed, 27 Nov 2024 18:04:53 +0300 Subject: [PATCH 7/7] Add Aqua, remove extraneous exports --- src/HerbCore.jl | 4 ---- test/Project.toml | 1 + test/runtests.jl | 3 +++ 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/HerbCore.jl b/src/HerbCore.jl index 941f168..1f7b8f8 100644 --- a/src/HerbCore.jl +++ b/src/HerbCore.jl @@ -32,8 +32,4 @@ export have_same_shape, AbstractConstraint, AbstractGrammar -# AbstractTrees interface -children, -nodevalue - end # module HerbCore diff --git a/test/Project.toml b/test/Project.toml index aa04d0c..cd6e197 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -1,3 +1,4 @@ [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" +Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/test/runtests.jl b/test/runtests.jl index b08f2a8..8e575b6 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,8 +1,11 @@ using AbstractTrees: children, nodevalue, treeheight +using Aqua using HerbCore using Test @testset "HerbCore.jl" verbose=true begin + @testset "Aqua Tests" Aqua.test_all(HerbCore) + @testset "AbstractTrees Interface" begin @test nodevalue(RuleNode(1)) == 1 @test isempty(children(RuleNode(1)))