How do I use enableWhenExpression?

I’m prototyping a first form according to FHIR Questionnaire. I managed to get enableWhen working for simple conditions. If I want to use a more complex condition, I have to use enableWhenExpression. I cannot find any example on how that should be used. Can anyone provide an example, please?

This is my first attempt (having no knowledge about CQL at all):

{
	"linkId": "xxx",
	"type": "integer",
	"enableWhenExpression": {
		"language": "text/cql",
		"expression": "(a1  <> 0) and (a2 = 1 or a2 = 3)"
	}
}

There’s a very recent example just added to the CI Build. (The extension itself is new, so not a lot of implementation yet.)
http://build.fhir.org/ig/HL7/sdc/behavior.html#example

Thanks! I missed that example. Good to have an example with enableWhenExpression in fhirpath.

I do, however, would like to see an example in CQL, since this seems to be easier to read and to parse.

We may write the expression parser ourselves, so I guess we could use any language or even devise our own. But, of course, it is better to use a standard within the context of questionnaires.

So, if someone could help us out with an enableWhenExpression in CQL that would be great.

Second attempt (using the mentioned example):

{
	"linkId": "a3",
 	"type": "integer",
 	"extension": [
		"url" : "http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-enableWhenExpression",
 		"valueExpression": {
 			"language": "text/cql",
 			"expression": "(a1  <> 0) and (a2 = 1 or a2 = 3)"
 		}
 	]
}

If I’m reading the SDC spec correctly, this would done as:

{
  "resourceType": "Questionnaire",
  "id": "example",
  "item": [
    {
      "linkId": "a3",
      "type": "integer",
      "extension": [
        {
          "url": "http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-enableWhenExpression",
          "valueExpression": {
            "language": "text/cql",
            "expression": "%resource.item I where (I.linkId = 'a1' and I.answer[0].value <> 0) or (I.linkId = 'a2' and I.answer[0].value in { 1, 2 })"
          }
        }
      ]
    }
  ]
}
1 Like

Thanks, that looks like it could be the correct way.

Without knowing CQL, this shows similarities to SQL. If that is true, then the expression is not a boolean value. Rather, it is a set that is either empty or not empty. That doesn’t feel good for a standard that otherwise seems to be so strict.

That’s a good point; in FHIRPath, there is a mechanism for interpreting a collection as a singleton, http://hl7.org/fhirpath/#singleton-evaluation-of-collections

Based on this, implementations would be expected to be able to interpret the results of evaluation using those same semantics, when a conditional expression is expected, and since CQL is a superset of FHIRPath, the same would apply. Basically, if it’s a conditional context, and the result is a collection, exists is used to implicitly convert the result to the expected boolean value.