Skip to content

Align device enableWhen with the pair rules for units, time zones, unknown sources and long chains - #180

Open
PSchmiedmayer wants to merge 6 commits into
questionnaire-enablewhen-numeric-comparisonfrom
questionnaire-enablewhen-gaps
Open

PSchmiedmayer wants to merge 6 commits into
questionnaire-enablewhen-numeric-comparisonfrom
questionnaire-enablewhen-gaps

Conversation

@PSchmiedmayer

@PSchmiedmayer PSchmiedmayer commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

♻️ Current situation & Problem

This PR builds on #175.

With #175, the form's enablement logic in GroveQuestionnaire and the pair rules of PairValidator evaluate enableWhen by FHIR R4. A response exported from the device should be decided by the pair rules exactly as the device decided it. Otherwise the device hides a required question the server expects, or shows one the server rejects as disabled. Five cases still differed:

  • Quantity units: the pair rules compare units as a FHIR Quantity records them (code and system, or unit text without a code). The form reduced the condition to one unitCode string and dropped the system. A coded unit without a system was also exported with a code and no system, which qty-3 forbids.
  • Time zones: importing a dateTime condition such as 2024-03-05T10:00:00+05:00 dropped the offset and fractional seconds, so the form compared 10:00 in the device's zone while the pair rules compared 05:00 UTC.
  • Sources with unknown enablement: when the question a condition reads is on a dependency cycle, the device left the condition open. The exported response omits that question, so the pair rules decide the condition without its answers.
  • Long chains: evaluation recursed once per dependency, so a long chain of conditional tasks could overflow a small thread stack.
  • Follow-ups of a disabled choice: a condition on such a follow-up task always counted as unsettled, so progress counted the gated task as one that may still be asked.

⚙️ Release Notes

  • Condition.Value.quantity carries the parts of a FHIR answerQuantity. A code without a system takes the system the question gives that unit, or otherwise counts as unit text. The form records an answer's unit with the same rule as the export (RecordedQuantityUnit) and orders it against the condition by the pair rules' unit rule; every other combination leaves the comparison open.

    .quantity(value: 80, system: URL(string: "http://unitsofmeasure.org"), code: "kg")  // by code and system
    .quantity(value: 80, unit: "kilogram")                                             // by unit text
  • A dateTime condition keeps its time zone and fractional seconds on import, so the form compares the same instant as the pair rules. Time values keep fractional seconds on import and export. A dateTime condition with a time and without a zone is not exported.

  • The answers of a question whose enablement cannot be evaluated count as absent, as in the exported response:

    cycle-a: enabled when cycle-b exists; cycle-b: enabled when cycle-a exists
    cycle-a != true -> enabled    cycle-a = true -> disabled
    

    Cycles are decided once per questionnaire from its structure, through conditions, enclosing groups and the choice task a follow-up belongs to. A condition that cannot be evaluated adds no dependency.

  • Evaluation places every task occurrence in document order and evaluates the dependencies with an explicit stack, so chain length no longer depends on the thread stack. The placement is cached on the root QuestionnaireResponses for its current revision.

  • A condition on a follow-up task is settled once the follow-up task is disabled for good.

  • Migration: Condition.Value.quantity(value:unitCode:) is now quantity(value:system:code:unit:), with defaults for everything but the value.

📚 Documentation

The documentation of Questionnaire.Condition.Value.quantity describes the unit rule. Code comments describe the occurrence placement, the cycle rule and the explicit evaluation stack.

✅ Testing

  • EnableWhenTests adds quantityUnitIdentityAgreesWithThePairRules, dateTimeConditionsKeepTheirTimeZone and sourcesWithUnknownEnablementAreDecidedAsThePairRulesDo, which answer each target where the form enables it and check the exported pair with PairValidator.
  • It adds followUpsOfAPermanentlyDisabledChoiceAreSettled, conditionsInAnUnselectedOptionReadTheOccurrenceWithoutAnswers, conditionsThatCannotBeEvaluatedCloseNoCycle, conditionsThatCannotBeEvaluatedAddNoDependency and customKindsCompareEveryValueThemselves.
  • longConditionChainsDoNotDependOnTheThreadStack replaces manyConditionalChoiceTasksEvaluateQuickly: 100 chained conditional choice tasks evaluated on a thread with a 128 KB stack, under a time limit.
  • quantityQuestionOperatorsCompareInTheirUnit and FHIRConformanceTests.exportedQuantitiesCarryTheirUnitSystem use the new quantity case.
  • The UI test helper TestAppUITests.tap(_:) now lets an element settle before asking whether it is hittable, because an answer or a swipe scrolls the page a moment later.

Code of Conduct & Contributing Guidelines

By creating and submitting this pull request, you agree to follow our Code of Conduct and Contributing Guidelines:

A response exported from the device is now decided by the pair rules as
the device decided it while the participant answered.

Quantity conditions carry the unit identity of a FHIR answerQuantity
(system, code, unit text). The device records an answer's unit with the
rule the response export uses (RecordedQuantityUnit), and orders it
against the condition with the pair rules' unit rule. A unit code
without a system takes the question's system or counts as text (qty-3),
also in the exported response.

A dateTime condition keeps its time zone and fractional seconds on
import and is exported with its zone; one with a time and no zone is not
exported. Time values keep their fraction of a second on import and
export.

The device places every task occurrence from the stored responses in
document order, as the pair rules place items, and evaluates the
occurrences a condition depends on with an explicit stack, so long
condition chains do not depend on the thread stack. The placement and
the cycle set are kept on the root for one revision. Cycles are decided
from the questionnaire structure over conditions that can be evaluated,
as in the pair rules. The answers of a question whose enablement is
unknown count as absent, as the export omits it. A follow-up task's
enablement includes its choice task's, so a condition on a follow-up of
a choice task that is disabled for good is settled.
@PSchmiedmayer
PSchmiedmayer added this pull request to stack #181 November 28, 2026 00:44
@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 7d4a43a6-6c02-497f-b737-00148fb8389e
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

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