local OtxScriptingSupportExample = {}
--[[OtxDeviceService: in Integer length, out List<Integer>]]
function OtxScriptingSupportExample.RandomList(n)
local t = {}
for i = 1, n do
t[i] = math.random(1, 1000000)
end
return t
end
--[[OtxDeviceService: in List<String> arr, out String]]
function OtxScriptingSupportExample.StringConcat(arr)
return table.concat(arr, "")
end
--[[OtxDeviceService: in List<Integer> arr, in Integer left, in Integer right, out List<Integer>]]
function OtxScriptingSupportExample.QuickSort(arr, left, right)
left = left or 1
right = right or #arr
if left == 0 then
left = 1
right = right + 1
end
if left >= right then
return arr
end
local pivot = arr[math.floor((left + right) / 2)]
local i, j = left, right
while i <= j do
while arr[i] < pivot do i = i + 1 end
while arr[j] > pivot do j = j - 1 end
if i <= j then
arr[i], arr[j] = arr[j], arr[i]
i = i + 1
j = j - 1
end
end
if left < j then
OtxScriptingSupportExample.QuickSort(arr, left, j)
end
if i < right then
OtxScriptingSupportExample.QuickSort(arr, i, right)
end
return arr
end
--[[OtxStructure: String Type, Map<String, Float> Props]]
local Shape = {
Type = "",
Props = {}
}
-- constructor
function Shape:new(o)
o = o or {}
local obj = {}
-- copy default values if not provided
for k,v in pairs(self) do
-- skip functions
if type(v) ~= "function" then
obj[k] = o[k] or v
end
end
obj.Props = o.Props or {}
setmetatable(obj, { __index = self })
return obj
end
--[[OtxDeviceService: in Float width, in Float height, out Shape]]
function OtxScriptingSupportExample.CreateRectangle(width, height)
local r = Shape:new({
Type = "Rectangle",
Props = {
Width = width or 0.0,
Height = height or 0.0
}
})
return r
end
--[[OtxDeviceService: in Shape shape, out Float]]
function OtxScriptingSupportExample.ComputeArea(shape)
if shape.Type == "Circle" then
return 3.14 * shape.Props.Radius * shape.Props.Radius
elseif shape.Type == "Rectangle" then
return shape.Props.Width * shape.Props.Height
else
error("Unknown shape type")
end
end
--[[OtxDeviceService: in Float radius, out BlackBox]]
function OtxScriptingSupportExample.CreateCircle(radius)
local c = Shape:new({
Type = "Circle",
Props = {
Radius = radius or 0.0
}
})
return c
end
--[[OtxDeviceService: in BlackBox shape, out Float]]
function OtxScriptingSupportExample.ComputePerimeter(shape)
if shape.Type == "Circle" then
-- Perimeter = 2 * π * r
return 2 * 3.14 * shape.Props.Radius
elseif shape.Type == "Rectangle" then
-- Perimeter = 2 * (w + h)
return 2 * (shape.Props.Width + shape.Props.Height)
else
error("Unknown shape type: " .. tostring(shape.Type))
end
end
--[[OtxDeviceService: in Map<String, Float> map, out Map<String, Shape>]]
function OtxScriptingSupportExample.CreateShapes(map)
local result = {}
for key, value in pairs(map) do
if type(value) ~= "number" then
error("Value for key '" .. tostring(key) .. "' must be float")
end
if key == "Circle" then
result[key] = Shape:new({
Type = "Circle",
Props = {
Radius = value or 0.0
}
})
elseif key == "Rectangle" then
result[key] = Shape:new({
Type = "Rectangle",
Props = {
Width = value or 0.0,
Height = value or 0.0
}
})
else
error("Shape '" .. tostring(key) .. "' not supported")
end
end
return result
end
return OtxScriptingSupportExample