Skip to content

Commit

Permalink
fixing MD parsing (#132)
Browse files Browse the repository at this point in the history
* fixing MD parsing

* updated LTS test, formatting

* minor adaption

* throw warning for unspoorted arrays/matrices
  • Loading branch information
ThummeTo authored Jan 31, 2025
1 parent a53d2be commit 5dd53c0
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/TestLatest.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Test v1 (latest)
name: Test (latest)

on:
workflow_dispatch:
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "FMIImport"
uuid = "9fcbc62e-52a0-44e9-a616-1359a0008194"
authors = ["TT <tobias.thummerer@informatik.uni-augsburg.de>", "LM <lars.mikelsons@informatik.uni-augsburg.de>", "JK <josef.kircher@student.uni-augsburg.de>"]
version = "1.0.8"
version = "1.0.9"

[deps]
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
Expand Down
175 changes: 109 additions & 66 deletions src/FMI3/md.jl
Original file line number Diff line number Diff line change
Expand Up @@ -288,17 +288,21 @@ function parseModelVariables(md::fmi3ModelDescription, nodes::EzXML.Node)
modelVariables[index].clocks =
parseArrayValueReferences(md, parseNode(node, "clocks", String))

if typename ("Clock", "String")
modelVariables[index].intermediateUpdate =
parseNode(node, "intermediateUpdate", Bool)
else
@warn "Unsupported typename `$(typename)` for modelVariable attribute `intermediateUpdate`."
if haskey(node, "intermediateUpdate")
if typename ("Clock", "String")
modelVariables[index].intermediateUpdate =
parseNode(node, "intermediateUpdate", Bool)
else
@warn "Unsupported typename `$(typename)` for modelVariable attribute `intermediateUpdate`."
end
end

if typename ("Clock", "String")
modelVariables[index].previous = parseNode(node, "previous", Bool)
else
@warn "Unsupported typename `$(typename)` for modelVariable attribute `previous`."
if haskey(node, "previous")
if typename ("Clock", "String")
modelVariables[index].previous = parseNode(node, "previous", Bool)
else
@warn "Unsupported typename `$(typename)` for modelVariable attribute `previous`."
end
end

if haskey(node, "initial")
Expand All @@ -310,87 +314,126 @@ function parseModelVariables(md::fmi3ModelDescription, nodes::EzXML.Node)
end
end

if typename ("Clock", "String", "Binary", "Boolean")
modelVariables[index].quantity = parseNode(node, "quantity", String)
else
@warn "Unsupported typename `$(typename)` for modelVariable attribute `quantity`."
if haskey(node, "quantity")
if typename ("Clock", "String", "Binary", "Boolean")
modelVariables[index].quantity = parseNode(node, "quantity", String)
else
@warn "Unsupported typename `$(typename)` for modelVariable attribute `quantity`."
end
end

if typename == "Float64" || typename == "Float32"
modelVariables[index].unit = parseNode(node, "unit", String)
modelVariables[index].displayUnit = parseNode(node, "displayUnit", String)
if haskey(node, "unit")
if typename == "Float64" || typename == "Float32"
modelVariables[index].unit = parseNode(node, "unit", String)
modelVariables[index].displayUnit = parseNode(node, "displayUnit", String)
else
@warn "Unsupported typename `$(typename)` for modelVariable attribute `unit`."
end
end

if typename != "String"
modelVariables[index].declaredType = parseNode(node, "declaredType", String)
else
@warn "Unsupported typename `$(typename)` for modelVariable attribute `declaredType`."
if haskey(node, "declaredType")
if typename != "String"
modelVariables[index].declaredType = parseNode(node, "declaredType", String)
else
@warn "Unsupported typename `$(typename)` for modelVariable attribute `declaredType`."
end
end

if typename ("Clock", "String", "Binary", "Boolean")
modelVariables[index].min =
parseNode(node, "min", stringToDataType(md, typename))
else
@warn "Unsupported typename `$(typename)` for modelVariable attribute `min`."
if haskey(node, "min")
if typename ("Clock", "String", "Binary", "Boolean")
modelVariables[index].min =
parseNode(node, "min", stringToDataType(md, typename))
else
@warn "Unsupported typename `$(typename)` for modelVariable attribute `min`."
end
end

if typename ("Clock", "String", "Binary", "Boolean")
modelVariables[index].max =
parseNode(node, "max", stringToDataType(md, typename))
else
@warn "Unsupported typename `$(typename)` for modelVariable attribute `max`."
if haskey(node, "max")
if typename ("Clock", "String", "Binary", "Boolean")
modelVariables[index].max =
parseNode(node, "max", stringToDataType(md, typename))
else
@warn "Unsupported typename `$(typename)` for modelVariable attribute `max`."
end
end

if typename == "Float64" || typename == "Float32"
modelVariables[index].nominal =
parseNode(node, "nominal", stringToDataType(md, typename))
modelVariables[index].unbounded =
parseNode(node, "unbounded", stringToDataType(md, typename))
if haskey(node, "nominal")
if typename == "Float64" || typename == "Float32"
modelVariables[index].nominal =
parseNode(node, "nominal", stringToDataType(md, typename))
else
@warn "Unsupported typename `$(typename)` for modelVariable attribute `nominal`."
end
end

if typename ("Binary", "Clock")
if !isnothing(node.firstelement) && node.firstelement.name == "Dimension"
substrings = split(node["start"], " ")
if haskey(node, "unbounded")
if typename == "Float64" || typename == "Float32"
modelVariables[index].unbounded =
parseNode(node, "unbounded", stringToDataType(md, typename))
else
@warn "Unsupported typename `$(typename)` for modelVariable attribute `unbounded`."
end
end

T = stringToDataType(md, typename)
modelVariables[index].start = Array{T}(undef, 0)
for string in substrings
push!(modelVariables[index].start, parseType(string, T))
if haskey(node, "start")
if typename ("Binary", "Clock")
@warn "Unsupported typename `$(typename)` for modelVariable attribute `start`."
elseif typename == "Enum"
for i = 1:length(md.enumerations)
if modelVariables[index].declaredType == md.enumerations[i][1] # identify the enum by the name
modelVariables[index].start =
md.enumerations[i][1+parseNode(node, "start", Int)] # find the enum value and set it
end
end
else
if typename == "Enum"
for i = 1:length(md.enumerations)
if modelVariables[index].declaredType == md.enumerations[i][1] # identify the enum by the name
modelVariables[index].start =
md.enumerations[i][1+parseNode(node, "start", Int)] # find the enum value and set it
end
elseif typename == "String"
for n in eachelement(node)
if n.name == "Start"
modelVariables[index].start = n["value"]
end
end
elseif typename == "Binary"
for n in eachelement(node)
if n.name == "Start"
modelVariables[index].start = pointer(n["value"])
end
end
else # all "common" types
dimensions = Vector{UInt32}()
for element in eachelement(node)
if element.name == "Dimension"
push!(dimensions, parseType(element["start"], UInt32))
end
end
if length(dimensions) > 0
# substrings = split(node["start"], " ")

# T = stringToDataType(md, typename)
# modelVariables[index].start = Vector{T}()
# for string in substrings
# push!(modelVariables[index].start, parseType(string, T))
# end
@warn "Parsing of multi-dimension variable start values not supported, yet.\nDimension is $(dimensions)"
else
modelVariables[index].start =
parseNode(node, "start", stringToDataType(md, typename))
end
end
else
@warn "Unsupported typename `$(typename)` for modelVariable attribute `start`."
end

if typename == "Float64" || typename == "Float32"
modelVariables[index].derivative =
parseNode(node, "derivative", fmi3ValueReference)
modelVariables[index].reinit = parseNode(node, "reinit", Bool)
if haskey(node, "derivative")
if typename == "Float64" || typename == "Float32"
modelVariables[index].derivative =
parseNode(node, "derivative", fmi3ValueReference)
else
@warn "Unsupported typename `$(typename)` for modelVariable attribute `derivative`."
end
end

if typename == "String"
for nod in eachelement(node)
if nod.name == "Start"
modelVariables[index].start = nod["value"]
end
end
elseif typename == "Binary"
for nod in eachelement(node)
if nod.name == "Start"
modelVariables[index].start = pointer(nod["value"])
end
if haskey(node, "reinit")
if typename == "Float64" || typename == "Float32"
modelVariables[index].reinit = parseNode(node, "reinit", Bool)
else
@warn "Unsupported typename `$(typename)` for modelVariable attribute `reinit`."
end
end

Expand Down

2 comments on commit 5dd53c0

@ThummeTo
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/124082

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.0.9 -m "<description of version>" 5dd53c02d24da7571058701d55fa8e42bf3bc824
git push origin v1.0.9

Please sign in to comment.