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)"
	}
}
1 Like

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.

I am a colleague of Eugene’s and I also looked into the problem. I have written a sample form that should satisfy the following requirements:

  • The measure-field in an instance of the treatment-group should only be enabled if in that instance of the group
    • the medicine is m2
    • and the patient was older than 65 years on the date in the instance.
  • The side effect field in an instance of the treatment-group should only be
    enabled if in that instance of the group
    • the medicine is m2
    • and the patient’s weight is more than 100 kilograms.

I wonder if my example is correct according to the standard and if it also satisfies the requirements.

Below the example:

{
	"resourceType": "Questionnaire",
	"status": "draft",
	"extension": [{
		"url": "http://hl7.org/fhir/StructureDefinition/variable",
		"valueExpression": {
			"name": "dateOfBirth",
			"language": "text/fhirpath",
			"expression": "item.where(linkId='dateOfBirth').answer.value"
		}
	},
	{
		"url": "http://hl7.org/fhir/StructureDefinition/variable",
		"valueExpression": {
			"name": "weight",
			"language": "text/fhirpath",
			"expression": "item.where(linkId='weight').answer.value"
		}
	}],
	"item": [{
		"linkId": "dateOfBirth",
		"text": "date of birth",
		"type": "date",
		"repeats": false
	},
	{
		"linkId": "weight",
		"text": "weight (kg)",
		"type": "integer",
		"repeats": false
	},
	{
		"linkId": "treatment",
		"text": "treatment",
		"type": "group",
		"repeats": true,
		"extension": [{
			"url": "http://hl7.org/fhir/StructureDefinition/variable",
			"valueExpression": {
				"name": "dateOfTreatment",
				"language": "text/fhirpath",
				"expression": "item.where(linkId='dateOfTreatment').answer.value"
			}
		},
		{
			"url": "http://hl7.org/fhir/StructureDefinition/variable",
			"valueExpression": {
				"name": "medicine",
				"language": "text/fhirpath",
				"expression": "item.where(linkId='medicine').answer.value"
			}
		}],
		"item": [{
			"linkId": "dateOfTreatment",
			"text": "date",
			"type": "date",
			"repeats": false
		},
		{
			"linkId": "medicine",
			"text": "medicine",
			"type": "choice",
			"repeats": false,
			"answerOption": [
				{
					"valueCoding" : {
						"code":"m1", 
						"display": "medicine 1"
					}
				},
				{
					"valueCoding" : {
						"code":"m2", 
						"display": "medicine 2"
					}
				},
				{
					"valueCoding" : {
						"code":"m3", 
						"display": "medicine 3"
					}
				}
			] 
		},
		{
			"linkId": "sideEffect",
			"text": "side effect",
			"type": "string",
			"repeats": false,
			"extension": [{
				"url": "http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-enableWhenExpression",
				"valueExpression": {
					"language": "text/fhirpath",
					"expression": "%weight>100 and %medicine.code='m2'"
				}
			}]
		},
		{
			"linkId": "measure",
			"text": "measure",
			"type": "string",
			"repeats": false,
			"extension": [{
				"url": "http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-enableWhenExpression",
				"valueExpression": {
					"language": "text/fhirpath",
					"expression": "%dateOfTreatment <= %dateOfBirth + 65 years and %medicine.code='m2'"
				}
			}]
		}]
	}]
}