Skip to content

Commit

Permalink
Merge pull request #31 from JuliaReach/mforets/updates
Browse files Browse the repository at this point in the history
Updates for Julia v1.0
  • Loading branch information
mforets authored Jan 27, 2019
2 parents 7c28ed1 + 0df8d8e commit 9f0f8bd
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 27 deletions.
3 changes: 2 additions & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ SymEngine
DataStructures
Reexport
FillArrays
LazySets
LazySets 1.5.3
Compat
5 changes: 5 additions & 0 deletions src/SX.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ __precompile__(true)

module SX

#=========================================
Compatibility for previous Julia versions
=========================================#
include("compat.jl")

import DataStructures, FillArrays
using EzXML, Reexport
@reexport using HybridSystems, MathematicalSystems, SymEngine
Expand Down
10 changes: 10 additions & 0 deletions src/compat.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Compat
using Compat: @warn

@static if VERSION >= v"0.7"
const _parse_s = Base.Meta.parse
const _parse_t = Base.parse
else
const _parse_s = parse
const _parse_t = parse
end
6 changes: 3 additions & 3 deletions src/convert.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ function is_hyperplane(expr::Expr)::Bool
# convert to symengine expressions
lhs = convert(Basic, expr.args[1])

if :args in fieldnames(expr.args[2])
if :args in fieldnames(typeof(expr.args[2]))
# treats the 4 in :(2*x1 = 4)
rhs = convert(Basic, expr.args[2].args[2])
else
Expand Down Expand Up @@ -276,7 +276,7 @@ function convert(::Type{LazySets.Hyperplane{N}}, expr::Expr; vars::Vector{SymEng
lhs = convert(Basic, expr.args[1])

# treats the 4 in :(2*x1 = 4)
rhs = :args in fieldnames(expr.args[2]) ? convert(Basic, expr.args[2].args[2]) : convert(Basic, expr.args[2])
rhs = :args in fieldnames(typeof(expr.args[2])) ? convert(Basic, expr.args[2].args[2]) : convert(Basic, expr.args[2])

# a1 x1 + ... + an xn + K = 0
eq = lhs - rhs
Expand Down Expand Up @@ -336,7 +336,7 @@ function free_symbols(expr::Expr, set_type::Type{<:LazySets.Hyperplane})
lhs = convert(Basic, expr.args[1])

# treats the 4 in :(2*x1 = 4)
rhs = :args in fieldnames(expr.args[2]) ? convert(Basic, expr.args[2].args[2]) : convert(Basic, expr.args[2])
rhs = :args in fieldnames(typeof(expr.args[2])) ? convert(Basic, expr.args[2].args[2]) : convert(Basic, expr.args[2])
return free_symbols(lhs - rhs)
end

Expand Down
10 changes: 5 additions & 5 deletions src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,19 @@ function readsxmodel(file; raw_dict=false,
transitionlabels = Set{String}()

# vector with the invariants for each location
invariants = Vector{Vector{Expr}}(nlocations)
invariants = Vector{Vector{Expr}}(undef, nlocations)

# vector with the ODE flow for each location
flows = Vector{Vector{Expr}}(nlocations)
flows = Vector{Vector{Expr}}(undef, nlocations)

# assignments for each transition
assignments = Vector{Vector{Expr}}(ntransitions)
assignments = Vector{Vector{Expr}}(undef, ntransitions)

# guards for each transition; subsets of state space where the state needs
# to be to make the transition
guards = Vector{Vector{Expr}}(ntransitions)
guards = Vector{Vector{Expr}}(undef, ntransitions)

switchings = Vector{AbstractSwitching}(ntransitions)
switchings = Vector{AbstractSwitching}(undef, ntransitions)

HDict = Dict("automaton"=>automaton,
"variables"=>variables,
Expand Down
22 changes: 11 additions & 11 deletions src/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ function parse_sxmath(s; assignment=false)
count_right_parentheses = s -> count(c -> c == ')', collect(s))
@assert count_left_parentheses(s) == count_right_parentheses(s) "the expression $(s) is not well formed"

expr = strip.(split(replace(s, "&&", "&"), "&"))
expr = strip.(split(replace(s, "&&" => "&"), "&"))
if assignment
expr = replace.(expr, ":=", "=")
expr = replace.(expr, Ref(":=" => "="))
end
expr = replace.(expr, "==", "=")
expr = replace.(expr, Ref("==" => "="))

# remove irrelevant parentheses from the beginning and the end
for (i, expri) in enumerate(expr)
Expand All @@ -156,7 +156,7 @@ function parse_sxmath(s; assignment=false)
end
end

return parse.(expr)
return _parse_s.(expr)
end

"""
Expand Down Expand Up @@ -248,9 +248,9 @@ function add_variable!(variables, field, id=1)
# d1 and d2 are the dimensions (d1=d2=1 for scalars)
@assert field["d1"] == "1" && field["d2"] == "1"

varname = parse(field["name"])
islocal = haskey(field, "local") ? parse(field["local"]) : false
iscontrolled = haskey(field, "controlled") ? parse(field["controlled"]) : true
varname = _parse_s(field["name"])
islocal = haskey(field, "local") ? _parse_s(field["local"]) : false
iscontrolled = haskey(field, "controlled") ? _parse_s(field["controlled"]) : true
dynamicstype = haskey(field, "dynamics") ? field["dynamics"] : "any"

variables[varname] = Dict("local"=>islocal,
Expand Down Expand Up @@ -293,15 +293,15 @@ and similarly `f` is the list of ODEs that define the flow for this location.
Both objects are vectors of symbolic expressions `Expr`.
"""
function parse_location(field)
id = parse(Int, field["id"])
id = _parse_t(Int, field["id"])
local I, f
for element in eachelement(field)
if nodename(element) == "invariant"
I = parse_sxmath(nodecontent(element))
elseif nodename(element) == "flow"
f = parse_sxmath(nodecontent(element))
else
warn("field $(nodename(element)) in location $(field["id"]) is ignored")
@warn("field $(nodename(element)) in location $(field["id"]) is ignored")
end
end
return (id, I, f)
Expand Down Expand Up @@ -330,7 +330,7 @@ the "assignment", or both); in that case this function returns an empty of
expressions for those cases.
"""
function parse_transition(field)
q, r = parse(Int, field["source"]), parse(Int, field["target"])
q, r = _parse_t(Int, field["source"]), _parse_t(Int, field["target"])
G = Vector{Expr}()
A = Vector{Expr}()
for element in eachelement(field)
Expand All @@ -339,7 +339,7 @@ function parse_transition(field)
elseif nodename(element) == "assignment"
A = parse_sxmath(nodecontent(element), assignment=true)
else
warn("field $(nodename(element)) in transition $(field["source"])$(field["target"]) is ignored")
@warn("field $(nodename(element)) in transition $(field["source"])$(field["target"]) is ignored")
end
end
return (q, r, G, A)
Expand Down
12 changes: 6 additions & 6 deletions src/symbolic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function linearHS(HDict; ST=ConstrainedLinearControlContinuousSystem,
nlocations, ntransitions = HDict["nlocations"], HDict["ntransitions"]

# vector of modes (flows, invariants)
modes = Vector{ST}(nlocations)
modes = Vector{ST}(undef, nlocations)

for id_location in eachindex(flows)

Expand All @@ -79,10 +79,10 @@ function linearHS(HDict; ST=ConstrainedLinearControlContinuousSystem,
local_state_variables = convert.(Basic, [fi.args[1].args[1] for fi in flows[id_location]])

# dynamics matrix
A = Matrix{N}(n, n)
A = Matrix{N}(undef, n, n)

# forcing terms
B = Matrix{N}(n, m)
B = Matrix{N}(undef, n, m)
C = zeros(N, n) # constant terms

# track if there are constant terms
Expand Down Expand Up @@ -153,7 +153,7 @@ function linearHS(HDict; ST=ConstrainedLinearControlContinuousSystem,
end

# reset maps (assignments, guards) for each transition (equations)
resetmaps = Vector{STD}(ntransitions)
resetmaps = Vector{STD}(undef, ntransitions)

for id_transition in eachindex(assignments)

Expand All @@ -172,10 +172,10 @@ function linearHS(HDict; ST=ConstrainedLinearControlContinuousSystem,
local_state_variables = convert.(Basic, [ai.args[1].args[1] for ai in assignments[id_transition]])

# dynamics matrix
Ar = Matrix{N}(n, n)
Ar = Matrix{N}(undef, n, n)

# forcing terms
Br = Matrix{N}(n, m)
Br = Matrix{N}(undef, n, m)
Cr = zeros(N, n) # constant terms

# track if there are constant terms
Expand Down
1 change: 1 addition & 0 deletions test/REQUIRE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
julia 0.6
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using SX
using Base.Test
using Compat, Compat.Test

include("unit_examples.jl")
include("unit_parse.jl")
Expand Down

0 comments on commit 9f0f8bd

Please sign in to comment.