helios-fhirpath 0.2.0

This is an implementation of HL7's FHIRPath Specification.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
# fhirpath-lab server engine API
![](static/Square44x44Logo.scale-150.png "Fhirpath-lab logo")

Some fhirpath engines supported in the fhirpath-lab require server processing
to be able to evaluate expressions. This is typically for 2 reasons:

* Language/engine is not available in the browser (e.g. .NET/JAVA)
* Hosting the service seperates the engine maintenance from the fhirpath-lab itself

Currently the Firely SDK for .NET and the HAPI FHIR server/FHIR for Linux(IBM) for JAVA require a hosted service.

These hosted services are available for use by anyone, but are not guaranteed to be available, and when called by the fhirpath-lab will come in through one of these callers:

* https://fhirpath-lab.com
* https://dev.fhirpath-lab.com
* https://fhirpath-lab.azurewebsites.net
* https://fhirpath-lab-dev.azurewebsites.net/
* http://localhost:3000 - Which is where I test locally to the services

As the fhirpath-lab is a web only client, you'll need to implement CORS for the above domains to be able to call your service.

To add in another engine into the fhirpath-lab, the VueJS code will need to be updated to include the new engine:
* static/config.json - Add in the new service URL
* static/config.local.json - Add in the new service URL (for local debugging only)
* helpers/user_settings.ts - Provide the named form of the new service
* pages/FhirPath/index.vue
    - include the new engine in the `executionEngines` array (end of file)
    - update `evaluateFhirPathExpression` to support the newly selected engine (easiest to copy the HAPI portion)
    - ensure that your new engine name is included in the `trace` message that reports usage, and doesn't conflict with any other engine names<br/>*(i.e. don't report `HAPI` if you're not using the HAPI engine)*

## API Definition
The API takes a FHIR Parameters resource as the input, and returns a FHIR Parameters resource as the output.

The engine should evaluate the context expression against the test resource, and then run the expression against each item returned (If no context expression is provided, just evaluate the expression on the test resource).

### Available Endpoints

- `POST /` - Auto-detects FHIR version from the resource
- `POST /r4` - Forces R4 processing (if compiled with R4 feature)
- `POST /r4b` - Forces R4B processing (if compiled with R4B feature)
- `POST /r5` - Forces R5 processing (if compiled with R5 feature)
- `POST /r6` - Forces R6 processing (if compiled with R6 feature)

Version-specific endpoints are useful when you want to ensure your resource is processed with a specific FHIR version, overriding the auto-detection.

### Input Parameters Resource:
| Parameter Name | Description | Type | Cardinality |
|----------------|-------------|------|:-----------:|
| `context` | The context to execute the expression against | string | 0..1 |
| `expression` | The fhirpath expression to execute | string | 1..1 |
| `validate` | Whether to validate the expression before executing it (experimental) | boolean | 0..1 |
| `variables` | The variables to pass to the expression | multi-part | 0..* |
| *(variables.part.name)* | The name of a variable to use | string | 1..1 | 
| *(variables.part.value)* | The value of the variable| *(any)* | 0..1 | 
| `resource` | The resource to execute the expression against | Resource | 1..1 |
| `terminologyServer` | The terminology server to use for terminology lookups if not natively supported | string | 0..1 |

### Output Parameters Resource:
| Parameter Name | Description | Type | Cardinality |
|----------------|-------------|------|:-----------:|
| `parameters` | The parameters that were passed in - detailed below | multi-part | 1..1 |
| `result` | The result of the expression execution - each iteration of the result is for a different context<br/>The context will be in the valueString property, and the result(s) in the part collection | string | 0..* |

