Skip to content

Commit

Permalink
engine(lua): Add cloe.alias_signals and rework cloe.require_signals_enum
Browse files Browse the repository at this point in the history
  • Loading branch information
cassava committed Oct 20, 2023
1 parent 35bf417 commit c76a9dc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 27 deletions.
36 changes: 27 additions & 9 deletions engine/lua/cloe/engine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ function engine.log(level, fmt, ...)
api.log(level, "lua", msg)
end

--- Alias a set of signals in the Cloe data broker.
---
--- @param list table
--- @return table
function engine.alias_signals(list)
api.initial_input.signal_aliases = luax.tbl_extend("force", api.initial_input.signal_aliases, list)
return api.initial_input.signal_aliases
end

--- Require a set of signals to be made available via the Cloe data broker.
---
--- @param list table signals to merge into main list of required signals
Expand All @@ -116,31 +125,40 @@ function engine.require_signals(list)
return api.initial_input.signal_requires
end

--- Require a set of signals from a signals enum list.
--- Optionally alias and require a set of signals from a signals enum list.
---
--- This allows you to make an enum somewhere which the language server
--- can use for autocompletion and which you can use as an alias:
---
--- ---@enum Sig
--- local Sig = {
--- DriverDoorLatch = "vehicle::framework::chassis::driver_door::latch",
--- DriverDoorLatch = "vehicle::framework::chassis::.*driver_door::latch",
--- VehicleMps = "vehicle::sensors::chassis::velocity",
--- }
--- cloe.require_signals_enum(Sig)
--- cloe.require_signals_enum(Sig, true)
---
--- Later, you can use the enum with cloe.signal():
---
--- cloe.signal(Sig.DriverDoorLatch)
---
--- @deprecated EXPERIMENTAL
--- @param enum table
--- @return table
function engine.require_signals_enum(enum)
--- @param alias boolean whether to treat signal names as alias regular expressions
--- @return nil
function engine.require_signals_enum(enum, alias)
local signals = {}
for _, v in pairs(enum) do
table.insert(signals, v)
if alias then
local aliases = {}
for key, sigregex in pairs(enum) do
table.insert(aliases, {sigregex, key})
table.insert(signals, key)
end
engine.alias_signals(aliases)
else
for _, signame in pairs(enum) do
table.insert(signals, signame)
end
end
return engine.require_signals(signals)
engine.require_signals(signals)
end

--- Return full list of loaded signals.
Expand Down
10 changes: 8 additions & 2 deletions engine/tests/test_lua05_apply_stack.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ cloe.schedule_these {
{ run = "realtime_factor=-1" },
}

cloe.schedule { on = "time=60", run = "succeed" }

cloe.schedule {
on = "loop",
pin = true,
Expand All @@ -36,6 +34,14 @@ cloe.schedule {
end
}

cloe.schedule_test {
id = "precondition",
on = "start",
run = function(z)
z:printf("hello there")
end
}

-- Check that schedule_test works as intended.
cloe.schedule_test {
-- Note that this is the same ID as used in BATS.
Expand Down
25 changes: 9 additions & 16 deletions engine/tests/test_lua08_apply_project.lua
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
local cloe = require("cloe")
local proj = cloe.require("project")

proj.configure_all {
with_server = false,
with_noisy_sensor = true,
}

-- All the conditions we want to fail on:
cloe.schedule_these {
run = "fail",
{ on = "time=5" },
}

-- All the things we want to do on start:
proj.set_realtime_factor(-1)
cloe.schedule { on = "time=60", run = "succeed" }
do
local proj = cloe.require("project")
proj.configure_all {
with_server = false,
with_noisy_sensor = true,
}
proj.set_realtime_factor(-1)
end

cloe.schedule {
on = "loop",
Expand All @@ -34,7 +27,7 @@ cloe.schedule_test {
--- @param sync Sync
run = function(z, sync)
z:printf("Entering test")
z:assert(true)
z:expect("string")

z:printf("Asserting something...")
z:assert_eq(sync:time():s(), 0, "time is 0s at start")
Expand Down

0 comments on commit c76a9dc

Please sign in to comment.