|
| 1 | +--- |
| 2 | +title: Units CLI |
| 3 | +weight: 20 |
| 4 | +chapter: false |
| 5 | +--- |
| 6 | + |
| 7 | +# QUDT Units CLI |
| 8 | + |
| 9 | +The S2DM Units CLI provides integration with the [QUDT (Quantities, Units, Dimensions and Types)](https://qudt.org/) reference model to generate standardized GraphQL unit enums. This replaces static unit definitions with a dynamic system that synchronizes directly from the authoritative QUDT repository. |
| 10 | + |
| 11 | +## Features |
| 12 | + |
| 13 | +- **Dynamic Unit Synchronization**: Fetch unit definitions from QUDT's GitHub repository |
| 14 | +- **Version Management**: Support for specific QUDT versions with automatic latest version detection |
| 15 | +- **Semantic References**: Generated enums include `@reference` directives linking to QUDT IRIs with version tags |
| 16 | +- **UCUM Integration**: Includes UCUM codes in descriptive comments for standardization |
| 17 | +- **SDL Validation**: Generated GraphQL enums are validated for correctness |
| 18 | +- **Automatic Cleanup**: Removes stale files before sync to ensure fresh state |
| 19 | + |
| 20 | +## Commands |
| 21 | + |
| 22 | +### `s2dm units sync` |
| 23 | + |
| 24 | +Synchronizes unit definitions from the QUDT repository and generates GraphQL enum files. |
| 25 | + |
| 26 | +**Syntax:** |
| 27 | +```bash |
| 28 | +s2dm units sync --output-dir <path> [--version <version>] [--dry-run] |
| 29 | +``` |
| 30 | + |
| 31 | +**Examples:** |
| 32 | +```bash |
| 33 | +# Sync latest version |
| 34 | +s2dm units sync --output-dir units |
| 35 | + |
| 36 | +# Sync specific version |
| 37 | +s2dm units sync --version 3.1.5 --output-dir units |
| 38 | + |
| 39 | +# Check what would be generated without creating files |
| 40 | +s2dm units sync --output-dir units --dry-run |
| 41 | + |
| 42 | +# Sync to custom directory |
| 43 | +s2dm units sync --version 3.1.5 --output-dir path/to/units/external_qudt |
| 44 | +``` |
| 45 | + |
| 46 | +**Options:** |
| 47 | +- `--output-dir` (required): Directory where generated unit enums will be written |
| 48 | +- `--version` (optional): QUDT version string. Defaults to latest release if not specified |
| 49 | +- `--dry-run` (optional): Show how many enum files would be generated without creating them |
| 50 | + |
| 51 | +**Output:** |
| 52 | +- Creates `<QuantityKindUnitEnum>.graphql` files (e.g., `LengthUnitEnum.graphql`) |
| 53 | +- Generates `metadata.json` with version information |
| 54 | +- Reports number of enum files generated |
| 55 | + |
| 56 | +### `s2dm units check-version` |
| 57 | + |
| 58 | +Compares the local synced QUDT version with the latest available version. |
| 59 | + |
| 60 | +**Syntax:** |
| 61 | +```bash |
| 62 | +s2dm units check-version --units-dir <path> |
| 63 | +``` |
| 64 | + |
| 65 | +**Example:** |
| 66 | +```bash |
| 67 | +s2dm units check-version --units-dir units/external_qudt |
| 68 | +``` |
| 69 | + |
| 70 | +**Options:** |
| 71 | +- `--units-dir` (required): Directory containing generated unit enums |
| 72 | + |
| 73 | +**Output:** |
| 74 | +- `✓ Everything is up to date.` if current version matches latest |
| 75 | +- `⚠ There is a newer release available: X.Y.Z` if update is needed |
| 76 | + |
| 77 | +## Generated Enum Format |
| 78 | + |
| 79 | +Each generated enum follows this structure: |
| 80 | + |
| 81 | +```graphql |
| 82 | +# Generated from QUDT units catalog version 3.1.5 |
| 83 | +enum LengthUnitEnum @reference(uri: "http://qudt.org/vocab/quantitykind/Length", versionTag: "3.1.5") { |
| 84 | + """Meter | UCUM: m""" |
| 85 | + M @reference(uri: "http://qudt.org/vocab/unit/M", versionTag: "3.1.5") |
| 86 | + |
| 87 | + """Kilometer | UCUM: km""" |
| 88 | + KILOM @reference(uri: "http://qudt.org/vocab/unit/KiloM", versionTag: "3.1.5") |
| 89 | + |
| 90 | + """Centimeter | UCUM: cm""" |
| 91 | + CENTIM @reference(uri: "http://qudt.org/vocab/unit/CentiM", versionTag: "3.1.5") |
| 92 | + ... |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +**Key Elements:** |
| 97 | +- **Enum name**: `<QuantityKind>UnitEnum` in PascalCase |
| 98 | +- **@reference on enum**: Links to QUDT quantity kind IRI with version tag |
| 99 | +- **@reference on values**: Links to QUDT unit IRI with version tag |
| 100 | +- **Triple-quote docstrings**: Include human-readable label and UCUM code |
| 101 | +- **Value names**: Use QUDT URI segments converted to SCREAMING_SNAKE_CASE |
| 102 | + |
| 103 | +## Directory Structure |
| 104 | + |
| 105 | +When using both QUDT and custom units, organize them as follows: |
| 106 | + |
| 107 | +``` |
| 108 | +units/ |
| 109 | +├── external_qudt/ # QUDT units (generated) |
| 110 | +│ ├── metadata.json # Version metadata |
| 111 | +│ ├── LengthUnitEnum.graphql # Length units |
| 112 | +│ ├── VelocityUnitEnum.graphql # Velocity units |
| 113 | +│ └── ... (500+ more unit enums) |
| 114 | +└── custom/ # Custom units (manual) |
| 115 | + ├── MyDomainUnitEnum.graphql # Custom domain units |
| 116 | + └── SpecialUnitEnum.graphql # Special use case units |
| 117 | +``` |
| 118 | + |
| 119 | +## Workflow Example |
| 120 | + |
| 121 | +1. **Initial Setup**: Sync units from QUDT |
| 122 | + ```bash |
| 123 | + s2dm units sync --version 3.1.5 --output-dir units/external_qudt |
| 124 | + ``` |
| 125 | + |
| 126 | +2. **Use Units in Schema Development**: Reference generated enums |
| 127 | + ```graphql |
| 128 | + type Vehicle { |
| 129 | + speed(unit: VelocityUnitEnum = KM_PER_HR): Float |
| 130 | + acceleration(unit: AccelerationUnitEnum = M_PER_SEC2): Float |
| 131 | + } |
| 132 | + ``` |
| 133 | + |
| 134 | +3. **Check for Updates**: Periodically check for new QUDT versions |
| 135 | + ```bash |
| 136 | + s2dm units check-version --units-dir units/external_qudt |
| 137 | + ``` |
| 138 | + |
| 139 | +4. **Update When Needed**: Sync newer versions as they become available |
| 140 | + ```bash |
| 141 | + s2dm units sync --version 3.2.0 --output-dir units/external_qudt |
| 142 | + ``` |
| 143 | + |
| 144 | +## Integration with Registry Commands |
| 145 | + |
| 146 | +The units system integrates seamlessly with S2DM registry commands. The registry commands automatically detect and use unit enums from your schema composition: |
| 147 | + |
| 148 | +```bash |
| 149 | +# Generate concept IDs (automatically detects unit enums) |
| 150 | +s2dm registry id -s schema.graphql |
| 151 | + |
| 152 | +# Initialize registry (automatically includes unit enums) |
| 153 | +s2dm registry init -s schema.graphql -o registry.json |
| 154 | + |
| 155 | +# Update registry (automatically includes unit enums) |
| 156 | +s2dm registry update -s new_schema.graphql -r registry.json |
| 157 | +``` |
| 158 | + |
| 159 | +## Why URI-Based Symbols? |
| 160 | + |
| 161 | +S2DM uses QUDT URI segments instead of labels for enum values because: |
| 162 | + |
| 163 | +- **Consistency**: URI segments are standardized and don't vary across languages |
| 164 | +- **Reliability**: Labels can be inconsistent, missing, or contain formatting issues |
| 165 | +- **Brevity**: URI segments are shorter and more practical for code usage (e.g., `M` vs `METER`) |
| 166 | +- **Semantic Accuracy**: URI segments represent the canonical QUDT identifier |
| 167 | + |
| 168 | +## Technical Details |
| 169 | + |
| 170 | +### Data Source |
| 171 | +- **Repository**: [qudt/qudt-public-repo](https://github.com/qudt/qudt-public-repo) |
| 172 | +- **File**: `src/main/rdf/vocab/unit/VOCAB_QUDT-UNITS-ALL.ttl` |
| 173 | +- **Format**: RDF Turtle (TTL) |
| 174 | + |
| 175 | +### Deduplication |
| 176 | +- Uses `DISTINCT` in SPARQL queries to eliminate exact duplicates |
| 177 | +- Filters out deprecated units to prevent symbol conflicts |
| 178 | +- Prefers entries with UCUM codes when duplicates remain |
| 179 | + |
| 180 | +### Error Handling |
| 181 | +- **Network Issues**: Graceful failure with informative error messages |
| 182 | +- **Invalid Units**: Units that cannot generate valid symbols are skipped |
| 183 | +- **SDL Validation**: Generated GraphQL enums are validated for correctness |
| 184 | +- **Version Check**: Safe handling of GitHub API rate limits |
0 commit comments