Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterwln committed Apr 10, 2024
2 parents cdf370c + 7ef0e32 commit 3aa237d
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 31 deletions.
18 changes: 9 additions & 9 deletions benchmark/benchmarks.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using BenchmarkTools

import RxEnvironments: __send!
const SUITE = BenchmarkGroup()

include("mockenv.jl")
Expand All @@ -9,8 +9,8 @@ function simple_discrete_benchmarks()
SUITE = BenchmarkGroup(["Simple Discrete Environment"])
env = RxEnvironment(MockEnvironment(); is_discrete=true)
agent = add!(env, MockEntity())
SUITE["send to agent"] = @benchmarkable send!($agent, $env, 10.0)
SUITE["send to environment"] = @benchmarkable send!($env, $agent, 10.0)
SUITE["send to agent"] = @benchmarkable __send!($agent, $env, 10.0)
SUITE["send to environment"] = @benchmarkable __send!($env, $agent, 10.0)
return SUITE
end

Expand All @@ -25,17 +25,17 @@ function complex_discrete_benchmarks()
add!(hearing_aid, agent)
add!(hearing_aid, user)

SUITE["send to agent"] = @benchmarkable send!($hearing_aid, $world, view([1.0, 2.0, 3.0], 1:2))
SUITE["send to environment"] = @benchmarkable send!($world, $hearing_aid, 10.0)
SUITE["send to agent"] = @benchmarkable __send!($hearing_aid, $world, view([1.0, 2.0, 3.0], 1:2))
SUITE["send to environment"] = @benchmarkable __send!($world, $hearing_aid, 10.0)
return SUITE
end

function simple_continuous_benchmarks()
SUITE = BenchmarkGroup(["Simple Continuous Environment"])
env = RxEnvironment(MockEnvironment(); is_discrete=false)
agent = add!(env, MockEntity())
SUITE["send to agent"] = @benchmarkable send!($agent, $env, 10.0)
SUITE["send to environment"] = @benchmarkable send!($env, $agent, 10.0)
SUITE["send to agent"] = @benchmarkable __send!($agent, $env, 10.0)
SUITE["send to environment"] = @benchmarkable __send!($env, $agent, 10.0)
return SUITE
end

Expand All @@ -50,8 +50,8 @@ function complex_continuous_benchmarks()
add!(hearing_aid, agent)
add!(hearing_aid, user)

SUITE["send to agent"] = @benchmarkable send!($hearing_aid, $world, view([1.0, 2.0, 3.0], 1:2))
SUITE["send to environment"] = @benchmarkable send!($world, $hearing_aid, 10.0)
SUITE["send to agent"] = @benchmarkable __send!($hearing_aid, $world, view([1.0, 2.0, 3.0], 1:2))
SUITE["send to environment"] = @benchmarkable __send!($world, $hearing_aid, 10.0)
return SUITE
end

Expand Down
18 changes: 17 additions & 1 deletion src/abstractentity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,23 @@ function send!(
recipient::Union{AbstractEntity,Rocket.Actor},
emitter::AbstractEntity,
action::Any,
)
if recipient subscribers(emitter)
__send!(recipient, emitter, action)
else
throw(NotSubscribedException(emitter, recipient))
end
end

"""
__send!(recipient::AbstractEntity, emitter::AbstractEntity, action::Any)
Send an action from `emitter` to `recipient`.
"""
function __send!(
recipient::Union{AbstractEntity,Rocket.Actor},
emitter::AbstractEntity,
action::Any,
)
actuator = get_actuator(emitter, recipient)::Actuator{action_type(decorated(emitter))}
send_action!(actuator, action)
Expand Down Expand Up @@ -272,7 +289,6 @@ receive!(recipient::AbstractEntity, observation::Observation) =
receive!(recipient::AbstractEntity, emitter::AbstractEntity, observation::Any) =
receive!(decorated(recipient), decorated(emitter), observation)
receive!(recipient::AbstractEntity, obs::TimerMessage) = receive!(decorated(recipient), obs)
receive!(recipient::AbstractEntity, observation::Any) = nothing
receive!(recipient, emitter, observation) = nothing
receive!(recipient, observation) = nothing

Expand Down
1 change: 0 additions & 1 deletion src/exceptions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,5 @@ Base.showerror(io::IO, e::SelfSubscriptionException) =

mutable struct NotPausedException <: Exception end


Base.showerror(io::IO, e::NotPausedException) =
print(io, "Trying to access paused time for unpaused entity")
2 changes: 0 additions & 2 deletions src/markovblanket.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ end

Rocket.subscribe!(observations::Observations, actor::Rocket.Actor{T} where {T}) =
subscribe!(target(observations), actor)
Rocket.subscribe!(observations::Observations, actor::F where {F<:AbstractActorFactory}) =
subscribe!(target(observations) |> map(Any, (x) -> data(x)), actor)

