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
29 changes: 15 additions & 14 deletions src/read_MTG/expand_node.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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<U87<.<U93<U94<.<U96<U97+.+U100",r"(?<=.)(?=[</+])");
nodes, shared = MultiScaleTreeGraph.expand_node!(x,1)
(AbstractString["/A1", "+U85", "/U86", "<U87", "<U88", "<U89", "<U90", "<U91", "<U92", "<U93", "<U94", "<U95", "<U96", "<U97", "+U98", "+U99", "+U100"], Any[87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100])
x = MultiScaleTreeGraph.split_MTG_elements("/A1/U87<.<U89<U90")
nodes, shared = MultiScaleTreeGraph.expand_node!(x, 1)
# nodes: ["/A1", "/U87", "<U88", "<U89", "<U90"]
# shared: positions 2 through 4
```
"""
function expand_node!(x, line)

node_to_expand = findall(y -> 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)
Expand Down
24 changes: 16 additions & 8 deletions src/read_MTG/parse_mtg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions test/files/index-ranges.mtg
Original file line number Diff line number Diff line change
@@ -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<<E14 10
^<E21<E22<.<E24<E25 20
^+E31++E33 30
^+E41+.+E43<E51<.<E52 40
2 changes: 2 additions & 0 deletions test/files/openalea/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Preserve the upstream tab-delimited fixtures, including empty cells at line ends.
*.mtg -text whitespace=-blank-at-eol,-blank-at-eof
Loading
Loading