-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcontroller.jl
47 lines (38 loc) · 1.24 KB
/
controller.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import Random
@system Controller begin
config ~ ::Config(override)
context(config) ~ ::Context(context)
end
"""
instance(S; <keyword arguments>) -> S
Make an instance of system `S` with an initial condition specified in configuration and additional options.
See also: [`@config`](@ref), [`simulate`](@ref)
# Arguments
- `S::Type{<:System}`: type of system to be instantiated.
# Keyword Arguments
- `config=()`: configuration containing parameter values for the system.
- `options=()`: keyword arguments passed down to the constructor of `S`; named tuple expected.
- `seed=nothing`: random seed initialized before parsing configuration and making an instance.
# Examples
```julia-repl
julia> @system S(Controller) begin
a => 1 ~ preserve(parameter)
b(a) ~ accumulate
end;
julia> instance(S)
S
context = <Context>
config = <Config>
a = 1.0
b = 0.0
```
"""
instance(S::Type{<:System}; config=(), options=(), seed=nothing) = begin
!isnothing(seed) && Random.seed!(seed)
c = configure(config)
#HACK: support placeholder (0) for the controller name
c = configure(((k == Symbol(0) ? namefor(S) : k) => v for (k, v) in c)...)
s = S(; config=c, options...)
update!(s)
end
export Controller, instance