Skip to content

Additional medication to and from string improvements#114

Merged
halcwb merged 3 commits intoinformedica:masterfrom
halcwb:fixMedFromString
Jan 25, 2026
Merged

Additional medication to and from string improvements#114
halcwb merged 3 commits intoinformedica:masterfrom
halcwb:fixMedFromString

Conversation

@halcwb
Copy link
Collaborator

@halcwb halcwb commented Jan 25, 2026

This pull request introduces several improvements and fixes to the medication and dosing modules, focusing on better handling and representation of dose units, more robust parsing and validation, and enhanced test/demo coverage. The most significant changes include the introduction of a new field label for dose units, improved parsing logic for medication text inputs, stricter dose rule validation, and expanded test scenarios.

Dose Unit Handling and Parsing:

  • Added a new field label DoseUnit ([dun]) in DoseLimit.FieldLabels and integrated it into both serialization and parsing logic for dose limits and medication. This ensures that dose units are explicitly labeled and correctly processed when converting to and from string representations. [1] [2] [3] [4]
  • Introduced a new function toStringEngShortWithoutGroup in Units to generate a short English string for units without group annotation, used for displaying dose units.

Dose Rule Validation:

  • Refactored doseRuleDataIsValid to use the parsed dose type, enabling stricter and more context-aware validation for different dosing scenarios (e.g., requiring time fields for timed doses, frequency for discontinuous doses, etc.). This prevents invalid or incomplete dose rules from being accepted.

Medication Parsing Improvements:

  • Updated the medication parser to recognize and correctly process the new [dun] label, skipping empty/default values and reporting errors for unknown dose units.
  • Improved support for parsing qts (quantities) with value-units, adding error handling for invalid formats.

Testing, Demos, and Output:

  • Added comprehensive demo/test scenarios for various medication order types (once, once-timed, discontinuous, timed) using the new labeled output format, ensuring correct parsing and processing of dose units and limits.
  • Improved log output for medication creation events to include more detailed information.
  • Adjusted the order printing logic to handle cases where the dose quantity is empty, providing a sensible fallback ("eenmalig"). [1] [2]

Miscellaneous:

  • Minor documentation and comment updates for clarity in unit parsing and string conversion logic. [1] [2]
  • Added exploratory/test code for unit parsing in the API script.

Fixing medication from and to string enabling future medication template creation and testing of different scenario's. Includes improvements in depending code, like value unit.

@greptile: review

@greptile-apps
Copy link

greptile-apps bot commented Jan 25, 2026

Greptile Overview

Greptile Summary

This PR enhances medication and dose handling by introducing explicit DoseUnit labeling ([dun]), improving parsing robustness, and strengthening validation logic.

Key improvements:

  • Added DoseUnit field label for explicit dose unit serialization and parsing
  • Refactored doseRuleDataIsValid to use pattern matching on dose types, enabling context-aware validation (e.g., OnceTimed requires time fields, Discontinuous requires frequencies)
  • Enhanced medication parser to skip empty labeled values and handle qts (quantities) fields
  • Added fallback "eenmalig" display when dose quantity is empty
  • Commented out unit string replacement hack in ValueUnit.fromString - requires verification
  • Added comprehensive test scenarios for medication parsing

The changes are well-structured and improve code maintainability. The new validation logic is more explicit and type-safe.

Confidence Score: 4/5

  • This PR is safe to merge with minimal risk after verifying unit parsing tests
  • The changes are well-designed improvements to parsing and validation logic. Confidence reduced from 5 to 4 due to the commented-out unit string replacement hack in ValueUnit.fs which could potentially affect "x" parsing - this should be verified through tests before merging
  • Pay attention to src/Informedica.GenUNITS.Lib/ValueUnit.fs - ensure unit parsing tests pass after commenting out the "x" replacement hack

Important Files Changed

Filename Overview
src/Informedica.GenFORM.Lib/DoseLimit.fs Added DoseUnit field label and integrated it into serialization logic - clean implementation
src/Informedica.GenFORM.Lib/DoseRule.fs Refactored validation to use pattern matching on dose types for context-aware checks - improves robustness
src/Informedica.GenORDER.Lib/Medication.fs Added DoseUnit parsing, skip empty values, added qts support - improves parser flexibility and error handling
src/Informedica.GenORDER.Lib/Order.fs Added fallback to "eenmalig" when dose quantity is empty - handles edge case gracefully
src/Informedica.GenUNITS.Lib/ValueUnit.fs Commented out string replacement hack, added toStringEngShortWithoutGroup - one TODO remains unaddressed

Sequence Diagram

sequenceDiagram
    participant User
    participant Medication
    participant DoseLimit
    participant Units
    participant DoseRule
    participant DoseType
    
    User->>Medication: fromString(medicationText)
    Medication->>Medication: Parse medication structure
    
    Note over Medication: Process DoseLimit fields
    Medication->>DoseLimit: Parse labeled fields ([dun], [qty], etc.)
    
    alt Empty value
        Medication->>Medication: Skip field (default/unset)
    else DoseUnit field [dun]
        Medication->>Units: fromString(doseUnitStr)
        Units-->>Medication: Return Unit or None
        alt Valid unit
            Medication->>DoseLimit: Set DoseUnit
        else Invalid unit
            Medication->>Medication: Add error
        end
    else Other labeled field
        Medication->>Medication: Use field parser
        Medication->>DoseLimit: Update field value
    end
    
    Note over Medication: Process SolutionLimit fields
    alt qts field
        Medication->>Medication: parseValueUnitOpt(valueStr)
        Medication->>Medication: Update Quantities
    else Other fields
        Medication->>Medication: Use existing parsers
    end
    
    Medication->>DoseLimit: toString()
    DoseLimit->>Units: toStringEngShortWithoutGroup(DoseUnit)
    Units-->>DoseLimit: Short unit string
    DoseLimit-->>Medication: Formatted string with [dun] label
    
    Note over DoseRule: Validation with dose type awareness
    DoseRule->>DoseType: fromString(doseType, doseText)
    DoseType-->>DoseRule: Return DoseType variant
    
    alt NoDoseType
        DoseRule->>DoseRule: Return false (invalid)
    else Once
        DoseRule->>DoseRule: Return true (always valid)
    else OnceTimed
        DoseRule->>DoseRule: Check MaxTime and TimeUnit
    else Discontinuous
        DoseRule->>DoseRule: Check Frequencies and FreqUnit
    else Timed
        DoseRule->>DoseRule: Check Frequencies, FreqUnit, MaxTime, TimeUnit
    else Continuous
        DoseRule->>DoseRule: Check RateUnit and (MaxRate or MaxRateAdj)
    end
    
    DoseRule-->>User: Valid dose rules
Loading

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 file reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@halcwb halcwb merged commit 0faa907 into informedica:master Jan 25, 2026
4 checks passed
@halcwb halcwb deleted the fixMedFromString branch January 25, 2026 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant