|
1 | | - |
2 | 1 | """ |
3 | | - ModelGenerator(model, kwargs, plugins) |
| 2 | + ModelGenerator(model, kwargs, [ plugins ]) |
| 3 | +
|
| 4 | +The `ModelGenerator` structure is used to lazily create the model with the given `model` and `kwargs` and (optional) `plugins`. |
| 5 | +
|
| 6 | +# Fields |
| 7 | +- `model`: The model function to be used for creating the graph |
| 8 | +- `kwargs`: Named tuple of keyword arguments to be passed to the model |
| 9 | +- `plugins`: Collection of plugins to be used (optional) |
| 10 | +- `backend`: Backend to be used for model creation (defaults to model's default_backend) |
| 11 | +- `source`: Original source code of the model (for debugging purposes) |
| 12 | +
|
| 13 | +# Extended Functionality |
| 14 | +The ModelGenerator supports several extension methods: |
| 15 | +
|
| 16 | +- `with_plugins(generator, plugins)`: Create new generator with updated plugins |
| 17 | +- `with_backend(generator, backend)`: Create new generator with different backend |
| 18 | +- `with_source(generator, source)`: Create new generator with different source code |
| 19 | +
|
| 20 | +# Examples |
| 21 | +
|
| 22 | +```jldoctest |
| 23 | +julia> import GraphPPL: @model |
4 | 24 |
|
5 | | -The `ModelGenerator` structure is used to lazily create |
6 | | -the model with the given `model` and `kwargs` and `plugins`. |
| 25 | +julia> @model function beta_bernoulli(y) |
| 26 | + θ ~ Beta(1, 1) |
| 27 | + for i = eachindex(y) |
| 28 | + y[i] ~ Bernoulli(θ) |
| 29 | + end |
| 30 | + end |
| 31 | +
|
| 32 | +julia> generator = beta_bernoulli(y = rand(10)); |
| 33 | +
|
| 34 | +julia> struct CustomBackend end |
| 35 | +
|
| 36 | +julia> generator_with_backend = GraphPPL.with_backend(generator, CustomBackend()); |
| 37 | +
|
| 38 | +julia> generator_with_plugins = GraphPPL.with_plugins(generator, GraphPPL.PluginsCollection()); |
| 39 | +
|
| 40 | +julia> println(GraphPPL.getsource(generator)) |
| 41 | +function beta_bernoulli(y) |
| 42 | + θ ~ Beta(1, 1) |
| 43 | + for i = eachindex(y) |
| 44 | + y[i] ~ Bernoulli(θ) |
| 45 | + end |
| 46 | +end |
| 47 | +
|
| 48 | +julia> generator_with_source = GraphPPL.with_source(generator, "Hello, world!"); |
| 49 | +
|
| 50 | +julia> println(GraphPPL.getsource(generator_with_source)) |
| 51 | +Hello, world! |
| 52 | +``` |
| 53 | +
|
| 54 | +See also: [`with_plugins`](@ref), [`with_backend`](@ref), [`with_source`](@ref) |
7 | 55 | """ |
8 | | -struct ModelGenerator{G, K, P, B} |
| 56 | +struct ModelGenerator{G, K, P, B, S} |
9 | 57 | model::G |
10 | 58 | kwargs::K |
11 | 59 | plugins::P |
12 | 60 | backend::B |
| 61 | + source::S |
13 | 62 | end |
14 | 63 |
|
15 | | -ModelGenerator(model::G, kwargs::K) where {G, K} = ModelGenerator(model, kwargs, PluginsCollection(), default_backend(model)) |
| 64 | +ModelGenerator(model, kwargs) = ModelGenerator(model, kwargs, PluginsCollection()) |
| 65 | +ModelGenerator(model, kwargs, plugins) = ModelGenerator(model, kwargs, plugins, default_backend(model)) |
| 66 | +ModelGenerator(model, kwargs, plugins, backend) = ModelGenerator(model, kwargs, plugins, backend, nothing) |
16 | 67 |
|
17 | 68 | getmodel(generator::ModelGenerator) = generator.model |
18 | 69 | getkwargs(generator::ModelGenerator) = generator.kwargs |
19 | 70 | getplugins(generator::ModelGenerator) = generator.plugins |
20 | 71 | getbackend(generator::ModelGenerator) = generator.backend |
| 72 | +getsource(generator::ModelGenerator) = generator.source |
21 | 73 |
|
22 | 74 | """ |
23 | 75 | with_plugins(generator::ModelGenerator, plugins::PluginsCollection) |
24 | 76 |
|
25 | | -Attaches the `plugins` to the `generator`. For example: |
26 | | -```julia |
27 | | -plugins = GraphPPL.PluginsCollection(GraphPPL.NodeCreatedByPlugin()) |
28 | | -new_generator = GraphPPL.with_plugins(generator, plugins) |
29 | | -``` |
| 77 | +Overwrites the `plugins` specified in the `generator`. |
30 | 78 | """ |
31 | 79 | function with_plugins(generator::ModelGenerator, plugins::PluginsCollection) |
32 | | - return ModelGenerator(generator.model, generator.kwargs, generator.plugins + plugins, generator.backend) |
| 80 | + return ModelGenerator(generator.model, generator.kwargs, generator.plugins + plugins, generator.backend, generator.source) |
33 | 81 | end |
34 | 82 |
|
| 83 | +""" |
| 84 | + with_backend(generator::ModelGenerator, plugins::PluginsCollection) |
| 85 | +
|
| 86 | +Overwrites the `backend` specified in the `generator`. |
| 87 | +""" |
35 | 88 | function with_backend(generator::ModelGenerator, backend) |
36 | | - return ModelGenerator(generator.model, generator.kwargs, generator.plugins, backend) |
| 89 | + return ModelGenerator(generator.model, generator.kwargs, generator.plugins, backend, generator.source) |
| 90 | +end |
| 91 | + |
| 92 | +""" |
| 93 | + with_source(generator::ModelGenerator, source) |
| 94 | +
|
| 95 | +Overwrites the `source` specified in the `generator`. |
| 96 | +""" |
| 97 | +function with_source(generator::ModelGenerator, source) |
| 98 | + return ModelGenerator(generator.model, generator.kwargs, generator.plugins, generator.backend, source) |
37 | 99 | end |
38 | 100 |
|
39 | 101 | function create_model(generator::ModelGenerator) |
|
71 | 133 | ``` |
72 | 134 | """ |
73 | 135 | function create_model(callback, generator::ModelGenerator) |
74 | | - model = Model(getmodel(generator), getplugins(generator), getbackend(generator)) |
| 136 | + model = Model(getmodel(generator), getplugins(generator), getbackend(generator), getsource(generator)) |
75 | 137 | context = getcontext(model) |
76 | 138 |
|
77 | 139 | extrakwargs = callback(model, context) |
|
0 commit comments