GraphBuilder.build_model()

GraphBuilder.build_model()#

GraphBuilder.build_model(copy=False)[source]#

Builds a model from the graph.

Constructs a model containing all nodes and variables that were added to the graph builder and their recursive inputs. The outputs of the nodes are not added to the model automatically, so the root nodes always need to be added explicitly.

The standard workflow is to create the nodes and variables, add them to a graph builder, and construct a model from the graph builder. After the model has been constructed, some methods of the graph builder are not available anymore.

Parameters:

copy (bool) – Whether the nodes and variables should be copied when building the model. (default: False)

Return type:

Model

Returns:

The liesel model, which is a static graph built from the GraphBuilder.

Notes

If this method is called with the argument copy=False, all nodes and variables are removed from the graph builder, because most methods of the graph builder do not work with nodes that are part of a model.

Examples

We start by creating some variables:

>>> a = lsl.Var(1.0, name="a")
>>> b = lsl.Var(2.0, name="b")
>>> c = lsl.Var(lsl.Calc(lambda x, y: x + y, a, b), name="c")

We now initialize a GraphBuilder and add the root node c to it:

>>> gb = lsl.GraphBuilder()
>>> gb.add(c)
GraphBuilder(0 nodes, 1 vars)

We are now ready to build the model:

>>> model = gb.build_model()
>>> model
Model(9 nodes, 3 vars)

Note that when build_model() is called, all weak variables in the graph will be updated. So the value of c is now available:

>>> c.value
3.0

The graph builder is now empty:

>>> gb.vars
[]