#### Output Resource: `parameters` Part
| Part Name | Description | Type | Cardinality |
|-----------|-------------|------|:-----------:|
| evaluator | The name (and version) of the engine performing the evaluation | string | 1..1 |
| expectedReturnType | The expected return type of the expression (via static analysis) | string | 0..1 |
| parseDebugTree | The debug output from the parser as a JSON tree (Abstract Syntax Tree)<br/>*This is JSON embedded in the string* | string | 0..1 |
| parseDebug | The debug output from the parser (unformatted - just messages for debugging) | string | 0..1 |
| expression | The expression that was executed | string | 1..1 |
| context | The context that was used to execute the expression | string | 0..1 |
| resource | The resource that was used to execute the expression | Resource | 1..1 |
| variables | The variables that were passed to the expression | multi-part | 0..* |

#### Output Resource: `result` Part
The result part itself is also broken down into 2 parts:
* trace - `name` = `'trace'`, `valueString` = the name provided to the trace call in the expression,<br/> `part` = the trace output, with the name of those parts being the datatype also.
* result - `name` = datatype (this could be primitive types, resource type, or complex type)

> **Note:** if a value cannot be represented by a FHIR type in the parameters (such as backbone elements), then that output should be serialized into a JSON fragment and put into a string extension `http://fhir.forms-lab.com/StructureDefinition/json-value` on the part.
``` json
        {
          "extension": [
            {
              "url": "http://fhir.forms-lab.com/StructureDefinition/json-value",
              "valueString": "{\r\n  \"relationship\": [\r\n    {\r\n      \"coding\": [\r\n        {\r\n          \"system\": \"http://terminology.hl7.org/CodeSystem/v2-0131\",\r\n          \"code\": \"N\"\r\n        }\r\n      ]\r\n    }\r\n  ],\r\n  \"name\": {\r\n    \"family\": \"du Marché\",\r\n    \"_family\": {\r\n      \"extension\": [\r\n        {\r\n          \"url\": \"http://hl7.org/fhir/StructureDefinition/humanname-own-prefix\",\r\n          \"valueString\": \"VV\"\r\n        }\r\n      ]\r\n    },\r\n    \"given\": [\r\n      \"Bénédicte\"\r\n    ]\r\n  },\r\n  \"telecom\": [\r\n    {\r\n      \"system\": \"phone\",\r\n      \"value\": \"+33 (237) 998327\"\r\n    }\r\n  ],\r\n  \"address\": {\r\n    \"use\": \"home\",\r\n    \"type\": \"both\",\r\n    \"line\": [\r\n      \"534 Erewhon St\"\r\n    ],\r\n    \"city\": \"PleasantVille\",\r\n    \"district\": \"Rainbow\",\r\n    \"state\": \"Vic\",\r\n    \"postalCode\": \"3999\",\r\n    \"period\": {\r\n      \"start\": \"1974-12-25\"\r\n    }\r\n  },\r\n  \"gender\": \"female\",\r\n  \"period\": {\r\n    \"start\": \"2012\"\r\n  }\r\n}\r\n"
            }
          ],
          "name": "Patient#Contact"
        },
```


### Example Request:

