Skip to content

Commit d47eb46

Browse files
committed
Merge branch 'development'
2 parents cff94f8 + 7da8649 commit d47eb46

File tree

1 file changed

+41
-20
lines changed

1 file changed

+41
-20
lines changed

src/Symbolic.jl

Lines changed: 41 additions & 20 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, resetEventCounters, getEventCounters, substituteForEvents
1515

1616
using Base.Meta: isexpr
1717
#using OrderedCollections
@@ -77,11 +77,31 @@ 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
83103

84-
function resetCounters()
104+
function resetEventCounters()
85105
global nCrossingFunctions
86106
global nClocks
87107
global nSamples
@@ -90,38 +110,37 @@ function resetCounters()
90110
nSamples = 0
91111
end
92112

93-
function prepend(ex::Expr, prefix)
113+
function getEventCounters()
94114
global nCrossingFunctions
95115
global nClocks
96116
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
117+
return (nCrossingFunctions, nClocks, nSamples)
118+
end
119+
120+
substituteForEvents(ex) = ex
121+
122+
function substituteForEvents(ex::Expr)
123+
global nCrossingFunctions
124+
global nClocks
125+
global nSamples
126+
if ex.head in [:call, :kw]
127+
if ex.head == :call && ex.args[1] == :positive
106128
nCrossingFunctions += 1
107-
:(positive(instantiatedModel, $nCrossingFunctions, $(prepend(ex.args[2], prefix)), $(string(prepend(ex.args[2], prefix))), _leq_mode))
129+
:(positive(instantiatedModel, $nCrossingFunctions, $(substituteForEvents(ex.args[2])), $(string(substituteForEvents(ex.args[2]))), _leq_mode))
108130
elseif ex.head == :call && ex.args[1] == :Clock
109131
nClocks += 1
110-
:(Clock($(prepend(ex.args[2], prefix)), instantiatedModel, $nClocks))
132+
:(Clock($(substituteForEvents(ex.args[2])), instantiatedModel, $nClocks))
111133
elseif ex.head == :call && ex.args[1] == :sample
112134
nSamples += 1
113-
:(sample($(prepend(ex.args[2], prefix)), $(prepend(ex.args[3], prefix)), instantiatedModel, $nSamples))
135+
:(sample($(substituteForEvents(ex.args[2])), $(substituteForEvents(ex.args[3])), instantiatedModel, $nSamples))
114136
else
115-
Expr(ex.head, ex.args[1], [prepend(arg, prefix) for arg in ex.args[2:end]]...)
137+
Expr(ex.head, ex.args[1], [substituteForEvents(arg) for arg in ex.args[2:end]]...)
116138
end
117-
elseif ex.head == :macrocall
118-
eval(ex)
119139
else
120-
Expr(ex.head, [prepend(arg, prefix) for arg in ex.args]...)
140+
Expr(ex.head, [substituteForEvents(arg) for arg in ex.args]...)
121141
end
122142
end
123143

124-
125144
Incidence = Union{Symbol, Expr}
126145

127146
"""
@@ -183,6 +202,8 @@ function linearFactor(ex::Expr, x)
183202
(ex, 0, true)
184203
elseif isexpr(ex, :call) && ex.args[1] == :der
185204
if ex == x; (0, 1, true) else (ex, 0, true) end
205+
elseif isexpr(ex, :call) && ex.args[1] == :positive
206+
(ex, 0, true)
186207
elseif isexpr(ex, :call)
187208
func = ex.args[1]
188209
arguments = ex.args[2:end]

0 commit comments

Comments
 (0)