Skip to content

Commit 1f4219f

Browse files
dczuladacmoesel
authored andcommitted
This alias (#119)
* $this can be used as an alias * Test for a Function that generates a $this * simplify if-else
1 parent ec229b1 commit 1f4219f

File tree

5 files changed

+273
-6
lines changed

5 files changed

+273
-6
lines changed

src/example/browser/cql4browsers.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45245,10 +45245,10 @@
4524545245

4524645246
Context.prototype.get = function(identifier) {
4524745247
var ref;
45248-
if (identifier === "$this") {
45249-
return this.context_values;
45250-
} else if (typeof this.context_values[identifier] !== 'undefined') {
45248+
if (typeof this.context_values[identifier] !== 'undefined') {
4525145249
return this.context_values[identifier];
45250+
} else if (identifier === "$this") {
45251+
return this.context_values;
4525245252
} else {
4525345253
return (ref = this.parent) != null ? ref.get(identifier) : void 0;
4525445254
}

src/runtime/context.coffee

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ module.exports.Context = class Context
9292
@parent?.getConcept(name)
9393

9494
get: (identifier) ->
95-
if identifier == "$this"
96-
@context_values
9795
# Check for undefined because if its null, we actually *do* want to return null (rather than looking at parent),
9896
# but if it's really undefined, *then* look at the parent
99-
else if typeof @context_values[identifier] isnt 'undefined'
97+
if typeof @context_values[identifier] isnt 'undefined'
10098
@context_values[identifier]
99+
else if identifier == "$this"
100+
@context_values
101101
else
102102
@parent?.get(identifier)
103103

test/elm/query/data.coffee

Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,261 @@ module.exports['DateRangeOptimizedQuery'] = {
399399
}
400400
}
401401

402+
### FunctionQuery
403+
library TestSnippet version '1'
404+
using QUICK
405+
context Patient
406+
define function "FunctionWithThis"(Encounter List<"Encounter">): Count(Encounter.period EncounterPeriod return EncounterPeriod)
407+
define queryWithThis: "FunctionWithThis"([Encounter] E) > 0
408+
###
409+
410+
module.exports['FunctionQuery'] = {
411+
"library" : {
412+
"identifier" : {
413+
"id" : "TestSnippet",
414+
"version" : "1"
415+
},
416+
"schemaIdentifier" : {
417+
"id" : "urn:hl7-org:elm",
418+
"version" : "r1"
419+
},
420+
"usings" : {
421+
"def" : [ {
422+
"localIdentifier" : "System",
423+
"uri" : "urn:hl7-org:elm-types:r1"
424+
}, {
425+
"localId" : "1",
426+
"localIdentifier" : "QUICK",
427+
"uri" : "http://hl7.org/fhir"
428+
} ]
429+
},
430+
"statements" : {
431+
"def" : [ {
432+
"name" : "Patient",
433+
"context" : "Patient",
434+
"expression" : {
435+
"type" : "SingletonFrom",
436+
"operand" : {
437+
"dataType" : "{http://hl7.org/fhir}Patient",
438+
"templateId" : "patient-qicore-qicore-patient",
439+
"type" : "Retrieve"
440+
}
441+
}
442+
}, {
443+
"localId" : "10",
444+
"name" : "FunctionWithThis",
445+
"context" : "Patient",
446+
"accessLevel" : "Public",
447+
"type" : "FunctionDef",
448+
"annotation" : [ {
449+
"type" : "Annotation",
450+
"s" : {
451+
"r" : "10",
452+
"s" : [ {
453+
"value" : [ "define function ","\"FunctionWithThis\"","(","Encounter"," " ]
454+
}, {
455+
"r" : "3",
456+
"s" : [ {
457+
"value" : [ "List<" ]
458+
}, {
459+
"r" : "2",
460+
"s" : [ {
461+
"value" : [ "\"Encounter\"" ]
462+
} ]
463+
}, {
464+
"value" : [ ">" ]
465+
} ]
466+
}, {
467+
"value" : [ "): " ]
468+
}, {
469+
"r" : "9",
470+
"s" : [ {
471+
"r" : "9",
472+
"s" : [ {
473+
"value" : [ "Count","(" ]
474+
}, {
475+
"r" : "8",
476+
"s" : [ {
477+
"s" : [ {
478+
"r" : "5",
479+
"s" : [ {
480+
"r" : "4",
481+
"s" : [ {
482+
"s" : [ {
483+
"value" : [ "Encounter",".","period" ]
484+
} ]
485+
} ]
486+
}, {
487+
"value" : [ " ","EncounterPeriod" ]
488+
} ]
489+
} ]
490+
}, {
491+
"value" : [ " " ]
492+
}, {
493+
"r" : "7",
494+
"s" : [ {
495+
"value" : [ "return " ]
496+
}, {
497+
"r" : "6",
498+
"s" : [ {
499+
"value" : [ "EncounterPeriod" ]
500+
} ]
501+
} ]
502+
} ]
503+
}, {
504+
"value" : [ ")" ]
505+
} ]
506+
} ]
507+
} ]
508+
}
509+
} ],
510+
"expression" : {
511+
"localId" : "9",
512+
"type" : "Count",
513+
"source" : {
514+
"localId" : "8",
515+
"type" : "Query",
516+
"source" : [ {
517+
"localId" : "5",
518+
"alias" : "EncounterPeriod",
519+
"expression" : {
520+
"localId" : "4",
521+
"type" : "Query",
522+
"source" : [ {
523+
"alias" : "$this",
524+
"expression" : {
525+
"name" : "Encounter",
526+
"type" : "OperandRef"
527+
}
528+
} ],
529+
"where" : {
530+
"type" : "Not",
531+
"operand" : {
532+
"type" : "IsNull",
533+
"operand" : {
534+
"path" : "period",
535+
"type" : "Property",
536+
"source" : {
537+
"name" : "$this",
538+
"type" : "AliasRef"
539+
}
540+
}
541+
}
542+
},
543+
"return" : {
544+
"expression" : {
545+
"path" : "period",
546+
"type" : "Property",
547+
"source" : {
548+
"name" : "$this",
549+
"type" : "AliasRef"
550+
}
551+
}
552+
}
553+
}
554+
} ],
555+
"relationship" : [ ],
556+
"return" : {
557+
"localId" : "7",
558+
"expression" : {
559+
"localId" : "6",
560+
"name" : "EncounterPeriod",
561+
"type" : "AliasRef"
562+
}
563+
}
564+
}
565+
},
566+
"operand" : [ {
567+
"name" : "Encounter",
568+
"operandTypeSpecifier" : {
569+
"localId" : "3",
570+
"type" : "ListTypeSpecifier",
571+
"elementType" : {
572+
"localId" : "2",
573+
"name" : "{http://hl7.org/fhir}Encounter",
574+
"type" : "NamedTypeSpecifier"
575+
}
576+
}
577+
} ]
578+
}, {
579+
"localId" : "17",
580+
"name" : "queryWithThis",
581+
"context" : "Patient",
582+
"accessLevel" : "Public",
583+
"annotation" : [ {
584+
"type" : "Annotation",
585+
"s" : {
586+
"r" : "17",
587+
"s" : [ {
588+
"value" : [ "define ","queryWithThis",": " ]
589+
}, {
590+
"r" : "16",
591+
"s" : [ {
592+
"r" : "14",
593+
"s" : [ {
594+
"value" : [ "\"FunctionWithThis\"","(" ]
595+
}, {
596+
"r" : "13",
597+
"s" : [ {
598+
"s" : [ {
599+
"r" : "12",
600+
"s" : [ {
601+
"r" : "11",
602+
"s" : [ {
603+
"r" : "11",
604+
"s" : [ {
605+
"value" : [ "[","Encounter","]" ]
606+
} ]
607+
} ]
608+
}, {
609+
"value" : [ " ","E" ]
610+
} ]
611+
} ]
612+
} ]
613+
}, {
614+
"value" : [ ")" ]
615+
} ]
616+
}, {
617+
"r" : "15",
618+
"value" : [ " ",">"," ","0" ]
619+
} ]
620+
} ]
621+
}
622+
} ],
623+
"expression" : {
624+
"localId" : "16",
625+
"type" : "Greater",
626+
"operand" : [ {
627+
"localId" : "14",
628+
"name" : "FunctionWithThis",
629+
"type" : "FunctionRef",
630+
"operand" : [ {
631+
"localId" : "13",
632+
"type" : "Query",
633+
"source" : [ {
634+
"localId" : "12",
635+
"alias" : "E",
636+
"expression" : {
637+
"localId" : "11",
638+
"dataType" : "{http://hl7.org/fhir}Encounter",
639+
"templateId" : "encounter-qicore-qicore-encounter",
640+
"type" : "Retrieve"
641+
}
642+
} ],
643+
"relationship" : [ ]
644+
} ]
645+
}, {
646+
"localId" : "15",
647+
"valueType" : "{urn:hl7-org:elm-types:r1}Integer",
648+
"value" : "0",
649+
"type" : "Literal"
650+
} ]
651+
}
652+
} ]
653+
}
654+
}
655+
}
656+
402657
### IncludesQuery
403658
library TestSnippet version '1'
404659
using QUICK

test/elm/query/data.cql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ define EncountersDuringMP: [Encounter] E where E.period during MeasurementPeriod
55
define AmbulatoryEncountersDuringMP: [Encounter: "Ambulatory/ED Visit"] E where E.period during MeasurementPeriod
66
define AmbulatoryEncountersIncludedInMP: [Encounter: "Ambulatory/ED Visit"] E where E.period included in MeasurementPeriod
77

8+
// @Test: FunctionQuery
9+
define function "FunctionWithThis"(Encounter List<"Encounter">): Count(Encounter.period EncounterPeriod return EncounterPeriod)
10+
define queryWithThis: "FunctionWithThis"([Encounter] E) > 0
11+
812
// @Test: IncludesQuery
913
valueset "Ambulatory/ED Visit": '2.16.840.1.113883.3.464.1003.101.12.1061'
1014
parameter MeasurementPeriod default Interval[DateTime(2013, 1, 1), DateTime(2014, 1, 1))

test/elm/query/test.coffee

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ describe 'DateRangeOptimizedQuery', ->
2424
e.should.have.length(1)
2525
e[0].id().should.equal 'http://cqframework.org/3/5'
2626

27+
describe 'FunctionQuery', ->
28+
@beforeEach ->
29+
setup @, data, [ p1 ], vsets
30+
31+
it 'function with this' , ->
32+
functionReturnsDates = @queryWithThis.exec(@ctx)
33+
functionReturnsDates.should.eql true
34+
2735
describe.skip 'IncludesQuery', ->
2836
@beforeEach ->
2937
setup @, data, [ p1 ], vsets

0 commit comments

Comments
 (0)