From 277c6897f25cab5123aa7a836181ee2cdd80ed0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Vezy?= Date: Fri, 20 Nov 2026 12:06:42 +0100 Subject: [PATCH] Fix MTG range attributes and test OpenAlea files --- src/read_MTG/expand_node.jl | 29 +- src/read_MTG/parse_mtg.jl | 24 +- test/files/index-ranges.mtg | 23 + test/files/openalea/.gitattributes | 2 + test/files/openalea/LICENSE | 520 +++++++++ test/files/openalea/README.md | 17 + test/files/openalea/mtg3.mtg | 50 + test/files/openalea/mtg51.mtg | 55 + test/files/openalea/test10_agraf.mtg | 1152 +++++++++++++++++++ test/files/openalea/test12_wij10.mtg | 225 ++++ test/files/openalea/test6_apricot2.mtg | 144 +++ test/files/openalea/test8_boutdenoylum2.mtg | 309 +++++ test/test-read_mtg.jl | 56 +- 13 files changed, 2583 insertions(+), 23 deletions(-) create mode 100644 test/files/index-ranges.mtg create mode 100644 test/files/openalea/.gitattributes create mode 100644 test/files/openalea/LICENSE create mode 100644 test/files/openalea/README.md create mode 100644 test/files/openalea/mtg3.mtg create mode 100644 test/files/openalea/mtg51.mtg create mode 100644 test/files/openalea/test10_agraf.mtg create mode 100644 test/files/openalea/test12_wij10.mtg create mode 100644 test/files/openalea/test6_apricot2.mtg create mode 100644 test/files/openalea/test8_boutdenoylum2.mtg diff --git a/src/read_MTG/expand_node.jl b/src/read_MTG/expand_node.jl index bac90738..52ecce74 100644 --- a/src/read_MTG/expand_node.jl +++ b/src/read_MTG/expand_node.jl @@ -6,50 +6,51 @@ Expand the elements denoted by the syntactic sugar "<<", "<.<", "++" or "+.+" # Arguments - `x::Array{String}`: A split MTG line (e.g. c("/P1","/A1")) -- `line::Array{Int64,1}`: The current line index (mutated) in the file. Only -used as information when erroring. +- `line::Integer`: The current line number, used in error messages. # Returns A Tuple of: - the split MTG line with all nodes explicitly -- the nodes with common attributes (when using `<.<` or `+.+`) +- the positions of nodes that get the row's attributes (when using `<.<` or `+.+`) # Examples ```julia -x = split("/A1+U85/U86 y in ("<", "<.", "+", "+."), x) relative_index = 0 - shared = [] + shared = BitSet() for i in node_to_expand j = i + relative_index - link = replace(parse_MTG_node(x[j])[1], "." => "") + operator = x[j] + link = first(operator) node_before = parse_MTG_node(x[j - 1]) node_after = parse_MTG_node(x[j + 1]) - expanded_index = collect((node_before[3] + 1):(node_after[3] - 1)) if node_before[2] != node_after[2] error("Found nodes to expand at line $line but ", "the symbols of the nodes before and after syntactic ", - "sugar ($(x[i])) do not match.") + "sugar ($operator) do not match.") end - expanded_nodes = join([link, node_before[2]]) .* string.(expanded_index) + expanded_index = (node_before[3] + 1):(node_after[3] - 1) + expanded_nodes = string.(link, node_before[2], expanded_index) - if occursin(x[j], "<.") || occursin(x[j], "+.") - append!(shared, collect((node_before[3]):(node_after[3]))) + if operator in ("<.", "+.") + # Include both endpoints, using their positions after expansion. + union!(shared, (j - 1):(j + length(expanded_nodes))) end x = [x[1:(j - 1)]; expanded_nodes; x[(j + 1):end]] relative_index = relative_index + length(expanded_nodes) - 1 - shared end (x, shared) diff --git a/src/read_MTG/parse_mtg.jl b/src/read_MTG/parse_mtg.jl index 513867da..02b56254 100644 --- a/src/read_MTG/parse_mtg.jl +++ b/src/read_MTG/parse_mtg.jl @@ -167,7 +167,13 @@ function _parse_MTG_node_attr_fields( continue end - if node_type[i] == "INT" + typ = node_type[i] + is_real = typ == "REAL" || (typ == "ALPHA" && feature_name in (:Width, :Length)) + if (typ in ("INT", "BOOLEAN", "DD/MM/YY") || is_real) && isempty(strip(field)) + continue + end + + if typ == "INT" try node_attr[feature_name] = parse(Int, field) catch e @@ -178,7 +184,7 @@ function _parse_MTG_node_attr_fields( _mtg_node_data_text(node_data, diagnostic_start)) end end - elseif node_type[i] == "BOOLEAN" + elseif typ == "BOOLEAN" try node_attr[feature_name] = parse(Bool, field) catch e @@ -189,7 +195,7 @@ function _parse_MTG_node_attr_fields( _mtg_node_data_text(node_data, diagnostic_start)) end end - elseif node_type[i] == "DD/MM/YY" + elseif typ == "DD/MM/YY" try node_attr[feature_name] = Date(field, dateformat"d/m/y") catch e @@ -200,7 +206,7 @@ function _parse_MTG_node_attr_fields( _mtg_node_data_text(node_data, diagnostic_start)) end end - elseif node_type[i] == "REAL" || (node_type[i] == "ALPHA" && feature_name in (:Width, :Length)) + elseif is_real try node_attr[feature_name] = parse(Float64, field) catch e @@ -261,7 +267,7 @@ It may also add several nodes if the line contains several MTG elements. function parse_line_to_node!(tree_dict, l, line, attr_column_start, last_node_column, node_id, mtg_type, features, feature_names, classes) splitted_MTG = split(l[1], "\t") - node_column = findfirst(x -> length(x) > 0, splitted_MTG) + node_column = findfirst(x -> !isempty(strip(x)), splitted_MTG) if attr_column_start < node_column error( @@ -275,8 +281,8 @@ function parse_line_to_node!(tree_dict, l, line, attr_column_start, last_node_co ) end - node = split_MTG_elements(splitted_MTG[node_column]) - node, shared = expand_node!(node, 1) + node = split_MTG_elements(strip(splitted_MTG[node_column])) + node, shared = expand_node!(node, line[1]) # Get node attributes: if features !== nothing @@ -332,8 +338,10 @@ function parse_line_to_node!(tree_dict, l, line, attr_column_start, last_node_co symbol = node_element[2] - if k == length(node) || findfirst(x -> x == k, shared) !== nothing + if k == length(node) node_k_attr = node_attr + elseif k in shared + node_k_attr = ColumnarAttrs(node_attr) else node_k_attr = init_empty_attr() end diff --git a/test/files/index-ranges.mtg b/test/files/index-ranges.mtg new file mode 100644 index 00000000..c05c94c1 --- /dev/null +++ b/test/files/index-ranges.mtg @@ -0,0 +1,23 @@ +CODE: FORM-A + +CLASSES: +SYMBOL SCALE DECOMPOSITION INDEXATION DEFINITION +P 1 FREE FREE EXPLICIT +E 2 FREE FREE EXPLICIT + +DESCRIPTION: +LEFT RIGHT RELTYPE MAX +E E + ? +E E < 1 + +FEATURES: +NAME TYPE +value INT + +MTG: +ENTITY-CODE value +/P1 +^/E11< Dict(pairs(node_attributes(n)))) == traverse(mtg2, n -> Dict(pairs(node_attributes(n)))) end +@testset "mtg index ranges and row attributes" begin + root = read_mtg(joinpath(@__DIR__, "files", "index-ranges.mtg")) + segments = traverse(root, identity; symbol=:E) + @test index.(segments) == [11, 12, 13, 14, 21, 22, 23, 24, 25, 31, 32, 33, 41, 42, 43, 51, 52] + @test link.(segments) == [:/, fill(:<, 8)..., fill(:+, 6)..., :<, :<] + @test parent(first(segments)) === root + @test all(parent(segments[k]) === segments[k-1] for k in 2:length(segments)) + @test [attribute(n, :value) for n in segments] == [ + nothing, nothing, nothing, 10, nothing, 20, 20, 20, 20, + nothing, nothing, 30, 40, 40, 40, 40, 40, + ] + + attribute!(segments[6], :value, 99) + @test attribute(segments[6], :value) == 99 + @test attribute(segments[7], :value) == 20 +end + +@testset "OpenAlea reader fixtures" begin + # OpenAlea's counts exclude its implicit scale-zero root here. + for (file, counts) in ( + ("mtg3.mtg", (12,)), + ("mtg51.mtg", (1, 4, 13)), + ("test6_apricot2.mtg", (1, 10, 20, 65)), + ("test8_boutdenoylum2.mtg", (1, 63, 198)), + ("test10_agraf.mtg", (1, 484, 2994)), + ("test12_wij10.mtg", (1, 93, 93, 316)), + ) + @testset "$file" begin + root = read_mtg(joinpath(@__DIR__, "files", "openalea", file)) + scales = traverse(root, scale) + @test length(root) == sum(counts) + @test [count(==(s), scales) for s in eachindex(counts)] == collect(counts) + + if file == "test10_agraf.mtg" + # Issue #116 starts with P1, U94 and the row ^/E1< attribute(n, :dist) === nothing, segments[1:end-1]) + @test attribute(last(segments), :dist) == 825 + elseif file == "test6_apricot2.mtg" + @test any(n -> index(n) == 2 && link(n) == :+, traverse(root, identity; symbol=:B)) + leaf = only(traverse(root, identity; symbol=:L, filter_fun=n -> index(n) == 261)) + @test attribute(leaf, :DBC) === nothing + @test attribute(leaf, :L1) == 3455 + end + end + end +end + @testset "mtg with no attributes" begin mtg = read_mtg("files/palm.mtg") @test names(mtg) == [:scales, :description, :symbols] @@ -136,7 +189,7 @@ end ) feature_names = Symbol.(features.NAME) node_data = split( - "/Plant1\t7\ttrue\t24/08/2026\t1.5\tlabel-value\t2.25\tNA", + "/Plant1\t7\ttrue\t24/08/2026\t1.5\t \t2.25\tNA", "\t", ) expected = _legacy_parse_attrs_for_test( @@ -150,6 +203,7 @@ end @test actual[:alive] === true @test actual[:when] == Date(2026, 8, 24) @test actual[:measure] === 1.5 + @test actual[:label] == " " @test actual[:Width] === 2.25 @test typeof(actual[:label]) == typeof(expected[:label]) @test !haskey(actual, :skipped)