<?xml version="1.0" encoding="UTF-8"?>

<StructureMap xmlns="http://hl7.org/fhir">
  <id value="ExtractOpticalPrescription"/>
  <text>
    <status value="generated"/><div xmlns="http://www.w3.org/1999/xhtml"><p class="res-header-id"><b>Generated Narrative: StructureMap ExtractOpticalPrescription</b></p><a name="ExtractOpticalPrescription"> </a><a name="hcExtractOpticalPrescription"> </a><pre class="fml">
<span style="color: #cc00cc">/// <b>url</b> = </span><span style="color: blue">'https://fhir.slade360.co.ke/fhir/StructureMap/ExtractOpticalPrescription'</span>
<span style="color: #cc00cc">/// <b>name</b> = </span><span style="color: blue">'ExtractOpticalPrescription'</span>
<span style="color: #cc00cc">/// <b>title</b> = </span><span style="color: blue">'Optical Prescription Extraction'</span>
<span style="color: #cc00cc">/// <b>status</b> = </span><span style="color: blue">'active'</span>
<span style="color: #cc00cc">/// <b>description</b> = </span><span style="color: blue">'The optical prescription out as one VisionPrescription plus one Observation per pupillary distance and visual acuity the prescriber wrote. VisionPrescription is the resource R5 defines for exactly this -- an authorization for the provision of glasses and/or contact lenses -- and it takes the powers: sphere, cylinder, axis and the reading addition for each eye, and the contact lens power, back curve, diameter, brand, colour and wear period. It has no element for pupillary distance or visual acuity, and LOINC names both on a glasses lens prescription per eye at distance and near, so those become LOINC-coded Observations rather than notes. Lens type and coating, which VisionPrescription also cannot code, go into lensSpecification.note, which R5 defines as notes for special requirements such as coatings and lens materials.'</span>

<b>uses</b><span style="color: navy"> &quot;</span><a href="http://hl7.org/fhir/R5/questionnaireresponse.html" title="QuestionnaireResponse">http://hl7.org/fhir/StructureDefinition/QuestionnaireResponse</a><span style="color: navy">&quot; </span><b>alias </b>QR <b>as </b><b>source</b>
<b>uses</b><span style="color: navy"> &quot;</span><a href="http://hl7.org/fhir/R5/bundle.html" title="Bundle">http://hl7.org/fhir/StructureDefinition/Bundle</a><span style="color: navy">&quot; </span><b>alias </b>Bundle <b>as </b><b>target</b>
<b>uses</b><span style="color: navy"> &quot;</span><a href="http://hl7.org/fhir/R5/visionprescription.html" title="VisionPrescription">http://hl7.org/fhir/StructureDefinition/VisionPrescription</a><span style="color: navy">&quot; </span><b>alias </b>VisionPrescription <b>as </b><b>target</b>
<b>uses</b><span style="color: navy"> &quot;</span><a href="http://hl7.org/fhir/R5/observation.html" title="Observation">http://hl7.org/fhir/StructureDefinition/Observation</a><span style="color: navy">&quot; </span><b>alias </b>Observation <b>as </b><b>target</b>