Rocket.next!(
observations::Observations{ContinuousEntity,<:T},
Expand Down
2 changes: 1 addition & 1 deletion src/rxentity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function Rocket.on_next!(
foreach(subscribers(subject)) do listener
if emits(subject, listener, observation)
action = what_to_send(listener, subject, observation)
send!(listener, subject, action)
__send!(listener, subject, action)
end
end
end
Expand Down
8 changes: 6 additions & 2 deletions test/entity_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
e = create_entity(MockEntity())
@test !is_active(e)
@test e isa RxEnvironments.RxEntity{MockEntity}
@test occursin(r"Continuous", repr(e))
end

@testset "decorated" begin
Expand Down Expand Up @@ -190,7 +191,7 @@

unsubscribe!(first_entity, second_entity)
@test !is_subscribed(second_entity, first_entity)
@test_throws KeyError send!(
@test_throws RxEnvironments.NotSubscribedException send!(
second_entity,
first_entity,
1,
Expand Down Expand Up @@ -372,6 +373,7 @@ end
@testset "constructor" begin
e = create_entity(MockEntity(); is_discrete=true)
@test e isa RxEnvironments.RxEntity{MockEntity}
@test occursin(r"Discrete", repr(e))
end

@testset "markov blanket functionality" begin
Expand All @@ -398,6 +400,8 @@ end
@test last(obs) === RxEnvironments.ObservationCollection((
RxEnvironments.Observation(second_entity, nothing),
))
@test length(last(obs)) == 1
@test data(last(obs)) === nothing
@test data.(obs.values) == [nothing]

next!(
Expand Down Expand Up @@ -497,7 +501,7 @@ end

unsubscribe!(first_entity, second_entity)
@test !is_subscribed(second_entity, first_entity)
@test_throws KeyError send!(
@test_throws RxEnvironments.NotSubscribedException send!(
second_entity,
first_entity,
1,
Expand Down
14 changes: 14 additions & 0 deletions test/environment_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@
terminate!(env)
end
end

@testset "keep actor" begin
using Rocket
using RxEnvironments

let e = RxEnvironment(MockEntity())
other = create_entity(MockEntity())
add!(e, other)
actor = keep(Any)
subscribe!(e, actor)
send!(e, other, 1)
@test last(actor) == nothing
end
end
end

@testitem "discrete environment" begin
Expand Down
23 changes: 22 additions & 1 deletion test/exceptions_tests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

@testitem "NotSubscribedException" begin
using RxEnvironments

Expand All @@ -12,6 +11,9 @@
@test origin(exception) === env
@test recipient(exception) === agent
@test_throws NotSubscribedException throw(exception)
buf = IOBuffer()
showerror(buf, exception)
@test occursin(r"Entity (.*?) is not subscribed to (.*?)", String(take!(buf)))
end

end
Expand All @@ -29,6 +31,9 @@ end
@test first_entity(exception) === env
@test second_entity(exception) === agent
@test_throws MixedStateSpaceException throw(exception)
buf = IOBuffer()
showerror(buf, exception)
@test occursin(r"Entities (.*?) and (.*?) have different state spaces", String(take!(buf)))
end
end

Expand All @@ -43,5 +48,21 @@ end
@test exception isa SelfSubscriptionException
@test entity(exception) === env
@test_throws SelfSubscriptionException throw(exception)
buf = IOBuffer()
showerror(buf, exception)
@test occursin(r"Entity cannot subscribe to itself, attempted in (.*?)", String(take!(buf)))
end
end

@testitem "NotPausedException" begin
using RxEnvironments

import RxEnvironments: NotPausedException
let exception = NotPausedException()
@test exception isa NotPausedException
@test_throws NotPausedException throw(exception)
buf = IOBuffer()
showerror(buf, exception)
@test occursin(r"Trying to access paused time for unpaused entity", String(take!(buf)))
end
end
25 changes: 11 additions & 14 deletions test/markovblanket_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ end
@test !is_subscribed(agent, env)
@test is_subscribed(second_agent, env)
@test length(subscribers(env)) == 1
@test_throws KeyError send!(env, agent, 10)
@test_throws RxEnvironments.NotSubscribedException send!(env, agent, 10)

unsubscribe!(env, second_agent)
@test !is_subscribed(agent, env)
Expand Down Expand Up @@ -139,16 +139,13 @@ end
end
end

# @testitem "send branching" begin
# using RxEnvironments
# import RxEnvironments: DiscreteEntity, create_entity
# include("../mockenvironment.jl")
# let env = RxEnvironment(MockEnvironment(0.0); discrete = true)
# agent = add!(env, MockAgent())
# obs = subscribe_to_observations!(agent, RxEnvironments.keep(Any))
# send!(env, agent, 10)
# send!(env, agent, 10.0)
# @test obs.values[1] == "Integer"

# end
# end
@testitem "show" begin
using RxEnvironments

include("mockenvironment.jl")

let e = RxEnvironment(MockEntity())
markov_blanket = RxEnvironments.markov_blanket(e)
@test occursin(r"MarkovBlanket{RxEnvironments.ContinuousEntity}", repr(markov_blanket))
end
end

4 comments on commit 3aa237d

@wouterwln
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator Register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error while trying to register: Action not recognized: Register

@wouterwln
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/104636

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.9 -m "<description of version>" 3aa237df9f093b9317c563a7050a27c08c472d9d
git push origin v0.2.9

Please sign in to comment.