Skip to content

Commit

Permalink
Fix table sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
SouthEndMusic committed Apr 11, 2024
1 parent e842bdf commit c975b0c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
5 changes: 5 additions & 0 deletions core/src/validation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ sort_by_id_state_level(row) = (row.node_id, row.control_state, row.level)
sort_by_priority(row) = (row.node_id, row.priority)
sort_by_priority_time(row) = (row.node_id, row.priority, row.time)
sort_by_subgrid_level(row) = (row.subgrid_id, row.basin_level)
sort_by_condition(row) =
(row.node_id, row.listen_node_type, row.listen_node_id, row.variable, row.greater_than)
sort_by_id_name(row) = (row.node_id, row.name)
sort_by_truth_state(row) = (row.node_id, row.truth_state)

# get the right sort by function given the Schema, with sort_by_id as the default
sort_by_function(table::StructVector{<:Legolas.AbstractRecord}) = sort_by_id
Expand All @@ -112,6 +115,8 @@ sort_by_function(table::StructVector{UserDemandStaticV1}) = sort_by_priority
sort_by_function(table::StructVector{UserDemandTimeV1}) = sort_by_priority_time
sort_by_function(table::StructVector{BasinSubgridV1}) = sort_by_subgrid_level
sort_by_function(table::StructVector{DiscreteControlCompoundvariableV1}) = sort_by_id_name
sort_by_function(table::StructVector{DiscreteControlConditionV1}) = sort_by_condition
sort_by_function(table::StructVector{DiscreteControlLogicV1}) = sort_by_truth_state

const TimeSchemas = Union{
BasinTimeV1,
Expand Down
10 changes: 9 additions & 1 deletion python/ribasim/ribasim/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,15 @@ class DiscreteControl(MultiNodeModel):
)
condition: TableModel[DiscreteControlConditionSchema] = Field(
default_factory=TableModel[DiscreteControlConditionSchema],
json_schema_extra={"sort_keys": ["node_id"]},
json_schema_extra={
"sort_keys": [
"node_id",
"listen_node_type",
"listen_node_id",
"variable",
"greater_than",
]
},
)
logic: TableModel[DiscreteControlLogicSchema] = Field(
default_factory=TableModel[DiscreteControlLogicSchema],
Expand Down
1 change: 1 addition & 0 deletions python/ribasim/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def test_sort(level_setpoint_with_minmax, tmp_path):
assert table.df.iloc[0]["greater_than"] == 15.0
assert table._sort_keys == [
"node_id",
"listen_node_type",
"listen_node_id",
"variable",
"greater_than",
Expand Down

0 comments on commit c975b0c

Please sign in to comment.