Skip to content

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

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

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

Conversation

@PSchmiedmayer

Copy link
Copy Markdown
Member

♻️ Current situation & Problem

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

  • Quantity units. The pair rules compare units the way a FHIR Quantity records them: code and system, unit text where there is no code, and no unit only with no unit. The form reduced the condition to one unitCode string that mixed code and display text, and it dropped the system. A condition with the unit text kilogram therefore never matched a question coded kg with that display, and a condition in another system matched by code alone. A coded unit without a system was also exported as a Quantity 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 kept the wall-clock time and dropped the offset and the fraction of a second. The form then compared 10:00 in the device's own zone, while the pair rules compared the instant 05:00 UTC. A dateTime condition with a time could not be exported at all.
  • Sources with unknown enablement. When the question a condition reads has an enablement that cannot be evaluated, for example because it is on a dependency cycle, the device left the condition open and the task disabled. The exported response omits such a question, so the pair rules decide the condition without its answers: != true holds and = true does not.
  • Long chains. Evaluating a condition recursed once for every task it depended on, through their conditions and the choice tasks enclosing them. About 30 chained conditional choice tasks overflowed a 512 KB thread stack.
  • Follow-ups of a disabled choice. A condition on a follow-up task beneath a choice task without a selection always counted as unsettled, so progress counted the gated task as one that may still be asked, also when the choice task itself was disabled for good.

⚙️ Release Notes

  • Quantity unit identity as FHIR records it. Questionnaire.Condition.Value.quantity carries the parts of a FHIR answerQuantity:

    .quantity(value: 80, system: URL(string: "http://unitsofmeasure.org"), code: "kg")  // by code and system
    .quantity(value: 80, unit: "kilogram")                                             // by unit text

    A code without a system takes the system the question gives that unit; without one, it counts as unit text, since FHIR records a unit code only together with its system. The form records an answer's unit with the same rule the export uses (RecordedQuantityUnit, shared by both), and orders it against the condition with the pair rules' unit rule: a condition with a code orders answers with that code and, where it names one, that system; one with only unit text orders answers with that text; one without a unit orders answers without a unit. Every other combination leaves the comparison open. A response and a condition with a code but no system are exported with the unit as text. The NumberQuestion operators compare in the question's unit, code and display text.

  • Date-time conditions keep their offset. Importing a dateTime condition with a time keeps its time zone and fractional seconds in the DateComponents, so the form compares the same instant as the pair rules. Time values keep their fractional seconds on import and export, in conditions, answer options, initial values and answers. Exporting a dateTime condition writes the time with its zone; a condition with a time and without a zone is not exported, since the instant it names would be open.

  • The answers of a question that is not enabled count as absent. This now also holds where the question's enablement cannot be evaluated, as the exported response omits the question:

    cycle-a: enabled when cycle-b exists; cycle-b: enabled when cycle-a exists  -> both cannot be evaluated
    cycle-a != true  -> enabled      cycle-a = true  -> disabled      cycle-a exists = false  -> enabled
    

    As in the pair rules, a task or group whose enablement depends on itself, through the tasks its conditions reference, the groups enclosing it, or the choice task it follows up on, cannot be evaluated. This is decided once per questionnaire from its structure. A condition that cannot be evaluated, such as one with a value of a type the question's answers do not have, closes no cycle and adds no dependency; a custom question kind compares every value itself. A follow-up task's enablement now includes that of its choice task, as an item's includes its enclosing item's in the pair rules.

  • Evaluation without recursion over tasks. For each evaluation, the form places every occurrence of every task in document order, from the stored responses: a follow-up task beneath every selected option, or once without answers while the choice task has no selection. It then evaluates the occurrences the gated task depends on with an explicit stack, dependencies first, so the length of a chain of conditions does not depend on the thread's stack. The placement is kept on the root QuestionnaireResponses for as long as its revision stays, so a render pass places the occurrences once per container. In the container of an unselected option, where a follow-up task's condition can still be evaluated, the follow-up tasks also occur once without answers, as they do in the response.

  • Settled follow-ups. A condition on a follow-up task is settled when the follow-up task is disabled for good, for example beneath a choice task that a settled condition disables, so progress no longer counts the gated task. An answer stored beneath a choice task that is disabled, but could still become enabled, leaves the condition unsettled.

  • Source changes. Condition.Value.quantity(value:unitCode:) is 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:
    • quantityUnitIdentityAgreesWithThePairRules: a coded question, a question whose unit code has no system, and a question with unit options, against conditions by code and system, by code alone, by unit text, in another system (also with !=), without a unit, and in another unit option; each target is answered where the form enables it, and PairValidator reports no disabled item and no missing required item on the exported pair;
    • dateTimeConditionsKeepTheirTimeZone: an answer at 05:30 UTC against conditions with a +05:00 offset, in UTC, and with a fraction of a second, checked against PairValidator in the same way, and the exported condition's zone and time;
    • sourcesWithUnknownEnablementAreDecidedAsThePairRulesDo: !=, = and exists on a question on a dependency cycle, checked against PairValidator;
    • followUpsOfAPermanentlyDisabledChoiceAreSettled: a condition on the follow-up task of a choice task, unsettled while the choice task can still be enabled, also with a stored selection and answer beneath it, and settled once it is disabled for good;
    • conditionsInAnUnselectedOptionReadTheOccurrenceWithoutAnswers: != and not hasResponse on a follow-up task, evaluated in the container of an option that is not selected;
    • conditionsThatCannotBeEvaluatedCloseNoCycle: a condition with a value of another type between two tasks that reference each other;
    • conditionsThatCannotBeEvaluatedAddNoDependency: the same tasks with a third one in a group whose condition reads one of them, so the result does not depend on which task is evaluated first;
    • customKindsCompareEveryValueThemselves: an ordered comparison on a string against a custom question kind that orders its responses;
    • longConditionChainsDoNotDependOnTheThreadStack replaces manyConditionalChoiceTasksEvaluateQuickly: 100 chained conditional choice tasks with answered follow-ups, evaluated for enablement and settledness on a thread with a 128 KB stack, under a time limit;
    • quantityQuestionOperatorsCompareInTheirUnit expects the operators to carry the question's unit system, code and display text.
  • FHIRConformanceTests.exportedQuantitiesCarryTheirUnitSystem builds its condition with the new quantity(value:code:) case.

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

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • 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