UNPKG

cql-execution

Version:

An execution framework for the Clinical Quality Language (CQL)

1,653 lines (1,298 loc) 43 kB
// Generated by CoffeeScript 1.9.3 (function() { var Add, AliasRef, And, BooleanLiteral, ByExpression, CalculateAgeAt, CalculateAgeInYearsAtFunctionRef, CodeFunctionRef, Context, DT, DateFunctionRef, DateTimeFunctionRef, DecimalLiteral, Distinct, Divide, DurationBetween, Equal, Exists, Expression, ExpressionDef, ExpressionRef, FunctionDef, FunctionRef, Greater, GreaterOrEqual, Identifier, IdentifierRef, In, InValueSet, InValueSetFunctionRef, Includes, IntegerLiteral, Intersect, Interval, IsNull, Less, LessOrEqual, Library, List, Literal, MultiSource, Multiply, Negate, Not, Null, Or, ParameterDef, ParameterRef, Patient, Property, QP, Quantity, Query, QueryDefineRef, Results, Retrieve, SingletonFrom, Sort, Start, StringLiteral, Subtract, Tuple, Union, VS, ValueSetDef, ValueSetRef, With, Without, Xor, allTrue, build, buildFunctionRef, buildLiteral, constructByName, cs, functionExists, typeIsArray, extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, hasProp = {}.hasOwnProperty, indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; Patient = require('./cql-patient').Patient; DT = require('./cql-datatypes'); QP = require('./fhir/models'); VS = require('./vsac-code-service'); cs = new VS.CodeService("rdingwell", "Test#1234", "https://vsac.nlm.nih.gov/vsac/ws/Ticket", "https://vsac.nlm.nih.gov/vsac/ws/RetrieveValueSet"); cs.getProxyTicket(); typeIsArray = Array.isArray || function(value) { return {}.toString.call(value) === '[object Array]'; }; functionExists = function(name) { return eval("typeof " + name) === "function"; }; constructByName = function(name, json) { return eval("new " + name + "(json)"); }; build = function(json) { var child; if (json == null) { return json; } if (typeIsArray(json)) { return (function() { var j, len, results; results = []; for (j = 0, len = json.length; j < len; j++) { child = json[j]; results.push(build(child)); } return results; })(); } if (json.type === "FunctionRef") { return new buildFunctionRef(json); } else if (json.type === "Literal") { return buildLiteral(json); } else if (functionExists(json.type)) { return constructByName(json.type, json); } else { return null; } }; buildFunctionRef = function(json) { if (functionExists(json.name + "FunctionRef")) { return constructByName(json.name + "FunctionRef", json); } else { return new FunctionRef(json); } }; buildLiteral = function(json) { switch (json.valueType) { case "{http://www.w3.org/2001/XMLSchema}bool": return new BooleanLiteral(json); case "{http://www.w3.org/2001/XMLSchema}int": return new IntegerLiteral(json); case "{http://www.w3.org/2001/XMLSchema}decimal": return new DecimalLiteral(json); case "{http://www.w3.org/2001/XMLSchema}string": return new StringLiteral(json); default: return new Literal(json); } }; Function.prototype.property = function(prop, desc) { return Object.defineProperty(this.prototype, prop, desc); }; Library = (function() { function Library(json) { var expr, j, k, l, len, len1, len2, param, ref, ref1, ref2, ref3, ref4, ref5, ref6, ref7, ref8, valueset; this.parameters = {}; ref2 = (ref = (ref1 = json.library.parameters) != null ? ref1.def : void 0) != null ? ref : []; for (j = 0, len = ref2.length; j < len; j++) { param = ref2[j]; this.parameters[param.name] = new ParameterDef(param); } this.valuesets = {}; ref5 = (ref3 = (ref4 = json.library.valueSets) != null ? ref4.def : void 0) != null ? ref3 : []; for (k = 0, len1 = ref5.length; k < len1; k++) { valueset = ref5[k]; this.valuesets[valueset.name] = new ValueSetDef(valueset); } this.expressions = {}; ref8 = (ref6 = (ref7 = json.library.statements) != null ? ref7.def : void 0) != null ? ref6 : []; for (l = 0, len2 = ref8.length; l < len2; l++) { expr = ref8[l]; this.expressions[expr.name] = expr.type === "FunctionDef" ? new FunctionDef(expr) : new ExpressionDef(expr); } } Library.prototype.get = function(identifier) { return this.expressions[identifier]; }; Library.prototype.getValueSet = function(identifier) { return this.valuesets[identifier]; }; Library.prototype.getParameter = function(name) { return this.parameters[name]; }; Library.prototype.exec = function(ctx) { var expr, key, p, patient_ctx, r, ref; Results(r = new Results()); while (p = ctx.currentPatient()) { patient_ctx = ctx.childContext(); ref = this.expressions; for (key in ref) { expr = ref[key]; if (expr.context === "Patient") { r.recordPatientResult(patient_ctx.currentPatient().id(), key, expr.exec(patient_ctx)); } } ctx.nextPatient(); } return r; }; return Library; })(); Context = (function() { function Context(parent, _patientSource, _codeService, _parameters) { this.parent = parent; this._patientSource = _patientSource != null ? _patientSource : null; this._codeService = _codeService != null ? _codeService : null; this._parameters = _parameters != null ? _parameters : {}; this.context_values = {}; } Context.property("parameters", { get: function() { var ref; return this._parameters || ((ref = this.parent) != null ? ref.parameters : void 0); }, set: function(params) { return this._parameters = params; } }); Context.property("patientSource", { get: function() { var ref; return this._patientSource || ((ref = this.parent) != null ? ref.patientSource : void 0); }, set: function(ps) { return this._patientSource = ps; } }); Context.property("codeService", { get: function() { var ref; return this._codeService || ((ref = this.parent) != null ? ref.codeService : void 0); }, set: function(cs) { return this._codeService = cs; } }); Context.prototype.withPatients = function(patientSource) { this.patientSource = patientSource; return this; }; Context.prototype.withParameters = function(params) { this.parameters = params != null ? params : {}; return this; }; Context.prototype.withCodeService = function(cs) { this.codeService = cs; return this; }; Context.prototype.rootContext = function() { var ref; if (this.parent instanceof Library) { return this; } else { return (ref = this.parent) != null ? ref.rootContext() : void 0; } }; Context.prototype.childContext = function(context_values) { var ctx; if (context_values == null) { context_values = {}; } ctx = new Context(this); ctx.context_values = context_values; return ctx; }; Context.prototype.getParameter = function(name) { var ref; return (ref = this.parent) != null ? ref.getParameter(name) : void 0; }; Context.prototype.getValueSet = function(name) { var ref; return (ref = this.parent) != null ? ref.getValueSet(name) : void 0; }; Context.prototype.get = function(identifier) { var ref; return this.context_values[identifier] || ((ref = this.parent) != null ? ref.get(identifier) : void 0); }; Context.prototype.set = function(identifier, value) { return this.context_values[identifier] = value; }; Context.prototype.currentPatient = function() { return this.patientSource.currentPatient(); }; Context.prototype.nextPatient = function() { return this.patientSource.nextPatient(); }; return Context; })(); Results = (function() { function Results() { this.patientResults = {}; this.populationResults = {}; } Results.prototype.recordPatientResult = function(patientId, resultName, result) { var base; if ((base = this.patientResults)[patientId] == null) { base[patientId] = {}; } return this.patientResults[patientId][resultName] = result; }; Results.prototype.recordPopulationResult = function(resultName, result) { return this.populationResults[resultName] = result; }; return Results; })(); Expression = (function() { function Expression(json) { var op; if (json.operand != null) { op = build(json.operand); if (typeIsArray(json.operand)) { this.args = op; } else { this.arg = op; } } } Expression.prototype.exec = function(ctx) { return this; }; Expression.prototype.execArgs = function(ctx) { var arg, j, len, ref, results; switch (false) { case this.args == null: ref = this.args; results = []; for (j = 0, len = ref.length; j < len; j++) { arg = ref[j]; results.push(arg.exec(ctx)); } return results; case this.arg == null: return this.arg.exec(ctx); default: return null; } }; return Expression; })(); ExpressionDef = (function(superClass) { extend(ExpressionDef, superClass); function ExpressionDef(json) { ExpressionDef.__super__.constructor.apply(this, arguments); this.name = json.name; this.context = json.context; this.expression = build(json.expression); } ExpressionDef.prototype.exec = function(ctx) { var ref, value; value = (ref = this.expression) != null ? ref.exec(ctx) : void 0; ctx.rootContext().set(this.name, value); return value; }; return ExpressionDef; })(Expression); ExpressionRef = (function(superClass) { extend(ExpressionRef, superClass); function ExpressionRef(json) { ExpressionRef.__super__.constructor.apply(this, arguments); this.name = json.name; } ExpressionRef.prototype.exec = function(ctx) { var value; value = ctx.get(this.name); if (value instanceof Expression) { value = value.exec(ctx); } return value; }; return ExpressionRef; })(Expression); ValueSetDef = (function(superClass) { extend(ValueSetDef, superClass); function ValueSetDef(json) { ValueSetDef.__super__.constructor.apply(this, arguments); this.name = json.name; this.id = json.id; this.version = json.version; } ValueSetDef.prototype.exec = function(ctx) { var ref, valueset; valueset = (ref = ctx.codeService.findValueSet(this.id, this.version)) != null ? ref : new DT.ValueSet(this.id, this.version); ctx.rootContext().set(this.name, valueset); return valueset; }; return ValueSetDef; })(Expression); ValueSetRef = (function(superClass) { extend(ValueSetRef, superClass); function ValueSetRef(json) { ValueSetRef.__super__.constructor.apply(this, arguments); this.name = json.name; } ValueSetRef.prototype.exec = function(ctx) { var valueset; valueset = ctx.getValueSet(this.name); if (valueset instanceof Expression) { valueset = valueset.exec(ctx); } return valueset; }; return ValueSetRef; })(Expression); ParameterDef = (function(superClass) { extend(ParameterDef, superClass); function ParameterDef(json) { ParameterDef.__super__.constructor.apply(this, arguments); this.name = json.name; this["default"] = build(json["default"]); } ParameterDef.prototype.exec = function(ctx) { var ref; if (((ctx != null ? ctx.parameters[this.name] : void 0) != null)) { return ctx.parameters[this.name]; } else { return (ref = this["default"]) != null ? ref.exec(ctx) : void 0; } }; return ParameterDef; })(Expression); ParameterRef = (function(superClass) { extend(ParameterRef, superClass); function ParameterRef(json) { ParameterRef.__super__.constructor.apply(this, arguments); this.name = json.name; } ParameterRef.prototype.exec = function(ctx) { var ref; return (ref = ctx.getParameter(this.name)) != null ? ref.exec(ctx) : void 0; }; return ParameterRef; })(Expression); And = (function(superClass) { extend(And, superClass); function And(json) { And.__super__.constructor.apply(this, arguments); } And.prototype.exec = function(ctx) { var ref; return (ref = DT.ThreeValuedLogic).and.apply(ref, this.execArgs(ctx)); }; return And; })(Expression); Or = (function(superClass) { extend(Or, superClass); function Or(json) { Or.__super__.constructor.apply(this, arguments); } Or.prototype.exec = function(ctx) { var ref; return (ref = DT.ThreeValuedLogic).or.apply(ref, this.execArgs(ctx)); }; return Or; })(Expression); Xor = (function(superClass) { extend(Xor, superClass); function Xor(json) { Xor.__super__.constructor.apply(this, arguments); } Xor.prototype.exec = function(ctx) { var ref; return (ref = DT.ThreeValuedLogic).xor.apply(ref, this.execArgs(ctx)); }; return Xor; })(Expression); Not = (function(superClass) { extend(Not, superClass); function Not(json) { Not.__super__.constructor.apply(this, arguments); } Not.prototype.exec = function(ctx) { return DT.ThreeValuedLogic.not(this.execArgs(ctx)); }; return Not; })(Expression); IsNull = (function(superClass) { extend(IsNull, superClass); function IsNull(json) { IsNull.__super__.constructor.apply(this, arguments); } IsNull.prototype.exec = function(ctx) { return this.execArgs(ctx) === null; }; return IsNull; })(Expression); FunctionDef = (function(superClass) { extend(FunctionDef, superClass); function FunctionDef(json) { FunctionDef.__super__.constructor.apply(this, arguments); this.name = json.name; this.expression = build(json.expression); this.parameters = json.parameter; } FunctionDef.prototype.exec = function(ctx) { return this; }; return FunctionDef; })(Expression); FunctionRef = (function(superClass) { extend(FunctionRef, superClass); function FunctionRef(json) { FunctionRef.__super__.constructor.apply(this, arguments); this.name = json.name; } FunctionRef.prototype.exec = function(ctx) { var args, child_ctx, functionDef, i, j, len, p, ref; functionDef = ctx.get(this.name); args = this.execArgs(ctx); child_ctx = ctx.childContext(); if (args.length !== functionDef.parameters.length) { thow("incorrect number of arguments supplied"); } ref = functionDef.parameters; for (i = j = 0, len = ref.length; j < len; i = ++j) { p = ref[i]; child_ctx.set(p.name, args[i]); } return functionDef.expression.exec(child_ctx); }; return FunctionRef; })(Expression); CodeFunctionRef = (function(superClass) { extend(CodeFunctionRef, superClass); function CodeFunctionRef(json) { CodeFunctionRef.__super__.constructor.apply(this, arguments); } CodeFunctionRef.prototype.exec = function(ctx) { return (function(func, args, ctor) { ctor.prototype = func.prototype; var child = new ctor, result = func.apply(child, args); return Object(result) === result ? result : child; })(DT.Code, this.execArgs(ctx), function(){}); }; return CodeFunctionRef; })(FunctionRef); InValueSetFunctionRef = (function(superClass) { extend(InValueSetFunctionRef, superClass); function InValueSetFunctionRef(json) { InValueSetFunctionRef.__super__.constructor.apply(this, arguments); } InValueSetFunctionRef.prototype.exec = function(ctx) { var args, item, ref, valueSet; args = this.execArgs(ctx); item = args[0]; valueSet = (ref = ctx.codeService).findValueSet.apply(ref, args.slice(1)); if (valueSet != null) { return valueSet.hasCode(item); } else { return false; } }; return InValueSetFunctionRef; })(FunctionRef); DateFunctionRef = (function(superClass) { extend(DateFunctionRef, superClass); function DateFunctionRef(json) { DateFunctionRef.__super__.constructor.apply(this, arguments); } DateFunctionRef.prototype.exec = function(ctx) { return (function(func, args, ctor) { ctor.prototype = func.prototype; var child = new ctor, result = func.apply(child, args); return Object(result) === result ? result : child; })(DT.DateTime, this.execArgs(ctx), function(){}); }; return DateFunctionRef; })(FunctionRef); DateTimeFunctionRef = (function(superClass) { extend(DateTimeFunctionRef, superClass); function DateTimeFunctionRef(json) { DateTimeFunctionRef.__super__.constructor.apply(this, arguments); } return DateTimeFunctionRef; })(DateFunctionRef); Greater = (function(superClass) { extend(Greater, superClass); function Greater(json) { Greater.__super__.constructor.apply(this, arguments); } Greater.prototype.exec = function(ctx) { var args; args = this.execArgs(ctx).map(function(x) { return DT.Uncertainty.from(x); }); return args[0].greaterThan(args[1]); }; return Greater; })(Expression); GreaterOrEqual = (function(superClass) { extend(GreaterOrEqual, superClass); function GreaterOrEqual(json) { GreaterOrEqual.__super__.constructor.apply(this, arguments); } GreaterOrEqual.prototype.exec = function(ctx) { var args; args = this.execArgs(ctx).map(function(x) { return DT.Uncertainty.from(x); }); return args[0].greaterThanOrEquals(args[1]); }; return GreaterOrEqual; })(Expression); Equal = (function(superClass) { extend(Equal, superClass); function Equal(json) { Equal.__super__.constructor.apply(this, arguments); } Equal.prototype.exec = function(ctx) { var args; args = this.execArgs(ctx).map(function(x) { return DT.Uncertainty.from(x); }); return args[0].equals(args[1]); }; return Equal; })(Expression); LessOrEqual = (function(superClass) { extend(LessOrEqual, superClass); function LessOrEqual(json) { LessOrEqual.__super__.constructor.apply(this, arguments); } LessOrEqual.prototype.exec = function(ctx) { var args; args = this.execArgs(ctx).map(function(x) { return DT.Uncertainty.from(x); }); return args[0].lessThanOrEquals(args[1]); }; return LessOrEqual; })(Expression); Less = (function(superClass) { extend(Less, superClass); function Less(json) { Less.__super__.constructor.apply(this, arguments); } Less.prototype.exec = function(ctx) { var args; args = this.execArgs(ctx).map(function(x) { return DT.Uncertainty.from(x); }); return args[0].lessThan(args[1]); }; return Less; })(Expression); List = (function(superClass) { extend(List, superClass); function List(json) { var ref; List.__super__.constructor.apply(this, arguments); this.elements = (ref = build(json.element)) != null ? ref : []; } List.prototype.exec = function(ctx) { var item, j, len, ref, results; ref = this.elements; results = []; for (j = 0, len = ref.length; j < len; j++) { item = ref[j]; results.push(item.exec(ctx)); } return results; }; return List; })(Expression); Exists = (function(superClass) { extend(Exists, superClass); function Exists(json) { Exists.__super__.constructor.apply(this, arguments); } Exists.prototype.exec = function(ctx) { var ref; return ((ref = this.execArgs(ctx)) != null ? ref.length : void 0) > 0; }; return Exists; })(Expression); Interval = (function(superClass) { extend(Interval, superClass); function Interval(json) { Interval.__super__.constructor.apply(this, arguments); this.lowClosed = json.lowClosed; this.highClosed = json.highClosed; this.low = build(json.low); this.high = build(json.high); } Interval.prototype.exec = function(ctx) { return new DT.Interval(this.low.exec(ctx), this.high.exec(ctx), this.lowClosed, this.highClosed); }; return Interval; })(Expression); Includes = (function(superClass) { extend(Includes, superClass); function Includes(json) { Includes.__super__.constructor.apply(this, arguments); } Includes.prototype.exec = function(ctx) { var args; args = this.execArgs(ctx); return args[0].includes(args[1]); }; return Includes; })(Expression); Identifier = (function(superClass) { extend(Identifier, superClass); function Identifier(json) { Identifier.__super__.constructor.apply(this, arguments); this.identifier = json; } Identifier.prototype.exec = function(ctx) { return ctx.get(this.identifier); }; return Identifier; })(Expression); Start = (function(superClass) { extend(Start, superClass); function Start(json) { Start.__super__.constructor.apply(this, arguments); } Start.prototype.exec = function(ctx) { return this.arg.exec(ctx).low; }; return Start; })(Expression); Union = (function(superClass) { extend(Union, superClass); function Union(json) { Union.__super__.constructor.apply(this, arguments); } Union.prototype.exec = function(ctx) { return this.execArgs(ctx).reduce(function(x, y) { return x.concat(y); }); }; return Union; })(Expression); Intersect = (function(superClass) { extend(Intersect, superClass); function Intersect(json) { Intersect.__super__.constructor.apply(this, arguments); } Intersect.prototype.exec = function(ctx) { return this.execArgs(ctx).reduce(function(x, y) { var itm, j, len, results; results = []; for (j = 0, len = x.length; j < len; j++) { itm = x[j]; if (indexOf.call(y, itm) >= 0) { results.push(itm); } } return results; }); }; return Intersect; })(Expression); Distinct = (function(superClass) { extend(Distinct, superClass); function Distinct(json) { Distinct.__super__.constructor.apply(this, arguments); this.source = build(json.source); } Distinct.prototype.exec = function(ctx) { var container, itm, j, key, len, ref, results, value; container = {}; ref = this.source.exec(ctx); for (j = 0, len = ref.length; j < len; j++) { itm = ref[j]; container[itm] = itm; } results = []; for (key in container) { value = container[key]; results.push(value); } return results; }; return Distinct; })(Expression); SingletonFrom = (function(superClass) { extend(SingletonFrom, superClass); function SingletonFrom(json) { SingletonFrom.__super__.constructor.apply(this, arguments); } SingletonFrom.prototype.exec = function(ctx) { var arg; arg = this.execArgs(ctx); if (arg.length > 1) { throw new Error('IllegalArgument: \'SingletonFrom\' requires a 0 or 1 arg array'); } else if (arg.length === 1) { return arg[0]; } else { return null; } }; return SingletonFrom; })(Expression); In = (function(superClass) { extend(In, superClass); function In(json) { In.__super__.constructor.apply(this, arguments); } In.prototype.exec = function(ctx) { var container, item, ref; ref = this.execArgs(ctx), item = ref[0], container = ref[1]; switch (false) { case !typeIsArray(container): return indexOf.call(container, item) >= 0; case !(container instanceof DT.ValueSet): return container.hasCode(item); } }; return In; })(Expression); InValueSet = (function(superClass) { extend(InValueSet, superClass); function InValueSet(json) { InValueSet.__super__.constructor.apply(this, arguments); this.code = build(json.code); this.valueset = new ValueSetRef(json.valueset); } InValueSet.prototype.exec = function(ctx) { var code, valueset; code = this.code.exec(ctx); valueset = this.valueset.exec(ctx); if ((code != null) && (valueset != null)) { return valueset.hasCode(code); } else { return false; } }; return InValueSet; })(Expression); Add = (function(superClass) { extend(Add, superClass); function Add(json) { Add.__super__.constructor.apply(this, arguments); } Add.prototype.exec = function(ctx) { return this.execArgs(ctx).reduce(function(x, y) { return x + y; }); }; return Add; })(Expression); Subtract = (function(superClass) { extend(Subtract, superClass); function Subtract(json) { Subtract.__super__.constructor.apply(this, arguments); } Subtract.prototype.exec = function(ctx) { return this.execArgs(ctx).reduce(function(x, y) { return x - y; }); }; return Subtract; })(Expression); Multiply = (function(superClass) { extend(Multiply, superClass); function Multiply(json) { Multiply.__super__.constructor.apply(this, arguments); } Multiply.prototype.exec = function(ctx) { return this.execArgs(ctx).reduce(function(x, y) { return x * y; }); }; return Multiply; })(Expression); Divide = (function(superClass) { extend(Divide, superClass); function Divide(json) { Divide.__super__.constructor.apply(this, arguments); } Divide.prototype.exec = function(ctx) { return this.execArgs(ctx).reduce(function(x, y) { return x / y; }); }; return Divide; })(Expression); Negate = (function(superClass) { extend(Negate, superClass); function Negate(json) { Negate.__super__.constructor.apply(this, arguments); } Negate.prototype.exec = function(ctx) { return this.execArgs(ctx) * -1; }; return Negate; })(Expression); DurationBetween = (function(superClass) { extend(DurationBetween, superClass); function DurationBetween(json) { DurationBetween.__super__.constructor.apply(this, arguments); this.precision = json.precision; } DurationBetween.prototype.exec = function(ctx) { var args, ref, result; args = this.execArgs(ctx); result = args[0].durationBetween(args[1], (ref = this.precision) != null ? ref.toLowerCase() : void 0); if (result.isPoint()) { return result.low; } else { return result; } }; return DurationBetween; })(Expression); CalculateAgeAt = (function(superClass) { extend(CalculateAgeAt, superClass); function CalculateAgeAt(json) { CalculateAgeAt.__super__.constructor.apply(this, arguments); this.precision = json.precision; } CalculateAgeAt.prototype.exec = function(ctx) { var ageInMS, args, date0, date1, divisor; args = this.execArgs(ctx); date0 = args[0].toJSDate().getTime(); date1 = args[1].toJSDate().getTime(); ageInMS = date1 - date0; divisor = (function() { switch (this.precision) { case 'Year': return 1000 * 60 * 60 * 24 * 365; case 'Month': return (1000 * 60 * 60 * 24 * 365) / 12; case 'Day': return 1000 * 60 * 60 * 24; case 'Hour': return 1000 * 60 * 60; case 'Minute': return 1000 * 60; case 'Second': return 1000; default: return 1; } }).call(this); return Math.floor(ageInMS / divisor); }; return CalculateAgeAt; })(FunctionRef); CalculateAgeInYearsAtFunctionRef = (function(superClass) { extend(CalculateAgeInYearsAtFunctionRef, superClass); function CalculateAgeInYearsAtFunctionRef(json1) { this.json = json1; this.json.precision = "Year"; CalculateAgeInYearsAtFunctionRef.__super__.constructor.call(this, this.json); } return CalculateAgeInYearsAtFunctionRef; })(CalculateAgeAt); Literal = (function(superClass) { extend(Literal, superClass); function Literal(json) { Literal.__super__.constructor.apply(this, arguments); this.valueType = json.valueType; this.value = json.value; } Literal.prototype.exec = function(ctx) { return this.value; }; return Literal; })(Expression); BooleanLiteral = (function(superClass) { extend(BooleanLiteral, superClass); function BooleanLiteral(json) { BooleanLiteral.__super__.constructor.apply(this, arguments); this.value = this.value === 'true'; } BooleanLiteral.prototype.exec = function(ctx) { return this.value; }; return BooleanLiteral; })(Literal); IntegerLiteral = (function(superClass) { extend(IntegerLiteral, superClass); function IntegerLiteral(json) { IntegerLiteral.__super__.constructor.apply(this, arguments); this.value = parseInt(this.value, 10); } IntegerLiteral.prototype.exec = function(ctx) { return this.value; }; return IntegerLiteral; })(Literal); DecimalLiteral = (function(superClass) { extend(DecimalLiteral, superClass); function DecimalLiteral(json) { DecimalLiteral.__super__.constructor.apply(this, arguments); this.value = parseFloat(this.value); } DecimalLiteral.prototype.exec = function(ctx) { return this.value; }; return DecimalLiteral; })(Literal); StringLiteral = (function(superClass) { extend(StringLiteral, superClass); function StringLiteral(json) { StringLiteral.__super__.constructor.apply(this, arguments); } StringLiteral.prototype.exec = function(ctx) { return this.value; }; return StringLiteral; })(Literal); Null = (function(superClass) { extend(Null, superClass); function Null(json) { Null.__super__.constructor.apply(this, arguments); } Null.prototype.exec = function(ctx) { return null; }; return Null; })(Literal); Quantity = (function(superClass) { extend(Quantity, superClass); function Quantity(json) { Quantity.__super__.constructor.apply(this, arguments); this.unit = json.unit; this.value = json.value; } Quantity.prototype.exec = function(ctx) { return this; }; return Quantity; })(Expression); IdentifierRef = (function(superClass) { extend(IdentifierRef, superClass); function IdentifierRef(json) { IdentifierRef.__super__.constructor.apply(this, arguments); this.name = json.name; } IdentifierRef.prototype.exec = function(ctx) { return ctx.get(this.name); }; return IdentifierRef; })(Expression); Property = (function(superClass) { extend(Property, superClass); function Property(json) { Property.__super__.constructor.apply(this, arguments); this.scope = json.scope; this.source = build(json.source); this.path = json.path; } Property.prototype.exec = function(ctx) { var _obj, curr_obj, curr_val, j, len, obj, part, parts, ref, ref1, val; obj = this.scope != null ? ctx.get(this.scope) : this.source; if (obj instanceof Expression) { obj = obj.exec(ctx); } val = (ref = obj != null ? obj[this.path] : void 0) != null ? ref : obj != null ? typeof obj.get === "function" ? obj.get(this.path) : void 0 : void 0; if (!val) { parts = this.path.split("."); curr_obj = obj; curr_val = null; for (j = 0, len = parts.length; j < len; j++) { part = parts[j]; _obj = (ref1 = curr_obj != null ? curr_obj[part] : void 0) != null ? ref1 : curr_obj != null ? typeof curr_obj.get === "function" ? curr_obj.get(part) : void 0 : void 0; curr_obj = _obj instanceof Function ? _obj.call(curr_obj) : _obj; } val = curr_obj; } if (val instanceof Function) { return val.call(obj); } else { return val; } }; return Property; })(Expression); Tuple = (function(superClass) { extend(Tuple, superClass); function Tuple(json) { var el; Tuple.__super__.constructor.apply(this, arguments); this.elements = (function() { var j, len, ref, results; ref = json.element; results = []; for (j = 0, len = ref.length; j < len; j++) { el = ref[j]; results.push({ name: el.name, value: build(el.value) }); } return results; })(); } Tuple.prototype.exec = function(ctx) { var el, j, len, ref, ref1, val; val = {}; ref = this.elements; for (j = 0, len = ref.length; j < len; j++) { el = ref[j]; val[el.name] = (ref1 = el.value) != null ? ref1.exec(ctx) : void 0; } return val; }; return Tuple; })(Expression); Retrieve = (function(superClass) { extend(Retrieve, superClass); function Retrieve(json) { Retrieve.__super__.constructor.apply(this, arguments); this.datatype = json.dataType; this.templateId = json.templateId; this.codeProperty = json.codeProperty; this.codes = build(json.codes); this.dateProperty = json.dateProperty; this.dateRange = build(json.dateRange); } Retrieve.prototype.exec = function(ctx) { var r, range, records, ref, valueset; records = (ref = ctx.currentPatient()) != null ? ref.findRecords(this.templateId) : void 0; if (this.codes) { valueset = this.codes.exec(ctx); records = (function() { var j, len, results; results = []; for (j = 0, len = records.length; j < len; j++) { r = records[j]; if (valueset.hasCode(r.getCode(this.codeProperty))) { results.push(r); } } return results; }).call(this); } if (this.dateRange) { range = this.dateRange.exec(ctx); records = (function() { var j, len, results; results = []; for (j = 0, len = records.length; j < len; j++) { r = records[j]; if (range.includes(r.getDateOrInterval(this.dateProperty))) { results.push(r); } } return results; }).call(this); } return records; }; return Retrieve; })(Expression); AliasRef = (function(superClass) { extend(AliasRef, superClass); function AliasRef(json) { AliasRef.__super__.constructor.apply(this, arguments); this.name = json.name; } AliasRef.prototype.exec = function(ctx) { return ctx != null ? ctx.get(this.name) : void 0; }; return AliasRef; })(Expression); QueryDefineRef = (function(superClass) { extend(QueryDefineRef, superClass); function QueryDefineRef(json) { QueryDefineRef.__super__.constructor.apply(this, arguments); } return QueryDefineRef; })(AliasRef); With = (function(superClass) { extend(With, superClass); function With(json) { With.__super__.constructor.apply(this, arguments); this.alias = json.alias; this.expression = build(json.expression); this.suchThat = build(json.suchThat); } With.prototype.exec = function(ctx) { var childCtx, rec, records, returns; records = this.expression.exec(ctx); returns = (function() { var j, len, results; results = []; for (j = 0, len = records.length; j < len; j++) { rec = records[j]; childCtx = ctx.childContext(); childCtx.set(this.alias, rec); results.push(this.suchThat.exec(childCtx)); } return results; }).call(this); return returns.some(function(x) { return x; }); }; return With; })(Expression); Without = (function(superClass) { extend(Without, superClass); function Without(json) { Without.__super__.constructor.apply(this, arguments); } Without.prototype.exec = function(ctx) { return !Without.__super__.exec.call(this, ctx); }; return Without; })(With); ByExpression = (function(superClass) { extend(ByExpression, superClass); function ByExpression(json) { ByExpression.__super__.constructor.apply(this, arguments); this.expression = build(json.expression); this.direction = json.direction; this.low_order = this.direction === "asc" ? -1 : 1; this.high_order = this.low_order * -1; } ByExpression.prototype.exec = function(a, b) { var a_val, b_val, ctx; ctx = new Context(); ctx.context_values = a; a_val = this.expression.exec(ctx); ctx.context_values = b; b_val = this.expression.exec(ctx); if (a_val === b_val) { return 0; } else if (a_val < b_val) { return this.low_order; } else { return this.high_order; } }; return ByExpression; })(Expression); Sort = (function() { function Sort(json) { this.by = build(json != null ? json.by : void 0); } Sort.prototype.sort = function(values) { var self; self = this; if (this.by) { return values.sort(function(a, b) { var item, j, len, order, ref; order = 0; ref = self.by; for (j = 0, len = ref.length; j < len; j++) { item = ref[j]; order = item.exec(a, b); if (order !== 0) { break; } } return order; }); } }; return Sort; })(); MultiSource = (function() { function MultiSource(sources) { this.sources = sources; this.sources = typeIsArray(this.sources) ? this.sources : [this.sources]; this.alias = this.sources[0].alias; this.expression = build(this.sources[0].expression); if (this.sources.length > 1) { this.rest = new MultiSource(this.sources.slice(1)); } } MultiSource.prototype.aliases = function() { var a; a = [this.alias]; if (this.rest) { a = a.concat(this.rest.aliases()); } return a; }; MultiSource.prototype.forEach = function(ctx, func) { var j, len, rctx, rec, ref, results; if (this.records == null) { this.records = this.expression.exec(ctx); } ref = this.records; results = []; for (j = 0, len = ref.length; j < len; j++) { rec = ref[j]; rctx = new Context(ctx); rctx.set(this.alias, rec); if (this.rest) { results.push(this.rest.forEach(rctx, func)); } else { results.push(func(rctx)); } } return results; }; return MultiSource; })(); allTrue = function(things) { if (typeIsArray(things)) { return things.every(function(x) { return x; }); } else { return things; } }; Query = (function(superClass) { extend(Query, superClass); function Query(json) { var d, ref; Query.__super__.constructor.apply(this, arguments); this.sources = new MultiSource(json.source); this.definitions = (function() { var j, len, ref, ref1, results; ref1 = (ref = json.define) != null ? ref : []; results = []; for (j = 0, len = ref1.length; j < len; j++) { d = ref1[j]; results.push({ identifier: d.identifier, expression: build(d.expression) }); } return results; })(); this.relationship = build(json.relationship); this.where = build(json.where); this["return"] = build((ref = json["return"]) != null ? ref.expression : void 0); this.aliases = this.sources.aliases(); this.sort = new Sort(json.sort); } Query.prototype.exec = function(ctx) { var ref, returnedValues, self; self = this; returnedValues = []; this.sources.forEach(ctx, function(rctx) { var child_ctx, def, j, len, passed, ref, rel, relations, val; ref = self.definitions; for (j = 0, len = ref.length; j < len; j++) { def = ref[j]; rctx.set(def.identifier, def.expression.exec(rctx)); } relations = (function() { var k, len1, ref1, results; ref1 = self.relationship; results = []; for (k = 0, len1 = ref1.length; k < len1; k++) { rel = ref1[k]; child_ctx = rctx.childContext(); results.push(rel.exec(child_ctx)); } return results; })(); passed = allTrue(relations); passed = passed && (self.where ? self.where.exec(rctx) : passed); if (passed) { if (self["return"]) { val = self["return"].exec(rctx); if (returnedValues.indexOf(val) === -1) { return returnedValues.push(val); } } else { if (self.aliases.length === 1) { return returnedValues.push(rctx.get(self.aliases[0])); } else { return returnedValues.push(rctx.context_values); } } } }); if ((ref = this.sort) != null) { ref.sort(returnedValues); } return returnedValues; }; return Query; })(Expression); module.exports.Library = Library; module.exports.Context = Context; module.exports.Results = Results; }).call(this); //# sourceMappingURL=cql-exec.js.map