set_schema: Build or extend a fluxCore schema with hybrid shorthand

View source: R/schema_helpers.R

set_schemaR Documentation

Build or extend a fluxCore schema with hybrid shorthand

Description

Constructs a validated fluxCore schema from a hybrid vars specification. Each element of vars may be either a type-name string (e.g. "count") or a fully-specified list (e.g. list(type = "positive_numeric", max = 20)). Optionally merges onto an existing schema, with explicit overwrite and remove controls.

Usage

set_schema(
  vars = NULL,
  schema = NULL,
  remove = NULL,
  overwrite = FALSE,
  time_spec = NULL,
  decision_points = NULL,
  decision_groups = NULL
)

Arguments

vars

Named list (or character vector) of variable specs. Each element is either a single type-name string or a list containing type plus any recognized schema fields (min, max, levels, default, coerce, validate, allow_na, required, blocks).

schema

Optional existing schema to extend. If NULL, a new schema is created. May be either a plain variables list or a full schema list (with a ⁠$variables⁠ field) — both forms are accepted.

remove

Optional character vector of variable names to remove from schema before merging vars. Errors if any name is not present.

overwrite

Logical scalar. If FALSE (default), adding a variable already present in schema is an error. If TRUE, existing entries are replaced.

time_spec

Optional time_spec() object. When provided, the returned value is a full schema list with ⁠$variables⁠, ⁠$time_spec⁠, ⁠$decision_points⁠, and ⁠$decision_groups⁠, ready to pass directly to load_model().

decision_points

Optional list of DecisionPoint() objects to attach to the schema. Requires time_spec to also be provided. When supplied, the returned value is a full schema list (see time_spec above).

decision_groups

Optional list of GroupedDecisionPoint() objects. Members reference ids in decision_points. Requires time_spec; group and leaf ids are validated in one shared namespace.

Details

When time_spec, decision_points, or decision_groups is supplied, set_schema() returns a full schema list (with ⁠$variables⁠, ⁠$time_spec⁠, ⁠$decision_points⁠, and ⁠$decision_groups⁠) suitable for direct use with load_model(). When none is supplied, it returns just the validated variables spec (backward-compatible with prior usage and with Entity$new(schema = ...)).

Value

A validated fluxCore variables spec (named list), or — when time_spec, decision_points, or decision_groups is supplied — a full schema list with ⁠$variables⁠, ⁠$time_spec⁠, ⁠$decision_points⁠, and ⁠$decision_groups⁠.

Examples

  # Variables only (backward-compatible):
  vars <- set_schema(vars = list(
    route_zone  = list(type = "categorical",
                       levels = c("urban", "suburban", "rural")),
    battery_pct = "percent",
    payload_kg  = list(type = "positive_numeric", max = 20),
    deliveries  = "count",
    prob_rain   = "probability"
  ))

  # Full schema for load_model():
  dp <- DecisionPoint(id = "dp1", trigger = "event_A",
                      allowed_actions = c("accept", "decline"))
  schema <- set_schema(
    vars             = list(battery_pct = "percent"),
    time_spec        = time_spec(unit = "hours"),
    decision_points  = list(dp),
    decision_groups  = NULL
  )
  # Full schemas also contain schema$decision_groups (NULL here).


fluxCore documentation built on Sept. 22, 2026, 5:07 p.m.