Model Builder
The ModelBuilder is the entry point for defining a model.
Use its factory methods to add tables and named values, then call
write_to_file() to emit the model definition JSON.
Related pages:
Tables — the table types (data, derived, joined, union)
Fields — fields that live on tables
Named Values — calculations and parameters
Formulas — the formula language and built-in functions
- class ModelBuilder[source]
Bases:
objectTop-level entry point for building a Daitum model.
A model holds tables and named values (calculations and parameters). Build one incrementally with the
add_*factory methods, then callwrite_to_file()to emit the JSON output.- add_calculation(id, formula, model_level=False, tracking_group=None)[source]
Add a
Calculationto the model.- Parameters:
id (
str) – Unique identifier for the calculation. Must not collide with any existing calculation or parameter id.formula (
Operand|float|int|bool|str) – The expression. AFormulaor any value accepted byCONST()(number, bool, or string), which is wrapped automatically.model_level (
bool) – IfTrue, the calculation is evaluated once for the whole model. IfFalse(the default) it is scenario-level.tracking_group (
str|None) – Optional change-tracking group. When set, a sibling*_TRACKING_*calculation is generated with references to other tracked named values and fields rewritten to their tracking ids.
- Return type:
Calculation- Returns:
The newly added
Calculation.- Raises:
ValueError – If a calculation or parameter with the same
idalready exists in the model.
- add_parameter(id, data_type, value, model_level=False, tracking_group=None)[source]
Add a
Parameterto the model.- Parameters:
id (
str) – Unique identifier for the parameter.data_type (
BaseDataType) – Data type of the parameter — aDataType,ObjectDataType, orMapDataType.value (
Any) – Initial value. Must be compatible withdata_type.model_level (
bool) – IfTrue, the parameter is shared across all scenarios. IfFalse(the default) it is scenario-level.tracking_group (
str|None) – Optional change-tracking group. When set, a sibling*_TRACKING_*parameter is generated with the same value.
- Return type:
Parameter- Returns:
The newly added
Parameter.
- add_data_table(id)[source]
Add a
DataTableto the model.A data table holds raw imported rows and is the only table type that supports decision variables.
- Parameters:
id (
str) – Unique identifier for the table. Must not collide with any existing table id.- Return type:
- Returns:
The newly added
DataTable. Use itsadd_data_field/add_calculated_field/set_key_columnmethods to populate it.- Raises:
ValueError – If a table with the same
idalready exists.
- add_derived_table(id, source_table, group_by=None, filter_field=None)[source]
Add a
DerivedTableto the model.A derived table is a grouped, filtered, and/or sorted view of an existing source table.
- Parameters:
id (
str) – Unique identifier for the derived table.source_table (
Table) – The table whose rows the derived table draws from.group_by (
list[Field] |None) – Optional fields to group rows by. When supplied, non-group fields must be added with anAggregationMethod.filter_field (
Field|None) – OptionalBOOLEANfield onsource_table; only rows where it isTrueare kept.
- Return type:
- Returns:
The newly added
DerivedTable.- Raises:
ValueError – If a table with the same
idalready exists.
- add_joined_table(id, join_conditions)[source]
Add a
JoinedTableto the model.A joined table is the result of joining two or more tables on a list of
JoinConditionrows.- Parameters:
id (
str) – Unique identifier for the joined table.join_conditions (
list[JoinCondition]) – One condition per joined table pairing. TheJoinTypeof each condition controls how unmatched rows are handled.
- Return type:
- Returns:
The newly added
JoinedTable.- Raises:
ValueError – If a table with the same
idalready exists.
- add_union_table(id, source_tables)[source]
Add a
UnionTableto the model.A union table stacks rows from multiple source tables. Pass a
UnionSourceinstead of a bareTablewhen the source schema does not line up with the union table’s fields.- Parameters:
id (
str) – Unique identifier for the union table.source_tables (
list[Table|UnionSource]) – The tables (or wrappedUnionSourceinstances) whose rows are stacked.
- Return type:
- Returns:
The newly added
UnionTable.- Raises:
ValueError – If a table with the same
idalready exists.
- set_partial_evaluation_allowed(partial_evaluation_allowed=True)[source]
Toggle partial evaluation for this model.
When enabled (the default), the platform re-evaluates only the calculations affected by a change rather than the whole model, which speeds up UI rendering. Disable only if a model has dependencies that the partial evaluator cannot track correctly.
- Parameters:
partial_evaluation_allowed (
bool) –Trueto allow partial evaluation.
- set_data_validation_rule(named_value)[source]
Gate optimisation on a boolean named value.
Optimisation is allowed only while
named_valueevaluates toTrue. When it isFalseand the UI definition specifies a validation screen, the user is redirected there on attempting to start optimisation.- Parameters:
named_value (
Parameter|Calculation) – AParameterorCalculationof typeBOOLEAN.- Raises:
ValueError – If
named_valueis not of boolean data type.
- get_table(id)[source]
Return the table with the given id.
- Parameters:
id (
str) – Identifier of the table to look up.- Return type:
- Returns:
The matching
Table.- Raises:
ValueError – If no table with that id exists in the model.
- get_named_value(id)[source]
Return the named value (parameter or calculation) with the given id.
- Parameters:
id (
str) – Identifier of the named value to look up.- Return type:
Parameter|Calculation- Returns:
The matching
ParameterorCalculation. Parameters are searched first, so a parameter is returned if both exist with the same id (whichadd_parameter/add_calculationwould normally prevent).- Raises:
ValueError – If no named value with that id exists in the model.
- get_validation_state()[source]
Return (and lazily build) a calculation summarising current validity.
The calculation evaluates to the highest
SEVERITY_RANKamong every field, parameter, and calculation whose__invalid__formula is currentlyTrue, or0when nothing is invalid. Useful as the gating value forset_data_validation_rule().The result is registered in the model under the id
__validation_state__. Subsequent calls return the already-registered calculation without rebuilding it, so call this only after every validator has been attached.- Return type:
Calculation|Parameter- Returns:
A model-level
Calculationwhose integer value is the highest severity rank of any currently-invalid entity.
- write_to_file(model_directory)[source]
Serialise the model into the canonical Daitum directory layout.
Writes three files under
model_directory, creating it if it does not exist:model-definition.json— the full model definition frombuild().scenarios/Initial/named-values.json— scenario-level named values (those added withmodel_level=False).model-data/named-values.json— model-level named values (those added withmodel_level=True).
- build()[source]
Build the JSON-compatible dict representation of the model.
On the first call, change-tracking metadata is resolved: tracked calculated fields have their formulas rewritten to reference the
*_TRACKING_*ids of any other tracked named values and fields. This step is idempotent and only runs once per builder.
- to_named_value_dict(model_level)[source]
Build the JSON-compatible dict of parameter values for one scope.
- Parameters:
model_level (
bool) – WhenTrue, include only model-level parameters (written tomodel-data/named-values.json). WhenFalse, include only scenario-level parameters (written toscenarios/Initial/named-values.json).- Return type:
- Returns:
{"values": {parameter_id: value_dict, ...}}.