@@ -11,7 +11,7 @@ Examples of use can be found in TestSymbolic.jl
11
11
module Symbolic
12
12
13
13
export removeBlock, makeDerVar, append, prepend, Incidence, findIncidence!, linearFactor, solveEquation,
14
- isLinear, getCoefficients, substitute, removeUnits, resetCounters
14
+ isLinear, getCoefficients, substitute, removeUnits, resetEventCounters, getEventCounters, substituteForEvents
15
15
16
16
using Base. Meta: isexpr
17
17
# using OrderedCollections
@@ -77,11 +77,31 @@ prepend(ex::Symbol, prefix::Nothing) = ex
77
77
prepend (arr:: Array{Expr,1} , prefix) = [prepend (a, prefix) for a in arr]
78
78
# prepend(dict::OrderedCollections.OrderedDict{Symbol,Expr}, prefix) = OrderedDict([prepend(k, prefix) => prepend(k, prefix) for (k,v) in dict])
79
79
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
+
80
100
nCrossingFunctions = 0
81
101
nClocks = 0
82
102
nSamples = 0
83
103
84
- function resetCounters ()
104
+ function resetEventCounters ()
85
105
global nCrossingFunctions
86
106
global nClocks
87
107
global nSamples
@@ -90,38 +110,37 @@ function resetCounters()
90
110
nSamples = 0
91
111
end
92
112
93
- function prepend (ex :: Expr , prefix )
113
+ function getEventCounters ( )
94
114
global nCrossingFunctions
95
115
global nClocks
96
116
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
106
128
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))
108
130
elseif ex. head == :call && ex. args[1 ] == :Clock
109
131
nClocks += 1
110
- :(Clock ($ (prepend (ex. args[2 ], prefix )), instantiatedModel, $ nClocks))
132
+ :(Clock ($ (substituteForEvents (ex. args[2 ])), instantiatedModel, $ nClocks))
111
133
elseif ex. head == :call && ex. args[1 ] == :sample
112
134
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))
114
136
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 ]]. .. )
116
138
end
117
- elseif ex. head == :macrocall
118
- eval (ex)
119
139
else
120
- Expr (ex. head, [prepend (arg, prefix ) for arg in ex. args]. .. )
140
+ Expr (ex. head, [substituteForEvents (arg) for arg in ex. args]. .. )
121
141
end
122
142
end
123
143
124
-
125
144
Incidence = Union{Symbol, Expr}
126
145
127
146
"""
@@ -183,6 +202,8 @@ function linearFactor(ex::Expr, x)
183
202
(ex, 0 , true )
184
203
elseif isexpr (ex, :call ) && ex. args[1 ] == :der
185
204
if ex == x; (0 , 1 , true ) else (ex, 0 , true ) end
205
+ elseif isexpr (ex, :call ) && ex. args[1 ] == :positive
206
+ (ex, 0 , true )
186
207
elseif isexpr (ex, :call )
187
208
func = ex. args[1 ]
188
209
arguments = ex. args[2 : end ]
0 commit comments