Skip to content

Commit ae18706

Browse files
Introduced substituteForEvents.
1 parent fc5075a commit ae18706

File tree

1 file changed

+33
-19
lines changed

1 file changed

+33
-19
lines changed

src/Symbolic.jl

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Examples of use can be found in TestSymbolic.jl
1111
module Symbolic
1212

1313
export removeBlock, makeDerVar, append, prepend, Incidence, findIncidence!, linearFactor, solveEquation,
14-
isLinear, getCoefficients, substitute, removeUnits, resetCounters
14+
isLinear, getCoefficients, substitute, removeUnits, resetCounters, substituteForEvents
1515

1616
using Base.Meta: isexpr
1717
#using OrderedCollections
@@ -77,6 +77,26 @@ prepend(ex::Symbol, prefix::Nothing) = ex
7777
prepend(arr::Array{Expr,1}, prefix) = [prepend(a, prefix) for a in arr]
7878
#prepend(dict::OrderedCollections.OrderedDict{Symbol,Expr}, prefix) = OrderedDict([prepend(k, prefix) => prepend(k, prefix) for (k,v) in dict])
7979

80+
function prepend(ex::Expr, prefix)
81+
if typeof(prefix) == Array{Any,1} && length(prefix) == 0
82+
ex
83+
elseif ex.head == :. && ex.args[1] == :up
84+
ex.args[2].value
85+
elseif ex.head in [:call, :kw]
86+
if false #ex.head == :call && ex.args[1] == :der
87+
e = Symbol(ex)
88+
:($prefix.$e)
89+
else
90+
Expr(ex.head, ex.args[1], [prepend(arg, prefix) for arg in ex.args[2:end]]...)
91+
end
92+
elseif ex.head == :macrocall
93+
eval(ex)
94+
else
95+
Expr(ex.head, [prepend(arg, prefix) for arg in ex.args]...)
96+
end
97+
end
98+
99+
80100
nCrossingFunctions = 0
81101
nClocks = 0
82102
nSamples = 0
@@ -90,38 +110,30 @@ function resetCounters()
90110
nSamples = 0
91111
end
92112

93-
function prepend(ex::Expr, prefix)
113+
substituteForEvents(ex) = ex
114+
115+
function substituteForEvents(ex::Expr)
94116
global nCrossingFunctions
95117
global nClocks
96118
global nSamples
97-
if typeof(prefix) == Array{Any,1} && length(prefix) == 0
98-
ex
99-
elseif ex.head == :. && ex.args[1] == :up
100-
ex.args[2].value
101-
elseif ex.head in [:call, :kw]
102-
if false #ex.head == :call && ex.args[1] == :der
103-
e = Symbol(ex)
104-
:($prefix.$e)
105-
elseif ex.head == :call && ex.args[1] == :positive
119+
if ex.head in [:call, :kw]
120+
if ex.head == :call && ex.args[1] == :positive
106121
nCrossingFunctions += 1
107-
:(positive(instantiatedModel, $nCrossingFunctions, $(prepend(ex.args[2], prefix)), $(string(prepend(ex.args[2], prefix))), _leq_mode))
122+
:(positive(instantiatedModel, $nCrossingFunctions, $(substituteForEvents(ex.args[2])), $(string(substituteForEvents(ex.args[2]))), _leq_mode))
108123
elseif ex.head == :call && ex.args[1] == :Clock
109124
nClocks += 1
110-
:(Clock($(prepend(ex.args[2], prefix)), instantiatedModel, $nClocks))
125+
:(Clock($(substituteForEvents(ex.args[2])), instantiatedModel, $nClocks))
111126
elseif ex.head == :call && ex.args[1] == :sample
112127
nSamples += 1
113-
:(sample($(prepend(ex.args[2], prefix)), $(prepend(ex.args[3], prefix)), instantiatedModel, $nSamples))
128+
:(sample($(substituteForEvents(ex.args[2])), $(substituteForEvents(ex.args[3])), instantiatedModel, $nSamples))
114129
else
115-
Expr(ex.head, ex.args[1], [prepend(arg, prefix) for arg in ex.args[2:end]]...)
130+
Expr(ex.head, ex.args[1], [substituteForEvents(arg) for arg in ex.args[2:end]]...)
116131
end
117-
elseif ex.head == :macrocall
118-
eval(ex)
119132
else
120-
Expr(ex.head, [prepend(arg, prefix) for arg in ex.args]...)
133+
Expr(ex.head, [substituteForEvents(arg) for arg in ex.args]...)
121134
end
122135
end
123136

124-
125137
Incidence = Union{Symbol, Expr}
126138

127139
"""
@@ -183,6 +195,8 @@ function linearFactor(ex::Expr, x)
183195
(ex, 0, true)
184196
elseif isexpr(ex, :call) && ex.args[1] == :der
185197
if ex == x; (0, 1, true) else (ex, 0, true) end
198+
elseif isexpr(ex, :call) && ex.args[1] == :positive
199+
(ex, 0, true)
186200
elseif isexpr(ex, :call)
187201
func = ex.args[1]
188202
arguments = ex.args[2:end]

0 commit comments

Comments
 (0)