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

Merge pull request #68 from LCSB-BioCore/mk-merge-65

Minor cleaning&merge of #65
parents 80581368 e6b27664
......@@ -9,6 +9,7 @@ Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
SBML_jll = "bb12108a-f4ef-5f88-8ef3-0b33ff7017f1"
SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
[compat]
......
......@@ -2,6 +2,7 @@ module SBML
using SBML_jll, Libdl, Pkg
using SparseArrays
using Symbolics
include("structs.jl")
include("version.jl")
......@@ -10,6 +11,7 @@ include("readsbml.jl")
include("converters.jl")
include("math.jl")
include("utils.jl")
include("symbolics.jl")
sbml = (sym::Symbol) -> dlsym(SBML_jll.libsbml_handle, sym)
......
default_symbolics_mapping = Dict(
"+" => :+,
"-" => :-,
"*" => :*,
"/" => :/,
"power" => :^,
"lt" => :<,
"leq" => :<=,
"geq" => :>=,
"gt" => :>,
"ceiling" => :ceil,
"floor" => :floor,
"piecewise" => :(Core.ifelse),
#TODO extend this in the future
)
allowed_sym(x, allowed_funs) =
haskey(allowed_funs, x) ? allowed_funs[x] :
throw(DomainError(x, "Unknown SBML function"))
function Base.convert(::Type{Num}, x::SBML.Math; mapping = default_symbolics_mapping)
conv(x::SBML.MathApply) = eval(allowed_sym(x.fn, mapping))(conv.(x.args)...)
conv(x::SBML.MathIdent) = Num(Variable(Symbol(x.id))).val
conv(x::SBML.MathVal) = x.val
conv(x::SBML.MathLambda) = throw(DomainError(x, "can't translate lambdas to symbolics"))
conv(x)
end
......@@ -2,10 +2,12 @@
using Test, SHA, SparseArrays, Downloads
using SBML
using SBML: Model, Reaction, Species
using Symbolics
import Pkg
@testset "SBML test suite" begin
include("version.jl")
include("ecoli_flux.jl")
include("loadmodels.jl")
include("symbolics.jl")
end
@testset "Math to Symbolics conversions" begin
@variables A B C D E
test = SBML.MathApply(
"*",
SBML.Math[
SBML.MathApply(
"+",
SBML.Math[
SBML.MathApply(
"*",
SBML.Math[SBML.MathIdent("A"), SBML.MathIdent("B")],
),
SBML.MathApply(
"-",
SBML.Math[SBML.MathApply(
"*",
SBML.Math[SBML.MathIdent("C"), SBML.MathIdent("D")],
)],
),
],
),
SBML.MathIdent("E"),
],
)
@test isequal(convert(Num, test), (A * B - C * D) * E)
test = SBML.MathApply(
"piecewise",
SBML.Math[
SBML.MathApply("lt", SBML.Math[SBML.MathVal(1), SBML.MathVal(0)]),
SBML.MathVal(123),
SBML.MathVal(456),
],
)
@test isequal(convert(Num, test), 456)
end
Supports Markdown
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