DataTable

A DataTable holds plain rows of data — imported records or decision variables that the optimiser writes to. It is the only table type that supports decision variables and ComboField.

Basic setup

The example below builds two data tables. Products holds imported product data; Orders references a row in Products via an object reference field and uses it to calculate the cost of the order:

from daitum_model import ModelBuilder, DataType

model = ModelBuilder()

# Products: imported data.
products = model.add_data_table("Products")
products.set_key_column("Product Id")
products.add_data_field("Product Id", DataType.STRING)
products.add_data_field("Price", DataType.DECIMAL)

# Orders: a data field, an object reference field pointing at a Product,
# and a calculated field that uses the referenced Product's price.
orders = model.add_data_table("Orders")
orders.set_key_column("Order Id")
orders.add_data_field("Order Id", DataType.STRING)
product = orders.add_object_reference_field("Product", products)
quantity = orders.add_data_field("Quantity", DataType.INTEGER)
orders.add_calculated_field("Order Cost", product["Price"] * quantity)

A DataTable accepts every field type described in Fields

set_key_column designates the field used to uniquely identify each row — Product Id and Order Id above. This is the field used to look up and reference rows in this table from elsewhere, for example when validating that an add_object_reference_field value on another table matches an existing row here. A table does not require a key column, but one is needed whenever other tables reference it.

API Reference

class DataTable(id)[source]

Bases: Table

Data Tables are used wherever plain data is required, including all input tables. Notably, optimiser decision variables can only appear in Data Tables, as these cells contain plain data that the optimiser writes.

In addition to holding data fields, Data Tables often include calculated fields, which can capture a significant portion of the model’s logic.

add_data_field(id, data_type)[source]

Adds a DataField to the table.

Parameters:
  • id (str) – The id of the data field.

  • data_type (BaseDataType) – The data type of the field.

Returns:

The created DataField object.

Return type:

DataField

add_combo_field(id, formula, calculate_in_optimiser)[source]

Adds a ComboField to the table.

Parameters:
  • id (str) – The id of the calculated field.

  • formula (Operand | float | int | bool | str) – The formula used to calculate the field.

  • calculate_in_optimiser (bool) – Specifies whether the formula is evaluated during optimisation.

Returns:

The created ComboField object.

Return type:

ComboField