@@ -11,7 +11,7 @@ Examples of use can be found in TestSymbolic.jl
1111module Symbolic
1212
1313export removeBlock, makeDerVar, append, prepend, Incidence, findIncidence!, linearFactor, solveEquation,
14- isLinear, getCoefficients, substitute, removeUnits
14+ isLinear, getCoefficients, substitute, removeUnits, resetCounters
1515
1616using Base. Meta: isexpr
1717# using OrderedCollections
@@ -73,12 +73,27 @@ prepend(ex, prefix) = ex
7373# prepend(ex::Symbol, prefix::Array{Symbol,1}) = if length(prefix) == 0; ex else prepend(prepend(ex, prefix[end]), prefix[1:end-1]) end
7474# prepend(ex::QuoteNode, prefix::Symbol) = Expr(:., prefix, QuoteNode(ex))
7575prepend (ex:: Symbol , prefix) = if prefix == nothing ; ex elseif ex in [:time , :sim , :leq_mode ]; ex else Expr (:., prefix, QuoteNode (ex)) end # TODO : Fix sim
76- # prepend(ex::Symbol, prefix::Nothing) = (println("Prepend"); ex)
76+ prepend (ex:: Symbol , prefix:: Nothing ) = ex
7777prepend (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+ nCrossingFunctions = 0
81+ nClocks = 0
82+ nSamples = 0
83+
84+ function resetCounters ()
85+ global nCrossingFunctions
86+ global nClocks
87+ global nSamples
88+ nCrossingFunctions = 0
89+ nClocks = 0
90+ nSamples = 0
91+ end
92+
8093function prepend (ex:: Expr , prefix)
8194 global nCrossingFunctions
95+ global nClocks
96+ global nSamples
8297 if typeof (prefix) == Array{Any,1 } && length (prefix) == 0
8398 ex
8499 elseif ex. head == :. && ex. args[1 ] == :up
@@ -89,7 +104,13 @@ function prepend(ex::Expr, prefix)
89104 :($ prefix.$ e)
90105 elseif ex. head == :call && ex. args[1 ] == :positive
91106 nCrossingFunctions += 1
92- :(ModiaMath. positive! (sim, $ nCrossingFunctions, $ (prepend (ex. args[2 ], prefix)), $ (string (prepend (ex. args[2 ], prefix))), leq_mode= leq_mode))
107+ :(positive (instantiatedModel, $ nCrossingFunctions, $ (prepend (ex. args[2 ], prefix)), $ (string (prepend (ex. args[2 ], prefix))), _leq_mode))
108+ elseif ex. head == :call && ex. args[1 ] == :Clock
109+ nClocks += 1
110+ :(Clock ($ (prepend (ex. args[2 ], prefix)), instantiatedModel, $ nClocks))
111+ elseif ex. head == :call && ex. args[1 ] == :sample
112+ nSamples += 1
113+ :(sample ($ (prepend (ex. args[2 ], prefix)), $ (prepend (ex. args[3 ], prefix)), instantiatedModel, $ nSamples))
93114 else
94115 Expr (ex. head, ex. args[1 ], [prepend (arg, prefix) for arg in ex. args[2 : end ]]. .. )
95116 end
@@ -112,7 +133,7 @@ Traverses an expression and finds incidences of Symbols and der(...)
112133* `incidence`: array of incidences. New incidences of `ex` are pushed.
113134"""
114135findIncidence! (ex, incidence:: Array{Incidence,1} ) = nothing
115- findIncidence! (s:: Symbol , incidence:: Array{Incidence,1} ) = push! (incidence, s)
136+ findIncidence! (s:: Symbol , incidence:: Array{Incidence,1} ) = begin if s != :(:); push! (incidence, s) end end
116137findIncidence! (arr:: Array{Any,1} , incidence:: Array{Incidence,1} ) = [findIncidence! (a, incidence) for a in arr]
117138findIncidence! (arr:: Array{Expr,1} , incidence:: Array{Incidence,1} ) = [findIncidence! (a, incidence) for a in arr]
118139function findIncidence! (ex:: Expr , incidence:: Array{Incidence,1} )
0 commit comments