``` rest
POST: [service-url]
Content-Type: application/json
```
``` json
{
  "resourceType": "Parameters",
  "parameter": [
    {
      "name": "expression",
      "valueString": "trace('trc').given.join(' ')\n.combine(family).join(', ') | %varValue"
    },
    {
      "name": "context",
      "valueString": "name"
    },
    {
      "name": "validate",
      "valueBoolean": true
    },
    {
      "name": "variables",
      "part": [
        {
          "name": "varValue",
          "valueString": "testMe"
        }
      ]
    },
    {
      "name": "resource",
      "resource": {
        "resourceType": "Patient",
        "id": "example",
        "identifier": [
          {
            "use": "usual",
            "type": {
              "coding": [
                {
                  "system": "http://terminology.hl7.org/CodeSystem/v2-0203",
                  "code": "MR"
                }
              ]
            },
            "system": "urn:oid:1.2.36.146.595.217.0.1",
            "value": "12345",
            "period": {
              "start": "2001-05-06"
            },
            "assigner": {
              "display": "Acme Healthcare"
            }
          }
        ],
        "active": true,
        "name": [
          {
            "use": "official",
            "family": "Chalmers",
            "given": [
              "Peter",
              "James"
            ]
          },
          {
            "use": "usual",
            "given": [
              "Jim"
            ]
          },
          {
            "use": "maiden",
            "family": "Windsor",
            "given": [
              "Peter",
              "James"
            ],
            "period": {
              "end": "2002"
            }
          }
        ],
        "telecom": [
          {
            "use": "home"
          },
          {
            "system": "phone",
            "value": "(03) 5555 6473",
            "use": "work",
            "rank": 1
          },
          {
            "system": "phone",
            "value": "(03) 3410 5613",
            "use": "mobile",
            "rank": 2
          },
          {
            "system": "phone",
            "value": "(03) 5555 8834",
            "use": "old",
            "period": {
              "end": "2014"
            }
          }
        ],
        "gender": "male",
        "birthDate": "1974-12-25",
        "_birthDate": {
          "extension": [
            {
              "url": "http://hl7.org/fhir/StructureDefinition/patient-birthTime",
              "valueDateTime": "1974-12-26T06:35:45+11:00"
            }
          ]
        },
        "deceasedBoolean": false,
        "address": [
          {
            "use": "home",
            "type": "both",
            "text": "534 Erewhon St PeasantVille, Rainbow, Vic  3999",
            "line": [
              "534 Erewhon St"
            ],
            "city": "PleasantVille",
            "district": "Rainbow",
            "state": "Vic",
            "postalCode": "3999",
            "period": {
              "start": "1974-12-25"
            }
          }
        ],
        "contact": [
          {
            "relationship": [
              {
                "coding": [
                  {
                    "system": "http://terminology.hl7.org/CodeSystem/v2-0131",
                    "code": "N"
                  }
                ]
              }
            ],
            "name": {
              "family": "du Marché",
              "_family": {
                "extension": [
                  {
                    "url": "http://hl7.org/fhir/StructureDefinition/humanname-own-prefix",
                    "valueString": "VV"
                  }
                ]
              },
              "given": [
                "Bénédicte"
              ]
            },
            "telecom": [
              {
                "system": "phone",
                "value": "+33 (237) 998327"
              }
            ],
            "address": {
              "use": "home",
              "type": "both",
              "line": [
                "534 Erewhon St"
              ],
              "city": "PleasantVille",
              "district": "Rainbow",
              "state": "Vic",
              "postalCode": "3999",
              "period": {
                "start": "1974-12-25"
              }
            },
            "gender": "female",
            "period": {
              "start": "2012"
            }
          }
        ],
        "managingOrganization": {
          "reference": "Organization/1"
        }
      }
    },
    {
      "name": "terminologyserver",
      "valueString": "https://sqlonfhir-r4.azurewebsites.net/fhir"
    }
  ]
}
```

### Example Response:

``` json
{
  "resourceType": "Parameters",
  "id": "fhirpath",
  "parameter": [
    {
      "name": "parameters",
      "part": [
        {
          "name": "evaluator",
          "valueString": "Firely-5.3.0 (R4B)"
        },
        {
          "name": "expectedReturnType",
          "valueString": "string[]"
        },
        {
          "name": "parseDebugTree",
          "valueString": "{\r\n  \"ExpressionType\": \"BinaryExpression\",\r\n  \"Name\": \"|\",\r\n  \"Arguments\": [\r\n    {\r\n      \"ExpressionType\": \"FunctionCallExpression\",\r\n      \"Name\": \"join\",\r\n      \"Arguments\": [\r\n        {\r\n          \"ExpressionType\": \"FunctionCallExpression\",\r\n          \"Name\": \"combine\",\r\n          \"Arguments\": [\r\n            {\r\n              \"ExpressionType\": \"FunctionCallExpression\",\r\n              \"Name\": \"join\",\r\n              \"Arguments\": [\r\n                {\r\n                  \"ExpressionType\": \"ChildExpression\",\r\n                  \"Name\": \"given\",\r\n                  \"Arguments\": [\r\n                    {\r\n                      \"ExpressionType\": \"FunctionCallExpression\",\r\n                      \"Name\": \"trace\",\r\n                      \"Arguments\": [\r\n                        {\r\n                          \"ExpressionType\": \"AxisExpression\",\r\n                          \"Name\": \"builtin.that\",\r\n                          \"ReturnType\": \"HumanName\"\r\n                        },\r\n                        {\r\n                          \"ExpressionType\": \"ConstantExpression\",\r\n                          \"Name\": \"trc\",\r\n                          \"ReturnType\": \"string\"\r\n                        }\r\n                      ],\r\n                      \"ReturnType\": \"HumanName\"\r\n                    }\r\n                  ],\r\n                  \"ReturnType\": \"string[]\"\r\n                },\r\n                {\r\n                  \"ExpressionType\": \"ConstantExpression\",\r\n                  \"Name\": \" \",\r\n                  \"ReturnType\": \"string\"\r\n                }\r\n              ],\r\n              \"ReturnType\": \"string\"\r\n            },\r\n            {\r\n              \"ExpressionType\": \"ChildExpression\",\r\n              \"Name\": \"family\",\r\n              \"Arguments\": [\r\n                {\r\n                  \"ExpressionType\": \"AxisExpression\",\r\n                  \"Name\": \"builtin.that\",\r\n                  \"ReturnType\": \"HumanName\"\r\n                }\r\n              ],\r\n              \"ReturnType\": \"string\"\r\n            }\r\n          ],\r\n          \"ReturnType\": \"string[]\"\r\n        },\r\n        {\r\n          \"ExpressionType\": \"ConstantExpression\",\r\n          \"Name\": \", \",\r\n          \"ReturnType\": \"string\"\r\n        }\r\n      ],\r\n      \"ReturnType\": \"string\"\r\n    },\r\n    {\r\n      \"ExpressionType\": \"VariableRefExpression\",\r\n      \"Name\": \"varValue\",\r\n      \"ReturnType\": \"string\"\r\n    }\r\n  ],\r\n  \"ReturnType\": \"string[]\"\r\n}"
        },
        {
          "name": "parseDebug",
          "valueString": "trace('trc') : HumanName\r\n.given : string[]\r\n.join(' ') : string\r\n.combine(family : string\r\n) : string[]\r\n.join(', ') : string\r\n|\r\n%varValue : string\r\n\r\n : string[] (op: |)\r\n"
        },
        {
          "name": "context",
          "valueString": "name"
        },
        {
          "name": "expression",
          "valueString": "trace('trc').given.join(' ')\n.combine(family).join(', ') | %varValue"
        },
        {
          "name": "resource",
          "resource": {
            "resourceType": "Patient",
            "id": "example",
            "identifier": [
              {
                "use": "usual",
                "type": {
                  "coding": [
                    {
                      "system": "http://terminology.hl7.org/CodeSystem/v2-0203",
                      "code": "MR"
                    }
                  ]
                },
                "system": "urn:oid:1.2.36.146.595.217.0.1",
                "value": "12345",
                "period": {
                  "start": "2001-05-06"
                },
                "assigner": {
                  "display": "Acme Healthcare"
                }
              }
            ],
            "active": true,
            "name": [
              {
                "use": "official",
                "family": "Chalmers",
                "given": [
                  "Peter",
                  "James"
                ]
              },
              {
                "use": "usual",
                "given": [
                  "Jim"
                ]
              },
              {
                "use": "maiden",
                "family": "Windsor",
                "given": [
                  "Peter",
                  "James"
                ],
                "period": {
                  "end": "2002"
                }
              }
            ],
            "telecom": [
              {
                "use": "home"
              },
              {
                "system": "phone",
                "value": "(03) 5555 6473",
                "use": "work",
                "rank": 1
              },
              {
                "system": "phone",
                "value": "(03) 3410 5613",
                "use": "mobile",
                "rank": 2
              },
              {
                "system": "phone",
                "value": "(03) 5555 8834",
                "use": "old",
                "period": {
                  "end": "2014"
                }
              }
            ],
            "gender": "male",
            "birthDate": "1974-12-25",
            "_birthDate": {
              "extension": [
                {
                  "url": "http://hl7.org/fhir/StructureDefinition/patient-birthTime",
                  "valueDateTime": "1974-12-25T14:35:45-05:00"
                }
              ]
            },
            "deceasedBoolean": false,
            "address": [
              {
                "use": "home",
                "type": "both",
                "text": "534 Erewhon St PeasantVille, Rainbow, Vic  3999",
                "line": [
                  "534 Erewhon St"
                ],
                "city": "PleasantVille",
                "district": "Rainbow",
                "state": "Vic",
                "postalCode": "3999",
                "period": {
                  "start": "1974-12-25"
                }
              }
            ],
            "contact": [
              {
                "relationship": [
                  {
                    "coding": [
                      {
                        "system": "http://terminology.hl7.org/CodeSystem/v2-0131",
                        "code": "N"
                      }
                    ]
                  }
                ],
                "name": {
                  "family": "du Marché",
                  "_family": {
                    "extension": [
                      {
                        "url": "http://hl7.org/fhir/StructureDefinition/humanname-own-prefix",
                        "valueString": "VV"
                      }
                    ]
                  },
                  "given": [
                    "Bénédicte"
                  ]
                },
                "telecom": [
                  {
                    "system": "phone",
                    "value": "+33 (237) 998327"
                  }
                ],
                "address": {
                  "use": "home",
                  "type": "both",
                  "line": [
                    "534 Erewhon St"
                  ],
                  "city": "PleasantVille",
                  "district": "Rainbow",
                  "state": "Vic",
                  "postalCode": "3999",
                  "period": {
                    "start": "1974-12-25"
                  }
                },
                "gender": "female",
                "period": {
                  "start": "2012"
                }
              }
            ],
            "managingOrganization": {
              "reference": "Organization/1"
            }
          }
        },
        {
          "name": "terminologyServerUrl",
          "valueString": "https://sqlonfhir-r4.azurewebsites.net/fhir"
        },
        {
          "name": "variables",
          "part": [
            {
              "name": "varValue",
              "valueString": "testMe"
            }
          ]
        }
      ]
    },
    {
      "name": "result",
      "valueString": "Patient.name[0]",
      "part": [
        {
          "name": "string",
          "valueString": "Peter James, Chalmers"
        },
        {
          "name": "string",
          "valueString": "testMe"
        },
        {
          "name": "trace",
          "valueString": "trc",
          "part": [
            {
              "name": "HumanName",
              "valueHumanName": {
                "use": "official",
                "family": "Chalmers",
                "given": [
                  "Peter",
                  "James"
                ]
              }
            }
          ]
        }
      ]
    },
    {
      "name": "result",
      "valueString": "Patient.name[1]",
      "part": [
        {
          "name": "string",
          "valueString": "Jim"
        },
        {
          "name": "string",
          "valueString": "testMe"
        },
        {
          "name": "trace",
          "valueString": "trc",
          "part": [
            {
              "name": "HumanName",
              "valueHumanName": {
                "use": "usual",
                "given": [
                  "Jim"
                ]
              }
            }
          ]
        }
      ]
    },
    {
      "name": "result",
      "valueString": "Patient.name[2]",
      "part": [
        {
          "name": "string",
          "valueString": "Peter James, Windsor"
        },
        {
          "name": "string",
          "valueString": "testMe"
        },
        {
          "name": "trace",
          "valueString": "trc",
          "part": [
            {
              "name": "HumanName",
              "valueHumanName": {
                "use": "maiden",
                "family": "Windsor",
                "given": [
                  "Peter",
                  "James"
                ],
                "period": {
                  "end": "2002"
                }
              }
            }
          ]
        }
      ]
    }
  ]
}
```