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

Use macros to define messages rather than eval #45

Merged
merged 22 commits into from
Feb 7, 2024
Merged
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
c6aabb1
refactor(msg): use macros to define messages rather than eval (WIP)
mchitre Jan 23, 2024
a3c5363
refactor: improve message implementation
mchitre Jan 28, 2024
d64b36a
fix: register messages defined in Fjage
mchitre Jan 29, 2024
f3eb9cb
style: cosmetic improvements to code
mchitre Jan 29, 2024
630f264
refactor: improve GenericMessages and JSONification
mchitre Jan 29, 2024
d429569
test: fix test cases and cosmetic improvements
mchitre Jan 29, 2024
02fb6ba
refactor: make invalid properties for messages throw errors unless in…
mchitre Jan 31, 2024
1b2a489
refactor: drop unnecessary property handling code
mchitre Jan 31, 2024
12c3fe9
test: fix fjage version for testing
mchitre Jan 31, 2024
eee6eef
fix(msg): drop eval for performative and let it be interpolated in
mchitre Feb 1, 2024
fe5470a
test: add test case that uses performative in message definition
mchitre Feb 1, 2024
51bcd26
fix(msg): only assume Fjage symbol to be in scope of caller
mchitre Feb 1, 2024
7a15935
feat(msg): improve pretty printing
mchitre Feb 1, 2024
bba1083
doc: update docs to reflect new message definition API
mchitre Feb 1, 2024
993e261
refactor: Escape more narrowly in @message
ettersi Feb 2, 2024
030e6c8
fix(kwdef): Copy over Base.isexpr
ettersi Feb 4, 2024
868f916
fix(container): update container to use new message format
mchitre Feb 4, 2024
0088453
fix(msg): handle NaNs, inheritance and fix readonly property in Param…
mchitre Feb 4, 2024
55c290e
refactor(msg): use _Concrete prefix and use applicable() instead of t…
mchitre Feb 5, 2024
9af5a95
refactor: replace reporterror with logerror
mchitre Feb 5, 2024
80bdf6c
fix(kwdef): Delete @show that was added for debugging
ettersi Feb 7, 2024
7c5afd6
docs(kwdef): xref PR
ettersi Feb 7, 2024
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
37 changes: 28 additions & 9 deletions src/msg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module Performative
end

"Base class for messages transmitted by one agent to another."
abstract type Message end
abstract type Message <: AbstractDict{Symbol,Any} end

"""
classname(msg::Message)
Expand All @@ -34,15 +34,18 @@ include("kwdef.jl")
function _message(classname, perf, sdef)
if @capture(sdef, struct T_ <: P_ fields__ end)
if T == P
extra2 = :( Fjage.classname(::Type{$(T)}) = $(classname) )
T = Symbol("_" * string(T))
mchitre marked this conversation as resolved.
Show resolved Hide resolved
extra = :( $(P)(; kwargs...) = $(T)(; kwargs...) )
extra1 = :( $(P)(; kwargs...) = $(T)(; kwargs...) )
else
extra = :()
extra1 = :()
extra2 = :()
end
T = esc(T)
P = esc(P)
fields .= esc.(fields)
extra = esc(extra)
extra1 = esc(extra1)
extra2 = esc(extra2)
push!(fields, :(messageID::String = string(uuid4())))
push!(fields, :(performative::Symbol = $perf))
push!(fields, :(sender::Union{AgentID,Nothing} = nothing))
Expand All @@ -54,7 +57,8 @@ function _message(classname, perf, sdef)
Fjage.classname(::Type{$(T)}) = $(classname)
Fjage.classname(::$(T)) = $(classname)
Fjage._messageclasses[$(classname)] = $(T)
$extra
$extra1
$extra2
end
elseif @capture(sdef, struct T_ fields__ end)
T = esc(T)
Expand Down Expand Up @@ -123,9 +127,14 @@ for the module.
function registermessages(msg=subtypes(Message))
for T ∈ msg
T <: GenericMessage && continue
s = classname(T)
_messageclasses[s] = T
registermessages(subtypes(T))
try
s = classname(T)
_messageclasses[s] = T
registermessages(subtypes(T))
catch
mchitre marked this conversation as resolved.
Show resolved Hide resolved
# types with no classname defined are abstract
# and do not need to be registered
end
end
end

Expand All @@ -151,7 +160,16 @@ end
function trysetproperty!(s::Message, p::Symbol, v)
hasfield(typeof(s), p) || return s
ftype = fieldtype(typeof(s), p)
setfield!(s, p, convert(ftype, v))
ftype === Symbol && (v = Symbol(v))
if v === nothing
ftype === Float32 && (v = NaN32)
ftype === Float64 && (v = NaN64)
end
try
setfield!(s, p, convert(ftype, v))
catch ex
@warn "Error setting field $p to $v: $ex"
mchitre marked this conversation as resolved.
Show resolved Hide resolved
end
end

# immutable dictionary interface for Messages
Expand Down Expand Up @@ -332,6 +350,7 @@ end
param::Union{String,Nothing} = nothing
value::Union{Any,Nothing} = nothing
values::Union{Dict{String,Any},Nothing} = nothing
readonly::Vector{String} = String[]
end

# convenience methods and pretty printing for parameters
Expand Down
Loading