Skip to content

Commit

Permalink
read_esdl now outputs graph; assets read separate
Browse files Browse the repository at this point in the history
NOTE: asset-to-graph conversion is currently very lenient with
incomplete connections. Test cases have also not yet been updated
to be complete & valid
  • Loading branch information
sjvrijn committed Nov 15, 2023
1 parent 652a567 commit a2f9215
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
39 changes: 38 additions & 1 deletion src/io.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export create_parameters_and_sets_from_file,
create_graph, save_solution_to_file, compute_rp_partitions, read_esdl
create_graph, save_solution_to_file, compute_rp_partitions, read_esdl, read_esdl_assets

using EzXML

Expand Down Expand Up @@ -385,6 +385,43 @@ An ESDL file can contain multiple instances. If multiple instances
are present in the file, a specific instance has to be selected.
"""
function read_esdl(file_path; instance_name = nothing)
esdl_assets = read_esdl_assets(file_path; instance_name = instance_name)

Check warning on line 388 in src/io.jl

View check run for this annotation

Codecov / codecov/patch

src/io.jl#L387-L388

Added lines #L387 - L388 were not covered by tests

# Gather all in-ports
id_to_index = Dict{String,Int}()
for (to_id, asset) in enumerate(esdl_assets)
for port in eachelement(asset)
if port.name != "port" || port["xsi:type"] != "esdl:InPort"
continue

Check warning on line 395 in src/io.jl

View check run for this annotation

Codecov / codecov/patch

src/io.jl#L391-L395

Added lines #L391 - L395 were not covered by tests
end
id_to_index[port["id"]] = to_id
end
end

Check warning on line 399 in src/io.jl

View check run for this annotation

Codecov / codecov/patch

src/io.jl#L397-L399

Added lines #L397 - L399 were not covered by tests

# Create graph based on out-ports and previously gathered in-ports
graph = Graphs.DiGraph(length(esdl_assets))
for (from_id, asset) in enumerate(esdl_assets)
for port in eachelement(asset)
if port.name != "port" || port["xsi:type"] != "esdl:OutPort"
continue

Check warning on line 406 in src/io.jl

View check run for this annotation

Codecov / codecov/patch

src/io.jl#L402-L406

Added lines #L402 - L406 were not covered by tests
end
flows = eachsplit(port["connectedTo"], " ")
for flow in flows
if !haskey(id_to_index, flow)

Check warning on line 410 in src/io.jl

View check run for this annotation

Codecov / codecov/patch

src/io.jl#L408-L410

Added lines #L408 - L410 were not covered by tests
# throw(ErrorException("No InPort with id '$flow' was found"))
println("No InPort with id '$flow' was found, ignoring...")
continue

Check warning on line 413 in src/io.jl

View check run for this annotation

Codecov / codecov/patch

src/io.jl#L412-L413

Added lines #L412 - L413 were not covered by tests
end
to_id = id_to_index[flow]
Graphs.add_edge!(graph, from_id, to_id)
end
end
end

Check warning on line 419 in src/io.jl

View check run for this annotation

Codecov / codecov/patch

src/io.jl#L415-L419

Added lines #L415 - L419 were not covered by tests

return graph

Check warning on line 421 in src/io.jl

View check run for this annotation

Codecov / codecov/patch

src/io.jl#L421

Added line #L421 was not covered by tests
end

function read_esdl_assets(file_path; instance_name = nothing)
doc = readxml(file_path)
doc_root = root(doc)
if countelements(doc_root) == 0 || firstelement(doc_root).name != "instance"
Expand Down
12 changes: 7 additions & 5 deletions test/test-io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,31 @@ end
@testset "ESDL parsing" begin
@testset "instance detection error handling" begin
@testset "no instance" begin
@test_throws ErrorException read_esdl(joinpath(ESDL_FOLDER, "no_instance.esdl"))
@test_throws ErrorException read_esdl_assets(
joinpath(ESDL_FOLDER, "no_instance.esdl"),
)
end

@testset "two instances" begin
@test_throws ErrorException read_esdl(
@test_throws ErrorException read_esdl_assets(
joinpath(ESDL_FOLDER, "two_instances.esdl"),
)
end
end

@testset "instance asset parsing" begin
@testset "unnamed single instance" begin
assets = read_esdl(joinpath(ESDL_FOLDER, "one_instance.esdl"))
assets = read_esdl_assets(joinpath(ESDL_FOLDER, "one_instance.esdl"))
@test length(assets) == 8
end

@testset "named instance out of multiple" begin
flat_assets = read_esdl(
flat_assets = read_esdl_assets(
joinpath(ESDL_FOLDER, "two_instances.esdl");
instance_name = "Flat",
)
@test length(flat_assets) == 4
nested_assets = read_esdl(
nested_assets = read_esdl_assets(
joinpath(ESDL_FOLDER, "two_instances.esdl");
instance_name = "Nested",
)
Expand Down

0 comments on commit a2f9215

Please sign in to comment.