Unverified Commit 2d847b13 authored by Miroslav Kratochvil's avatar Miroslav Kratochvil Committed by GitHub
Browse files

Merge pull request #128 from LCSB-BioCore/mk-relational-ops

fix parsing of relational operators
parents 63c09484 fba1a4fb
......@@ -16,6 +16,25 @@ parse_math_children(ast::VPtr)::Vector{Math} = [
i = 1:ccall(sbml(:ASTNode_getNumChildren), Cuint, (VPtr,), ast)
]
# Mapping of AST node type value subset to relational operations. Depends on
# `ASTNodeType.h` (also see below the case with AST_NAME_TIME)
const relational_opers = Dict{Int32,String}(
308 => "eq",
309 => "geq",
310 => "gt",
311 => "leq",
312 => "lt",
313 => "neq",
)
function relational_oper(t::Int)
haskey(relational_opers, t) ||
throw(DomainError(t, "Unknown ASTNodeType value for relational operator"))
relational_opers[t]
end
"""
parse_math(ast::VPtr)::Math
......@@ -42,11 +61,16 @@ function parse_math(ast::VPtr)::Math
return MathVal(ccall(sbml(:ASTNode_getReal), Cdouble, (VPtr,), ast))
elseif ast_is(ast, :ASTNode_isFunction)
return MathApply(get_string(ast, :ASTNode_getName), parse_math_children(ast))
elseif ast_is(ast, :ASTNode_isOperator) || ast_is(ast, :ASTNode_isRelational)
elseif ast_is(ast, :ASTNode_isOperator)
return MathApply(
string(Char(ccall(sbml(:ASTNode_getCharacter), Cchar, (VPtr,), ast))),
parse_math_children(ast),
)
elseif ast_is(ast, :ASTNode_isRelational)
return MathApply(
relational_oper(Int(ccall(sbml(:ASTNode_getType), Cint, (VPtr,), ast))),
parse_math_children(ast),
)
elseif ast_is(ast, :ASTNode_isLambda)
children = parse_math_children(ast)
if !isempty(children)
......
......@@ -45,6 +45,7 @@ const default_symbolics_mapping = Dict{String,Any}(
"ln" => :log,
"log" => :sbmlLog,
"lt" => :<,
"neq" => :(sbmlNeq),
"piecewise" => :(sbmlPiecewise),
"power" => :^,
"root" => :sbmlRoot,
......@@ -66,6 +67,7 @@ function sbmlPiecewise(args...)
end
end
sbmlNeq(a, b) = !isequal(a, b)
sbmlLog(x) = log(x, 10)
sbmlLog(base, x) = log(base, x)
......
......@@ -56,6 +56,14 @@ sbmlfiles = [
0,
0,
),
# this contains a relational operator
(
joinpath(@__DIR__, "data", "sbml00191.xml"),
"https://raw.githubusercontent.com/sbmlteam/sbml-test-suite/master/cases/semantic/00191/00191-sbml-l3v2.xml",
"c474e94888767d70f9e9e03b32778f18069641563953de60dabac7daa7f481ce",
4,
2,
),
]
@testset "Loading of models from various sources" begin
......@@ -163,3 +171,10 @@ end
@test test_math.args[2].fn == "sin"
@test test_math.args[2].args[1].val == 2.1
end
@testset "relational operators are decoded correctly" begin
test_math =
readSBML(joinpath(@__DIR__, "data", "sbml00191.xml")).reactions["reaction2"].kinetic_math
@test test_math.args[2].fn == "geq"
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment