CSV Tabular Data (.csv)
@modelscript/csv (languages/csv) elevates empirical measurement series, calibration look-up tables, and parameter sweeps into first-class typed symbols inside the WorkspaceIndex.
Capabilities
- Native GLR Parsing & Delimiter Auto-Detection: Parses RFC 4180 CSV documents into linear-memory CST rows and cells (
csv_document,header_row,data_row,field,quoted_field) whiledetectDelimiter(languages/csv/src/measurements.ts) automatically detects comma (,), semicolon (;), or tab (\t) delimiters and handles quoted strings with escaped""sequences. - Modelica Package Generation (
parseCsvToModelica): Converts any CSV dataset directly into a strongly typed Modelicapackagecontaining:constant Integer numRowsandconstant Integer numCols- A 2D
constant Real values[numRows, numCols]matrix suitable forModelica.Blocks.Tables.CombiTable1D/CombiTable2D - Sanitized 1D column vectors (
constant Real <colName>[numRows]) with automatic duplicate-name disambiguation
- Salsa Virtual Component Queries (
CSVVirtualComponent): Exposes every CSV column as a queryable virtual symbol in theQueryEngine(languages/csv/src/queries.ts):resolvedType: Infers"Real","Integer","Boolean", or"String"from column cellsclassInstance: Maps to"Modelica.Blocks.Sources.CombiTimeTable"variability: Returns"constant"causality: Returns"output"resolvedArrayDimensions/arrayDimensions: Returns[rowCount]effectiveModification: Synthesizes{ table: "[[...]]" }array literals from column data
- Bidirectional Cell Writeback (
writeback): When a parameter or table entry is modified during calibration or optimization,csvLanguage.writeback(languages/csv/src/language.ts) updates the exact[row, col]cell or column header in place while preserving the file's original delimiter and line endings.
Semantic Linter & Diagnostic Codes
lintCsv(sourceText) (languages/csv/src/linter.ts) performs six structural and numerical integrity checks on CSV datasets:
| Code | Severity | Condition Checked |
|---|---|---|
CSV_UNCLOSED_QUOTE | error | A quoted field contains an unclosed " at the end of a line |
CSV_EMPTY_HEADER | warning | A column in the header row has an empty name |
CSV_DUPLICATE_HEADER | warning | Two or more columns share the same header identifier |
CSV_COLUMN_COUNT_MISMATCH | error | A data row contains fewer or more cells than the header row |
CSV_INVALID_NUMERIC_CELL | error | A cell in a numeric column (where row 1 is numeric) contains non-numeric text, NaN, or Infinity |
CSV_NON_MONOTONIC_TIME | warning | The first column (time / t) is not strictly monotonically increasing ( |
Polyglot TGG Rules
@modelscript/csv registers 2 bidirectional Triple Graph Grammar (TGG) rules in languages/csv/src/language.ts:
| Rule Name | Source (csv) | Target (modelica) | Semantics |
|---|---|---|---|
CsvDocumentToModelicaPackage | CSVDocument | ClassDefinition (package) | Projects a CSV file into a Modelica data package with values[numRows, numCols] |
CsvVirtualComponentToModelicaComponent | CSVVirtualComponent | ComponentClause (parameter Real) | Binds a typed CSV column to a Modelica parameter vector or CombiTimeTable source |
Command Line Interface
bash
# Lint CSV datasets for column mismatches, unclosed quotes, and non-monotonic time
msx lint trajectory_data.csv
# Output simulation trajectories directly to standard CSV format
msx simulate CircuitModel.mo --stop-time=1.0 --step-size=0.001 --format=csv -o sim_results.csv