<b>group </b>ExtractOpticalPrescription<span style="color: navy">(</span><b>source</b> <span style="color: maroon">qr</span><span style="color: navy"> : </span>QR, <b>target</b> <span style="color: maroon">bundle</span><span style="color: navy"> : </span>Bundle<span style="color: navy">)</span><span style="color: navy"> {
</span>  qr<span style="color: navy"><b> -&gt; </b></span>bundle.type = <span style="color: blue">'transaction'</span> <i>&quot;setBundleType&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">// </span><span style="color: green">The prescription itself, created only when every element VisionPrescription</span>
  <span style="color: navy">// </span><span style="color: green">makes 1..1 can be filled from the response -- not bent to fit:</span>
  <span style="color: navy">// </span><span style="color: green">lensSpecification  at least one lens power. The patterns are every power</span>
  <span style="color: navy">// </span><span style="color: green">item on the form -- the eyewear distance and near rows,</span>
  <span style="color: navy">// </span><span style="color: green">the reading addition and the contact lens rows -- and</span>
  <span style="color: navy">// </span><span style="color: green">deliberately not the pupillary distance or the visual</span>
  <span style="color: navy">// </span><span style="color: green">acuity, which are measurements and extract on their own</span>
  <span style="color: navy">// </span><span style="color: green">below, nor the transposed rows, which are the same</span>
  <span style="color: navy">// </span><span style="color: green">prescription computed the other way round.</span>
  <span style="color: navy">// </span><span style="color: green">patient            the response's subject, when it is a Patient.</span>
  <span style="color: navy">// </span><span style="color: green">dateWritten,       the response's authored. The form does not ask for a</span>
  <span style="color: navy">// </span><span style="color: green">created            date; the client stamps it.</span>
  <span style="color: navy">// </span><span style="color: green">prescriber         the response's author (else source) when it is a</span>
  <span style="color: navy">// </span><span style="color: green">Practitioner or a PractitionerRole. The form does not</span>
  <span style="color: navy">// </span><span style="color: green">ask who; the client stamps the practitioner recording</span>
  <span style="color: navy">// </span><span style="color: green">it.</span>
  <span style="color: navy">// </span><span style="color: green">A response short of any of these extracts no VisionPrescription. That is</span>
  <span style="color: navy">// </span><span style="color: green">the rule held rather than worked round: the alternative is a prescription</span>
  <span style="color: navy">// </span><span style="color: green">stored without a prescriber, or with one invented. The pupillary distances</span>
  <span style="color: navy">// </span><span style="color: green">and visual acuities still extract, because they need none of it.</span>
  qr<b> where </b><span style="color: navy">(</span>(repeat(item).where(linkId.matches('^(eyewear|contact-lens)/(right|left)/((distance|near)/)?(sph|cyl|add|power|base-curve|diameter)$')).answer.value.ofType(decimal).exists() or repeat(item).where(linkId.matches('^(eyewear|contact-lens)/(right|left)/((distance|near)/)?axis$')).answer.value.ofType(integer).exists()) and subject.where(reference.matches('(^|/)Patient/[^/]+$')).exists() and authored.exists() and (author.where(reference.matches('(^|/)(Practitioner|PractitionerRole)/[^/]+$')).exists() or source.where(reference.matches('(^|/)(Practitioner|PractitionerRole)/[^/]+$')).exists())<span style="color: navy">)</span><span style="color: navy"><b> -&gt; </b></span> bundle.entry<b> as </b><span style="color: maroon">entry</span><span style="color: navy">, </span> entry.resource = <b>create</b><span style="color: navy">(</span><span style="color: blue">'VisionPrescription'</span><span style="color: navy">)</span><b> as </b><span style="color: maroon">vp</span><b> then</b><span style="color: navy"> {
</span>    qr<span style="color: navy"><b> -&gt; </b></span> vp<span style="color: navy">, </span> entry<b> then </b>BuildVisionPrescriptionBase<span style="color: navy">(</span><span style="color: maroon">qr</span><span style="color: navy">, </span><span style="color: maroon">vp</span><span style="color: navy">, </span><span style="color: maroon">entry</span><span style="color: navy">)</span> <i>&quot;base&quot;</i><span style="color: navy">;</span>
    qr<span style="color: navy"><b> -&gt; </b></span>vp<b> then </b>SetPrescriber<span style="color: navy">(</span><span style="color: maroon">qr</span><span style="color: navy">, </span><span style="color: maroon">vp</span><span style="color: navy">)</span> <i>&quot;prescriber&quot;</i><span style="color: navy">;</span>
    qr.item<b> as </b><span style="color: maroon">ew</span><b> where </b><span style="color: navy">(</span>linkId = 'eyewear'<span style="color: navy">)</span><b> then </b>SpectacleLenses<span style="color: navy">(</span><span style="color: maroon">qr</span><span style="color: navy">, </span><span style="color: maroon">ew</span><span style="color: navy">, </span><span style="color: maroon">vp</span><span style="color: navy">)</span> <i>&quot;eyewear&quot;</i><span style="color: navy">;</span>
    qr.item<b> as </b><span style="color: maroon">cl</span><b> where </b><span style="color: navy">(</span>linkId = 'contact-lens'<span style="color: navy">)</span><b> then </b>ContactLenses<span style="color: navy">(</span><span style="color: maroon">qr</span><span style="color: navy">, </span><span style="color: maroon">cl</span><span style="color: navy">, </span><span style="color: maroon">vp</span><span style="color: navy">)</span> <i>&quot;contactLens&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;prescription&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">// </span><span style="color: green">Pupillary distance and visual acuity, one Observation each. These extract</span>
  <span style="color: navy">// </span><span style="color: green">whether or not there is a prescription: a PD taken for a frame fitting is</span>
  <span style="color: navy">// </span><span style="color: green">worth keeping on its own.</span>
  qr.item<b> as </b><span style="color: maroon">ew</span><b> where </b><span style="color: navy">(</span>linkId = 'eyewear'<span style="color: navy">)</span><b> then </b>EyewearMeasurements<span style="color: navy">(</span><span style="color: maroon">qr</span><span style="color: navy">, </span><span style="color: maroon">ew</span><span style="color: navy">, </span><span style="color: maroon">bundle</span><span style="color: navy">)</span> <i>&quot;measurements&quot;</i><span style="color: navy">;</span>
<span style="color: navy">}

</span><span style="color: navy">// </span><span style="color: green">─────────────────────────────────────────────────────────────────────────────</span>
<span style="color: navy">// </span><span style="color: green">The prescription</span>
<span style="color: navy">// </span><span style="color: green">─────────────────────────────────────────────────────────────────────────────</span>
<b>group </b>BuildVisionPrescriptionBase<span style="color: navy">(</span><b>source</b> <span style="color: maroon">qr</span><span style="color: navy"> : </span>QR, <b>target</b> <span style="color: maroon">vp</span><span style="color: navy"> : </span>VisionPrescription, <b>target</b> <span style="color: maroon">entry</span><span style="color: navy">)</span><span style="color: navy"> {
</span>  qr<span style="color: navy"><b> -&gt; </b></span>vp<b> then </b>SetResponseProvenance<span style="color: navy">(</span><span style="color: maroon">qr</span><span style="color: navy">, </span><span style="color: maroon">vp</span><span style="color: navy">)</span> <i>&quot;responseProvenance&quot;</i><span style="color: navy">;</span>
  qr<span style="color: navy"><b> -&gt; </b></span>vp.id = <b>uuid</b><span style="color: navy">(</span><span style="color: navy">)</span><b> then </b>SetVisionPrescriptionFullUrl<span style="color: navy">(</span><span style="color: maroon">vp</span><span style="color: navy">, </span><span style="color: maroon">entry</span><span style="color: navy">)</span> <i>&quot;idAndFullUrl&quot;</i><span style="color: navy">;</span>
  qr<span style="color: navy"><b> -&gt; </b></span>entry.request<b> as </b><span style="color: maroon">request</span><b> then</b><span style="color: navy"> {
</span>    qr<span style="color: navy"><b> -&gt; </b></span>request.method = <span style="color: blue">'POST'</span> <i>&quot;requestMethod&quot;</i><span style="color: navy">;</span>
    qr<span style="color: navy"><b> -&gt; </b></span>request.url = <span style="color: blue">'VisionPrescription'</span> <i>&quot;requestUrl&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;entryRequest&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">// </span><span style="color: green">`active`: a prescription recorded on this form is one somebody is about to</span>
  <span style="color: navy">// </span><span style="color: green">dispense against. fm-status has no `completed`.</span>
  qr<span style="color: navy"><b> -&gt; </b></span>vp.status = <span style="color: blue">'active'</span> <i>&quot;status&quot;</i><span style="color: navy">;</span>
  qr.subject<b> as </b><span style="color: maroon">s</span><b> where </b><span style="color: navy">(</span>(reference.exists() and (reference.startsWith('Patient/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Patient')))<span style="color: navy">)</span><span style="color: navy"><b> -&gt; </b></span>vp.patient = <span style="color: maroon">s</span> <i>&quot;patient&quot;</i><span style="color: navy">;</span>
  qr.encounter<b> as </b><span style="color: maroon">e</span><b> where </b><span style="color: navy">(</span>(reference.exists() and (reference.startsWith('Encounter/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Encounter')))<span style="color: navy">)</span><span style="color: navy"><b> -&gt; </b></span>vp.encounter = <span style="color: maroon">e</span><span style="color: navy">;</span>
  <span style="color: navy">// </span><span style="color: green">Both dates, 1..1 each, are the response's `authored`. The form does not ask</span>
  <span style="color: navy">// </span><span style="color: green">for a prescription date: the client stamps the moment it was recorded, and</span>
  <span style="color: navy">// </span><span style="color: green">that is when this prescription was written and when this record was made.</span>
  qr.authored<b> as </b><span style="color: maroon">t</span><span style="color: navy"><b> -&gt; </b></span>vp.dateWritten = <span style="color: maroon">t</span> <i>&quot;dateWritten&quot;</i><span style="color: navy">;</span>
  qr.authored<b> as </b><span style="color: maroon">t</span><span style="color: navy"><b> -&gt; </b></span>vp.created = <span style="color: maroon">t</span> <i>&quot;created&quot;</i><span style="color: navy">;</span>
<span style="color: navy">}

</span><span style="color: navy">// </span><span style="color: green">The prescriber is 1..1, and the form does not ask for one. It is the</span>
<span style="color: navy">// </span><span style="color: green">practitioner who recorded the prescription, which the client stamps on the</span>
<span style="color: navy">// </span><span style="color: green">response as `author`; `source` is taken when there is no author. Either is</span>
<span style="color: navy">// </span><span style="color: green">copied across only when it points at a Practitioner or a PractitionerRole --</span>
<span style="color: navy">// </span><span style="color: green">the two types VisionPrescription.prescriber allows -- because `author` may</span>
<span style="color: navy">// </span><span style="color: green">also name a Device, a Patient or an Organization.</span>
<span style="color: navy">// </span><span style="color: green">Nothing is guessed when neither qualifies: the rule that creates the</span>
<span style="color: navy">// </span><span style="color: green">prescription is guarded on one of them, so this group always finds one.</span>
<b>group </b>SetPrescriber<span style="color: navy">(</span><b>source</b> <span style="color: maroon">qr</span><span style="color: navy"> : </span>QR, <b>target</b> <span style="color: maroon">vp</span><span style="color: navy"> : </span>VisionPrescription<span style="color: navy">)</span><span style="color: navy"> {
</span>  qr.author<b> as </b><span style="color: maroon">a</span><b> where </b><span style="color: navy">(</span>(reference.matches('(^|/)(Practitioner|PractitionerRole)/[^/]+$')) and ((reference.exists() and (reference.startsWith('Practitioner/') or reference.startsWith('PractitionerRole/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Practitioner') or ('/' + type).endsWith('/PractitionerRole'))))<span style="color: navy">)</span><span style="color: navy"><b> -&gt; </b></span>vp.prescriber = <span style="color: maroon">a</span> <i>&quot;prescriberFromAuthor&quot;</i><span style="color: navy">;</span>
  qr<b> where </b><span style="color: navy">(</span>author.where(((reference.exists() and (reference.startsWith('Practitioner/') or reference.startsWith('PractitionerRole/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Practitioner') or ('/' + type).endsWith('/PractitionerRole'))))).exists().not()<span style="color: navy">)</span><b> then</b><span style="color: navy"> {
</span>    qr.source<b> as </b><span style="color: maroon">s</span><b> where </b><span style="color: navy">(</span>(reference.matches('(^|/)(Practitioner|PractitionerRole)/[^/]+$')) and ((reference.exists() and (reference.startsWith('Practitioner/') or reference.startsWith('PractitionerRole/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Practitioner') or ('/' + type).endsWith('/PractitionerRole'))))<span style="color: navy">)</span><span style="color: navy"><b> -&gt; </b></span>vp.prescriber = <span style="color: maroon">s</span> <i>&quot;prescriberFromSource&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;sourceFallback&quot;</i><span style="color: navy">;</span>
<span style="color: navy">}

</span><span style="color: navy">// </span><span style="color: green">── Spectacle lenses ─────────────────────────────────────────────────────────</span>
<span style="color: navy">// </span><span style="color: green">Up to two lens specifications per eye, one per row the prescriber filled in.</span>
<span style="color: navy">// </span><span style="color: green">The distance specification carries the distance vision (DV) row and the</span>
<span style="color: navy">// </span><span style="color: green">reading addition. That is the shape R5 itself uses for a spectacle</span>
<span style="color: navy">// </span><span style="color: green">prescription -- one specification per eye with the distance powers and</span>
<span style="color: navy">// </span><span style="color: green">`add` -- so a consumer that reads only that one still gets a complete</span>
<span style="color: navy">// </span><span style="color: green">multifocal prescription.</span>
<span style="color: navy">// </span><span style="color: green">The near specification carries the near vision (NV) row, when one was</span>
<span style="color: navy">// </span><span style="color: green">written. It is not derived from DV + ADD and it is not dropped when an ADD is</span>
<span style="color: navy">// </span><span style="color: green">present: the prescriber wrote it, and for reading glasses it is the whole</span>
<span style="color: navy">// </span><span style="color: green">prescription. The two cannot be told apart by any element R5 gives a lens</span>
<span style="color: navy">// </span><span style="color: green">specification, so `product` carries HL7's `lens` beside the local</span>
<span style="color: navy">// </span><span style="color: green">`spectacle-lens-distance` or `spectacle-lens-near` -- the same product at a</span>
<span style="color: navy">// </span><span style="color: green">finer grain, which the example-strength binding allows.</span>
<span style="color: navy">// </span><span style="color: green">linkIds are matched by suffix inside an eye, so the two eyes share one set</span>
<span style="color: navy">// </span><span style="color: green">of groups. The eye code is set by the caller, which is the only place that</span>
<span style="color: navy">// </span><span style="color: green">knows which eye it is in.</span>
<b>group </b>SpectacleLenses<span style="color: navy">(</span><b>source</b> <span style="color: maroon">qr</span><span style="color: navy"> : </span>QR, <b>source</b> <span style="color: maroon">ew</span>, <b>target</b> <span style="color: maroon">vp</span><span style="color: navy"> : </span>VisionPrescription<span style="color: navy">)</span><span style="color: navy"> {
</span>  ew.item<b> as </b><span style="color: maroon">eye</span><b> where </b><span style="color: navy">(</span>linkId = 'eyewear/right'<span style="color: navy">)</span><b> then</b><span style="color: navy"> {
</span>    eye<b> where </b><span style="color: navy">(</span>item.where(linkId.endsWith('/distance')).item.where(linkId.endsWith('/sph') or linkId.endsWith('/cyl')).answer.value.ofType(decimal).exists() or item.where(linkId.endsWith('/distance')).item.where(linkId.endsWith('/axis')).answer.value.ofType(integer).exists() or item.where(linkId.endsWith('/add')).answer.value.ofType(decimal).exists()<span style="color: navy">)</span><span style="color: navy"><b> -&gt; </b></span>vp.lensSpecification<b> as </b><span style="color: maroon">spec</span><b> then</b><span style="color: navy"> {
</span>      eye<span style="color: navy"><b> -&gt; </b></span>spec<b> then </b>DistanceSpectacleLens<span style="color: navy">(</span><span style="color: maroon">qr</span><span style="color: navy">, </span><span style="color: maroon">ew</span><span style="color: navy">, </span><span style="color: maroon">eye</span><span style="color: navy">, </span><span style="color: maroon">spec</span><span style="color: navy">)</span> <i>&quot;lens&quot;</i><span style="color: navy">;</span>
      eye<span style="color: navy"><b> -&gt; </b></span>spec.eye = <span style="color: blue">'right'</span> <i>&quot;eyeCode&quot;</i><span style="color: navy">;</span>
    <span style="color: navy">}</span> <i>&quot;rightDistance&quot;</i><span style="color: navy">;</span>
    eye<b> where </b><span style="color: navy">(</span>item.where(linkId.endsWith('/near')).item.where(linkId.endsWith('/sph') or linkId.endsWith('/cyl')).answer.value.ofType(decimal).exists() or item.where(linkId.endsWith('/near')).item.where(linkId.endsWith('/axis')).answer.value.ofType(integer).exists()<span style="color: navy">)</span><span style="color: navy"><b> -&gt; </b></span>vp.lensSpecification<b> as </b><span style="color: maroon">spec</span><b> then</b><span style="color: navy"> {
</span>      eye<span style="color: navy"><b> -&gt; </b></span>spec<b> then </b>NearSpectacleLens<span style="color: navy">(</span><span style="color: maroon">qr</span><span style="color: navy">, </span><span style="color: maroon">ew</span><span style="color: navy">, </span><span style="color: maroon">eye</span><span style="color: navy">, </span><span style="color: maroon">spec</span><span style="color: navy">)</span> <i>&quot;lens&quot;</i><span style="color: navy">;</span>
      eye<span style="color: navy"><b> -&gt; </b></span>spec.eye = <span style="color: blue">'right'</span> <i>&quot;eyeCode&quot;</i><span style="color: navy">;</span>
    <span style="color: navy">}</span> <i>&quot;rightNear&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;right&quot;</i><span style="color: navy">;</span>
  ew.item<b> as </b><span style="color: maroon">eye</span><b> where </b><span style="color: navy">(</span>linkId = 'eyewear/left'<span style="color: navy">)</span><b> then</b><span style="color: navy"> {
</span>    eye<b> where </b><span style="color: navy">(</span>item.where(linkId.endsWith('/distance')).item.where(linkId.endsWith('/sph') or linkId.endsWith('/cyl')).answer.value.ofType(decimal).exists() or item.where(linkId.endsWith('/distance')).item.where(linkId.endsWith('/axis')).answer.value.ofType(integer).exists() or item.where(linkId.endsWith('/add')).answer.value.ofType(decimal).exists()<span style="color: navy">)</span><span style="color: navy"><b> -&gt; </b></span>vp.lensSpecification<b> as </b><span style="color: maroon">spec</span><b> then</b><span style="color: navy"> {
</span>      eye<span style="color: navy"><b> -&gt; </b></span>spec<b> then </b>DistanceSpectacleLens<span style="color: navy">(</span><span style="color: maroon">qr</span><span style="color: navy">, </span><span style="color: maroon">ew</span><span style="color: navy">, </span><span style="color: maroon">eye</span><span style="color: navy">, </span><span style="color: maroon">spec</span><span style="color: navy">)</span> <i>&quot;lens&quot;</i><span style="color: navy">;</span>
      eye<span style="color: navy"><b> -&gt; </b></span>spec.eye = <span style="color: blue">'left'</span> <i>&quot;eyeCode&quot;</i><span style="color: navy">;</span>
    <span style="color: navy">}</span> <i>&quot;leftDistance&quot;</i><span style="color: navy">;</span>
    eye<b> where </b><span style="color: navy">(</span>item.where(linkId.endsWith('/near')).item.where(linkId.endsWith('/sph') or linkId.endsWith('/cyl')).answer.value.ofType(decimal).exists() or item.where(linkId.endsWith('/near')).item.where(linkId.endsWith('/axis')).answer.value.ofType(integer).exists()<span style="color: navy">)</span><span style="color: navy"><b> -&gt; </b></span>vp.lensSpecification<b> as </b><span style="color: maroon">spec</span><b> then</b><span style="color: navy"> {
</span>      eye<span style="color: navy"><b> -&gt; </b></span>spec<b> then </b>NearSpectacleLens<span style="color: navy">(</span><span style="color: maroon">qr</span><span style="color: navy">, </span><span style="color: maroon">ew</span><span style="color: navy">, </span><span style="color: maroon">eye</span><span style="color: navy">, </span><span style="color: maroon">spec</span><span style="color: navy">)</span> <i>&quot;lens&quot;</i><span style="color: navy">;</span>
      eye<span style="color: navy"><b> -&gt; </b></span>spec.eye = <span style="color: blue">'left'</span> <i>&quot;eyeCode&quot;</i><span style="color: navy">;</span>
    <span style="color: navy">}</span> <i>&quot;leftNear&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;left&quot;</i><span style="color: navy">;</span>
<span style="color: navy">}

</span><b>group </b>DistanceSpectacleLens<span style="color: navy">(</span><b>source</b> <span style="color: maroon">qr</span><span style="color: navy"> : </span>QR, <b>source</b> <span style="color: maroon">ew</span>, <b>source</b> <span style="color: maroon">eye</span>, <b>target</b> <span style="color: maroon">spec</span><span style="color: navy">)</span><span style="color: navy"> {
</span>  eye<span style="color: navy"><b> -&gt; </b></span>spec.product<b> as </b><span style="color: maroon">product</span><b> then</b><span style="color: navy"> {
</span>    eye<span style="color: navy"><b> -&gt; </b></span>product.coding<b> as </b><span style="color: maroon">coding</span><b> then</b><span style="color: navy"> {
</span>      eye<span style="color: navy"><b> -&gt; </b></span>coding.system = <span style="color: blue">'http://terminology.hl7.org/CodeSystem/ex-visionprescriptionproduct'</span> <i>&quot;hl7System&quot;</i><span style="color: navy">;</span>
      eye<span style="color: navy"><b> -&gt; </b></span>coding.code = <span style="color: blue">'lens'</span> <i>&quot;hl7Code&quot;</i><span style="color: navy">;</span>
      eye<span style="color: navy"><b> -&gt; </b></span>coding.display = <span style="color: blue">'Lens'</span> <i>&quot;hl7Display&quot;</i><span style="color: navy">;</span>
    <span style="color: navy">}</span> <i>&quot;hl7Coding&quot;</i><span style="color: navy">;</span>
    eye<span style="color: navy"><b> -&gt; </b></span>product.coding<b> as </b><span style="color: maroon">coding</span><b> then</b><span style="color: navy"> {
</span>      eye<span style="color: navy"><b> -&gt; </b></span>coding.system = <span style="color: blue">'https://fhir.slade360.co.ke/fhir/CodeSystem/concept-codesystem'</span> <i>&quot;rowSystem&quot;</i><span style="color: navy">;</span>
      eye<span style="color: navy"><b> -&gt; </b></span>coding.code = <span style="color: blue">'spectacle-lens-distance'</span> <i>&quot;rowCode&quot;</i><span style="color: navy">;</span>
      eye<span style="color: navy"><b> -&gt; </b></span>coding.display = <span style="color: blue">'Spectacle lens for distance vision'</span> <i>&quot;rowDisplay&quot;</i><span style="color: navy">;</span>
    <span style="color: navy">}</span> <i>&quot;rowCoding&quot;</i><span style="color: navy">;</span>
    eye<span style="color: navy"><b> -&gt; </b></span>product.text = <span style="color: blue">'Spectacle lens — distance vision (DV)'</span> <i>&quot;productText&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;product&quot;</i><span style="color: navy">;</span>
  eye.item<b> as </b><span style="color: maroon">row</span><b> where </b><span style="color: navy">(</span>linkId.endsWith('/distance')<span style="color: navy">)</span><b> then</b><span style="color: navy"> {
</span>    row<span style="color: navy"><b> -&gt; </b></span>spec<b> then </b>LensPowers<span style="color: navy">(</span><span style="color: maroon">row</span><span style="color: navy">, </span><span style="color: maroon">spec</span><span style="color: navy">)</span> <i>&quot;powers&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;distanceRow&quot;</i><span style="color: navy">;</span>
  eye.item<b> as </b><span style="color: maroon">it</span><b> where </b><span style="color: navy">(</span>linkId.endsWith('/add') and answer.value.ofType(decimal).exists()<span style="color: navy">)</span><b> then</b><span style="color: navy"> {
</span>    it.answer first<b> as </b><span style="color: maroon">a</span><b> then</b><span style="color: navy"> {
</span>      a.value<b> as </b><span style="color: maroon">v</span><span style="color: navy"><b> -&gt; </b></span>spec.add = <span style="color: maroon">v</span> <i>&quot;addValue&quot;</i><span style="color: navy">;</span>
    <span style="color: navy">}</span> <i>&quot;addAnswer&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;addition&quot;</i><span style="color: navy">;</span>
  ew<span style="color: navy"><b> -&gt; </b></span>spec<b> then </b>SpectacleLensNotes<span style="color: navy">(</span><span style="color: maroon">qr</span><span style="color: navy">, </span><span style="color: maroon">ew</span><span style="color: navy">, </span><span style="color: maroon">spec</span><span style="color: navy">)</span> <i>&quot;notes&quot;</i><span style="color: navy">;</span>
<span style="color: navy">}

</span><b>group </b>NearSpectacleLens<span style="color: navy">(</span><b>source</b> <span style="color: maroon">qr</span><span style="color: navy"> : </span>QR, <b>source</b> <span style="color: maroon">ew</span>, <b>source</b> <span style="color: maroon">eye</span>, <b>target</b> <span style="color: maroon">spec</span><span style="color: navy">)</span><span style="color: navy"> {
</span>  eye<span style="color: navy"><b> -&gt; </b></span>spec.product<b> as </b><span style="color: maroon">product</span><b> then</b><span style="color: navy"> {
</span>    eye<span style="color: navy"><b> -&gt; </b></span>product.coding<b> as </b><span style="color: maroon">coding</span><b> then</b><span style="color: navy"> {
</span>      eye<span style="color: navy"><b> -&gt; </b></span>coding.system = <span style="color: blue">'http://terminology.hl7.org/CodeSystem/ex-visionprescriptionproduct'</span> <i>&quot;hl7System&quot;</i><span style="color: navy">;</span>
      eye<span style="color: navy"><b> -&gt; </b></span>coding.code = <span style="color: blue">'lens'</span> <i>&quot;hl7Code&quot;</i><span style="color: navy">;</span>
      eye<span style="color: navy"><b> -&gt; </b></span>coding.display = <span style="color: blue">'Lens'</span> <i>&quot;hl7Display&quot;</i><span style="color: navy">;</span>
    <span style="color: navy">}</span> <i>&quot;hl7Coding&quot;</i><span style="color: navy">;</span>
    eye<span style="color: navy"><b> -&gt; </b></span>product.coding<b> as </b><span style="color: maroon">coding</span><b> then</b><span style="color: navy"> {
</span>      eye<span style="color: navy"><b> -&gt; </b></span>coding.system = <span style="color: blue">'https://fhir.slade360.co.ke/fhir/CodeSystem/concept-codesystem'</span> <i>&quot;rowSystem&quot;</i><span style="color: navy">;</span>
      eye<span style="color: navy"><b> -&gt; </b></span>coding.code = <span style="color: blue">'spectacle-lens-near'</span> <i>&quot;rowCode&quot;</i><span style="color: navy">;</span>
      eye<span style="color: navy"><b> -&gt; </b></span>coding.display = <span style="color: blue">'Spectacle lens for near vision'</span> <i>&quot;rowDisplay&quot;</i><span style="color: navy">;</span>
    <span style="color: navy">}</span> <i>&quot;rowCoding&quot;</i><span style="color: navy">;</span>
    eye<span style="color: navy"><b> -&gt; </b></span>product.text = <span style="color: blue">'Spectacle lens — near vision (NV)'</span> <i>&quot;productText&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;product&quot;</i><span style="color: navy">;</span>
  eye.item<b> as </b><span style="color: maroon">row</span><b> where </b><span style="color: navy">(</span>linkId.endsWith('/near')<span style="color: navy">)</span><b> then</b><span style="color: navy"> {
</span>    row<span style="color: navy"><b> -&gt; </b></span>spec<b> then </b>LensPowers<span style="color: navy">(</span><span style="color: maroon">row</span><span style="color: navy">, </span><span style="color: maroon">spec</span><span style="color: navy">)</span> <i>&quot;powers&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;nearRow&quot;</i><span style="color: navy">;</span>
  ew<span style="color: navy"><b> -&gt; </b></span>spec<b> then </b>SpectacleLensNotes<span style="color: navy">(</span><span style="color: maroon">qr</span><span style="color: navy">, </span><span style="color: maroon">ew</span><span style="color: navy">, </span><span style="color: maroon">spec</span><span style="color: navy">)</span> <i>&quot;notes&quot;</i><span style="color: navy">;</span>
<span style="color: navy">}

</span><span style="color: navy">// </span><span style="color: green">Sphere, cylinder and axis off one row. Typed guards, because a rule that</span>
<span style="color: navy">// </span><span style="color: green">assigns an answer of the wrong type to a decimal element does not skip -- it</span>
<span style="color: navy">// </span><span style="color: green">fails the whole extraction.</span>
<b>group </b>LensPowers<span style="color: navy">(</span><b>source</b> <span style="color: maroon">row</span>, <b>target</b> <span style="color: maroon">spec</span><span style="color: navy">)</span><span style="color: navy"> {
</span>  row.item<b> as </b><span style="color: maroon">it</span><b> where </b><span style="color: navy">(</span>linkId.endsWith('/sph') and answer.value.ofType(decimal).exists()<span style="color: navy">)</span><b> then</b><span style="color: navy"> {
</span>    it.answer first<b> as </b><span style="color: maroon">a</span><b> then</b><span style="color: navy"> {
</span>      a.value<b> as </b><span style="color: maroon">v</span><span style="color: navy"><b> -&gt; </b></span>spec.sphere = <span style="color: maroon">v</span> <i>&quot;sphereValue&quot;</i><span style="color: navy">;</span>
    <span style="color: navy">}</span> <i>&quot;sphereAnswer&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;sphere&quot;</i><span style="color: navy">;</span>
  row.item<b> as </b><span style="color: maroon">it</span><b> where </b><span style="color: navy">(</span>linkId.endsWith('/cyl') and answer.value.ofType(decimal).exists()<span style="color: navy">)</span><b> then</b><span style="color: navy"> {
</span>    it.answer first<b> as </b><span style="color: maroon">a</span><b> then</b><span style="color: navy"> {
</span>      a.value<b> as </b><span style="color: maroon">v</span><span style="color: navy"><b> -&gt; </b></span>spec.cylinder = <span style="color: maroon">v</span> <i>&quot;cylinderValue&quot;</i><span style="color: navy">;</span>
    <span style="color: navy">}</span> <i>&quot;cylinderAnswer&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;cylinder&quot;</i><span style="color: navy">;</span>
  row.item<b> as </b><span style="color: maroon">it</span><b> where </b><span style="color: navy">(</span>linkId.endsWith('/axis') and answer.value.ofType(integer).exists()<span style="color: navy">)</span><b> then</b><span style="color: navy"> {
</span>    it.answer first<b> as </b><span style="color: maroon">a</span><b> then</b><span style="color: navy"> {
</span>      a.value<b> as </b><span style="color: maroon">v</span><span style="color: navy"><b> -&gt; </b></span>spec.axis = <span style="color: maroon">v</span> <i>&quot;axisValue&quot;</i><span style="color: navy">;</span>
    <span style="color: navy">}</span> <i>&quot;axisAnswer&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;axis&quot;</i><span style="color: navy">;</span>
<span style="color: navy">}

</span><span style="color: navy">// </span><span style="color: green">Lens type and coating, as the words the prescriber ticked. VisionPrescription</span>
<span style="color: navy">// </span><span style="color: green">has no coded element for either; `note` is where R5 says they go (&quot;notes for</span>
<span style="color: navy">// </span><span style="color: green">special requirements such as coatings and lens materials&quot;), and the codes</span>
<span style="color: navy">// </span><span style="color: green">themselves stay on the response. Both go on every spectacle specification,</span>
<span style="color: navy">// </span><span style="color: green">because the paper ticks them once for the pair.</span>
<b>group </b>SpectacleLensNotes<span style="color: navy">(</span><b>source</b> <span style="color: maroon">qr</span><span style="color: navy"> : </span>QR, <b>source</b> <span style="color: maroon">ew</span>, <b>target</b> <span style="color: maroon">spec</span><span style="color: navy">)</span><span style="color: navy"> {
</span>  ew.item<b> as </b><span style="color: maroon">it</span><b> where </b><span style="color: navy">(</span>(linkId = 'eyewear/lens-types') and answer.value.ofType(Coding).exists()<span style="color: navy">)</span><span style="color: navy"><b> -&gt; </b></span>spec.note<b> as </b><span style="color: maroon">note</span><b> then</b><span style="color: navy"> {
</span>    it<span style="color: navy"><b> -&gt; </b></span>note.text = evaluate<span style="color: navy">(</span>it<span style="color: navy">, </span>'Lens type: ' &amp; answer.value.ofType(Coding).select(iif(display.exists(), display, code)).join(', ')<span style="color: navy">)</span> <i>&quot;lensTypeText&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;lensTypes&quot;</i><span style="color: navy">;</span>
  ew.item<b> as </b><span style="color: maroon">it</span><b> where </b><span style="color: navy">(</span>(linkId = 'eyewear/lens-coating') and answer.value.ofType(Coding).exists()<span style="color: navy">)</span><span style="color: navy"><b> -&gt; </b></span>spec.note<b> as </b><span style="color: maroon">note</span><b> then</b><span style="color: navy"> {
</span>    it<span style="color: navy"><b> -&gt; </b></span>note.text = evaluate<span style="color: navy">(</span>it<span style="color: navy">, </span>'Lens coating: ' &amp; answer.value.ofType(Coding).select(iif(display.exists(), display, code)).join(', ')<span style="color: navy">)</span> <i>&quot;lensCoatingText&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;lensCoating&quot;</i><span style="color: navy">;</span>
  qr<span style="color: navy"><b> -&gt; </b></span>spec<b> then </b>PrescriptionNotes<span style="color: navy">(</span><span style="color: maroon">qr</span><span style="color: navy">, </span><span style="color: maroon">spec</span><span style="color: navy">)</span> <i>&quot;prescriptionNotes&quot;</i><span style="color: navy">;</span>
<span style="color: navy">}

</span><span style="color: navy">// </span><span style="color: green">The free-text prescription notes. VisionPrescription has no note of its own,</span>
<span style="color: navy">// </span><span style="color: green">only one per lens specification, so the notes go on each specification --</span>
<span style="color: navy">// </span><span style="color: green">repeated rather than put on whichever happens to be first, which a consumer</span>
<span style="color: navy">// </span><span style="color: green">reading one eye would miss.</span>
<b>group </b>PrescriptionNotes<span style="color: navy">(</span><b>source</b> <span style="color: maroon">qr</span><span style="color: navy"> : </span>QR, <b>target</b> <span style="color: maroon">spec</span><span style="color: navy">)</span><span style="color: navy"> {
</span>  qr.item<b> as </b><span style="color: maroon">it</span><b> where </b><span style="color: navy">(</span>(linkId = 'notes') and answer.value.ofType(string).exists()<span style="color: navy">)</span><span style="color: navy"><b> -&gt; </b></span>spec.note<b> as </b><span style="color: maroon">note</span><b> then</b><span style="color: navy"> {
</span>    it.answer first<b> as </b><span style="color: maroon">a</span><b> then</b><span style="color: navy"> {
</span>      a.value<b> as </b><span style="color: maroon">v</span><span style="color: navy"><b> -&gt; </b></span>note.text = <span style="color: maroon">v</span> <i>&quot;noteText&quot;</i><span style="color: navy">;</span>
    <span style="color: navy">}</span> <i>&quot;noteAnswer&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;notes&quot;</i><span style="color: navy">;</span>
<span style="color: navy">}

</span><span style="color: navy">// </span><span style="color: green">── Contact lenses ───────────────────────────────────────────────────────────</span>
<span style="color: navy">// </span><span style="color: green">One specification per eye with anything written on it. The contact lens</span>
<span style="color: navy">// </span><span style="color: green">power goes to `power`, R5's &quot;contact lens power&quot;, which is what its own</span>
<span style="color: navy">// </span><span style="color: green">contact lens example uses; `sphere` stays the spectacle lens power. Brand,</span>
<span style="color: navy">// </span><span style="color: green">colour and the replacement schedule are asked once for the pair and copied</span>
<span style="color: navy">// </span><span style="color: green">onto both eyes, because R5 holds them per specification.</span>
<b>group </b>ContactLenses<span style="color: navy">(</span><b>source</b> <span style="color: maroon">qr</span><span style="color: navy"> : </span>QR, <b>source</b> <span style="color: maroon">cl</span>, <b>target</b> <span style="color: maroon">vp</span><span style="color: navy"> : </span>VisionPrescription<span style="color: navy">)</span><span style="color: navy"> {
</span>  cl.item<b> as </b><span style="color: maroon">eye</span><b> where </b><span style="color: navy">(</span>(linkId = 'contact-lens/right') and (item.answer.value.ofType(decimal).exists() or item.answer.value.ofType(integer).exists())<span style="color: navy">)</span><span style="color: navy"><b> -&gt; </b></span>vp.lensSpecification<b> as </b><span style="color: maroon">spec</span><b> then</b><span style="color: navy"> {
</span>    eye<span style="color: navy"><b> -&gt; </b></span>spec<b> then </b>ContactLens<span style="color: navy">(</span><span style="color: maroon">qr</span><span style="color: navy">, </span><span style="color: maroon">cl</span><span style="color: navy">, </span><span style="color: maroon">eye</span><span style="color: navy">, </span><span style="color: maroon">spec</span><span style="color: navy">)</span> <i>&quot;lens&quot;</i><span style="color: navy">;</span>
    eye<span style="color: navy"><b> -&gt; </b></span>spec.eye = <span style="color: blue">'right'</span> <i>&quot;eyeCode&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;right&quot;</i><span style="color: navy">;</span>
  cl.item<b> as </b><span style="color: maroon">eye</span><b> where </b><span style="color: navy">(</span>(linkId = 'contact-lens/left') and (item.answer.value.ofType(decimal).exists() or item.answer.value.ofType(integer).exists())<span style="color: navy">)</span><span style="color: navy"><b> -&gt; </b></span>vp.lensSpecification<b> as </b><span style="color: maroon">spec</span><b> then</b><span style="color: navy"> {
</span>    eye<span style="color: navy"><b> -&gt; </b></span>spec<b> then </b>ContactLens<span style="color: navy">(</span><span style="color: maroon">qr</span><span style="color: navy">, </span><span style="color: maroon">cl</span><span style="color: navy">, </span><span style="color: maroon">eye</span><span style="color: navy">, </span><span style="color: maroon">spec</span><span style="color: navy">)</span> <i>&quot;lens&quot;</i><span style="color: navy">;</span>
    eye<span style="color: navy"><b> -&gt; </b></span>spec.eye = <span style="color: blue">'left'</span> <i>&quot;eyeCode&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;left&quot;</i><span style="color: navy">;</span>
<span style="color: navy">}

</span><b>group </b>ContactLens<span style="color: navy">(</span><b>source</b> <span style="color: maroon">qr</span><span style="color: navy"> : </span>QR, <b>source</b> <span style="color: maroon">cl</span>, <b>source</b> <span style="color: maroon">eye</span>, <b>target</b> <span style="color: maroon">spec</span><span style="color: navy">)</span><span style="color: navy"> {
</span>  eye<span style="color: navy"><b> -&gt; </b></span>spec.product<b> as </b><span style="color: maroon">product</span><b> then</b><span style="color: navy"> {
</span>    eye<span style="color: navy"><b> -&gt; </b></span>product.coding<b> as </b><span style="color: maroon">coding</span><b> then</b><span style="color: navy"> {
</span>      eye<span style="color: navy"><b> -&gt; </b></span>coding.system = <span style="color: blue">'http://terminology.hl7.org/CodeSystem/ex-visionprescriptionproduct'</span> <i>&quot;hl7System&quot;</i><span style="color: navy">;</span>
      eye<span style="color: navy"><b> -&gt; </b></span>coding.code = <span style="color: blue">'contact'</span> <i>&quot;hl7Code&quot;</i><span style="color: navy">;</span>
      eye<span style="color: navy"><b> -&gt; </b></span>coding.display = <span style="color: blue">'Contact Lens'</span> <i>&quot;hl7Display&quot;</i><span style="color: navy">;</span>
    <span style="color: navy">}</span> <i>&quot;hl7Coding&quot;</i><span style="color: navy">;</span>
    eye<span style="color: navy"><b> -&gt; </b></span>product.text = <span style="color: blue">'Contact lens'</span> <i>&quot;productText&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;product&quot;</i><span style="color: navy">;</span>
  eye.item<b> as </b><span style="color: maroon">it</span><b> where </b><span style="color: navy">(</span>linkId.endsWith('/power') and answer.value.ofType(decimal).exists()<span style="color: navy">)</span><b> then</b><span style="color: navy"> {
</span>    it.answer first<b> as </b><span style="color: maroon">a</span><b> then</b><span style="color: navy"> {
</span>      a.value<b> as </b><span style="color: maroon">v</span><span style="color: navy"><b> -&gt; </b></span>spec.power = <span style="color: maroon">v</span> <i>&quot;powerValue&quot;</i><span style="color: navy">;</span>
    <span style="color: navy">}</span> <i>&quot;powerAnswer&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;power&quot;</i><span style="color: navy">;</span>
  eye.item<b> as </b><span style="color: maroon">it</span><b> where </b><span style="color: navy">(</span>linkId.endsWith('/cyl') and answer.value.ofType(decimal).exists()<span style="color: navy">)</span><b> then</b><span style="color: navy"> {
</span>    it.answer first<b> as </b><span style="color: maroon">a</span><b> then</b><span style="color: navy"> {
</span>      a.value<b> as </b><span style="color: maroon">v</span><span style="color: navy"><b> -&gt; </b></span>spec.cylinder = <span style="color: maroon">v</span> <i>&quot;cylinderValue&quot;</i><span style="color: navy">;</span>
    <span style="color: navy">}</span> <i>&quot;cylinderAnswer&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;cylinder&quot;</i><span style="color: navy">;</span>
  eye.item<b> as </b><span style="color: maroon">it</span><b> where </b><span style="color: navy">(</span>linkId.endsWith('/axis') and answer.value.ofType(integer).exists()<span style="color: navy">)</span><b> then</b><span style="color: navy"> {
</span>    it.answer first<b> as </b><span style="color: maroon">a</span><b> then</b><span style="color: navy"> {
</span>      a.value<b> as </b><span style="color: maroon">v</span><span style="color: navy"><b> -&gt; </b></span>spec.axis = <span style="color: maroon">v</span> <i>&quot;axisValue&quot;</i><span style="color: navy">;</span>
    <span style="color: navy">}</span> <i>&quot;axisAnswer&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;axis&quot;</i><span style="color: navy">;</span>
  eye.item<b> as </b><span style="color: maroon">it</span><b> where </b><span style="color: navy">(</span>linkId.endsWith('/add') and answer.value.ofType(decimal).exists()<span style="color: navy">)</span><b> then</b><span style="color: navy"> {
</span>    it.answer first<b> as </b><span style="color: maroon">a</span><b> then</b><span style="color: navy"> {
</span>      a.value<b> as </b><span style="color: maroon">v</span><span style="color: navy"><b> -&gt; </b></span>spec.add = <span style="color: maroon">v</span> <i>&quot;addValue&quot;</i><span style="color: navy">;</span>
    <span style="color: navy">}</span> <i>&quot;addAnswer&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;addition&quot;</i><span style="color: navy">;</span>
  eye.item<b> as </b><span style="color: maroon">it</span><b> where </b><span style="color: navy">(</span>linkId.endsWith('/base-curve') and answer.value.ofType(decimal).exists()<span style="color: navy">)</span><b> then</b><span style="color: navy"> {
</span>    it.answer first<b> as </b><span style="color: maroon">a</span><b> then</b><span style="color: navy"> {
</span>      a.value<b> as </b><span style="color: maroon">v</span><span style="color: navy"><b> -&gt; </b></span>spec.backCurve = <span style="color: maroon">v</span> <i>&quot;backCurveValue&quot;</i><span style="color: navy">;</span>
    <span style="color: navy">}</span> <i>&quot;backCurveAnswer&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;backCurve&quot;</i><span style="color: navy">;</span>
  eye.item<b> as </b><span style="color: maroon">it</span><b> where </b><span style="color: navy">(</span>linkId.endsWith('/diameter') and answer.value.ofType(decimal).exists()<span style="color: navy">)</span><b> then</b><span style="color: navy"> {
</span>    it.answer first<b> as </b><span style="color: maroon">a</span><b> then</b><span style="color: navy"> {
</span>      a.value<b> as </b><span style="color: maroon">v</span><span style="color: navy"><b> -&gt; </b></span>spec.diameter = <span style="color: maroon">v</span> <i>&quot;diameterValue&quot;</i><span style="color: navy">;</span>
    <span style="color: navy">}</span> <i>&quot;diameterAnswer&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;diameter&quot;</i><span style="color: navy">;</span>
  cl.item<b> as </b><span style="color: maroon">it</span><b> where </b><span style="color: navy">(</span>(linkId = 'contact-lens/brand') and answer.value.ofType(string).exists()<span style="color: navy">)</span><b> then</b><span style="color: navy"> {
</span>    it.answer first<b> as </b><span style="color: maroon">a</span><b> then</b><span style="color: navy"> {
</span>      a.value<b> as </b><span style="color: maroon">v</span><span style="color: navy"><b> -&gt; </b></span>spec.brand = <span style="color: maroon">v</span> <i>&quot;brandValue&quot;</i><span style="color: navy">;</span>
    <span style="color: navy">}</span> <i>&quot;brandAnswer&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;brand&quot;</i><span style="color: navy">;</span>
  cl.item<b> as </b><span style="color: maroon">it</span><b> where </b><span style="color: navy">(</span>(linkId = 'contact-lens/colour') and answer.value.ofType(string).exists()<span style="color: navy">)</span><b> then</b><span style="color: navy"> {
</span>    it.answer first<b> as </b><span style="color: maroon">a</span><b> then</b><span style="color: navy"> {
</span>      a.value<b> as </b><span style="color: maroon">v</span><span style="color: navy"><b> -&gt; </b></span>spec.color = <span style="color: maroon">v</span> <i>&quot;colourValue&quot;</i><span style="color: navy">;</span>
    <span style="color: navy">}</span> <i>&quot;colourAnswer&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;colour&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">// </span><span style="color: green">The replacement schedule is R5's `duration`, &quot;the recommended maximum wear</span>
  <span style="color: navy">// </span><span style="color: green">period for the lens&quot;, as a UCUM Quantity. One rule per answer, each guarded</span>
  <span style="color: navy">// </span><span style="color: green">on its own code, so an answer this map does not know writes no duration</span>
  <span style="color: navy">// </span><span style="color: green">rather than an empty Quantity.</span>
  cl.item<b> as </b><span style="color: maroon">it</span><b> where </b><span style="color: navy">(</span>(linkId = 'contact-lens/replacement') and (answer.value.ofType(Coding).code = 'contact-lens-replace-daily')<span style="color: navy">)</span><span style="color: navy"><b> -&gt; </b></span>spec.duration<b> as </b><span style="color: maroon">d</span><b> then</b><span style="color: navy"> {
</span>    it<span style="color: navy"><b> -&gt; </b></span>d.value = <span style="color: blue">1</span> <i>&quot;value&quot;</i><span style="color: navy">;</span>
    it<span style="color: navy"><b> -&gt; </b></span>d.unit = <span style="color: blue">'day'</span> <i>&quot;unit&quot;</i><span style="color: navy">;</span>
    it<span style="color: navy"><b> -&gt; </b></span>d.system = <span style="color: blue">'http://unitsofmeasure.org'</span> <i>&quot;system&quot;</i><span style="color: navy">;</span>
    it<span style="color: navy"><b> -&gt; </b></span>d.code = <span style="color: blue">'d'</span> <i>&quot;code&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;daily&quot;</i><span style="color: navy">;</span>
  cl.item<b> as </b><span style="color: maroon">it</span><b> where </b><span style="color: navy">(</span>(linkId = 'contact-lens/replacement') and (answer.value.ofType(Coding).code = 'contact-lens-replace-two-weekly')<span style="color: navy">)</span><span style="color: navy"><b> -&gt; </b></span>spec.duration<b> as </b><span style="color: maroon">d</span><b> then</b><span style="color: navy"> {
</span>    it<span style="color: navy"><b> -&gt; </b></span>d.value = <span style="color: blue">2</span> <i>&quot;value&quot;</i><span style="color: navy">;</span>
    it<span style="color: navy"><b> -&gt; </b></span>d.unit = <span style="color: blue">'weeks'</span> <i>&quot;unit&quot;</i><span style="color: navy">;</span>
    it<span style="color: navy"><b> -&gt; </b></span>d.system = <span style="color: blue">'http://unitsofmeasure.org'</span> <i>&quot;system&quot;</i><span style="color: navy">;</span>
    it<span style="color: navy"><b> -&gt; </b></span>d.code = <span style="color: blue">'wk'</span> <i>&quot;code&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;twoWeekly&quot;</i><span style="color: navy">;</span>
  cl.item<b> as </b><span style="color: maroon">it</span><b> where </b><span style="color: navy">(</span>(linkId = 'contact-lens/replacement') and (answer.value.ofType(Coding).code = 'contact-lens-replace-monthly')<span style="color: navy">)</span><span style="color: navy"><b> -&gt; </b></span>spec.duration<b> as </b><span style="color: maroon">d</span><b> then</b><span style="color: navy"> {
</span>    it<span style="color: navy"><b> -&gt; </b></span>d.value = <span style="color: blue">1</span> <i>&quot;value&quot;</i><span style="color: navy">;</span>
    it<span style="color: navy"><b> -&gt; </b></span>d.unit = <span style="color: blue">'month'</span> <i>&quot;unit&quot;</i><span style="color: navy">;</span>
    it<span style="color: navy"><b> -&gt; </b></span>d.system = <span style="color: blue">'http://unitsofmeasure.org'</span> <i>&quot;system&quot;</i><span style="color: navy">;</span>
    it<span style="color: navy"><b> -&gt; </b></span>d.code = <span style="color: blue">'mo'</span> <i>&quot;code&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;monthly&quot;</i><span style="color: navy">;</span>
  cl.item<b> as </b><span style="color: maroon">it</span><b> where </b><span style="color: navy">(</span>(linkId = 'contact-lens/replacement') and (answer.value.ofType(Coding).code = 'contact-lens-replace-quarterly')<span style="color: navy">)</span><span style="color: navy"><b> -&gt; </b></span>spec.duration<b> as </b><span style="color: maroon">d</span><b> then</b><span style="color: navy"> {
</span>    it<span style="color: navy"><b> -&gt; </b></span>d.value = <span style="color: blue">3</span> <i>&quot;value&quot;</i><span style="color: navy">;</span>
    it<span style="color: navy"><b> -&gt; </b></span>d.unit = <span style="color: blue">'months'</span> <i>&quot;unit&quot;</i><span style="color: navy">;</span>
    it<span style="color: navy"><b> -&gt; </b></span>d.system = <span style="color: blue">'http://unitsofmeasure.org'</span> <i>&quot;system&quot;</i><span style="color: navy">;</span>
    it<span style="color: navy"><b> -&gt; </b></span>d.code = <span style="color: blue">'mo'</span> <i>&quot;code&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;quarterly&quot;</i><span style="color: navy">;</span>
  cl.item<b> as </b><span style="color: maroon">it</span><b> where </b><span style="color: navy">(</span>(linkId = 'contact-lens/replacement') and (answer.value.ofType(Coding).code = 'contact-lens-replace-yearly')<span style="color: navy">)</span><span style="color: navy"><b> -&gt; </b></span>spec.duration<b> as </b><span style="color: maroon">d</span><b> then</b><span style="color: navy"> {
</span>    it<span style="color: navy"><b> -&gt; </b></span>d.value = <span style="color: blue">1</span> <i>&quot;value&quot;</i><span style="color: navy">;</span>
    it<span style="color: navy"><b> -&gt; </b></span>d.unit = <span style="color: blue">'year'</span> <i>&quot;unit&quot;</i><span style="color: navy">;</span>
    it<span style="color: navy"><b> -&gt; </b></span>d.system = <span style="color: blue">'http://unitsofmeasure.org'</span> <i>&quot;system&quot;</i><span style="color: navy">;</span>
    it<span style="color: navy"><b> -&gt; </b></span>d.code = <span style="color: blue">'a'</span> <i>&quot;code&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;yearly&quot;</i><span style="color: navy">;</span>
  qr<span style="color: navy"><b> -&gt; </b></span>spec<b> then </b>PrescriptionNotes<span style="color: navy">(</span><span style="color: maroon">qr</span><span style="color: navy">, </span><span style="color: maroon">spec</span><span style="color: navy">)</span> <i>&quot;prescriptionNotes&quot;</i><span style="color: navy">;</span>
<span style="color: navy">}

</span><span style="color: navy">// </span><span style="color: green">─────────────────────────────────────────────────────────────────────────────</span>
<span style="color: navy">// </span><span style="color: green">Pupillary distance and visual acuity</span>
<span style="color: navy">// </span><span style="color: green">Neither has a home in VisionPrescription, and LOINC names both exactly, per</span>
<span style="color: navy">// </span><span style="color: green">eye, on a glasses lens prescription -- so each is its own Observation under</span>
<span style="color: navy">// </span><span style="color: green">its own code rather than prose in a note. The codes were looked up, not</span>
<span style="color: navy">// </span><span style="color: green">assumed; each display below is LOINC's own:</span>
<span style="color: navy">// </span><span style="color: green">distance (DV)            near (NV)</span>
<span style="color: navy">// </span><span style="color: green">R-PD       28840-7                  28841-5       mm</span>
<span style="color: navy">// </span><span style="color: green">L-PD       28857-1                  55985-6       mm</span>
<span style="color: navy">// </span><span style="color: green">R-VA       28831-6                  28839-9       as written</span>
<span style="color: navy">// </span><span style="color: green">L-VA       28847-2                  28856-3       as written</span>
<span style="color: navy">// </span><span style="color: green">The PD without a qualifier is LOINC's distance PD; near is the one it</span>
<span style="color: navy">// </span><span style="color: green">qualifies. Visual acuity is kept as the string the prescriber wrote -- 6/6,</span>
<span style="color: navy">// </span><span style="color: green">6/9, N6, CF -- because the notations mix Snellen fractions, N-point print</span>
<span style="color: navy">// </span><span style="color: green">sizes and counting fingers, and forcing them into a Ratio would lose the ones</span>
<span style="color: navy">// </span><span style="color: green">that are not fractions.</span>
<span style="color: navy">// </span><span style="color: green">Nothing on VisionPrescription can point at these. They are tied to it by the</span>
<span style="color: navy">// </span><span style="color: green">encounter, by `derivedFrom` naming the same response, and by the same</span>
<span style="color: navy">// </span><span style="color: green">provenance tag.</span>
<span style="color: navy">// </span><span style="color: green">─────────────────────────────────────────────────────────────────────────────</span>
<b>group </b>EyewearMeasurements<span style="color: navy">(</span><b>source</b> <span style="color: maroon">qr</span><span style="color: navy"> : </span>QR, <b>source</b> <span style="color: maroon">ew</span>, <b>target</b> <span style="color: maroon">bundle</span><span style="color: navy"> : </span>Bundle<span style="color: navy">)</span><span style="color: navy"> {
</span>  ew.item<b> as </b><span style="color: maroon">eye</span><b> where </b><span style="color: navy">(</span>linkId = 'eyewear/right'<span style="color: navy">)</span><b> then</b><span style="color: navy"> {
</span>    eye.item<b> as </b><span style="color: maroon">row</span><b> where </b><span style="color: navy">(</span>linkId = 'eyewear/right/distance'<span style="color: navy">)</span><b> then</b><span style="color: navy"> {
</span>      row.item<b> as </b><span style="color: maroon">it</span><b> where </b><span style="color: navy">(</span>(linkId = 'eyewear/right/distance/pd') and answer.value.ofType(decimal).exists()<span style="color: navy">)</span><span style="color: navy"><b> -&gt; </b></span> bundle.entry<b> as </b><span style="color: maroon">entry</span><span style="color: navy">, </span> entry.resource = <b>create</b><span style="color: navy">(</span><span style="color: blue">'Observation'</span><span style="color: navy">)</span><b> as </b><span style="color: maroon">obs</span><b> then</b><span style="color: navy"> {
</span>        qr<span style="color: navy"><b> -&gt; </b></span> obs<span style="color: navy">, </span> entry<b> then </b>BuildObservationBase<span style="color: navy">(</span><span style="color: maroon">qr</span><span style="color: navy">, </span><span style="color: maroon">obs</span><span style="color: navy">, </span><span style="color: maroon">entry</span><span style="color: navy">)</span> <i>&quot;base&quot;</i><span style="color: navy">;</span>
        it<span style="color: navy"><b> -&gt; </b></span>obs.code<b> as </b><span style="color: maroon">code</span><b> then</b><span style="color: navy"> {
</span>          it<span style="color: navy"><b> -&gt; </b></span>code.coding<b> as </b><span style="color: maroon">coding</span><b> then</b><span style="color: navy"> {
</span>            it<span style="color: navy"><b> -&gt; </b></span>coding.system = <span style="color: blue">'http://loinc.org'</span> <i>&quot;system&quot;</i><span style="color: navy">;</span>
            it<span style="color: navy"><b> -&gt; </b></span>coding.code = <span style="color: blue">'28840-7'</span> <i>&quot;code&quot;</i><span style="color: navy">;</span>
            it<span style="color: navy"><b> -&gt; </b></span>coding.display = <span style="color: blue">'Right glasses lens prescription Pupillary distance.monocular'</span> <i>&quot;display&quot;</i><span style="color: navy">;</span>
          <span style="color: navy">}</span> <i>&quot;coding&quot;</i><span style="color: navy">;</span>
          it<span style="color: navy"><b> -&gt; </b></span>code.text = <span style="color: blue">'Right eye pupillary distance, distance vision (R-PD, DV)'</span> <i>&quot;text&quot;</i><span style="color: navy">;</span>
        <span style="color: navy">}</span> <i>&quot;code&quot;</i><span style="color: navy">;</span>
        it<span style="color: navy"><b> -&gt; </b></span>obs<b> then </b>SetMillimetres<span style="color: navy">(</span><span style="color: maroon">it</span><span style="color: navy">, </span><span style="color: maroon">obs</span><span style="color: navy">)</span> <i>&quot;value&quot;</i><span style="color: navy">;</span>
      <span style="color: navy">}</span> <i>&quot;rightDistancePd&quot;</i><span style="color: navy">;</span>
      row.item<b> as </b><span style="color: maroon">it</span><b> where </b><span style="color: navy">(</span>(linkId = 'eyewear/right/distance/va') and answer.value.ofType(string).exists()<span style="color: navy">)</span><span style="color: navy"><b> -&gt; </b></span> bundle.entry<b> as </b><span style="color: maroon">entry</span><span style="color: navy">, </span> entry.resource = <b>create</b><span style="color: navy">(</span><span style="color: blue">'Observation'</span><span style="color: navy">)</span><b> as </b><span style="color: maroon">obs</span><b> then</b><span style="color: navy"> {
</span>        qr<span style="color: navy"><b> -&gt; </b></span> obs<span style="color: navy">, </span> entry<b> then </b>BuildObservationBase<span style="color: navy">(</span><span style="color: maroon">qr</span><span style="color: navy">, </span><span style="color: maroon">obs</span><span style="color: navy">, </span><span style="color: maroon">entry</span><span style="color: navy">)</span> <i>&quot;base&quot;</i><span style="color: navy">;</span>
        it<span style="color: navy"><b> -&gt; </b></span>obs.code<b> as </b><span style="color: maroon">code</span><b> then</b><span style="color: navy"> {
</span>          it<span style="color: navy"><b> -&gt; </b></span>code.coding<b> as </b><span style="color: maroon">coding</span><b> then</b><span style="color: navy"> {
</span>            it<span style="color: navy"><b> -&gt; </b></span>coding.system = <span style="color: blue">'http://loinc.org'</span> <i>&quot;system&quot;</i><span style="color: navy">;</span>
            it<span style="color: navy"><b> -&gt; </b></span>coding.code = <span style="color: blue">'28831-6'</span> <i>&quot;code&quot;</i><span style="color: navy">;</span>
            it<span style="color: navy"><b> -&gt; </b></span>coding.display = <span style="color: blue">'Right glasses lens prescription Visual acuity far'</span> <i>&quot;display&quot;</i><span style="color: navy">;</span>
          <span style="color: navy">}</span> <i>&quot;coding&quot;</i><span style="color: navy">;</span>
          it<span style="color: navy"><b> -&gt; </b></span>code.text = <span style="color: blue">'Right eye visual acuity, distance vision (R-VA, DV)'</span> <i>&quot;text&quot;</i><span style="color: navy">;</span>
        <span style="color: navy">}</span> <i>&quot;code&quot;</i><span style="color: navy">;</span>
        it<span style="color: navy"><b> -&gt; </b></span>obs<b> then </b>SetAcuity<span style="color: navy">(</span><span style="color: maroon">it</span><span style="color: navy">, </span><span style="color: maroon">obs</span><span style="color: navy">)</span> <i>&quot;value&quot;</i><span style="color: navy">;</span>
      <span style="color: navy">}</span> <i>&quot;rightDistanceVa&quot;</i><span style="color: navy">;</span>
    <span style="color: navy">}</span> <i>&quot;rightDistance&quot;</i><span style="color: navy">;</span>
    eye.item<b> as </b><span style="color: maroon">row</span><b> where </b><span style="color: navy">(</span>linkId = 'eyewear/right/near'<span style="color: navy">)</span><b> then</b><span style="color: navy"> {
</span>      row.item<b> as </b><span style="color: maroon">it</span><b> where </b><span style="color: navy">(</span>(linkId = 'eyewear/right/near/pd') and answer.value.ofType(decimal).exists()<span style="color: navy">)</span><span style="color: navy"><b> -&gt; </b></span> bundle.entry<b> as </b><span style="color: maroon">entry</span><span style="color: navy">, </span> entry.resource = <b>create</b><span style="color: navy">(</span><span style="color: blue">'Observation'</span><span style="color: navy">)</span><b> as </b><span style="color: maroon">obs</span><b> then</b><span style="color: navy"> {
</span>        qr<span style="color: navy"><b> -&gt; </b></span> obs<span style="color: navy">, </span> entry<b> then </b>BuildObservationBase<span style="color: navy">(</span><span style="color: maroon">qr</span><span style="color: navy">, </span><span style="color: maroon">obs</span><span style="color: navy">, </span><span style="color: maroon">entry</span><span style="color: navy">)</span> <i>&quot;base&quot;</i><span style="color: navy">;</span>
        it<span style="color: navy"><b> -&gt; </b></span>obs.code<b> as </b><span style="color: maroon">code</span><b> then</b><span style="color: navy"> {
</span>          it<span style="color: navy"><b> -&gt; </b></span>code.coding<b> as </b><span style="color: maroon">coding</span><b> then</b><span style="color: navy"> {
</span>            it<span style="color: navy"><b> -&gt; </b></span>coding.system = <span style="color: blue">'http://loinc.org'</span> <i>&quot;system&quot;</i><span style="color: navy">;</span>
            it<span style="color: navy"><b> -&gt; </b></span>coding.code = <span style="color: blue">'28841-5'</span> <i>&quot;code&quot;</i><span style="color: navy">;</span>
            it<span style="color: navy"><b> -&gt; </b></span>coding.display = <span style="color: blue">'Right glasses lens prescription Pupillary distance.monocular near'</span> <i>&quot;display&quot;</i><span style="color: navy">;</span>
          <span style="color: navy">}</span> <i>&quot;coding&quot;</i><span style="color: navy">;</span>
          it<span style="color: navy"><b> -&gt; </b></span>code.text = <span style="color: blue">'Right eye pupillary distance, near vision (R-PD, NV)'</span> <i>&quot;text&quot;</i><span style="color: navy">;</span>
        <span style="color: navy">}</span> <i>&quot;code&quot;</i><span style="color: navy">;</span>
        it<span style="color: navy"><b> -&gt; </b></span>obs<b> then </b>SetMillimetres<span style="color: navy">(</span><span style="color: maroon">it</span><span style="color: navy">, </span><span style="color: maroon">obs</span><span style="color: navy">)</span> <i>&quot;value&quot;</i><span style="color: navy">;</span>
      <span style="color: navy">}</span> <i>&quot;rightNearPd&quot;</i><span style="color: navy">;</span>
      row.item<b> as </b><span style="color: maroon">it</span><b> where </b><span style="color: navy">(</span>(linkId = 'eyewear/right/near/va') and answer.value.ofType(string).exists()<span style="color: navy">)</span><span style="color: navy"><b> -&gt; </b></span> bundle.entry<b> as </b><span style="color: maroon">entry</span><span style="color: navy">, </span> entry.resource = <b>create</b><span style="color: navy">(</span><span style="color: blue">'Observation'</span><span style="color: navy">)</span><b> as </b><span style="color: maroon">obs</span><b> then</b><span style="color: navy"> {
</span>        qr<span style="color: navy"><b> -&gt; </b></span> obs<span style="color: navy">, </span> entry<b> then </b>BuildObservationBase<span style="color: navy">(</span><span style="color: maroon">qr</span><span style="color: navy">, </span><span style="color: maroon">obs</span><span style="color: navy">, </span><span style="color: maroon">entry</span><span style="color: navy">)</span> <i>&quot;base&quot;</i><span style="color: navy">;</span>
        it<span style="color: navy"><b> -&gt; </b></span>obs.code<b> as </b><span style="color: maroon">code</span><b> then</b><span style="color: navy"> {
</span>          it<span style="color: navy"><b> -&gt; </b></span>code.coding<b> as </b><span style="color: maroon">coding</span><b> then</b><span style="color: navy"> {
</span>            it<span style="color: navy"><b> -&gt; </b></span>coding.system = <span style="color: blue">'http://loinc.org'</span> <i>&quot;system&quot;</i><span style="color: navy">;</span>
            it<span style="color: navy"><b> -&gt; </b></span>coding.code = <span style="color: blue">'28839-9'</span> <i>&quot;code&quot;</i><span style="color: navy">;</span>
            it<span style="color: navy"><b> -&gt; </b></span>coding.display = <span style="color: blue">'Right glasses lens prescription Visual acuity near'</span> <i>&quot;display&quot;</i><span style="color: navy">;</span>
          <span style="color: navy">}</span> <i>&quot;coding&quot;</i><span style="color: navy">;</span>
          it<span style="color: navy"><b> -&gt; </b></span>code.text = <span style="color: blue">'Right eye visual acuity, near vision (R-VA, NV)'</span> <i>&quot;text&quot;</i><span style="color: navy">;</span>
        <span style="color: navy">}</span> <i>&quot;code&quot;</i><span style="color: navy">;</span>
        it<span style="color: navy"><b> -&gt; </b></span>obs<b> then </b>SetAcuity<span style="color: navy">(</span><span style="color: maroon">it</span><span style="color: navy">, </span><span style="color: maroon">obs</span><span style="color: navy">)</span> <i>&quot;value&quot;</i><span style="color: navy">;</span>
      <span style="color: navy">}</span> <i>&quot;rightNearVa&quot;</i><span style="color: navy">;</span>
    <span style="color: navy">}</span> <i>&quot;rightNear&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;right&quot;</i><span style="color: navy">;</span>
  ew.item<b> as </b><span style="color: maroon">eye</span><b> where </b><span style="color: navy">(</span>linkId = 'eyewear/left'<span style="color: navy">)</span><b> then</b><span style="color: navy"> {
</span>    eye.item<b> as </b><span style="color: maroon">row</span><b> where </b><span style="color: navy">(</span>linkId = 'eyewear/left/distance'<span style="color: navy">)</span><b> then</b><span style="color: navy"> {
</span>      row.item<b> as </b><span style="color: maroon">it</span><b> where </b><span style="color: navy">(</span>(linkId = 'eyewear/left/distance/pd') and answer.value.ofType(decimal).exists()<span style="color: navy">)</span><span style="color: navy"><b> -&gt; </b></span> bundle.entry<b> as </b><span style="color: maroon">entry</span><span style="color: navy">, </span> entry.resource = <b>create</b><span style="color: navy">(</span><span style="color: blue">'Observation'</span><span style="color: navy">)</span><b> as </b><span style="color: maroon">obs</span><b> then</b><span style="color: navy"> {
</span>        qr<span style="color: navy"><b> -&gt; </b></span> obs<span style="color: navy">, </span> entry<b> then </b>BuildObservationBase<span style="color: navy">(</span><span style="color: maroon">qr</span><span style="color: navy">, </span><span style="color: maroon">obs</span><span style="color: navy">, </span><span style="color: maroon">entry</span><span style="color: navy">)</span> <i>&quot;base&quot;</i><span style="color: navy">;</span>
        it<span style="color: navy"><b> -&gt; </b></span>obs.code<b> as </b><span style="color: maroon">code</span><b> then</b><span style="color: navy"> {
</span>          it<span style="color: navy"><b> -&gt; </b></span>code.coding<b> as </b><span style="color: maroon">coding</span><b> then</b><span style="color: navy"> {
</span>            it<span style="color: navy"><b> -&gt; </b></span>coding.system = <span style="color: blue">'http://loinc.org'</span> <i>&quot;system&quot;</i><span style="color: navy">;</span>
            it<span style="color: navy"><b> -&gt; </b></span>coding.code = <span style="color: blue">'28857-1'</span> <i>&quot;code&quot;</i><span style="color: navy">;</span>
            it<span style="color: navy"><b> -&gt; </b></span>coding.display = <span style="color: blue">'Left glasses lens prescription Pupillary distance.monocular'</span> <i>&quot;display&quot;</i><span style="color: navy">;</span>
          <span style="color: navy">}</span> <i>&quot;coding&quot;</i><span style="color: navy">;</span>
          it<span style="color: navy"><b> -&gt; </b></span>code.text = <span style="color: blue">'Left eye pupillary distance, distance vision (L-PD, DV)'</span> <i>&quot;text&quot;</i><span style="color: navy">;</span>
        <span style="color: navy">}</span> <i>&quot;code&quot;</i><span style="color: navy">;</span>
        it<span style="color: navy"><b> -&gt; </b></span>obs<b> then </b>SetMillimetres<span style="color: navy">(</span><span style="color: maroon">it</span><span style="color: navy">, </span><span style="color: maroon">obs</span><span style="color: navy">)</span> <i>&quot;value&quot;</i><span style="color: navy">;</span>
      <span style="color: navy">}</span> <i>&quot;leftDistancePd&quot;</i><span style="color: navy">;</span>
      row.item<b> as </b><span style="color: maroon">it</span><b> where </b><span style="color: navy">(</span>(linkId = 'eyewear/left/distance/va') and answer.value.ofType(string).exists()<span style="color: navy">)</span><span style="color: navy"><b> -&gt; </b></span> bundle.entry<b> as </b><span style="color: maroon">entry</span><span style="color: navy">, </span> entry.resource = <b>create</b><span style="color: navy">(</span><span style="color: blue">'Observation'</span><span style="color: navy">)</span><b> as </b><span style="color: maroon">obs</span><b> then</b><span style="color: navy"> {
</span>        qr<span style="color: navy"><b> -&gt; </b></span> obs<span style="color: navy">, </span> entry<b> then </b>BuildObservationBase<span style="color: navy">(</span><span style="color: maroon">qr</span><span style="color: navy">, </span><span style="color: maroon">obs</span><span style="color: navy">, </span><span style="color: maroon">entry</span><span style="color: navy">)</span> <i>&quot;base&quot;</i><span style="color: navy">;</span>
        it<span style="color: navy"><b> -&gt; </b></span>obs.code<b> as </b><span style="color: maroon">code</span><b> then</b><span style="color: navy"> {
</span>          it<span style="color: navy"><b> -&gt; </b></span>code.coding<b> as </b><span style="color: maroon">coding</span><b> then</b><span style="color: navy"> {
</span>            it<span style="color: navy"><b> -&gt; </b></span>coding.system = <span style="color: blue">'http://loinc.org'</span> <i>&quot;system&quot;</i><span style="color: navy">;</span>
            it<span style="color: navy"><b> -&gt; </b></span>coding.code = <span style="color: blue">'28847-2'</span> <i>&quot;code&quot;</i><span style="color: navy">;</span>
            it<span style="color: navy"><b> -&gt; </b></span>coding.display = <span style="color: blue">'Left glasses lens prescription Visual acuity far'</span> <i>&quot;display&quot;</i><span style="color: navy">;</span>
          <span style="color: navy">}</span> <i>&quot;coding&quot;</i><span style="color: navy">;</span>
          it<span style="color: navy"><b> -&gt; </b></span>code.text = <span style="color: blue">'Left eye visual acuity, distance vision (L-VA, DV)'</span> <i>&quot;text&quot;</i><span style="color: navy">;</span>
        <span style="color: navy">}</span> <i>&quot;code&quot;</i><span style="color: navy">;</span>
        it<span style="color: navy"><b> -&gt; </b></span>obs<b> then </b>SetAcuity<span style="color: navy">(</span><span style="color: maroon">it</span><span style="color: navy">, </span><span style="color: maroon">obs</span><span style="color: navy">)</span> <i>&quot;value&quot;</i><span style="color: navy">;</span>
      <span style="color: navy">}</span> <i>&quot;leftDistanceVa&quot;</i><span style="color: navy">;</span>
    <span style="color: navy">}</span> <i>&quot;leftDistance&quot;</i><span style="color: navy">;</span>
    eye.item<b> as </b><span style="color: maroon">row</span><b> where </b><span style="color: navy">(</span>linkId = 'eyewear/left/near'<span style="color: navy">)</span><b> then</b><span style="color: navy"> {
</span>      row.item<b> as </b><span style="color: maroon">it</span><b> where </b><span style="color: navy">(</span>(linkId = 'eyewear/left/near/pd') and answer.value.ofType(decimal).exists()<span style="color: navy">)</span><span style="color: navy"><b> -&gt; </b></span> bundle.entry<b> as </b><span style="color: maroon">entry</span><span style="color: navy">, </span> entry.resource = <b>create</b><span style="color: navy">(</span><span style="color: blue">'Observation'</span><span style="color: navy">)</span><b> as </b><span style="color: maroon">obs</span><b> then</b><span style="color: navy"> {
</span>        qr<span style="color: navy"><b> -&gt; </b></span> obs<span style="color: navy">, </span> entry<b> then </b>BuildObservationBase<span style="color: navy">(</span><span style="color: maroon">qr</span><span style="color: navy">, </span><span style="color: maroon">obs</span><span style="color: navy">, </span><span style="color: maroon">entry</span><span style="color: navy">)</span> <i>&quot;base&quot;</i><span style="color: navy">;</span>
        it<span style="color: navy"><b> -&gt; </b></span>obs.code<b> as </b><span style="color: maroon">code</span><b> then</b><span style="color: navy"> {
</span>          it<span style="color: navy"><b> -&gt; </b></span>code.coding<b> as </b><span style="color: maroon">coding</span><b> then</b><span style="color: navy"> {
</span>            it<span style="color: navy"><b> -&gt; </b></span>coding.system = <span style="color: blue">'http://loinc.org'</span> <i>&quot;system&quot;</i><span style="color: navy">;</span>
            it<span style="color: navy"><b> -&gt; </b></span>coding.code = <span style="color: blue">'55985-6'</span> <i>&quot;code&quot;</i><span style="color: navy">;</span>
            it<span style="color: navy"><b> -&gt; </b></span>coding.display = <span style="color: blue">'Left glasses lens prescription Pupillary distance.monocular near'</span> <i>&quot;display&quot;</i><span style="color: navy">;</span>
          <span style="color: navy">}</span> <i>&quot;coding&quot;</i><span style="color: navy">;</span>
          it<span style="color: navy"><b> -&gt; </b></span>code.text = <span style="color: blue">'Left eye pupillary distance, near vision (L-PD, NV)'</span> <i>&quot;text&quot;</i><span style="color: navy">;</span>
        <span style="color: navy">}</span> <i>&quot;code&quot;</i><span style="color: navy">;</span>
        it<span style="color: navy"><b> -&gt; </b></span>obs<b> then </b>SetMillimetres<span style="color: navy">(</span><span style="color: maroon">it</span><span style="color: navy">, </span><span style="color: maroon">obs</span><span style="color: navy">)</span> <i>&quot;value&quot;</i><span style="color: navy">;</span>
      <span style="color: navy">}</span> <i>&quot;leftNearPd&quot;</i><span style="color: navy">;</span>
      row.item<b> as </b><span style="color: maroon">it</span><b> where </b><span style="color: navy">(</span>(linkId = 'eyewear/left/near/va') and answer.value.ofType(string).exists()<span style="color: navy">)</span><span style="color: navy"><b> -&gt; </b></span> bundle.entry<b> as </b><span style="color: maroon">entry</span><span style="color: navy">, </span> entry.resource = <b>create</b><span style="color: navy">(</span><span style="color: blue">'Observation'</span><span style="color: navy">)</span><b> as </b><span style="color: maroon">obs</span><b> then</b><span style="color: navy"> {
</span>        qr<span style="color: navy"><b> -&gt; </b></span> obs<span style="color: navy">, </span> entry<b> then </b>BuildObservationBase<span style="color: navy">(</span><span style="color: maroon">qr</span><span style="color: navy">, </span><span style="color: maroon">obs</span><span style="color: navy">, </span><span style="color: maroon">entry</span><span style="color: navy">)</span> <i>&quot;base&quot;</i><span style="color: navy">;</span>
        it<span style="color: navy"><b> -&gt; </b></span>obs.code<b> as </b><span style="color: maroon">code</span><b> then</b><span style="color: navy"> {
</span>          it<span style="color: navy"><b> -&gt; </b></span>code.coding<b> as </b><span style="color: maroon">coding</span><b> then</b><span style="color: navy"> {
</span>            it<span style="color: navy"><b> -&gt; </b></span>coding.system = <span style="color: blue">'http://loinc.org'</span> <i>&quot;system&quot;</i><span style="color: navy">;</span>
            it<span style="color: navy"><b> -&gt; </b></span>coding.code = <span style="color: blue">'28856-3'</span> <i>&quot;code&quot;</i><span style="color: navy">;</span>
            it<span style="color: navy"><b> -&gt; </b></span>coding.display = <span style="color: blue">'Left glasses lens prescription Visual acuity near'</span> <i>&quot;display&quot;</i><span style="color: navy">;</span>
          <span style="color: navy">}</span> <i>&quot;coding&quot;</i><span style="color: navy">;</span>
          it<span style="color: navy"><b> -&gt; </b></span>code.text = <span style="color: blue">'Left eye visual acuity, near vision (L-VA, NV)'</span> <i>&quot;text&quot;</i><span style="color: navy">;</span>
        <span style="color: navy">}</span> <i>&quot;code&quot;</i><span style="color: navy">;</span>
        it<span style="color: navy"><b> -&gt; </b></span>obs<b> then </b>SetAcuity<span style="color: navy">(</span><span style="color: maroon">it</span><span style="color: navy">, </span><span style="color: maroon">obs</span><span style="color: navy">)</span> <i>&quot;value&quot;</i><span style="color: navy">;</span>
      <span style="color: navy">}</span> <i>&quot;leftNearVa&quot;</i><span style="color: navy">;</span>
    <span style="color: navy">}</span> <i>&quot;leftNear&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;left&quot;</i><span style="color: navy">;</span>
<span style="color: navy">}

</span><span style="color: navy">// </span><span style="color: green">`Observation.value[x]` has no decimal choice, so the PD is built as a UCUM</span>
<span style="color: navy">// </span><span style="color: green">Quantity. The unit is fixed by the form.</span>
<b>group </b>SetMillimetres<span style="color: navy">(</span><b>source</b> <span style="color: maroon">it</span>, <b>target</b> <span style="color: maroon">obs</span><span style="color: navy"> : </span>Observation<span style="color: navy">)</span><span style="color: navy"> {
</span>  it.answer first<b> as </b><span style="color: maroon">a</span><b> then</b><span style="color: navy"> {
</span>    a.value<b> as </b><span style="color: maroon">v</span><span style="color: navy"><b> -&gt; </b></span>obs.value = <b>create</b><span style="color: navy">(</span><span style="color: blue">'Quantity'</span><span style="color: navy">)</span><b> as </b><span style="color: maroon">q</span><b> then</b><span style="color: navy"> {
</span>      v<span style="color: navy"><b> -&gt; </b></span>q.value = <span style="color: maroon">v</span> <i>&quot;quantityValue&quot;</i><span style="color: navy">;</span>
      v<span style="color: navy"><b> -&gt; </b></span>q.unit = <span style="color: blue">'mm'</span> <i>&quot;quantityUnit&quot;</i><span style="color: navy">;</span>
      v<span style="color: navy"><b> -&gt; </b></span>q.system = <span style="color: blue">'http://unitsofmeasure.org'</span> <i>&quot;quantitySystem&quot;</i><span style="color: navy">;</span>
      v<span style="color: navy"><b> -&gt; </b></span>q.code = <span style="color: blue">'mm'</span> <i>&quot;quantityCode&quot;</i><span style="color: navy">;</span>
    <span style="color: navy">}</span> <i>&quot;quantity&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span><span style="color: navy">;</span>
<span style="color: navy">}

</span><b>group </b>SetAcuity<span style="color: navy">(</span><b>source</b> <span style="color: maroon">it</span>, <b>target</b> <span style="color: maroon">obs</span><span style="color: navy"> : </span>Observation<span style="color: navy">)</span><span style="color: navy"> {
</span>  it.answer first<b> as </b><span style="color: maroon">a</span><b> then</b><span style="color: navy"> {
</span>    a.value<b> as </b><span style="color: maroon">v</span><span style="color: navy"><b> -&gt; </b></span>obs.value = <span style="color: maroon">v</span> <i>&quot;acuityValue&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span><span style="color: navy">;</span>
<span style="color: navy">}

</span><span style="color: navy">// </span><span style="color: green">─────────────────────────────────────────────────────────────────────────────</span>
<span style="color: navy">// </span><span style="color: green">Shared scaffolding. Repeated in each map rather than imported: the transform</span>
<span style="color: navy">// </span><span style="color: green">engine resolves `imports` against whatever StructureMaps happen to be on the</span>
<span style="color: navy">// </span><span style="color: green">server, and a form that stops extracting because a shared map was not</span>
<span style="color: navy">// </span><span style="color: green">uploaded is a worse failure than a duplicated group.</span>
<span style="color: navy">// </span><span style="color: green">─────────────────────────────────────────────────────────────────────────────</span>
<b>group </b>BuildObservationBase<span style="color: navy">(</span><b>source</b> <span style="color: maroon">qr</span><span style="color: navy"> : </span>QR, <b>target</b> <span style="color: maroon">obs</span><span style="color: navy"> : </span>Observation, <b>target</b> <span style="color: maroon">entry</span><span style="color: navy">)</span><span style="color: navy"> {
</span>  qr<span style="color: navy"><b> -&gt; </b></span>obs<b> then </b>SetResponseProvenance<span style="color: navy">(</span><span style="color: maroon">qr</span><span style="color: navy">, </span><span style="color: maroon">obs</span><span style="color: navy">)</span> <i>&quot;responseProvenance&quot;</i><span style="color: navy">;</span>
  qr<span style="color: navy"><b> -&gt; </b></span>obs.id = <b>uuid</b><span style="color: navy">(</span><span style="color: navy">)</span><b> then </b>SetObservationFullUrl<span style="color: navy">(</span><span style="color: maroon">obs</span><span style="color: navy">, </span><span style="color: maroon">entry</span><span style="color: navy">)</span> <i>&quot;idAndFullUrl&quot;</i><span style="color: navy">;</span>
  qr<span style="color: navy"><b> -&gt; </b></span>entry.request<b> as </b><span style="color: maroon">request</span><b> then</b><span style="color: navy"> {
</span>    qr<span style="color: navy"><b> -&gt; </b></span>request.method = <span style="color: blue">'POST'</span> <i>&quot;requestMethod&quot;</i><span style="color: navy">;</span>
    qr<span style="color: navy"><b> -&gt; </b></span>request.url = <span style="color: blue">'Observation'</span> <i>&quot;requestUrl&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;entryRequest&quot;</i><span style="color: navy">;</span>
  qr<span style="color: navy"><b> -&gt; </b></span>obs.status = <span style="color: blue">'final'</span> <i>&quot;status&quot;</i><span style="color: navy">;</span>
  qr<span style="color: navy"><b> -&gt; </b></span>obs.category<b> as </b><span style="color: maroon">cat</span><b> then</b><span style="color: navy"> {
</span>    qr<span style="color: navy"><b> -&gt; </b></span>cat.coding<b> as </b><span style="color: maroon">coding</span><b> then</b><span style="color: navy"> {
</span>      qr<span style="color: navy"><b> -&gt; </b></span>coding.system = <span style="color: blue">'http://terminology.hl7.org/CodeSystem/observation-category'</span> <i>&quot;categorySystem&quot;</i><span style="color: navy">;</span>
      qr<span style="color: navy"><b> -&gt; </b></span>coding.code = <span style="color: blue">'exam'</span> <i>&quot;categoryCode&quot;</i><span style="color: navy">;</span>
      qr<span style="color: navy"><b> -&gt; </b></span>coding.display = <span style="color: blue">'Exam'</span> <i>&quot;categoryDisplay&quot;</i><span style="color: navy">;</span>
    <span style="color: navy">}</span> <i>&quot;categoryCoding&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;category&quot;</i><span style="color: navy">;</span>
  qr.subject<b> as </b><span style="color: maroon">s</span><b> where </b><span style="color: navy">(</span>(reference.exists() and (reference.startsWith('Patient/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Patient')))<span style="color: navy">)</span><span style="color: navy"><b> -&gt; </b></span>obs.subject = <span style="color: maroon">s</span><span style="color: navy">;</span>
  qr.encounter<b> as </b><span style="color: maroon">e</span><b> where </b><span style="color: navy">(</span>(reference.exists() and (reference.startsWith('Encounter/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Encounter')))<span style="color: navy">)</span><span style="color: navy"><b> -&gt; </b></span>obs.encounter = <span style="color: maroon">e</span><span style="color: navy">;</span>
  <span style="color: navy">// </span><span style="color: green">Measured when the prescription was written, which is the response's</span>
  <span style="color: navy">// </span><span style="color: green">`authored` -- the form does not ask for a date.</span>
  qr.authored<b> as </b><span style="color: maroon">t</span><span style="color: navy"><b> -&gt; </b></span>obs.effective = <span style="color: maroon">t</span> <i>&quot;effective&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">// </span><span style="color: green">Measured by the prescriber, chosen the same way as on the prescription.</span>
  qr.author<b> as </b><span style="color: maroon">a</span><b> where </b><span style="color: navy">(</span>(reference.matches('(^|/)(Practitioner|PractitionerRole)/[^/]+$')) and ((reference.exists() and (reference.startsWith('Organization/') or reference.startsWith('Patient/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Organization') or ('/' + type).endsWith('/Patient'))))<span style="color: navy">)</span><span style="color: navy"><b> -&gt; </b></span>obs.performer = <span style="color: maroon">a</span> <i>&quot;performerFromAuthor&quot;</i><span style="color: navy">;</span>
  qr<b> where </b><span style="color: navy">(</span>author.where(((reference.exists() and (reference.startsWith('Organization/') or reference.startsWith('Patient/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Organization') or ('/' + type).endsWith('/Patient'))))).exists().not()<span style="color: navy">)</span><b> then</b><span style="color: navy"> {
</span>    qr.source<b> as </b><span style="color: maroon">s</span><b> where </b><span style="color: navy">(</span>(reference.matches('(^|/)(Practitioner|PractitionerRole)/[^/]+$')) and ((reference.exists() and (reference.startsWith('Organization/') or reference.startsWith('Patient/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Organization') or ('/' + type).endsWith('/Patient'))))<span style="color: navy">)</span><span style="color: navy"><b> -&gt; </b></span>obs.performer = <span style="color: maroon">s</span> <i>&quot;performerFromSource&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;performerSourceFallback&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">// </span><span style="color: green">A stored response's id already reads &quot;QuestionnaireResponse/&lt;id&gt;&quot;, so it is</span>
  <span style="color: navy">// </span><span style="color: green">a relative reference as it stands. The hasValue() guard is what stops an</span>
  <span style="color: navy">// </span><span style="color: green">unsaved response -- one posted straight to $extract rather than saved first</span>
  <span style="color: navy">// </span><span style="color: green">-- from writing &quot;QuestionnaireResponse/null&quot; into the record.</span>
  qr.id<b> as </b><span style="color: maroon">qid</span><b> where </b><span style="color: navy">(</span>$this.hasValue()<span style="color: navy">)</span><span style="color: navy"><b> -&gt; </b></span> obs.derivedFrom = <b>create</b><span style="color: navy">(</span><span style="color: blue">'Reference'</span><span style="color: navy">)</span><b> as </b><span style="color: maroon">ref</span><span style="color: navy">, </span> ref.reference = <span style="color: maroon">qid</span> <i>&quot;linkQuestionnaireResponse&quot;</i><span style="color: navy">;</span>
<span style="color: navy">}

</span><b>group </b>SetObservationFullUrl<span style="color: navy">(</span><b>source</b> <span style="color: maroon">obs</span><span style="color: navy"> : </span>Observation, <b>target</b> <span style="color: maroon">entry</span><span style="color: navy">)</span><span style="color: navy"> {
</span>  obs.id<b> as </b><span style="color: maroon">id</span><span style="color: navy"><b> -&gt; </b></span>entry.fullUrl = <b>append</b><span style="color: navy">(</span><span style="color: blue">'https://fhir.slade360.co.ke/fhir/Observation/'</span><span style="color: navy">, </span><span style="color: maroon">id</span><span style="color: navy">)</span> <i>&quot;assignFullUrl&quot;</i><span style="color: navy">;</span>
<span style="color: navy">}

</span><b>group </b>SetVisionPrescriptionFullUrl<span style="color: navy">(</span><b>source</b> <span style="color: maroon">vp</span><span style="color: navy"> : </span>VisionPrescription, <b>target</b> <span style="color: maroon">entry</span><span style="color: navy">)</span><span style="color: navy"> {
</span>  vp.id<b> as </b><span style="color: maroon">id</span><span style="color: navy"><b> -&gt; </b></span>entry.fullUrl = <b>append</b><span style="color: navy">(</span><span style="color: blue">'https://fhir.slade360.co.ke/fhir/VisionPrescription/'</span><span style="color: navy">, </span><span style="color: maroon">id</span><span style="color: navy">)</span> <i>&quot;assignFullUrl&quot;</i><span style="color: navy">;</span>
<span style="color: navy">}

</span><span style="color: navy">// </span><span style="color: green">── Provenance and tenancy ───────────────────────────────────────────────────</span>
<span style="color: navy">// </span><span style="color: green">Two tags on everything this map creates. The first says which form produced</span>
<span style="color: navy">// </span><span style="color: green">it -- the form's document type, not the questionnaire's id, because every</span>
<span style="color: navy">// </span><span style="color: green">environment mints its own ids and there is one set of maps. The second copies</span>
<span style="color: navy">// </span><span style="color: green">the tenant tags off the response, because nothing else on the path puts them</span>
<span style="color: navy">// </span><span style="color: green">there.</span>
<b>group </b>SetResponseProvenance<span style="color: navy">(</span><b>source</b> <span style="color: maroon">qr</span><span style="color: navy"> : </span>QR, <b>target</b> <span style="color: maroon">r</span><span style="color: navy">)</span><span style="color: navy"> {
</span>  qr<span style="color: navy"><b> -&gt; </b></span>r.meta<b> as </b><span style="color: maroon">m</span><b> then</b><span style="color: navy"> {
</span>    qr<span style="color: navy"><b> -&gt; </b></span>m.tag<b> as </b><span style="color: maroon">t</span><b> then</b><span style="color: navy"> {
</span>      qr<span style="color: navy"><b> -&gt; </b></span>t.system = <span style="color: blue">'http://slade360edi.com/questionnaire-provenance'</span> <i>&quot;provenanceSystem&quot;</i><span style="color: navy">;</span>
      qr<span style="color: navy"><b> -&gt; </b></span>t.code = <span style="color: blue">'optical-prescription'</span> <i>&quot;provenanceCode&quot;</i><span style="color: navy">;</span>
      qr<span style="color: navy"><b> -&gt; </b></span>t.display = <span style="color: blue">'Optical prescription'</span> <i>&quot;provenanceDisplay&quot;</i><span style="color: navy">;</span>
    <span style="color: navy">}</span> <i>&quot;provenanceTag&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;provenanceMeta&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">// </span><span style="color: green">`meta` is 0..1, so this lands on the same meta the rule above created</span>
  <span style="color: navy">// </span><span style="color: green">rather than a second one.</span>
  qr.meta<b> as </b><span style="color: maroon">qm</span><b> then</b><span style="color: navy"> {
</span>    qm.tag<b> as </b><span style="color: maroon">qt</span><span style="color: navy"><b> -&gt; </b></span> r.meta<b> as </b><span style="color: maroon">m</span><span style="color: navy">, </span> m.tag = <span style="color: maroon">qt</span> <i>&quot;copyTag&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;tenantTags&quot;</i><span style="color: navy">;</span>
<span style="color: navy">}

</span></pre></div>
  </text>
  <url value="https://fhir.slade360.co.ke/fhir/StructureMap/ExtractOpticalPrescription"/>
  <version value="0.1.0"/>
  <name value="ExtractOpticalPrescription"/>
  <title value="Optical Prescription Extraction"/>
  <status value="active"/>
  <date value="2026-10-06T18:48:22+00:00"/>
  <publisher value="Kathurima Kimathi"/>
  <contact>
    <name value="Kathurima Kimathi"/>
    <telecom>
      <system value="url"/>
      <value value="https://www.linkedin.com/in/kathurima-kimathi/"/>
    </telecom>
    <telecom>
      <system value="email"/>
      <value value="kathurimakimathi415@gmail.com"/>
    </telecom>
  </contact>
  <contact>
    <name value="Oscar John"/>
    <telecom>
      <system value="email"/>
      <value value="oscarjohnotieno@gmail.com"/>
      <use value="work"/>
    </telecom>
  </contact>
  <contact>
    <name value="Kennedy Omondi"/>
    <telecom>
      <system value="email"/>
      <value value="kennankole@gmail.com"/>
      <use value="work"/>
    </telecom>
  </contact>
  <description value="The optical prescription out as one VisionPrescription plus one Observation per pupillary distance and visual acuity the prescriber wrote. VisionPrescription is the resource R5 defines for exactly this -- an authorization for the provision of glasses and/or contact lenses -- and it takes the powers: sphere, cylinder, axis and the reading addition for each eye, and the contact lens power, back curve, diameter, brand, colour and wear period. It has no element for pupillary distance or visual acuity, and LOINC names both on a glasses lens prescription per eye at distance and near, so those become LOINC-coded Observations rather than notes. Lens type and coating, which VisionPrescription also cannot code, go into lensSpecification.note, which R5 defines as notes for special requirements such as coatings and lens materials."/>
  <structure>
    <url value="http://hl7.org/fhir/StructureDefinition/QuestionnaireResponse"/>
    <mode value="source"/>
    <alias value="QR"/>
  </structure>
  <structure>
    <url value="http://hl7.org/fhir/StructureDefinition/Bundle"/>
    <mode value="target"/>
    <alias value="Bundle"/>
  </structure>
  <structure>
    <url value="http://hl7.org/fhir/StructureDefinition/VisionPrescription"/>
    <mode value="target"/>
    <alias value="VisionPrescription"/>
  </structure>
  <structure>
    <url value="http://hl7.org/fhir/StructureDefinition/Observation"/>
    <mode value="target"/>
    <alias value="Observation"/>
  </structure>
  <group>
    <name value="ExtractOpticalPrescription"/>
    <input>
      <name value="qr"/>
      <type value="QR"/>
      <mode value="source"/>
    </input>
    <input>
      <name value="bundle"/>
      <type value="Bundle"/>
      <mode value="target"/>
    </input>
    <rule>
      <name value="setBundleType"/>
      <source>
        <context value="qr"/>
      </source>
      <target>
        <context value="bundle"/>
        <element value="type"/>
        <transform value="copy"/>
        <parameter>
          <valueString value="transaction"/>
        </parameter>
      </target>
    </rule>
    <rule>
      <name value="prescription"/>
      <source>
        <context value="qr"/>
        <condition value="(repeat(item).where(linkId.matches('^(eyewear|contact-lens)/(right|left)/((distance|near)/)?(sph|cyl|add|power|base-curve|diameter)$')).answer.value.ofType(decimal).exists() or repeat(item).where(linkId.matches('^(eyewear|contact-lens)/(right|left)/((distance|near)/)?axis$')).answer.value.ofType(integer).exists()) and subject.where(reference.matches('(^|/)Patient/[^/]+$')).exists() and authored.exists() and (author.where(reference.matches('(^|/)(Practitioner|PractitionerRole)/[^/]+$')).exists() or source.where(reference.matches('(^|/)(Practitioner|PractitionerRole)/[^/]+$')).exists())"/>
      </source>
      <target>
        <context value="bundle"/>
        <element value="entry"/>
        <variable value="entry"/>
      </target>
      <target>
        <context value="entry"/>
        <element value="resource"/>
        <variable value="vp"/>
        <transform value="create"/>
        <parameter>
          <valueString value="VisionPrescription"/>
        </parameter>
      </target>
      <rule>
        <name value="base"/>
        <source>
          <context value="qr"/>
        </source>
        <target>
          <context value="vp"/>
        </target>
        <target>
          <context value="entry"/>
        </target>
        <dependent>
          <name value="BuildVisionPrescriptionBase"/>
          <parameter>
            <valueId value="qr"/>
          </parameter>
          <parameter>
            <valueId value="vp"/>
          </parameter>
          <parameter>
            <valueId value="entry"/>
          </parameter>
        </dependent>
      </rule>
      <rule>
        <name value="prescriber"/>
        <source>
          <context value="qr"/>
        </source>
        <target>
          <context value="vp"/>
        </target>
        <dependent>
          <name value="SetPrescriber"/>
          <parameter>
            <valueId value="qr"/>
          </parameter>
          <parameter>
            <valueId value="vp"/>
          </parameter>
        </dependent>
      </rule>
      <rule>
        <name value="eyewear"/>
        <source>
          <context value="qr"/>
          <element value="item"/>
          <variable value="ew"/>
          <condition value="linkId = 'eyewear'"/>
        </source>
        <dependent>
          <name value="SpectacleLenses"/>
          <parameter>
            <valueId value="qr"/>
          </parameter>
          <parameter>
            <valueId value="ew"/>
          </parameter>
          <parameter>
            <valueId value="vp"/>
          </parameter>
        </dependent>
      </rule>
      <rule>
        <name value="contactLens"/>
        <source>
          <context value="qr"/>
          <element value="item"/>
          <variable value="cl"/>
          <condition value="linkId = 'contact-lens'"/>
        </source>
        <dependent>
          <name value="ContactLenses"/>
          <parameter>
            <valueId value="qr"/>
          </parameter>
          <parameter>
            <valueId value="cl"/>
          </parameter>
          <parameter>
            <valueId value="vp"/>
          </parameter>
        </dependent>
      </rule>
      <documentation value="The prescription itself, created only when every element VisionPrescription&#xD;&#xA;makes 1..1 can be filled from the response -- not bent to fit:&#xD;&#xA;lensSpecification  at least one lens power. The patterns are every power&#xD;&#xA;item on the form -- the eyewear distance and near rows,&#xD;&#xA;the reading addition and the contact lens rows -- and&#xD;&#xA;deliberately not the pupillary distance or the visual&#xD;&#xA;acuity, which are measurements and extract on their own&#xD;&#xA;below, nor the transposed rows, which are the same&#xD;&#xA;prescription computed the other way round.&#xD;&#xA;patient            the response's subject, when it is a Patient.&#xD;&#xA;dateWritten,       the response's authored. The form does not ask for a&#xD;&#xA;created            date; the client stamps it.&#xD;&#xA;prescriber         the response's author (else source) when it is a&#xD;&#xA;Practitioner or a PractitionerRole. The form does not&#xD;&#xA;ask who; the client stamps the practitioner recording&#xD;&#xA;it.&#xD;&#xA;A response short of any of these extracts no VisionPrescription. That is&#xD;&#xA;the rule held rather than worked round: the alternative is a prescription&#xD;&#xA;stored without a prescriber, or with one invented. The pupillary distances&#xD;&#xA;and visual acuities still extract, because they need none of it."/>
    </rule>
    <rule>
      <name value="measurements"/>
      <source>
        <context value="qr"/>
        <element value="item"/>
        <variable value="ew"/>
        <condition value="linkId = 'eyewear'"/>
      </source>
      <dependent>
        <name value="EyewearMeasurements"/>
        <parameter>
          <valueId value="qr"/>
        </parameter>
        <parameter>
          <valueId value="ew"/>
        </parameter>
        <parameter>
          <valueId value="bundle"/>
        </parameter>
      </dependent>
      <documentation value="Pupillary distance and visual acuity, one Observation each. These extract&#xD;&#xA;whether or not there is a prescription: a PD taken for a frame fitting is&#xD;&#xA;worth keeping on its own."/>
    </rule>
  </group>
  <group>
    <name value="BuildVisionPrescriptionBase"/>
    <documentation value="─────────────────────────────────────────────────────────────────────────────&#xD;&#xA;The prescription&#xD;&#xA;─────────────────────────────────────────────────────────────────────────────"/>
    <input>
      <name value="qr"/>
      <type value="QR"/>
      <mode value="source"/>
    </input>
    <input>
      <name value="vp"/>
      <type value="VisionPrescription"/>
      <mode value="target"/>
    </input>
    <input>
      <name value="entry"/>
      <mode value="target"/>
    </input>
    <rule>
      <name value="responseProvenance"/>
      <source>
        <context value="qr"/>
      </source>
      <target>
        <context value="vp"/>
      </target>
      <dependent>
        <name value="SetResponseProvenance"/>
        <parameter>
          <valueId value="qr"/>
        </parameter>
        <parameter>
          <valueId value="vp"/>
        </parameter>
      </dependent>
    </rule>
    <rule>
      <name value="idAndFullUrl"/>
      <source>
        <context value="qr"/>
      </source>
      <target>
        <context value="vp"/>
        <element value="id"/>
        <transform value="uuid"/>
      </target>
      <dependent>
        <name value="SetVisionPrescriptionFullUrl"/>
        <parameter>
          <valueId value="vp"/>
        </parameter>
        <parameter>
          <valueId value="entry"/>
        </parameter>
      </dependent>
    </rule>
    <rule>
      <name value="entryRequest"/>
      <source>
        <context value="qr"/>
      </source>
      <target>
        <context value="entry"/>
        <element value="request"/>
        <variable value="request"/>
      </target>
      <rule>
        <name value="requestMethod"/>
        <source>
          <context value="qr"/>
        </source>
        <target>
          <context value="request"/>
          <element value="method"/>
          <transform value="copy"/>
          <parameter>
            <valueString value="POST"/>
          </parameter>
        </target>
      </rule>
      <rule>
        <name value="requestUrl"/>
        <source>
          <context value="qr"/>
        </source>
        <target>
          <context value="request"/>
          <element value="url"/>
          <transform value="copy"/>
          <parameter>
            <valueString value="VisionPrescription"/>
          </parameter>
        </target>
      </rule>
    </rule>
    <rule>
      <name value="status"/>
      <source>
        <context value="qr"/>
      </source>
      <target>
        <context value="vp"/>
        <element value="status"/>
        <transform value="copy"/>
        <parameter>
          <valueString value="active"/>
        </parameter>
      </target>
      <documentation value="`active`: a prescription recorded on this form is one somebody is about to&#xD;&#xA;dispense against. fm-status has no `completed`."/>
    </rule>
    <rule>
      <name value="patient"/>
      <source>
        <context value="qr"/>
        <element value="subject"/>
        <variable value="s"/>
        <condition value="(reference.exists() and (reference.startsWith('Patient/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Patient')))"/>
      </source>
      <target>
        <context value="vp"/>
        <element value="patient"/>
        <transform value="copy"/>
        <parameter>
          <valueId value="s"/>
        </parameter>
      </target>
    </rule>
    <rule>
      <name value="encounter"/>
      <source>
        <context value="qr"/>
        <element value="encounter"/>
        <variable value="e"/>
        <condition value="(reference.exists() and (reference.startsWith('Encounter/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Encounter')))"/>
      </source>
      <target>
        <context value="vp"/>
        <element value="encounter"/>
        <transform value="copy"/>
        <parameter>
          <valueId value="e"/>
        </parameter>
      </target>
    </rule>
    <rule>
      <name value="dateWritten"/>
      <source>
        <context value="qr"/>
        <element value="authored"/>
        <variable value="t"/>
      </source>
      <target>
        <context value="vp"/>
        <element value="dateWritten"/>
        <transform value="copy"/>
        <parameter>
          <valueId value="t"/>
        </parameter>
      </target>
      <documentation value="Both dates, 1..1 each, are the response's `authored`. The form does not ask&#xD;&#xA;for a prescription date: the client stamps the moment it was recorded, and&#xD;&#xA;that is when this prescription was written and when this record was made."/>
    </rule>
    <rule>
      <name value="created"/>
      <source>
        <context value="qr"/>
        <element value="authored"/>
        <variable value="t"/>
      </source>
      <target>
        <context value="vp"/>
        <element value="created"/>
        <transform value="copy"/>
        <parameter>
          <valueId value="t"/>
        </parameter>
      </target>
    </rule>
  </group>
  <group>
    <name value="SetPrescriber"/>
    <documentation value="The prescriber is 1..1, and the form does not ask for one. It is the&#xD;&#xA;practitioner who recorded the prescription, which the client stamps on the&#xD;&#xA;response as `author`; `source` is taken when there is no author. Either is&#xD;&#xA;copied across only when it points at a Practitioner or a PractitionerRole --&#xD;&#xA;the two types VisionPrescription.prescriber allows -- because `author` may&#xD;&#xA;also name a Device, a Patient or an Organization.&#xD;&#xA;Nothing is guessed when neither qualifies: the rule that creates the&#xD;&#xA;prescription is guarded on one of them, so this group always finds one."/>
    <input>
      <name value="qr"/>
      <type value="QR"/>
      <mode value="source"/>
    </input>
    <input>
      <name value="vp"/>
      <type value="VisionPrescription"/>
      <mode value="target"/>
    </input>
    <rule>
      <name value="prescriberFromAuthor"/>
      <source>
        <context value="qr"/>
        <element value="author"/>
        <variable value="a"/>
        <condition value="(reference.matches('(^|/)(Practitioner|PractitionerRole)/[^/]+$')) and ((reference.exists() and (reference.startsWith('Practitioner/') or reference.startsWith('PractitionerRole/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Practitioner') or ('/' + type).endsWith('/PractitionerRole'))))"/>
      </source>
      <target>
        <context value="vp"/>
        <element value="prescriber"/>
        <transform value="copy"/>
        <parameter>
          <valueId value="a"/>
        </parameter>
      </target>
    </rule>
    <rule>
      <name value="sourceFallback"/>
      <source>
        <context value="qr"/>
        <condition value="author.where(((reference.exists() and (reference.startsWith('Practitioner/') or reference.startsWith('PractitionerRole/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Practitioner') or ('/' + type).endsWith('/PractitionerRole'))))).exists().not()"/>
      </source>
      <rule>
        <name value="prescriberFromSource"/>
        <source>
          <context value="qr"/>
          <element value="source"/>
          <variable value="s"/>
          <condition value="(reference.matches('(^|/)(Practitioner|PractitionerRole)/[^/]+$')) and ((reference.exists() and (reference.startsWith('Practitioner/') or reference.startsWith('PractitionerRole/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Practitioner') or ('/' + type).endsWith('/PractitionerRole'))))"/>
        </source>
        <target>
          <context value="vp"/>
          <element value="prescriber"/>
          <transform value="copy"/>
          <parameter>
            <valueId value="s"/>
          </parameter>
        </target>
      </rule>
    </rule>
  </group>
  <group>
    <name value="SpectacleLenses"/>
    <documentation value="── Spectacle lenses ─────────────────────────────────────────────────────────&#xD;&#xA;Up to two lens specifications per eye, one per row the prescriber filled in.&#xD;&#xA;The distance specification carries the distance vision (DV) row and the&#xD;&#xA;reading addition. That is the shape R5 itself uses for a spectacle&#xD;&#xA;prescription -- one specification per eye with the distance powers and&#xD;&#xA;`add` -- so a consumer that reads only that one still gets a complete&#xD;&#xA;multifocal prescription.&#xD;&#xA;The near specification carries the near vision (NV) row, when one was&#xD;&#xA;written. It is not derived from DV + ADD and it is not dropped when an ADD is&#xD;&#xA;present: the prescriber wrote it, and for reading glasses it is the whole&#xD;&#xA;prescription. The two cannot be told apart by any element R5 gives a lens&#xD;&#xA;specification, so `product` carries HL7's `lens` beside the local&#xD;&#xA;`spectacle-lens-distance` or `spectacle-lens-near` -- the same product at a&#xD;&#xA;finer grain, which the example-strength binding allows.&#xD;&#xA;linkIds are matched by suffix inside an eye, so the two eyes share one set&#xD;&#xA;of groups. The eye code is set by the caller, which is the only place that&#xD;&#xA;knows which eye it is in."/>
    <input>
      <name value="qr"/>
      <type value="QR"/>
      <mode value="source"/>
    </input>
    <input>
      <name value="ew"/>
      <mode value="source"/>
    </input>
    <input>
      <name value="vp"/>
      <type value="VisionPrescription"/>
      <mode value="target"/>
    </input>
    <rule>
      <name value="right"/>
      <source>
        <context value="ew"/>
        <element value="item"/>
        <variable value="eye"/>
        <condition value="linkId = 'eyewear/right'"/>
      </source>
      <rule>
        <name value="rightDistance"/>
        <source>
          <context value="eye"/>
          <condition value="item.where(linkId.endsWith('/distance')).item.where(linkId.endsWith('/sph') or linkId.endsWith('/cyl')).answer.value.ofType(decimal).exists() or item.where(linkId.endsWith('/distance')).item.where(linkId.endsWith('/axis')).answer.value.ofType(integer).exists() or item.where(linkId.endsWith('/add')).answer.value.ofType(decimal).exists()"/>
        </source>
        <target>
          <context value="vp"/>
          <element value="lensSpecification"/>
          <variable value="spec"/>
        </target>
        <rule>
          <name value="lens"/>
          <source>
            <context value="eye"/>
          </source>
          <target>
            <context value="spec"/>
          </target>
          <dependent>
            <name value="DistanceSpectacleLens"/>
            <parameter>
              <valueId value="qr"/>
            </parameter>
            <parameter>
              <valueId value="ew"/>
            </parameter>
            <parameter>
              <valueId value="eye"/>
            </parameter>
            <parameter>
              <valueId value="spec"/>
            </parameter>
          </dependent>
        </rule>
        <rule>
          <name value="eyeCode"/>
          <source>
            <context value="eye"/>
          </source>
          <target>
            <context value="spec"/>
            <element value="eye"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="right"/>
            </parameter>
          </target>
        </rule>
      </rule>
      <rule>
        <name value="rightNear"/>
        <source>
          <context value="eye"/>
          <condition value="item.where(linkId.endsWith('/near')).item.where(linkId.endsWith('/sph') or linkId.endsWith('/cyl')).answer.value.ofType(decimal).exists() or item.where(linkId.endsWith('/near')).item.where(linkId.endsWith('/axis')).answer.value.ofType(integer).exists()"/>
        </source>
        <target>
          <context value="vp"/>
          <element value="lensSpecification"/>
          <variable value="spec"/>
        </target>
        <rule>
          <name value="lens"/>
          <source>
            <context value="eye"/>
          </source>
          <target>
            <context value="spec"/>
          </target>
          <dependent>
            <name value="NearSpectacleLens"/>
            <parameter>
              <valueId value="qr"/>
            </parameter>
            <parameter>
              <valueId value="ew"/>
            </parameter>
            <parameter>
              <valueId value="eye"/>
            </parameter>
            <parameter>
              <valueId value="spec"/>
            </parameter>
          </dependent>
        </rule>
        <rule>
          <name value="eyeCode"/>
          <source>
            <context value="eye"/>
          </source>
          <target>
            <context value="spec"/>
            <element value="eye"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="right"/>
            </parameter>
          </target>
        </rule>
      </rule>
    </rule>
    <rule>
      <name value="left"/>
      <source>
        <context value="ew"/>
        <element value="item"/>
        <variable value="eye"/>
        <condition value="linkId = 'eyewear/left'"/>
      </source>
      <rule>
        <name value="leftDistance"/>
        <source>
          <context value="eye"/>
          <condition value="item.where(linkId.endsWith('/distance')).item.where(linkId.endsWith('/sph') or linkId.endsWith('/cyl')).answer.value.ofType(decimal).exists() or item.where(linkId.endsWith('/distance')).item.where(linkId.endsWith('/axis')).answer.value.ofType(integer).exists() or item.where(linkId.endsWith('/add')).answer.value.ofType(decimal).exists()"/>
        </source>
        <target>
          <context value="vp"/>
          <element value="lensSpecification"/>
          <variable value="spec"/>
        </target>
        <rule>
          <name value="lens"/>
          <source>
            <context value="eye"/>
          </source>
          <target>
            <context value="spec"/>
          </target>
          <dependent>
            <name value="DistanceSpectacleLens"/>
            <parameter>
              <valueId value="qr"/>
            </parameter>
            <parameter>
              <valueId value="ew"/>
            </parameter>
            <parameter>
              <valueId value="eye"/>
            </parameter>
            <parameter>
              <valueId value="spec"/>
            </parameter>
          </dependent>
        </rule>
        <rule>
          <name value="eyeCode"/>
          <source>
            <context value="eye"/>
          </source>
          <target>
            <context value="spec"/>
            <element value="eye"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="left"/>
            </parameter>
          </target>
        </rule>
      </rule>
      <rule>
        <name value="leftNear"/>
        <source>
          <context value="eye"/>
          <condition value="item.where(linkId.endsWith('/near')).item.where(linkId.endsWith('/sph') or linkId.endsWith('/cyl')).answer.value.ofType(decimal).exists() or item.where(linkId.endsWith('/near')).item.where(linkId.endsWith('/axis')).answer.value.ofType(integer).exists()"/>
        </source>
        <target>
          <context value="vp"/>
          <element value="lensSpecification"/>
          <variable value="spec"/>
        </target>
        <rule>
          <name value="lens"/>
          <source>
            <context value="eye"/>
          </source>
          <target>
            <context value="spec"/>
          </target>
          <dependent>
            <name value="NearSpectacleLens"/>
            <parameter>
              <valueId value="qr"/>
            </parameter>
            <parameter>
              <valueId value="ew"/>
            </parameter>
            <parameter>
              <valueId value="eye"/>
            </parameter>
            <parameter>
              <valueId value="spec"/>
            </parameter>
          </dependent>
        </rule>
        <rule>
          <name value="eyeCode"/>
          <source>
            <context value="eye"/>
          </source>
          <target>
            <context value="spec"/>
            <element value="eye"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="left"/>
            </parameter>
          </target>
        </rule>
      </rule>
    </rule>
  </group>
  <group>
    <name value="DistanceSpectacleLens"/>
    <input>
      <name value="qr"/>
      <type value="QR"/>
      <mode value="source"/>
    </input>
    <input>
      <name value="ew"/>
      <mode value="source"/>
    </input>
    <input>
      <name value="eye"/>
      <mode value="source"/>
    </input>
    <input>
      <name value="spec"/>
      <mode value="target"/>
    </input>
    <rule>
      <name value="product"/>
      <source>
        <context value="eye"/>
      </source>
      <target>
        <context value="spec"/>
        <element value="product"/>
        <variable value="product"/>
      </target>
      <rule>
        <name value="hl7Coding"/>
        <source>
          <context value="eye"/>
        </source>
        <target>
          <context value="product"/>
          <element value="coding"/>
          <variable value="coding"/>
        </target>
        <rule>
          <name value="hl7System"/>
          <source>
            <context value="eye"/>
          </source>
          <target>
            <context value="coding"/>
            <element value="system"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="http://terminology.hl7.org/CodeSystem/ex-visionprescriptionproduct"/>
            </parameter>
          </target>
        </rule>
        <rule>
          <name value="hl7Code"/>
          <source>
            <context value="eye"/>
          </source>
          <target>
            <context value="coding"/>
            <element value="code"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="lens"/>
            </parameter>
          </target>
        </rule>
        <rule>
          <name value="hl7Display"/>
          <source>
            <context value="eye"/>
          </source>
          <target>
            <context value="coding"/>
            <element value="display"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="Lens"/>
            </parameter>
          </target>
        </rule>
      </rule>
      <rule>
        <name value="rowCoding"/>
        <source>
          <context value="eye"/>
        </source>
        <target>
          <context value="product"/>
          <element value="coding"/>
          <variable value="coding"/>
        </target>
        <rule>
          <name value="rowSystem"/>
          <source>
            <context value="eye"/>
          </source>
          <target>
            <context value="coding"/>
            <element value="system"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="https://fhir.slade360.co.ke/fhir/CodeSystem/concept-codesystem"/>
            </parameter>
          </target>
        </rule>
        <rule>
          <name value="rowCode"/>
          <source>
            <context value="eye"/>
          </source>
          <target>
            <context value="coding"/>
            <element value="code"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="spectacle-lens-distance"/>
            </parameter>
          </target>
        </rule>
        <rule>
          <name value="rowDisplay"/>
          <source>
            <context value="eye"/>
          </source>
          <target>
            <context value="coding"/>
            <element value="display"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="Spectacle lens for distance vision"/>
            </parameter>
          </target>
        </rule>
      </rule>
      <rule>
        <name value="productText"/>
        <source>
          <context value="eye"/>
        </source>
        <target>
          <context value="product"/>
          <element value="text"/>
          <transform value="copy"/>
          <parameter>
            <valueString value="Spectacle lens — distance vision (DV)"/>
          </parameter>
        </target>
      </rule>
    </rule>
    <rule>
      <name value="distanceRow"/>
      <source>
        <context value="eye"/>
        <element value="item"/>
        <variable value="row"/>
        <condition value="linkId.endsWith('/distance')"/>
      </source>
      <rule>
        <name value="powers"/>
        <source>
          <context value="row"/>
        </source>
        <target>
          <context value="spec"/>
        </target>
        <dependent>
          <name value="LensPowers"/>
          <parameter>
            <valueId value="row"/>
          </parameter>
          <parameter>
            <valueId value="spec"/>
          </parameter>
        </dependent>
      </rule>
    </rule>
    <rule>
      <name value="addition"/>
      <source>
        <context value="eye"/>
        <element value="item"/>
        <variable value="it"/>
        <condition value="linkId.endsWith('/add') and answer.value.ofType(decimal).exists()"/>
      </source>
      <rule>
        <name value="addAnswer"/>
        <source>
          <context value="it"/>
          <element value="answer"/>
          <listMode value="first"/>
          <variable value="a"/>
        </source>
        <rule>
          <name value="addValue"/>
          <source>
            <context value="a"/>
            <element value="value"/>
            <variable value="v"/>
          </source>
          <target>
            <context value="spec"/>
            <element value="add"/>
            <transform value="copy"/>
            <parameter>
              <valueId value="v"/>
            </parameter>
          </target>
        </rule>
      </rule>
    </rule>
    <rule>
      <name value="notes"/>
      <source>
        <context value="ew"/>
      </source>
      <target>
        <context value="spec"/>
      </target>
      <dependent>
        <name value="SpectacleLensNotes"/>
        <parameter>
          <valueId value="qr"/>
        </parameter>
        <parameter>
          <valueId value="ew"/>
        </parameter>
        <parameter>
          <valueId value="spec"/>
        </parameter>
      </dependent>
    </rule>
  </group>
  <group>
    <name value="NearSpectacleLens"/>
    <input>
      <name value="qr"/>
      <type value="QR"/>
      <mode value="source"/>
    </input>
    <input>
      <name value="ew"/>
      <mode value="source"/>
    </input>
    <input>
      <name value="eye"/>
      <mode value="source"/>
    </input>
    <input>
      <name value="spec"/>
      <mode value="target"/>
    </input>
    <rule>
      <name value="product"/>
      <source>
        <context value="eye"/>
      </source>
      <target>
        <context value="spec"/>
        <element value="product"/>
        <variable value="product"/>
      </target>
      <rule>
        <name value="hl7Coding"/>
        <source>
          <context value="eye"/>
        </source>
        <target>
          <context value="product"/>
          <element value="coding"/>
          <variable value="coding"/>
        </target>
        <rule>
          <name value="hl7System"/>
          <source>
            <context value="eye"/>
          </source>
          <target>
            <context value="coding"/>
            <element value="system"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="http://terminology.hl7.org/CodeSystem/ex-visionprescriptionproduct"/>
            </parameter>
          </target>
        </rule>
        <rule>
          <name value="hl7Code"/>
          <source>
            <context value="eye"/>
          </source>
          <target>
            <context value="coding"/>
            <element value="code"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="lens"/>
            </parameter>
          </target>
        </rule>
        <rule>
          <name value="hl7Display"/>
          <source>
            <context value="eye"/>
          </source>
          <target>
            <context value="coding"/>
            <element value="display"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="Lens"/>
            </parameter>
          </target>
        </rule>
      </rule>
      <rule>
        <name value="rowCoding"/>
        <source>
          <context value="eye"/>
        </source>
        <target>
          <context value="product"/>
          <element value="coding"/>
          <variable value="coding"/>
        </target>
        <rule>
          <name value="rowSystem"/>
          <source>
            <context value="eye"/>
          </source>
          <target>
            <context value="coding"/>
            <element value="system"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="https://fhir.slade360.co.ke/fhir/CodeSystem/concept-codesystem"/>
            </parameter>
          </target>
        </rule>
        <rule>
          <name value="rowCode"/>
          <source>
            <context value="eye"/>
          </source>
          <target>
            <context value="coding"/>
            <element value="code"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="spectacle-lens-near"/>
            </parameter>
          </target>
        </rule>
        <rule>
          <name value="rowDisplay"/>
          <source>
            <context value="eye"/>
          </source>
          <target>
            <context value="coding"/>
            <element value="display"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="Spectacle lens for near vision"/>
            </parameter>
          </target>
        </rule>
      </rule>
      <rule>
        <name value="productText"/>
        <source>
          <context value="eye"/>
        </source>
        <target>
          <context value="product"/>
          <element value="text"/>
          <transform value="copy"/>
          <parameter>
            <valueString value="Spectacle lens — near vision (NV)"/>
          </parameter>
        </target>
      </rule>
    </rule>
    <rule>
      <name value="nearRow"/>
      <source>
        <context value="eye"/>
        <element value="item"/>
        <variable value="row"/>
        <condition value="linkId.endsWith('/near')"/>
      </source>
      <rule>
        <name value="powers"/>
        <source>
          <context value="row"/>
        </source>
        <target>
          <context value="spec"/>
        </target>
        <dependent>
          <name value="LensPowers"/>
          <parameter>
            <valueId value="row"/>
          </parameter>
          <parameter>
            <valueId value="spec"/>
          </parameter>
        </dependent>
      </rule>
    </rule>
    <rule>
      <name value="notes"/>
      <source>
        <context value="ew"/>
      </source>
      <target>
        <context value="spec"/>
      </target>
      <dependent>
        <name value="SpectacleLensNotes"/>
        <parameter>
          <valueId value="qr"/>
        </parameter>
        <parameter>
          <valueId value="ew"/>
        </parameter>
        <parameter>
          <valueId value="spec"/>
        </parameter>
      </dependent>
    </rule>
  </group>
  <group>
    <name value="LensPowers"/>
    <documentation value="Sphere, cylinder and axis off one row. Typed guards, because a rule that&#xD;&#xA;assigns an answer of the wrong type to a decimal element does not skip -- it&#xD;&#xA;fails the whole extraction."/>
    <input>
      <name value="row"/>
      <mode value="source"/>
    </input>
    <input>
      <name value="spec"/>
      <mode value="target"/>
    </input>
    <rule>
      <name value="sphere"/>
      <source>
        <context value="row"/>
        <element value="item"/>
        <variable value="it"/>
        <condition value="linkId.endsWith('/sph') and answer.value.ofType(decimal).exists()"/>
      </source>
      <rule>
        <name value="sphereAnswer"/>
        <source>
          <context value="it"/>
          <element value="answer"/>
          <listMode value="first"/>
          <variable value="a"/>
        </source>
        <rule>
          <name value="sphereValue"/>
          <source>
            <context value="a"/>
            <element value="value"/>
            <variable value="v"/>
          </source>
          <target>
            <context value="spec"/>
            <element value="sphere"/>
            <transform value="copy"/>
            <parameter>
              <valueId value="v"/>
            </parameter>
          </target>
        </rule>
      </rule>
    </rule>
    <rule>
      <name value="cylinder"/>
      <source>
        <context value="row"/>
        <element value="item"/>
        <variable value="it"/>
        <condition value="linkId.endsWith('/cyl') and answer.value.ofType(decimal).exists()"/>
      </source>
      <rule>
        <name value="cylinderAnswer"/>
        <source>
          <context value="it"/>
          <element value="answer"/>
          <listMode value="first"/>
          <variable value="a"/>
        </source>
        <rule>
          <name value="cylinderValue"/>
          <source>
            <context value="a"/>
            <element value="value"/>
            <variable value="v"/>
          </source>
          <target>
            <context value="spec"/>
            <element value="cylinder"/>
            <transform value="copy"/>
            <parameter>
              <valueId value="v"/>
            </parameter>
          </target>
        </rule>
      </rule>
    </rule>
    <rule>
      <name value="axis"/>
      <source>
        <context value="row"/>
        <element value="item"/>
        <variable value="it"/>
        <condition value="linkId.endsWith('/axis') and answer.value.ofType(integer).exists()"/>
      </source>
      <rule>
        <name value="axisAnswer"/>
        <source>
          <context value="it"/>
          <element value="answer"/>
          <listMode value="first"/>
          <variable value="a"/>
        </source>
        <rule>
          <name value="axisValue"/>
          <source>
            <context value="a"/>
            <element value="value"/>
            <variable value="v"/>
          </source>
          <target>
            <context value="spec"/>
            <element value="axis"/>
            <transform value="copy"/>
            <parameter>
              <valueId value="v"/>
            </parameter>
          </target>
        </rule>
      </rule>
    </rule>
  </group>
  <group>
    <name value="SpectacleLensNotes"/>
    <documentation value="Lens type and coating, as the words the prescriber ticked. VisionPrescription&#xD;&#xA;has no coded element for either; `note` is where R5 says they go (&quot;notes for&#xD;&#xA;special requirements such as coatings and lens materials&quot;), and the codes&#xD;&#xA;themselves stay on the response. Both go on every spectacle specification,&#xD;&#xA;because the paper ticks them once for the pair."/>
    <input>
      <name value="qr"/>
      <type value="QR"/>
      <mode value="source"/>
    </input>
    <input>
      <name value="ew"/>
      <mode value="source"/>
    </input>
    <input>
      <name value="spec"/>
      <mode value="target"/>
    </input>
    <rule>
      <name value="lensTypes"/>
      <source>
        <context value="ew"/>
        <element value="item"/>
        <variable value="it"/>
        <condition value="(linkId = 'eyewear/lens-types') and answer.value.ofType(Coding).exists()"/>
      </source>
      <target>
        <context value="spec"/>
        <element value="note"/>
        <variable value="note"/>
      </target>
      <rule>
        <name value="lensTypeText"/>
        <source>
          <context value="it"/>
        </source>
        <target>
          <context value="note"/>
          <element value="text"/>
          <transform value="evaluate"/>
          <parameter>
            <valueId value="it"/>
          </parameter>
          <parameter>
            <valueString value="'Lens type: ' &amp; answer.value.ofType(Coding).select(iif(display.exists(), display, code)).join(', ')"/>
          </parameter>
        </target>
      </rule>
    </rule>
    <rule>
      <name value="lensCoating"/>
      <source>
        <context value="ew"/>
        <element value="item"/>
        <variable value="it"/>
        <condition value="(linkId = 'eyewear/lens-coating') and answer.value.ofType(Coding).exists()"/>
      </source>
      <target>
        <context value="spec"/>
        <element value="note"/>
        <variable value="note"/>
      </target>
      <rule>
        <name value="lensCoatingText"/>
        <source>
          <context value="it"/>
        </source>
        <target>
          <context value="note"/>
          <element value="text"/>
          <transform value="evaluate"/>
          <parameter>
            <valueId value="it"/>
          </parameter>
          <parameter>
            <valueString value="'Lens coating: ' &amp; answer.value.ofType(Coding).select(iif(display.exists(), display, code)).join(', ')"/>
          </parameter>
        </target>
      </rule>
    </rule>
    <rule>
      <name value="prescriptionNotes"/>
      <source>
        <context value="qr"/>
      </source>
      <target>
        <context value="spec"/>
      </target>
      <dependent>
        <name value="PrescriptionNotes"/>
        <parameter>
          <valueId value="qr"/>
        </parameter>
        <parameter>
          <valueId value="spec"/>
        </parameter>
      </dependent>
    </rule>
  </group>
  <group>
    <name value="PrescriptionNotes"/>
    <documentation value="The free-text prescription notes. VisionPrescription has no note of its own,&#xD;&#xA;only one per lens specification, so the notes go on each specification --&#xD;&#xA;repeated rather than put on whichever happens to be first, which a consumer&#xD;&#xA;reading one eye would miss."/>
    <input>
      <name value="qr"/>
      <type value="QR"/>
      <mode value="source"/>
    </input>
    <input>
      <name value="spec"/>
      <mode value="target"/>
    </input>
    <rule>
      <name value="notes"/>
      <source>
        <context value="qr"/>
        <element value="item"/>
        <variable value="it"/>
        <condition value="(linkId = 'notes') and answer.value.ofType(string).exists()"/>
      </source>
      <target>
        <context value="spec"/>
        <element value="note"/>
        <variable value="note"/>
      </target>
      <rule>
        <name value="noteAnswer"/>
        <source>
          <context value="it"/>
          <element value="answer"/>
          <listMode value="first"/>
          <variable value="a"/>
        </source>
        <rule>
          <name value="noteText"/>
          <source>
            <context value="a"/>
            <element value="value"/>
            <variable value="v"/>
          </source>
          <target>
            <context value="note"/>
            <element value="text"/>
            <transform value="copy"/>
            <parameter>
              <valueId value="v"/>
            </parameter>
          </target>
        </rule>
      </rule>
    </rule>
  </group>
  <group>
    <name value="ContactLenses"/>
    <documentation value="── Contact lenses ───────────────────────────────────────────────────────────&#xD;&#xA;One specification per eye with anything written on it. The contact lens&#xD;&#xA;power goes to `power`, R5's &quot;contact lens power&quot;, which is what its own&#xD;&#xA;contact lens example uses; `sphere` stays the spectacle lens power. Brand,&#xD;&#xA;colour and the replacement schedule are asked once for the pair and copied&#xD;&#xA;onto both eyes, because R5 holds them per specification."/>
    <input>
      <name value="qr"/>
      <type value="QR"/>
      <mode value="source"/>
    </input>
    <input>
      <name value="cl"/>
      <mode value="source"/>
    </input>
    <input>
      <name value="vp"/>
      <type value="VisionPrescription"/>
      <mode value="target"/>
    </input>
    <rule>
      <name value="right"/>
      <source>
        <context value="cl"/>
        <element value="item"/>
        <variable value="eye"/>
        <condition value="(linkId = 'contact-lens/right') and (item.answer.value.ofType(decimal).exists() or item.answer.value.ofType(integer).exists())"/>
      </source>
      <target>
        <context value="vp"/>
        <element value="lensSpecification"/>
        <variable value="spec"/>
      </target>
      <rule>
        <name value="lens"/>
        <source>
          <context value="eye"/>
        </source>
        <target>
          <context value="spec"/>
        </target>
        <dependent>
          <name value="ContactLens"/>
          <parameter>
            <valueId value="qr"/>
          </parameter>
          <parameter>
            <valueId value="cl"/>
          </parameter>
          <parameter>
            <valueId value="eye"/>
          </parameter>
          <parameter>
            <valueId value="spec"/>
          </parameter>
        </dependent>
      </rule>
      <rule>
        <name value="eyeCode"/>
        <source>
          <context value="eye"/>
        </source>
        <target>
          <context value="spec"/>
          <element value="eye"/>
          <transform value="copy"/>
          <parameter>
            <valueString value="right"/>
          </parameter>
        </target>
      </rule>
    </rule>
    <rule>
      <name value="left"/>
      <source>
        <context value="cl"/>
        <element value="item"/>
        <variable value="eye"/>
        <condition value="(linkId = 'contact-lens/left') and (item.answer.value.ofType(decimal).exists() or item.answer.value.ofType(integer).exists())"/>
      </source>
      <target>
        <context value="vp"/>
        <element value="lensSpecification"/>
        <variable value="spec"/>
      </target>
      <rule>
        <name value="lens"/>
        <source>
          <context value="eye"/>
        </source>
        <target>
          <context value="spec"/>
        </target>
        <dependent>
          <name value="ContactLens"/>
          <parameter>
            <valueId value="qr"/>
          </parameter>
          <parameter>
            <valueId value="cl"/>
          </parameter>
          <parameter>
            <valueId value="eye"/>
          </parameter>
          <parameter>
            <valueId value="spec"/>
          </parameter>
        </dependent>
      </rule>
      <rule>
        <name value="eyeCode"/>
        <source>
          <context value="eye"/>
        </source>
        <target>
          <context value="spec"/>
          <element value="eye"/>
          <transform value="copy"/>
          <parameter>
            <valueString value="left"/>
          </parameter>
        </target>
      </rule>
    </rule>
  </group>
  <group>
    <name value="ContactLens"/>
    <input>
      <name value="qr"/>
      <type value="QR"/>
      <mode value="source"/>
    </input>
    <input>
      <name value="cl"/>
      <mode value="source"/>
    </input>
    <input>
      <name value="eye"/>
      <mode value="source"/>
    </input>
    <input>
      <name value="spec"/>
      <mode value="target"/>
    </input>
    <rule>
      <name value="product"/>
      <source>
        <context value="eye"/>
      </source>
      <target>
        <context value="spec"/>
        <element value="product"/>
        <variable value="product"/>
      </target>
      <rule>
        <name value="hl7Coding"/>
        <source>
          <context value="eye"/>
        </source>
        <target>
          <context value="product"/>
          <element value="coding"/>
          <variable value="coding"/>
        </target>
        <rule>
          <name value="hl7System"/>
          <source>
            <context value="eye"/>
          </source>
          <target>
            <context value="coding"/>
            <element value="system"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="http://terminology.hl7.org/CodeSystem/ex-visionprescriptionproduct"/>
            </parameter>
          </target>
        </rule>
        <rule>
          <name value="hl7Code"/>
          <source>
            <context value="eye"/>
          </source>
          <target>
            <context value="coding"/>
            <element value="code"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="contact"/>
            </parameter>
          </target>
        </rule>
        <rule>
          <name value="hl7Display"/>
          <source>
            <context value="eye"/>
          </source>
          <target>
            <context value="coding"/>
            <element value="display"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="Contact Lens"/>
            </parameter>
          </target>
        </rule>
      </rule>
      <rule>
        <name value="productText"/>
        <source>
          <context value="eye"/>
        </source>
        <target>
          <context value="product"/>
          <element value="text"/>
          <transform value="copy"/>
          <parameter>
            <valueString value="Contact lens"/>
          </parameter>
        </target>
      </rule>
    </rule>
    <rule>
      <name value="power"/>
      <source>
        <context value="eye"/>
        <element value="item"/>
        <variable value="it"/>
        <condition value="linkId.endsWith('/power') and answer.value.ofType(decimal).exists()"/>
      </source>
      <rule>
        <name value="powerAnswer"/>
        <source>
          <context value="it"/>
          <element value="answer"/>
          <listMode value="first"/>
          <variable value="a"/>
        </source>
        <rule>
          <name value="powerValue"/>
          <source>
            <context value="a"/>
            <element value="value"/>
            <variable value="v"/>
          </source>
          <target>
            <context value="spec"/>
            <element value="power"/>
            <transform value="copy"/>
            <parameter>
              <valueId value="v"/>
            </parameter>
          </target>
        </rule>
      </rule>
    </rule>
    <rule>
      <name value="cylinder"/>
      <source>
        <context value="eye"/>
        <element value="item"/>
        <variable value="it"/>
        <condition value="linkId.endsWith('/cyl') and answer.value.ofType(decimal).exists()"/>
      </source>
      <rule>
        <name value="cylinderAnswer"/>
        <source>
          <context value="it"/>
          <element value="answer"/>
          <listMode value="first"/>
          <variable value="a"/>
        </source>
        <rule>
          <name value="cylinderValue"/>
          <source>
            <context value="a"/>
            <element value="value"/>
            <variable value="v"/>
          </source>
          <target>
            <context value="spec"/>
            <element value="cylinder"/>
            <transform value="copy"/>
            <parameter>
              <valueId value="v"/>
            </parameter>
          </target>
        </rule>
      </rule>
    </rule>
    <rule>
      <name value="axis"/>
      <source>
        <context value="eye"/>
        <element value="item"/>
        <variable value="it"/>
        <condition value="linkId.endsWith('/axis') and answer.value.ofType(integer).exists()"/>
      </source>
      <rule>
        <name value="axisAnswer"/>
        <source>
          <context value="it"/>
          <element value="answer"/>
          <listMode value="first"/>
          <variable value="a"/>
        </source>
        <rule>
          <name value="axisValue"/>
          <source>
            <context value="a"/>
            <element value="value"/>
            <variable value="v"/>
          </source>
          <target>
            <context value="spec"/>
            <element value="axis"/>
            <transform value="copy"/>
            <parameter>
              <valueId value="v"/>
            </parameter>
          </target>
        </rule>
      </rule>
    </rule>
    <rule>
      <name value="addition"/>
      <source>
        <context value="eye"/>
        <element value="item"/>
        <variable value="it"/>
        <condition value="linkId.endsWith('/add') and answer.value.ofType(decimal).exists()"/>
      </source>
      <rule>
        <name value="addAnswer"/>
        <source>
          <context value="it"/>
          <element value="answer"/>
          <listMode value="first"/>
          <variable value="a"/>
        </source>
        <rule>
          <name value="addValue"/>
          <source>
            <context value="a"/>
            <element value="value"/>
            <variable value="v"/>
          </source>
          <target>
            <context value="spec"/>
            <element value="add"/>
            <transform value="copy"/>
            <parameter>
              <valueId value="v"/>
            </parameter>
          </target>
        </rule>
      </rule>
    </rule>
    <rule>
      <name value="backCurve"/>
      <source>
        <context value="eye"/>
        <element value="item"/>
        <variable value="it"/>
        <condition value="linkId.endsWith('/base-curve') and answer.value.ofType(decimal).exists()"/>
      </source>
      <rule>
        <name value="backCurveAnswer"/>
        <source>
          <context value="it"/>
          <element value="answer"/>
          <listMode value="first"/>
          <variable value="a"/>
        </source>
        <rule>
          <name value="backCurveValue"/>
          <source>
            <context value="a"/>
            <element value="value"/>
            <variable value="v"/>
          </source>
          <target>
            <context value="spec"/>
            <element value="backCurve"/>
            <transform value="copy"/>
            <parameter>
              <valueId value="v"/>
            </parameter>
          </target>
        </rule>
      </rule>
    </rule>
    <rule>
      <name value="diameter"/>
      <source>
        <context value="eye"/>
        <element value="item"/>
        <variable value="it"/>
        <condition value="linkId.endsWith('/diameter') and answer.value.ofType(decimal).exists()"/>
      </source>
      <rule>
        <name value="diameterAnswer"/>
        <source>
          <context value="it"/>
          <element value="answer"/>
          <listMode value="first"/>
          <variable value="a"/>
        </source>
        <rule>
          <name value="diameterValue"/>
          <source>
            <context value="a"/>
            <element value="value"/>
            <variable value="v"/>
          </source>
          <target>
            <context value="spec"/>
            <element value="diameter"/>
            <transform value="copy"/>
            <parameter>
              <valueId value="v"/>
            </parameter>
          </target>
        </rule>
      </rule>
    </rule>
    <rule>
      <name value="brand"/>
      <source>
        <context value="cl"/>
        <element value="item"/>
        <variable value="it"/>
        <condition value="(linkId = 'contact-lens/brand') and answer.value.ofType(string).exists()"/>
      </source>
      <rule>
        <name value="brandAnswer"/>
        <source>
          <context value="it"/>
          <element value="answer"/>
          <listMode value="first"/>
          <variable value="a"/>
        </source>
        <rule>
          <name value="brandValue"/>
          <source>
            <context value="a"/>
            <element value="value"/>
            <variable value="v"/>
          </source>
          <target>
            <context value="spec"/>
            <element value="brand"/>
            <transform value="copy"/>
            <parameter>
              <valueId value="v"/>
            </parameter>
          </target>
        </rule>
      </rule>
    </rule>
    <rule>
      <name value="colour"/>
      <source>
        <context value="cl"/>
        <element value="item"/>
        <variable value="it"/>
        <condition value="(linkId = 'contact-lens/colour') and answer.value.ofType(string).exists()"/>
      </source>
      <rule>
        <name value="colourAnswer"/>
        <source>
          <context value="it"/>
          <element value="answer"/>
          <listMode value="first"/>
          <variable value="a"/>
        </source>
        <rule>
          <name value="colourValue"/>
          <source>
            <context value="a"/>
            <element value="value"/>
            <variable value="v"/>
          </source>
          <target>
            <context value="spec"/>
            <element value="color"/>
            <transform value="copy"/>
            <parameter>
              <valueId value="v"/>
            </parameter>
          </target>
        </rule>
      </rule>
    </rule>
    <rule>
      <name value="daily"/>
      <source>
        <context value="cl"/>
        <element value="item"/>
        <variable value="it"/>
        <condition value="(linkId = 'contact-lens/replacement') and (answer.value.ofType(Coding).code = 'contact-lens-replace-daily')"/>
      </source>
      <target>
        <context value="spec"/>
        <element value="duration"/>
        <variable value="d"/>
      </target>
      <rule>
        <name value="value"/>
        <source>
          <context value="it"/>
        </source>
        <target>
          <context value="d"/>
          <element value="value"/>
          <transform value="copy"/>
          <parameter>
            <valueInteger value="1"/>
          </parameter>
        </target>
      </rule>
      <rule>
        <name value="unit"/>
        <source>
          <context value="it"/>
        </source>
        <target>
          <context value="d"/>
          <element value="unit"/>
          <transform value="copy"/>
          <parameter>
            <valueString value="day"/>
          </parameter>
        </target>
      </rule>
      <rule>
        <name value="system"/>
        <source>
          <context value="it"/>
        </source>
        <target>
          <context value="d"/>
          <element value="system"/>
          <transform value="copy"/>
          <parameter>
            <valueString value="http://unitsofmeasure.org"/>
          </parameter>
        </target>
      </rule>
      <rule>
        <name value="code"/>
        <source>
          <context value="it"/>
        </source>
        <target>
          <context value="d"/>
          <element value="code"/>
          <transform value="copy"/>
          <parameter>
            <valueString value="d"/>
          </parameter>
        </target>
      </rule>
      <documentation value="The replacement schedule is R5's `duration`, &quot;the recommended maximum wear&#xD;&#xA;period for the lens&quot;, as a UCUM Quantity. One rule per answer, each guarded&#xD;&#xA;on its own code, so an answer this map does not know writes no duration&#xD;&#xA;rather than an empty Quantity."/>
    </rule>
    <rule>
      <name value="twoWeekly"/>
      <source>
        <context value="cl"/>
        <element value="item"/>
        <variable value="it"/>
        <condition value="(linkId = 'contact-lens/replacement') and (answer.value.ofType(Coding).code = 'contact-lens-replace-two-weekly')"/>
      </source>
      <target>
        <context value="spec"/>
        <element value="duration"/>
        <variable value="d"/>
      </target>
      <rule>
        <name value="value"/>
        <source>
          <context value="it"/>
        </source>
        <target>
          <context value="d"/>
          <element value="value"/>
          <transform value="copy"/>
          <parameter>
            <valueInteger value="2"/>
          </parameter>
        </target>
      </rule>
      <rule>
        <name value="unit"/>
        <source>
          <context value="it"/>
        </source>
        <target>
          <context value="d"/>
          <element value="unit"/>
          <transform value="copy"/>
          <parameter>
            <valueString value="weeks"/>
          </parameter>
        </target>
      </rule>
      <rule>
        <name value="system"/>
        <source>
          <context value="it"/>
        </source>
        <target>
          <context value="d"/>
          <element value="system"/>
          <transform value="copy"/>
          <parameter>
            <valueString value="http://unitsofmeasure.org"/>
          </parameter>
        </target>
      </rule>
      <rule>
        <name value="code"/>
        <source>
          <context value="it"/>
        </source>
        <target>
          <context value="d"/>
          <element value="code"/>
          <transform value="copy"/>
          <parameter>
            <valueString value="wk"/>
          </parameter>
        </target>
      </rule>
    </rule>
    <rule>
      <name value="monthly"/>
      <source>
        <context value="cl"/>
        <element value="item"/>
        <variable value="it"/>
        <condition value="(linkId = 'contact-lens/replacement') and (answer.value.ofType(Coding).code = 'contact-lens-replace-monthly')"/>
      </source>
      <target>
        <context value="spec"/>
        <element value="duration"/>
        <variable value="d"/>
      </target>
      <rule>
        <name value="value"/>
        <source>
          <context value="it"/>
        </source>
        <target>
          <context value="d"/>
          <element value="value"/>
          <transform value="copy"/>
          <parameter>
            <valueInteger value="1"/>
          </parameter>
        </target>
      </rule>
      <rule>
        <name value="unit"/>
        <source>
          <context value="it"/>
        </source>
        <target>
          <context value="d"/>
          <element value="unit"/>
          <transform value="copy"/>
          <parameter>
            <valueString value="month"/>
          </parameter>
        </target>
      </rule>
      <rule>
        <name value="system"/>
        <source>
          <context value="it"/>
        </source>
        <target>
          <context value="d"/>
          <element value="system"/>
          <transform value="copy"/>
          <parameter>
            <valueString value="http://unitsofmeasure.org"/>
          </parameter>
        </target>
      </rule>
      <rule>
        <name value="code"/>
        <source>
          <context value="it"/>
        </source>
        <target>
          <context value="d"/>
          <element value="code"/>
          <transform value="copy"/>
          <parameter>
            <valueString value="mo"/>
          </parameter>
        </target>
      </rule>
    </rule>
    <rule>
      <name value="quarterly"/>
      <source>
        <context value="cl"/>
        <element value="item"/>
        <variable value="it"/>
        <condition value="(linkId = 'contact-lens/replacement') and (answer.value.ofType(Coding).code = 'contact-lens-replace-quarterly')"/>
      </source>
      <target>
        <context value="spec"/>
        <element value="duration"/>
        <variable value="d"/>
      </target>
      <rule>
        <name value="value"/>
        <source>
          <context value="it"/>
        </source>
        <target>
          <context value="d"/>
          <element value="value"/>
          <transform value="copy"/>
          <parameter>
            <valueInteger value="3"/>
          </parameter>
        </target>
      </rule>
      <rule>
        <name value="unit"/>
        <source>
          <context value="it"/>
        </source>
        <target>
          <context value="d"/>
          <element value="unit"/>
          <transform value="copy"/>
          <parameter>
            <valueString value="months"/>
          </parameter>
        </target>
      </rule>
      <rule>
        <name value="system"/>
        <source>
          <context value="it"/>
        </source>
        <target>
          <context value="d"/>
          <element value="system"/>
          <transform value="copy"/>
          <parameter>
            <valueString value="http://unitsofmeasure.org"/>
          </parameter>
        </target>
      </rule>
      <rule>
        <name value="code"/>
        <source>
          <context value="it"/>
        </source>
        <target>
          <context value="d"/>
          <element value="code"/>
          <transform value="copy"/>
          <parameter>
            <valueString value="mo"/>
          </parameter>
        </target>
      </rule>
    </rule>
    <rule>
      <name value="yearly"/>
      <source>
        <context value="cl"/>
        <element value="item"/>
        <variable value="it"/>
        <condition value="(linkId = 'contact-lens/replacement') and (answer.value.ofType(Coding).code = 'contact-lens-replace-yearly')"/>
      </source>
      <target>
        <context value="spec"/>
        <element value="duration"/>
        <variable value="d"/>
      </target>
      <rule>
        <name value="value"/>
        <source>
          <context value="it"/>
        </source>
        <target>
          <context value="d"/>
          <element value="value"/>
          <transform value="copy"/>
          <parameter>
            <valueInteger value="1"/>
          </parameter>
        </target>
      </rule>
      <rule>
        <name value="unit"/>
        <source>
          <context value="it"/>
        </source>
        <target>
          <context value="d"/>
          <element value="unit"/>
          <transform value="copy"/>
          <parameter>
            <valueString value="year"/>
          </parameter>
        </target>
      </rule>
      <rule>
        <name value="system"/>
        <source>
          <context value="it"/>
        </source>
        <target>
          <context value="d"/>
          <element value="system"/>
          <transform value="copy"/>
          <parameter>
            <valueString value="http://unitsofmeasure.org"/>
          </parameter>
        </target>
      </rule>
      <rule>
        <name value="code"/>
        <source>
          <context value="it"/>
        </source>
        <target>
          <context value="d"/>
          <element value="code"/>
          <transform value="copy"/>
          <parameter>
            <valueString value="a"/>
          </parameter>
        </target>
      </rule>
    </rule>
    <rule>
      <name value="prescriptionNotes"/>
      <source>
        <context value="qr"/>
      </source>
      <target>
        <context value="spec"/>
      </target>
      <dependent>
        <name value="PrescriptionNotes"/>
        <parameter>
          <valueId value="qr"/>
        </parameter>
        <parameter>
          <valueId value="spec"/>
        </parameter>
      </dependent>
    </rule>
  </group>
  <group>
    <name value="EyewearMeasurements"/>
    <documentation value="─────────────────────────────────────────────────────────────────────────────&#xD;&#xA;Pupillary distance and visual acuity&#xD;&#xA;Neither has a home in VisionPrescription, and LOINC names both exactly, per&#xD;&#xA;eye, on a glasses lens prescription -- so each is its own Observation under&#xD;&#xA;its own code rather than prose in a note. The codes were looked up, not&#xD;&#xA;assumed; each display below is LOINC's own:&#xD;&#xA;distance (DV)            near (NV)&#xD;&#xA;R-PD       28840-7                  28841-5       mm&#xD;&#xA;L-PD       28857-1                  55985-6       mm&#xD;&#xA;R-VA       28831-6                  28839-9       as written&#xD;&#xA;L-VA       28847-2                  28856-3       as written&#xD;&#xA;The PD without a qualifier is LOINC's distance PD; near is the one it&#xD;&#xA;qualifies. Visual acuity is kept as the string the prescriber wrote -- 6/6,&#xD;&#xA;6/9, N6, CF -- because the notations mix Snellen fractions, N-point print&#xD;&#xA;sizes and counting fingers, and forcing them into a Ratio would lose the ones&#xD;&#xA;that are not fractions.&#xD;&#xA;Nothing on VisionPrescription can point at these. They are tied to it by the&#xD;&#xA;encounter, by `derivedFrom` naming the same response, and by the same&#xD;&#xA;provenance tag.&#xD;&#xA;─────────────────────────────────────────────────────────────────────────────"/>
    <input>
      <name value="qr"/>
      <type value="QR"/>
      <mode value="source"/>
    </input>
    <input>
      <name value="ew"/>
      <mode value="source"/>
    </input>
    <input>
      <name value="bundle"/>
      <type value="Bundle"/>
      <mode value="target"/>
    </input>
    <rule>
      <name value="right"/>
      <source>
        <context value="ew"/>
        <element value="item"/>
        <variable value="eye"/>
        <condition value="linkId = 'eyewear/right'"/>
      </source>
      <rule>
        <name value="rightDistance"/>
        <source>
          <context value="eye"/>
          <element value="item"/>
          <variable value="row"/>
          <condition value="linkId = 'eyewear/right/distance'"/>
        </source>
        <rule>
          <name value="rightDistancePd"/>
          <source>
            <context value="row"/>
            <element value="item"/>
            <variable value="it"/>
            <condition value="(linkId = 'eyewear/right/distance/pd') and answer.value.ofType(decimal).exists()"/>
          </source>
          <target>
            <context value="bundle"/>
            <element value="entry"/>
            <variable value="entry"/>
          </target>
          <target>
            <context value="entry"/>
            <element value="resource"/>
            <variable value="obs"/>
            <transform value="create"/>
            <parameter>
              <valueString value="Observation"/>
            </parameter>
          </target>
          <rule>
            <name value="base"/>
            <source>
              <context value="qr"/>
            </source>
            <target>
              <context value="obs"/>
            </target>
            <target>
              <context value="entry"/>
            </target>
            <dependent>
              <name value="BuildObservationBase"/>
              <parameter>
                <valueId value="qr"/>
              </parameter>
              <parameter>
                <valueId value="obs"/>
              </parameter>
              <parameter>
                <valueId value="entry"/>
              </parameter>
            </dependent>
          </rule>
          <rule>
            <name value="code"/>
            <source>
              <context value="it"/>
            </source>
            <target>
              <context value="obs"/>
              <element value="code"/>
              <variable value="code"/>
            </target>
            <rule>
              <name value="coding"/>
              <source>
                <context value="it"/>
              </source>
              <target>
                <context value="code"/>
                <element value="coding"/>
                <variable value="coding"/>
              </target>
              <rule>
                <name value="system"/>
                <source>
                  <context value="it"/>
                </source>
                <target>
                  <context value="coding"/>
                  <element value="system"/>
                  <transform value="copy"/>
                  <parameter>
                    <valueString value="http://loinc.org"/>
                  </parameter>
                </target>
              </rule>
              <rule>
                <name value="code"/>
                <source>
                  <context value="it"/>
                </source>
                <target>
                  <context value="coding"/>
                  <element value="code"/>
                  <transform value="copy"/>
                  <parameter>
                    <valueString value="28840-7"/>
                  </parameter>
                </target>
              </rule>
              <rule>
                <name value="display"/>
                <source>
                  <context value="it"/>
                </source>
                <target>
                  <context value="coding"/>
                  <element value="display"/>
                  <transform value="copy"/>
                  <parameter>
                    <valueString value="Right glasses lens prescription Pupillary distance.monocular"/>
                  </parameter>
                </target>
              </rule>
            </rule>
            <rule>
              <name value="text"/>
              <source>
                <context value="it"/>
              </source>
              <target>
                <context value="code"/>
                <element value="text"/>
                <transform value="copy"/>
                <parameter>
                  <valueString value="Right eye pupillary distance, distance vision (R-PD, DV)"/>
                </parameter>
              </target>
            </rule>
          </rule>
          <rule>
            <name value="value"/>
            <source>
              <context value="it"/>
            </source>
            <target>
              <context value="obs"/>
            </target>
            <dependent>
              <name value="SetMillimetres"/>
              <parameter>
                <valueId value="it"/>
              </parameter>
              <parameter>
                <valueId value="obs"/>
              </parameter>
            </dependent>
          </rule>
        </rule>
        <rule>
          <name value="rightDistanceVa"/>
          <source>
            <context value="row"/>
            <element value="item"/>
            <variable value="it"/>
            <condition value="(linkId = 'eyewear/right/distance/va') and answer.value.ofType(string).exists()"/>
          </source>
          <target>
            <context value="bundle"/>
            <element value="entry"/>
            <variable value="entry"/>
          </target>
          <target>
            <context value="entry"/>
            <element value="resource"/>
            <variable value="obs"/>
            <transform value="create"/>
            <parameter>
              <valueString value="Observation"/>
            </parameter>
          </target>
          <rule>
            <name value="base"/>
            <source>
              <context value="qr"/>
            </source>
            <target>
              <context value="obs"/>
            </target>
            <target>
              <context value="entry"/>
            </target>
            <dependent>
              <name value="BuildObservationBase"/>
              <parameter>
                <valueId value="qr"/>
              </parameter>
              <parameter>
                <valueId value="obs"/>
              </parameter>
              <parameter>
                <valueId value="entry"/>
              </parameter>
            </dependent>
          </rule>
          <rule>
            <name value="code"/>
            <source>
              <context value="it"/>
            </source>
            <target>
              <context value="obs"/>
              <element value="code"/>
              <variable value="code"/>
            </target>
            <rule>
              <name value="coding"/>
              <source>
                <context value="it"/>
              </source>
              <target>
                <context value="code"/>
                <element value="coding"/>
                <variable value="coding"/>
              </target>
              <rule>
                <name value="system"/>
                <source>
                  <context value="it"/>
                </source>
                <target>
                  <context value="coding"/>
                  <element value="system"/>
                  <transform value="copy"/>
                  <parameter>
                    <valueString value="http://loinc.org"/>
                  </parameter>
                </target>
              </rule>
              <rule>
                <name value="code"/>
                <source>
                  <context value="it"/>
                </source>
                <target>
                  <context value="coding"/>
                  <element value="code"/>
                  <transform value="copy"/>
                  <parameter>
                    <valueString value="28831-6"/>
                  </parameter>
                </target>
              </rule>
              <rule>
                <name value="display"/>
                <source>
                  <context value="it"/>
                </source>
                <target>
                  <context value="coding"/>
                  <element value="display"/>
                  <transform value="copy"/>
                  <parameter>
                    <valueString value="Right glasses lens prescription Visual acuity far"/>
                  </parameter>
                </target>
              </rule>
            </rule>
            <rule>
              <name value="text"/>
              <source>
                <context value="it"/>
              </source>
              <target>
                <context value="code"/>
                <element value="text"/>
                <transform value="copy"/>
                <parameter>
                  <valueString value="Right eye visual acuity, distance vision (R-VA, DV)"/>
                </parameter>
              </target>
            </rule>
          </rule>
          <rule>
            <name value="value"/>
            <source>
              <context value="it"/>
            </source>
            <target>
              <context value="obs"/>
            </target>
            <dependent>
              <name value="SetAcuity"/>
              <parameter>
                <valueId value="it"/>
              </parameter>
              <parameter>
                <valueId value="obs"/>
              </parameter>
            </dependent>
          </rule>
        </rule>
      </rule>
      <rule>
        <name value="rightNear"/>
        <source>
          <context value="eye"/>
          <element value="item"/>
          <variable value="row"/>
          <condition value="linkId = 'eyewear/right/near'"/>
        </source>
        <rule>
          <name value="rightNearPd"/>
          <source>
            <context value="row"/>
            <element value="item"/>
            <variable value="it"/>
            <condition value="(linkId = 'eyewear/right/near/pd') and answer.value.ofType(decimal).exists()"/>
          </source>
          <target>
            <context value="bundle"/>
            <element value="entry"/>
            <variable value="entry"/>
          </target>
          <target>
            <context value="entry"/>
            <element value="resource"/>
            <variable value="obs"/>
            <transform value="create"/>
            <parameter>
              <valueString value="Observation"/>
            </parameter>
          </target>
          <rule>
            <name value="base"/>
            <source>
              <context value="qr"/>
            </source>
            <target>
              <context value="obs"/>
            </target>
            <target>
              <context value="entry"/>
            </target>
            <dependent>
              <name value="BuildObservationBase"/>
              <parameter>
                <valueId value="qr"/>
              </parameter>
              <parameter>
                <valueId value="obs"/>
              </parameter>
              <parameter>
                <valueId value="entry"/>
              </parameter>
            </dependent>
          </rule>
          <rule>
            <name value="code"/>
            <source>
              <context value="it"/>
            </source>
            <target>
              <context value="obs"/>
              <element value="code"/>
              <variable value="code"/>
            </target>
            <rule>
              <name value="coding"/>
              <source>
                <context value="it"/>
              </source>
              <target>
                <context value="code"/>
                <element value="coding"/>
                <variable value="coding"/>
              </target>
              <rule>
                <name value="system"/>
                <source>
                  <context value="it"/>
                </source>
                <target>
                  <context value="coding"/>
                  <element value="system"/>
                  <transform value="copy"/>
                  <parameter>
                    <valueString value="http://loinc.org"/>
                  </parameter>
                </target>
              </rule>
              <rule>
                <name value="code"/>
                <source>
                  <context value="it"/>
                </source>
                <target>
                  <context value="coding"/>
                  <element value="code"/>
                  <transform value="copy"/>
                  <parameter>
                    <valueString value="28841-5"/>
                  </parameter>
                </target>
              </rule>
              <rule>
                <name value="display"/>
                <source>
                  <context value="it"/>
                </source>
                <target>
                  <context value="coding"/>
                  <element value="display"/>
                  <transform value="copy"/>
                  <parameter>
                    <valueString value="Right glasses lens prescription Pupillary distance.monocular near"/>
                  </parameter>
                </target>
              </rule>
            </rule>
            <rule>
              <name value="text"/>
              <source>
                <context value="it"/>
              </source>
              <target>
                <context value="code"/>
                <element value="text"/>
                <transform value="copy"/>
                <parameter>
                  <valueString value="Right eye pupillary distance, near vision (R-PD, NV)"/>
                </parameter>
              </target>
            </rule>
          </rule>
          <rule>
            <name value="value"/>
            <source>
              <context value="it"/>
            </source>
            <target>
              <context value="obs"/>
            </target>
            <dependent>
              <name value="SetMillimetres"/>
              <parameter>
                <valueId value="it"/>
              </parameter>
              <parameter>
                <valueId value="obs"/>
              </parameter>
            </dependent>
          </rule>
        </rule>
        <rule>
          <name value="rightNearVa"/>
          <source>
            <context value="row"/>
            <element value="item"/>
            <variable value="it"/>
            <condition value="(linkId = 'eyewear/right/near/va') and answer.value.ofType(string).exists()"/>
          </source>
          <target>
            <context value="bundle"/>
            <element value="entry"/>
            <variable value="entry"/>
          </target>
          <target>
            <context value="entry"/>
            <element value="resource"/>
            <variable value="obs"/>
            <transform value="create"/>
            <parameter>
              <valueString value="Observation"/>
            </parameter>
          </target>
          <rule>
            <name value="base"/>
            <source>
              <context value="qr"/>
            </source>
            <target>
              <context value="obs"/>
            </target>
            <target>
              <context value="entry"/>
            </target>
            <dependent>
              <name value="BuildObservationBase"/>
              <parameter>
                <valueId value="qr"/>
              </parameter>
              <parameter>
                <valueId value="obs"/>
              </parameter>
              <parameter>
                <valueId value="entry"/>
              </parameter>
            </dependent>
          </rule>
          <rule>
            <name value="code"/>
            <source>
              <context value="it"/>
            </source>
            <target>
              <context value="obs"/>
              <element value="code"/>
              <variable value="code"/>
            </target>
            <rule>
              <name value="coding"/>
              <source>
                <context value="it"/>
              </source>
              <target>
                <context value="code"/>
                <element value="coding"/>
                <variable value="coding"/>
              </target>
              <rule>
                <name value="system"/>
                <source>
                  <context value="it"/>
                </source>
                <target>
                  <context value="coding"/>
                  <element value="system"/>
                  <transform value="copy"/>
                  <parameter>
                    <valueString value="http://loinc.org"/>
                  </parameter>
                </target>
              </rule>
              <rule>
                <name value="code"/>
                <source>
                  <context value="it"/>
                </source>
                <target>
                  <context value="coding"/>
                  <element value="code"/>
                  <transform value="copy"/>
                  <parameter>
                    <valueString value="28839-9"/>
                  </parameter>
                </target>
              </rule>
              <rule>
                <name value="display"/>
                <source>
                  <context value="it"/>
                </source>
                <target>
                  <context value="coding"/>
                  <element value="display"/>
                  <transform value="copy"/>
                  <parameter>
                    <valueString value="Right glasses lens prescription Visual acuity near"/>
                  </parameter>
                </target>
              </rule>
            </rule>
            <rule>
              <name value="text"/>
              <source>
                <context value="it"/>
              </source>
              <target>
                <context value="code"/>
                <element value="text"/>
                <transform value="copy"/>
                <parameter>
                  <valueString value="Right eye visual acuity, near vision (R-VA, NV)"/>
                </parameter>
              </target>
            </rule>
          </rule>
          <rule>
            <name value="value"/>
            <source>
              <context value="it"/>
            </source>
            <target>
              <context value="obs"/>
            </target>
            <dependent>
              <name value="SetAcuity"/>
              <parameter>
                <valueId value="it"/>
              </parameter>
              <parameter>
                <valueId value="obs"/>
              </parameter>
            </dependent>
          </rule>
        </rule>
      </rule>
    </rule>
    <rule>
      <name value="left"/>
      <source>
        <context value="ew"/>
        <element value="item"/>
        <variable value="eye"/>
        <condition value="linkId = 'eyewear/left'"/>
      </source>
      <rule>
        <name value="leftDistance"/>
        <source>
          <context value="eye"/>
          <element value="item"/>
          <variable value="row"/>
          <condition value="linkId = 'eyewear/left/distance'"/>
        </source>
        <rule>
          <name value="leftDistancePd"/>
          <source>
            <context value="row"/>
            <element value="item"/>
            <variable value="it"/>
            <condition value="(linkId = 'eyewear/left/distance/pd') and answer.value.ofType(decimal).exists()"/>
          </source>
          <target>
            <context value="bundle"/>
            <element value="entry"/>
            <variable value="entry"/>
          </target>
          <target>
            <context value="entry"/>
            <element value="resource"/>
            <variable value="obs"/>
            <transform value="create"/>
            <parameter>
              <valueString value="Observation"/>
            </parameter>
          </target>
          <rule>
            <name value="base"/>
            <source>
              <context value="qr"/>
            </source>
            <target>
              <context value="obs"/>
            </target>
            <target>
              <context value="entry"/>
            </target>
            <dependent>
              <name value="BuildObservationBase"/>
              <parameter>
                <valueId value="qr"/>
              </parameter>
              <parameter>
                <valueId value="obs"/>
              </parameter>
              <parameter>
                <valueId value="entry"/>
              </parameter>
            </dependent>
          </rule>
          <rule>
            <name value="code"/>
            <source>
              <context value="it"/>
            </source>
            <target>
              <context value="obs"/>
              <element value="code"/>
              <variable value="code"/>
            </target>
            <rule>
              <name value="coding"/>
              <source>
                <context value="it"/>
              </source>
              <target>
                <context value="code"/>
                <element value="coding"/>
                <variable value="coding"/>
              </target>
              <rule>
                <name value="system"/>
                <source>
                  <context value="it"/>
                </source>
                <target>
                  <context value="coding"/>
                  <element value="system"/>
                  <transform value="copy"/>
                  <parameter>
                    <valueString value="http://loinc.org"/>
                  </parameter>
                </target>
              </rule>
              <rule>
                <name value="code"/>
                <source>
                  <context value="it"/>
                </source>
                <target>
                  <context value="coding"/>
                  <element value="code"/>
                  <transform value="copy"/>
                  <parameter>
                    <valueString value="28857-1"/>
                  </parameter>
                </target>
              </rule>
              <rule>
                <name value="display"/>
                <source>
                  <context value="it"/>
                </source>
                <target>
                  <context value="coding"/>
                  <element value="display"/>
                  <transform value="copy"/>
                  <parameter>
                    <valueString value="Left glasses lens prescription Pupillary distance.monocular"/>
                  </parameter>
                </target>
              </rule>
            </rule>
            <rule>
              <name value="text"/>
              <source>
                <context value="it"/>
              </source>
              <target>
                <context value="code"/>
                <element value="text"/>
                <transform value="copy"/>
                <parameter>
                  <valueString value="Left eye pupillary distance, distance vision (L-PD, DV)"/>
                </parameter>
              </target>
            </rule>
          </rule>
          <rule>
            <name value="value"/>
            <source>
              <context value="it"/>
            </source>
            <target>
              <context value="obs"/>
            </target>
            <dependent>
              <name value="SetMillimetres"/>
              <parameter>
                <valueId value="it"/>
              </parameter>
              <parameter>
                <valueId value="obs"/>
              </parameter>
            </dependent>
          </rule>
        </rule>
        <rule>
          <name value="leftDistanceVa"/>
          <source>
            <context value="row"/>
            <element value="item"/>
            <variable value="it"/>
            <condition value="(linkId = 'eyewear/left/distance/va') and answer.value.ofType(string).exists()"/>
          </source>
          <target>
            <context value="bundle"/>
            <element value="entry"/>
            <variable value="entry"/>
          </target>
          <target>
            <context value="entry"/>
            <element value="resource"/>
            <variable value="obs"/>
            <transform value="create"/>
            <parameter>
              <valueString value="Observation"/>
            </parameter>
          </target>
          <rule>
            <name value="base"/>
            <source>
              <context value="qr"/>
            </source>
            <target>
              <context value="obs"/>
            </target>
            <target>
              <context value="entry"/>
            </target>
            <dependent>
              <name value="BuildObservationBase"/>
              <parameter>
                <valueId value="qr"/>
              </parameter>
              <parameter>
                <valueId value="obs"/>
              </parameter>
              <parameter>
                <valueId value="entry"/>
              </parameter>
            </dependent>
          </rule>
          <rule>
            <name value="code"/>
            <source>
              <context value="it"/>
            </source>
            <target>
              <context value="obs"/>
              <element value="code"/>
              <variable value="code"/>
            </target>
            <rule>
              <name value="coding"/>
              <source>
                <context value="it"/>
              </source>
              <target>
                <context value="code"/>
                <element value="coding"/>
                <variable value="coding"/>
              </target>
              <rule>
                <name value="system"/>
                <source>
                  <context value="it"/>
                </source>
                <target>
                  <context value="coding"/>
                  <element value="system"/>
                  <transform value="copy"/>
                  <parameter>
                    <valueString value="http://loinc.org"/>
                  </parameter>
                </target>
              </rule>
              <rule>
                <name value="code"/>
                <source>
                  <context value="it"/>
                </source>
                <target>
                  <context value="coding"/>
                  <element value="code"/>
                  <transform value="copy"/>
                  <parameter>
                    <valueString value="28847-2"/>
                  </parameter>
                </target>
              </rule>
              <rule>
                <name value="display"/>
                <source>
                  <context value="it"/>
                </source>
                <target>
                  <context value="coding"/>
                  <element value="display"/>
                  <transform value="copy"/>
                  <parameter>
                    <valueString value="Left glasses lens prescription Visual acuity far"/>
                  </parameter>
                </target>
              </rule>
            </rule>
            <rule>
              <name value="text"/>
              <source>
                <context value="it"/>
              </source>
              <target>
                <context value="code"/>
                <element value="text"/>
                <transform value="copy"/>
                <parameter>
                  <valueString value="Left eye visual acuity, distance vision (L-VA, DV)"/>
                </parameter>
              </target>
            </rule>
          </rule>
          <rule>
            <name value="value"/>
            <source>
              <context value="it"/>
            </source>
            <target>
              <context value="obs"/>
            </target>
            <dependent>
              <name value="SetAcuity"/>
              <parameter>
                <valueId value="it"/>
              </parameter>
              <parameter>
                <valueId value="obs"/>
              </parameter>
            </dependent>
          </rule>
        </rule>
      </rule>
      <rule>
        <name value="leftNear"/>
        <source>
          <context value="eye"/>
          <element value="item"/>
          <variable value="row"/>
          <condition value="linkId = 'eyewear/left/near'"/>
        </source>
        <rule>
          <name value="leftNearPd"/>
          <source>
            <context value="row"/>
            <element value="item"/>
            <variable value="it"/>
            <condition value="(linkId = 'eyewear/left/near/pd') and answer.value.ofType(decimal).exists()"/>
          </source>
          <target>
            <context value="bundle"/>
            <element value="entry"/>
            <variable value="entry"/>
          </target>
          <target>
            <context value="entry"/>
            <element value="resource"/>
            <variable value="obs"/>
            <transform value="create"/>
            <parameter>
              <valueString value="Observation"/>
            </parameter>
          </target>
          <rule>
            <name value="base"/>
            <source>
              <context value="qr"/>
            </source>
            <target>
              <context value="obs"/>
            </target>
            <target>
              <context value="entry"/>
            </target>
            <dependent>
              <name value="BuildObservationBase"/>
              <parameter>
                <valueId value="qr"/>
              </parameter>
              <parameter>
                <valueId value="obs"/>
              </parameter>
              <parameter>
                <valueId value="entry"/>
              </parameter>
            </dependent>
          </rule>
          <rule>
            <name value="code"/>
            <source>
              <context value="it"/>
            </source>
            <target>
              <context value="obs"/>
              <element value="code"/>
              <variable value="code"/>
            </target>
            <rule>
              <name value="coding"/>
              <source>
                <context value="it"/>
              </source>
              <target>
                <context value="code"/>
                <element value="coding"/>
                <variable value="coding"/>
              </target>
              <rule>
                <name value="system"/>
                <source>
                  <context value="it"/>
                </source>
                <target>
                  <context value="coding"/>
                  <element value="system"/>
                  <transform value="copy"/>
                  <parameter>
                    <valueString value="http://loinc.org"/>
                  </parameter>
                </target>
              </rule>
              <rule>
                <name value="code"/>
                <source>
                  <context value="it"/>
                </source>
                <target>
                  <context value="coding"/>
                  <element value="code"/>
                  <transform value="copy"/>
                  <parameter>
                    <valueString value="55985-6"/>
                  </parameter>
                </target>
              </rule>
              <rule>
                <name value="display"/>
                <source>
                  <context value="it"/>
                </source>
                <target>
                  <context value="coding"/>
                  <element value="display"/>
                  <transform value="copy"/>
                  <parameter>
                    <valueString value="Left glasses lens prescription Pupillary distance.monocular near"/>
                  </parameter>
                </target>
              </rule>
            </rule>
            <rule>
              <name value="text"/>
              <source>
                <context value="it"/>
              </source>
              <target>
                <context value="code"/>
                <element value="text"/>
                <transform value="copy"/>
                <parameter>
                  <valueString value="Left eye pupillary distance, near vision (L-PD, NV)"/>
                </parameter>
              </target>
            </rule>
          </rule>
          <rule>
            <name value="value"/>
            <source>
              <context value="it"/>
            </source>
            <target>
              <context value="obs"/>
            </target>
            <dependent>
              <name value="SetMillimetres"/>
              <parameter>
                <valueId value="it"/>
              </parameter>
              <parameter>
                <valueId value="obs"/>
              </parameter>
            </dependent>
          </rule>
        </rule>
        <rule>
          <name value="leftNearVa"/>
          <source>
            <context value="row"/>
            <element value="item"/>
            <variable value="it"/>
            <condition value="(linkId = 'eyewear/left/near/va') and answer.value.ofType(string).exists()"/>
          </source>
          <target>
            <context value="bundle"/>
            <element value="entry"/>
            <variable value="entry"/>
          </target>
          <target>
            <context value="entry"/>
            <element value="resource"/>
            <variable value="obs"/>
            <transform value="create"/>
            <parameter>
              <valueString value="Observation"/>
            </parameter>
          </target>
          <rule>
            <name value="base"/>
            <source>
              <context value="qr"/>
            </source>
            <target>
              <context value="obs"/>
            </target>
            <target>
              <context value="entry"/>
            </target>
            <dependent>
              <name value="BuildObservationBase"/>
              <parameter>
                <valueId value="qr"/>
              </parameter>
              <parameter>
                <valueId value="obs"/>
              </parameter>
              <parameter>
                <valueId value="entry"/>
              </parameter>
            </dependent>
          </rule>
          <rule>
            <name value="code"/>
            <source>
              <context value="it"/>
            </source>
            <target>
              <context value="obs"/>
              <element value="code"/>
              <variable value="code"/>
            </target>
            <rule>
              <name value="coding"/>
              <source>
                <context value="it"/>
              </source>
              <target>
                <context value="code"/>
                <element value="coding"/>
                <variable value="coding"/>
              </target>
              <rule>
                <name value="system"/>
                <source>
                  <context value="it"/>
                </source>
                <target>
                  <context value="coding"/>
                  <element value="system"/>
                  <transform value="copy"/>
                  <parameter>
                    <valueString value="http://loinc.org"/>
                  </parameter>
                </target>
              </rule>
              <rule>
                <name value="code"/>
                <source>
                  <context value="it"/>
                </source>
                <target>
                  <context value="coding"/>
                  <element value="code"/>
                  <transform value="copy"/>
                  <parameter>
                    <valueString value="28856-3"/>
                  </parameter>
                </target>
              </rule>
              <rule>
                <name value="display"/>
                <source>
                  <context value="it"/>
                </source>
                <target>
                  <context value="coding"/>
                  <element value="display"/>
                  <transform value="copy"/>
                  <parameter>
                    <valueString value="Left glasses lens prescription Visual acuity near"/>
                  </parameter>
                </target>
              </rule>
            </rule>
            <rule>
              <name value="text"/>
              <source>
                <context value="it"/>
              </source>
              <target>
                <context value="code"/>
                <element value="text"/>
                <transform value="copy"/>
                <parameter>
                  <valueString value="Left eye visual acuity, near vision (L-VA, NV)"/>
                </parameter>
              </target>
            </rule>
          </rule>
          <rule>
            <name value="value"/>
            <source>
              <context value="it"/>
            </source>
            <target>
              <context value="obs"/>
            </target>
            <dependent>
              <name value="SetAcuity"/>
              <parameter>
                <valueId value="it"/>
              </parameter>
              <parameter>
                <valueId value="obs"/>
              </parameter>
            </dependent>
          </rule>
        </rule>
      </rule>
    </rule>
  </group>
  <group>
    <name value="SetMillimetres"/>
    <documentation value="`Observation.value[x]` has no decimal choice, so the PD is built as a UCUM&#xD;&#xA;Quantity. The unit is fixed by the form."/>
    <input>
      <name value="it"/>
      <mode value="source"/>
    </input>
    <input>
      <name value="obs"/>
      <type value="Observation"/>
      <mode value="target"/>
    </input>
    <rule>
      <name value="answer"/>
      <source>
        <context value="it"/>
        <element value="answer"/>
        <listMode value="first"/>
        <variable value="a"/>
      </source>
      <rule>
        <name value="quantity"/>
        <source>
          <context value="a"/>
          <element value="value"/>
          <variable value="v"/>
        </source>
        <target>
          <context value="obs"/>
          <element value="value"/>
          <variable value="q"/>
          <transform value="create"/>
          <parameter>
            <valueString value="Quantity"/>
          </parameter>
        </target>
        <rule>
          <name value="quantityValue"/>
          <source>
            <context value="v"/>
          </source>
          <target>
            <context value="q"/>
            <element value="value"/>
            <transform value="copy"/>
            <parameter>
              <valueId value="v"/>
            </parameter>
          </target>
        </rule>
        <rule>
          <name value="quantityUnit"/>
          <source>
            <context value="v"/>
          </source>
          <target>
            <context value="q"/>
            <element value="unit"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="mm"/>
            </parameter>
          </target>
        </rule>
        <rule>
          <name value="quantitySystem"/>
          <source>
            <context value="v"/>
          </source>
          <target>
            <context value="q"/>
            <element value="system"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="http://unitsofmeasure.org"/>
            </parameter>
          </target>
        </rule>
        <rule>
          <name value="quantityCode"/>
          <source>
            <context value="v"/>
          </source>
          <target>
            <context value="q"/>
            <element value="code"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="mm"/>
            </parameter>
          </target>
        </rule>
      </rule>
    </rule>
  </group>
  <group>
    <name value="SetAcuity"/>
    <input>
      <name value="it"/>
      <mode value="source"/>
    </input>
    <input>
      <name value="obs"/>
      <type value="Observation"/>
      <mode value="target"/>
    </input>
    <rule>
      <name value="answer"/>
      <source>
        <context value="it"/>
        <element value="answer"/>
        <listMode value="first"/>
        <variable value="a"/>
      </source>
      <rule>
        <name value="acuityValue"/>
        <source>
          <context value="a"/>
          <element value="value"/>
          <variable value="v"/>
        </source>
        <target>
          <context value="obs"/>
          <element value="value"/>
          <transform value="copy"/>
          <parameter>
            <valueId value="v"/>
          </parameter>
        </target>
      </rule>
    </rule>
  </group>
  <group>
    <name value="BuildObservationBase"/>
    <documentation value="─────────────────────────────────────────────────────────────────────────────&#xD;&#xA;Shared scaffolding. Repeated in each map rather than imported: the transform&#xD;&#xA;engine resolves `imports` against whatever StructureMaps happen to be on the&#xD;&#xA;server, and a form that stops extracting because a shared map was not&#xD;&#xA;uploaded is a worse failure than a duplicated group.&#xD;&#xA;─────────────────────────────────────────────────────────────────────────────"/>
    <input>
      <name value="qr"/>
      <type value="QR"/>
      <mode value="source"/>
    </input>
    <input>
      <name value="obs"/>
      <type value="Observation"/>
      <mode value="target"/>
    </input>
    <input>
      <name value="entry"/>
      <mode value="target"/>
    </input>
    <rule>
      <name value="responseProvenance"/>
      <source>
        <context value="qr"/>
      </source>
      <target>
        <context value="obs"/>
      </target>
      <dependent>
        <name value="SetResponseProvenance"/>
        <parameter>
          <valueId value="qr"/>
        </parameter>
        <parameter>
          <valueId value="obs"/>
        </parameter>
      </dependent>
    </rule>
    <rule>
      <name value="idAndFullUrl"/>
      <source>
        <context value="qr"/>
      </source>
      <target>
        <context value="obs"/>
        <element value="id"/>
        <transform value="uuid"/>
      </target>
      <dependent>
        <name value="SetObservationFullUrl"/>
        <parameter>
          <valueId value="obs"/>
        </parameter>
        <parameter>
          <valueId value="entry"/>
        </parameter>
      </dependent>
    </rule>
    <rule>
      <name value="entryRequest"/>
      <source>
        <context value="qr"/>
      </source>
      <target>
        <context value="entry"/>
        <element value="request"/>
        <variable value="request"/>
      </target>
      <rule>
        <name value="requestMethod"/>
        <source>
          <context value="qr"/>
        </source>
        <target>
          <context value="request"/>
          <element value="method"/>
          <transform value="copy"/>
          <parameter>
            <valueString value="POST"/>
          </parameter>
        </target>
      </rule>
      <rule>
        <name value="requestUrl"/>
        <source>
          <context value="qr"/>
        </source>
        <target>
          <context value="request"/>
          <element value="url"/>
          <transform value="copy"/>
          <parameter>
            <valueString value="Observation"/>
          </parameter>
        </target>
      </rule>
    </rule>
    <rule>
      <name value="status"/>
      <source>
        <context value="qr"/>
      </source>
      <target>
        <context value="obs"/>
        <element value="status"/>
        <transform value="copy"/>
        <parameter>
          <valueString value="final"/>
        </parameter>
      </target>
    </rule>
    <rule>
      <name value="category"/>
      <source>
        <context value="qr"/>
      </source>
      <target>
        <context value="obs"/>
        <element value="category"/>
        <variable value="cat"/>
      </target>
      <rule>
        <name value="categoryCoding"/>
        <source>
          <context value="qr"/>
        </source>
        <target>
          <context value="cat"/>
          <element value="coding"/>
          <variable value="coding"/>
        </target>
        <rule>
          <name value="categorySystem"/>
          <source>
            <context value="qr"/>
          </source>
          <target>
            <context value="coding"/>
            <element value="system"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="http://terminology.hl7.org/CodeSystem/observation-category"/>
            </parameter>
          </target>
        </rule>
        <rule>
          <name value="categoryCode"/>
          <source>
            <context value="qr"/>
          </source>
          <target>
            <context value="coding"/>
            <element value="code"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="exam"/>
            </parameter>
          </target>
        </rule>
        <rule>
          <name value="categoryDisplay"/>
          <source>
            <context value="qr"/>
          </source>
          <target>
            <context value="coding"/>
            <element value="display"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="Exam"/>
            </parameter>
          </target>
        </rule>
      </rule>
    </rule>
    <rule>
      <name value="subject"/>
      <source>
        <context value="qr"/>
        <element value="subject"/>
        <variable value="s"/>
        <condition value="(reference.exists() and (reference.startsWith('Patient/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Patient')))"/>
      </source>
      <target>
        <context value="obs"/>
        <element value="subject"/>
        <transform value="copy"/>
        <parameter>
          <valueId value="s"/>
        </parameter>
      </target>
    </rule>
    <rule>
      <name value="encounter"/>
      <source>
        <context value="qr"/>
        <element value="encounter"/>
        <variable value="e"/>
        <condition value="(reference.exists() and (reference.startsWith('Encounter/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Encounter')))"/>
      </source>
      <target>
        <context value="obs"/>
        <element value="encounter"/>
        <transform value="copy"/>
        <parameter>
          <valueId value="e"/>
        </parameter>
      </target>
    </rule>
    <rule>
      <name value="effective"/>
      <source>
        <context value="qr"/>
        <element value="authored"/>
        <variable value="t"/>
      </source>
      <target>
        <context value="obs"/>
        <element value="effective"/>
        <transform value="copy"/>
        <parameter>
          <valueId value="t"/>
        </parameter>
      </target>
      <documentation value="Measured when the prescription was written, which is the response's&#xD;&#xA;`authored` -- the form does not ask for a date."/>
    </rule>
    <rule>
      <name value="performerFromAuthor"/>
      <source>
        <context value="qr"/>
        <element value="author"/>
        <variable value="a"/>
        <condition value="(reference.matches('(^|/)(Practitioner|PractitionerRole)/[^/]+$')) and ((reference.exists() and (reference.startsWith('Organization/') or reference.startsWith('Patient/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Organization') or ('/' + type).endsWith('/Patient'))))"/>
      </source>
      <target>
        <context value="obs"/>
        <element value="performer"/>
        <transform value="copy"/>
        <parameter>
          <valueId value="a"/>
        </parameter>
      </target>
      <documentation value="Measured by the prescriber, chosen the same way as on the prescription."/>
    </rule>
    <rule>
      <name value="performerSourceFallback"/>
      <source>
        <context value="qr"/>
        <condition value="author.where(((reference.exists() and (reference.startsWith('Organization/') or reference.startsWith('Patient/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Organization') or ('/' + type).endsWith('/Patient'))))).exists().not()"/>
      </source>
      <rule>
        <name value="performerFromSource"/>
        <source>
          <context value="qr"/>
          <element value="source"/>
          <variable value="s"/>
          <condition value="(reference.matches('(^|/)(Practitioner|PractitionerRole)/[^/]+$')) and ((reference.exists() and (reference.startsWith('Organization/') or reference.startsWith('Patient/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Organization') or ('/' + type).endsWith('/Patient'))))"/>
        </source>
        <target>
          <context value="obs"/>
          <element value="performer"/>
          <transform value="copy"/>
          <parameter>
            <valueId value="s"/>
          </parameter>
        </target>
      </rule>
    </rule>
    <rule>
      <name value="linkQuestionnaireResponse"/>
      <source>
        <context value="qr"/>
        <element value="id"/>
        <variable value="qid"/>
        <condition value="$this.hasValue()"/>
      </source>
      <target>
        <context value="obs"/>
        <element value="derivedFrom"/>
        <variable value="ref"/>
        <transform value="create"/>
        <parameter>
          <valueString value="Reference"/>
        </parameter>
      </target>
      <target>
        <context value="ref"/>
        <element value="reference"/>
        <transform value="copy"/>
        <parameter>
          <valueId value="qid"/>
        </parameter>
      </target>
      <documentation value="A stored response's id already reads &quot;QuestionnaireResponse/&lt;id&gt;&quot;, so it is&#xD;&#xA;a relative reference as it stands. The hasValue() guard is what stops an&#xD;&#xA;unsaved response -- one posted straight to $extract rather than saved first&#xD;&#xA;-- from writing &quot;QuestionnaireResponse/null&quot; into the record."/>
    </rule>
  </group>
  <group>
    <name value="SetObservationFullUrl"/>
    <input>
      <name value="obs"/>
      <type value="Observation"/>
      <mode value="source"/>
    </input>
    <input>
      <name value="entry"/>
      <mode value="target"/>
    </input>
    <rule>
      <name value="assignFullUrl"/>
      <source>
        <context value="obs"/>
        <element value="id"/>
        <variable value="id"/>
      </source>
      <target>
        <context value="entry"/>
        <element value="fullUrl"/>
        <transform value="append"/>
        <parameter>
          <valueString value="https://fhir.slade360.co.ke/fhir/Observation/"/>
        </parameter>
        <parameter>
          <valueId value="id"/>
        </parameter>
      </target>
    </rule>
  </group>
  <group>
    <name value="SetVisionPrescriptionFullUrl"/>
    <input>
      <name value="vp"/>
      <type value="VisionPrescription"/>
      <mode value="source"/>
    </input>
    <input>
      <name value="entry"/>
      <mode value="target"/>
    </input>
    <rule>
      <name value="assignFullUrl"/>
      <source>
        <context value="vp"/>
        <element value="id"/>
        <variable value="id"/>
      </source>
      <target>
        <context value="entry"/>
        <element value="fullUrl"/>
        <transform value="append"/>
        <parameter>
          <valueString value="https://fhir.slade360.co.ke/fhir/VisionPrescription/"/>
        </parameter>
        <parameter>
          <valueId value="id"/>
        </parameter>
      </target>
    </rule>
  </group>
  <group>
    <name value="SetResponseProvenance"/>
    <documentation value="── Provenance and tenancy ───────────────────────────────────────────────────&#xD;&#xA;Two tags on everything this map creates. The first says which form produced&#xD;&#xA;it -- the form's document type, not the questionnaire's id, because every&#xD;&#xA;environment mints its own ids and there is one set of maps. The second copies&#xD;&#xA;the tenant tags off the response, because nothing else on the path puts them&#xD;&#xA;there."/>
    <input>
      <name value="qr"/>
      <type value="QR"/>
      <mode value="source"/>
    </input>
    <input>
      <name value="r"/>
      <mode value="target"/>
    </input>
    <rule>
      <name value="provenanceMeta"/>
      <source>
        <context value="qr"/>
      </source>
      <target>
        <context value="r"/>
        <element value="meta"/>
        <variable value="m"/>
      </target>
      <rule>
        <name value="provenanceTag"/>
        <source>
          <context value="qr"/>
        </source>
        <target>
          <context value="m"/>
          <element value="tag"/>
          <variable value="t"/>
        </target>
        <rule>
          <name value="provenanceSystem"/>
          <source>
            <context value="qr"/>
          </source>
          <target>
            <context value="t"/>
            <element value="system"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="http://slade360edi.com/questionnaire-provenance"/>
            </parameter>
          </target>
        </rule>
        <rule>
          <name value="provenanceCode"/>
          <source>
            <context value="qr"/>
          </source>
          <target>
            <context value="t"/>
            <element value="code"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="optical-prescription"/>
            </parameter>
          </target>
        </rule>
        <rule>
          <name value="provenanceDisplay"/>
          <source>
            <context value="qr"/>
          </source>
          <target>
            <context value="t"/>
            <element value="display"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="Optical prescription"/>
            </parameter>
          </target>
        </rule>
      </rule>
    </rule>
    <rule>
      <name value="tenantTags"/>
      <source>
        <context value="qr"/>
        <element value="meta"/>
        <variable value="qm"/>
      </source>
      <rule>
        <name value="copyTag"/>
        <source>
          <context value="qm"/>
          <element value="tag"/>
          <variable value="qt"/>
        </source>
        <target>
          <context value="r"/>
          <element value="meta"/>
          <variable value="m"/>
        </target>
        <target>
          <context value="m"/>
          <element value="tag"/>
          <transform value="copy"/>
          <parameter>
            <valueId value="qt"/>
          </parameter>
        </target>
      </rule>
      <documentation value="`meta` is 0..1, so this lands on the same meta the rule above created&#xD;&#xA;rather than a second one."/>
    </rule>
  </group>
</StructureMap>