|
| 1 | +_nameof(s) = nameof(s) |
| 2 | +_nameof(s::Union{Int, Symbol}) = s |
| 3 | +abstract type StateMachineOperator end |
| 4 | +Base.broadcastable(x::StateMachineOperator) = Ref(x) |
| 5 | +Symbolics.hide_lhs(_::StateMachineOperator) = true |
| 6 | +struct InitialState <: StateMachineOperator |
| 7 | + s::Any |
| 8 | +end |
| 9 | +Base.show(io::IO, s::InitialState) = print(io, "initial_state(", _nameof(s.s), ")") |
| 10 | +initial_state(s) = Equation(InitialState(nothing), InitialState(s)) |
| 11 | + |
| 12 | +Base.@kwdef struct Transition{A, B, C} <: StateMachineOperator |
| 13 | + from::A = nothing |
| 14 | + to::B = nothing |
| 15 | + cond::C = nothing |
| 16 | + immediate::Bool = true |
| 17 | + reset::Bool = true |
| 18 | + synchronize::Bool = false |
| 19 | + priority::Int = 1 |
| 20 | + function Transition(from, to, cond, immediate, reset, synchronize, priority) |
| 21 | + cond = unwrap(cond) |
| 22 | + new{typeof(from), typeof(to), typeof(cond)}(from, to, cond, immediate, |
| 23 | + reset, synchronize, |
| 24 | + priority) |
| 25 | + end |
| 26 | +end |
| 27 | +function Base.:(==)(transition1::Transition, transition2::Transition) |
| 28 | + transition1.from == transition2.from && |
| 29 | + transition1.to == transition2.to && |
| 30 | + isequal(transition1.cond, transition2.cond) && |
| 31 | + transition1.immediate == transition2.immediate && |
| 32 | + transition1.reset == transition2.reset && |
| 33 | + transition1.synchronize == transition2.synchronize && |
| 34 | + transition1.priority == transition2.priority |
| 35 | +end |
| 36 | + |
| 37 | +""" |
| 38 | + transition(from, to, cond; immediate::Bool = true, reset::Bool = true, synchronize::Bool = false, priority::Int = 1) |
| 39 | +
|
| 40 | +Create a transition from state `from` to state `to` that is enabled when transitioncondition `cond` evaluates to `true`. |
| 41 | +
|
| 42 | +# Arguments: |
| 43 | +- `from`: The source state of the transition. |
| 44 | +- `to`: The target state of the transition. |
| 45 | +- `cond`: A transition condition that evaluates to a Bool, such as `ticksInState() >= 2`. |
| 46 | +- `immediate`: If `true`, the transition will fire at the same tick as it becomes true, if `false`, the actions of the state are evaluated first, and the transition fires during the next tick. |
| 47 | +- `reset`: If true, the destination state `to` is reset to its initial condition when the transition fires. |
| 48 | +- `synchronize`: If true, the transition will only fire if all sub-state machines in the source state are in their final (terminal) state. A final state is one that has no outgoing transitions. |
| 49 | +- `priority`: If a state has more than one outgoing transition, all outgoing transitions must have a unique priority. The transitions are evaluated in priority order, i.e., the transition with priority 1 is evaluated first. |
| 50 | +""" |
| 51 | +function transition(from, to, cond; |
| 52 | + immediate::Bool = true, reset::Bool = true, synchronize::Bool = false, |
| 53 | + priority::Int = 1) |
| 54 | + Equation( |
| 55 | + Transition(), Transition(; from, to, cond, immediate, reset, |
| 56 | + synchronize, priority)) |
| 57 | +end |
| 58 | +function Base.show(io::IO, s::Transition) |
| 59 | + print(io, _nameof(s.from), " → ", _nameof(s.to), " if (", s.cond, ") [") |
| 60 | + print(io, "immediate: ", Int(s.immediate), ", ") |
| 61 | + print(io, "reset: ", Int(s.reset), ", ") |
| 62 | + print(io, "sync: ", Int(s.synchronize), ", ") |
| 63 | + print(io, "prio: ", s.priority, "]") |
| 64 | +end |
| 65 | + |
| 66 | +function activeState end |
| 67 | +function entry end |
| 68 | +function ticksInState end |
| 69 | +function timeInState end |
| 70 | + |
| 71 | +for (s, T) in [(:timeInState, :Real), |
| 72 | + (:ticksInState, :Integer), |
| 73 | + (:entry, :Bool), |
| 74 | + (:activeState, :Bool)] |
| 75 | + seed = hash(s) |
| 76 | + @eval begin |
| 77 | + $s(x) = wrap(term($s, x)) |
| 78 | + SymbolicUtils.promote_symtype(::typeof($s), _...) = $T |
| 79 | + function SymbolicUtils.show_call(io, ::typeof($s), args) |
| 80 | + if isempty(args) |
| 81 | + print(io, $s, "()") |
| 82 | + else |
| 83 | + arg = only(args) |
| 84 | + print(io, $s, "(", arg isa Number ? arg : nameof(arg), ")") |
| 85 | + end |
| 86 | + end |
| 87 | + end |
| 88 | + if s != :activeState |
| 89 | + @eval $s() = wrap(term($s)) |
| 90 | + end |
| 91 | +end |
| 92 | + |
| 93 | +@doc """ |
| 94 | + timeInState() |
| 95 | + timeInState(state) |
| 96 | +
|
| 97 | +Get the time (in seconds) spent in a state in a finite state machine. |
| 98 | +
|
| 99 | +When used to query the time spent in the enclosing state, the method without arguments is used, i.e., |
| 100 | +```julia |
| 101 | +@mtkmodel FSM begin |
| 102 | + ... |
| 103 | + @equations begin |
| 104 | + var(k+1) ~ timeInState() >= 2 ? 0.0 : var(k) |
| 105 | + end |
| 106 | +end |
| 107 | +``` |
| 108 | +
|
| 109 | +If used to query the residence time of another state, the state is passed as an argument. |
| 110 | +
|
| 111 | +This operator can be used in both equations and transition conditions. |
| 112 | +
|
| 113 | +See also [`ticksInState`](@ref) and [`entry`](@ref) |
| 114 | +""" timeInState |
| 115 | + |
| 116 | +@doc """ |
| 117 | + ticksInState() |
| 118 | + ticksInState(state) |
| 119 | +
|
| 120 | +Get the number of ticks spent in a state in a finite state machine. |
| 121 | +
|
| 122 | +When used to query the number of ticks spent in the enclosing state, the method without arguments is used, i.e., |
| 123 | +```julia |
| 124 | +@mtkmodel FSM begin |
| 125 | + ... |
| 126 | + @equations begin |
| 127 | + var(k+1) ~ ticksInState() >= 2 ? 0.0 : var(k) |
| 128 | + end |
| 129 | +end |
| 130 | +``` |
| 131 | +
|
| 132 | +If used to query the number of ticks in another state, the state is passed as an argument. |
| 133 | +
|
| 134 | +This operator can be used in both equations and transition conditions. |
| 135 | +
|
| 136 | +See also [`timeInState`](@ref) and [`entry`](@ref) |
| 137 | +""" ticksInState |
| 138 | + |
| 139 | +@doc """ |
| 140 | + entry() |
| 141 | + entry(state) |
| 142 | +
|
| 143 | +When used in a finite-state machine, this operator returns true at the first tick when the state is active, and false otherwise. |
| 144 | +
|
| 145 | +When used to query the entry of the enclosing state, the method without arguments is used, when used to query the entry of another state, the state is passed as an argument. |
| 146 | +
|
| 147 | +This can be used to perform a unique action when entering a state. |
| 148 | +""" |
| 149 | +entry |
| 150 | + |
| 151 | +@doc """ |
| 152 | + activeState(state) |
| 153 | +
|
| 154 | +When used in a finite state machine, this operator returns `true` if the queried state is active and false otherwise. |
| 155 | +""" activeState |
0 commit comments