Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StateMachineOperators #1047

Merged
merged 3 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symbolics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export parse_expr_to_symbolic

include("error_hints.jl")
include("struct.jl")
include("operators.jl")

# Hacks to make wrappers "nicer"
const NumberTypes = Union{AbstractFloat,Integer,Complex{<:AbstractFloat},Complex{<:Integer}}
Expand Down
59 changes: 58 additions & 1 deletion src/equations.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
const NAMESPACE_SEPARATOR = '₊'

hide_lhs(_) = false

###
### Connection
###
struct Connection
systems
end
Connection() = Connection(nothing)
hide_lhs(_::Connection) = true

function connect(sys1, sys2, syss...)
syss = (sys1, sys2, syss...)
Expand All @@ -22,6 +28,57 @@
print(io, ")")
end

###
### State machine
###
_nameof(s) = nameof(s)
_nameof(s::Union{Int, Symbol}) = s
abstract type StateMachineOperator end
hide_lhs(_::StateMachineOperator) = true
struct InitialState <: StateMachineOperator
s
end
Base.show(io::IO, s::InitialState) = print(io, "initial_state(", _nameof(s.s), ")")
initial_state(s) = Equation(InitialState(nothing), InitialState(s))

Base.@kwdef struct Transition{A, B, C} <: StateMachineOperator
from::A = nothing
to::B = nothing
cond::C = nothing
immediate::Bool = true
reset::Bool = true
synchronize::Bool = false
priority::Int = 1
function Transition(from, to, cond, immediate, reset, synchronize, priority)
cond = unwrap(cond)
new{typeof(from), typeof(to), typeof(cond)}(from, to, cond, immediate,
reset, synchronize,
priority)
end
end
function Base.:(==)(transtion1::Transition, transition2::Transition)

Check warning on line 59 in src/equations.jl

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"transtion" should be "transition".
transition1.from == transition2.from &&
transition1.to == transition2.to &&
isequal(transition1.cond, transition2.cond) &&
transition1.immediate == transition2.immediate &&
transition1.reset == transition2.reset &&
transition1.synchronize == transition2.synchronize &&
transition1.priority == transition2.priority
end
function transition(from, to, cond;
immediate::Bool = true, reset::Bool = true, synchronize::Bool = false,
priority::Int = 1)
Equation(Transition(), Transition(; from, to, cond, immediate, reset,
synchronize, priority))
end
function Base.show(io::IO, s::Transition)
print(io, _nameof(s.from), " → ", _nameof(s.to), " [")
print(io, "immediate: ", s.immediate, ", ")
print(io, "reset: ", s.reset, ", ")
print(io, "synchronize: ", s.synchronize, ", ")
print(io, "priority: ", s.priority, "]")
end

"""
$(TYPEDEF)

Expand All @@ -43,7 +100,7 @@
Base.hash(a::Equation, salt::UInt) = hash(a.lhs, hash(a.rhs, salt))

function Base.show(io::IO, eq::Equation)
if eq.lhs isa Connection
if hide_lhs(eq.lhs)
show(io, eq.rhs)
else
print(io, eq.lhs, " ~ ", eq.rhs)
Expand Down
25 changes: 25 additions & 0 deletions src/operators.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function activeState end
function entry end
function ticksInState end
function timeInState end

for (s, T) in [(:timeInState, :Real),
(:ticksInState, :Integer),
(:entry, :Bool),
(:activeState, :Bool)]
seed = hash(s)
@eval begin
$s(x) = wrap(term($s, x))
SymbolicUtils.promote_symtype(::typeof($s), _...) = $T
function SymbolicUtils.show_call(io, ::typeof($s), args)
if isempty(args)
print(io, $s, "()")
else
print(io, $s, "(", nameof(only(args)), ")")
end
end
end
if s != :activeState
@eval $s() = wrap(term($s))
end
end
Loading