UNPKG

cql-execution

Version:

An execution framework for the Clinical Quality Language (CQL)

445 lines (383 loc) 16 kB
// Generated by CoffeeScript 1.12.7 (function() { var Context, Exception, Library, PatientContext, PopulationContext, dt, typeIsArray, util, 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; Library = require('../elm/library').Library; Exception = require('../datatypes/exception').Exception; typeIsArray = require('../util/util').typeIsArray; dt = require('../datatypes/datatypes'); util = require('util'); Function.prototype.property = function(prop, desc) { return Object.defineProperty(this.prototype, prop, desc); }; module.exports.Context = Context = (function() { function Context(parent, _codeService, _parameters) { this.parent = parent; this._codeService = _codeService != null ? _codeService : null; if (_parameters == null) { _parameters = {}; } this.context_values = {}; this.library_context = {}; this.localId_context = {}; this.checkParameters(_parameters); this._parameters = _parameters; } Context.property("parameters", { get: function() { var ref; return this._parameters || ((ref = this.parent) != null ? ref.parameters : void 0); }, set: function(params) { this.checkParameters(params); return this._parameters = params; } }); 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.withParameters = function(params) { this.parameters = params != null ? params : {}; return this; }; Context.prototype.withCodeService = function(cs) { this.codeService = cs; return this; }; Context.prototype.rootContext = function() { if (this.parent) { return this.parent.rootContext(); } else { return this; } }; Context.prototype.findRecords = function(profile) { var ref; return (ref = this.parent) != null ? ref.findRecords(profile) : 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.getLibraryContext = function(library) { var ref; return (ref = this.parent) != null ? ref.getLibraryContext(library) : void 0; }; Context.prototype.getLocalIdContext = function(localId) { var ref; return (ref = this.parent) != null ? ref.getLocalIdContext(localId) : void 0; }; Context.prototype.getParameter = function(name) { var ref; return (ref = this.parent) != null ? ref.getParameter(name) : void 0; }; Context.prototype.getParentParameter = function(name) { var ref; if (((ref = this.parent) != null ? ref.parameters[name] : void 0) != null) { return this.parent.parameters[name]; } else if (this.parent != null) { return this.parent.getParentParameter(name); } }; Context.prototype.getTimezoneOffset = function() { var ref; if (this.executionDateTime != null) { return this.executionDateTime.timezoneOffset; } else if (((ref = this.parent) != null ? ref.getTimezoneOffset : void 0) != null) { return this.parent.getTimezoneOffset(); } else { throw new Exception("No Timezone Offset has been set"); } }; Context.prototype.getExecutionDateTime = function() { var ref; if (this.executionDateTime != null) { return this.executionDateTime; } else if (((ref = this.parent) != null ? ref.getExecutionDateTime : void 0) != null) { return this.parent.getExecutionDateTime(); } else { throw new Exception("No Execution DateTime has been set"); } }; Context.prototype.getValueSet = function(name, library) { var ref; return (ref = this.parent) != null ? ref.getValueSet(name, library) : void 0; }; Context.prototype.getCodeSystem = function(name) { var ref; return (ref = this.parent) != null ? ref.getCodeSystem(name) : void 0; }; Context.prototype.getCode = function(name) { var ref; return (ref = this.parent) != null ? ref.getCode(name) : void 0; }; Context.prototype.getConcept = function(name) { var ref; return (ref = this.parent) != null ? ref.getConcept(name) : void 0; }; Context.prototype.get = function(identifier) { var ref; if (typeof this.context_values[identifier] !== 'undefined') { return this.context_values[identifier]; } else if (identifier === "$this") { return this.context_values; } else { return (ref = this.parent) != null ? ref.get(identifier) : void 0; } }; Context.prototype.set = function(identifier, value) { return this.context_values[identifier] = value; }; Context.prototype.setLocalIdWithResult = function(localId, value) { var ctx; ctx = this.localId_context[localId]; if (ctx === false || ctx === null || ctx === void 0 || ctx.length === 0) { return this.localId_context[localId] = value; } else { return ctx; } }; Context.prototype.getLocalIdResult = function(localId) { return this.localId_context[localId]; }; Context.prototype.getAllLocalIds = function() { var lib, libName, localIdResults, ref; localIdResults = {}; localIdResults[this.parent.source.library.identifier.id] = {}; localIdResults[this.parent.source.library.identifier.id] = this.localId_context; ref = this.library_context; for (libName in ref) { lib = ref[libName]; this.supportLibraryLocalIds(lib, localIdResults); } return localIdResults; }; Context.prototype.supportLibraryLocalIds = function(lib, localIdResults) { var ref, results, supportLib, supportLibName; if (localIdResults[lib.library.source.library.identifier.id] != null) { this.mergeLibraryLocalIdResults(localIdResults, lib.library.source.library.identifier.id, lib.localId_context); } else { localIdResults[lib.library.source.library.identifier.id] = lib.localId_context; } ref = lib.library_context; results = []; for (supportLibName in ref) { supportLib = ref[supportLibName]; results.push(this.supportLibraryLocalIds(supportLib, localIdResults)); } return results; }; Context.prototype.mergeLibraryLocalIdResults = function(localIdResults, libraryId, libraryResults) { var existingResult, localId, localIdResult, results; results = []; for (localId in libraryResults) { localIdResult = libraryResults[localId]; existingResult = localIdResults[libraryId][localId]; if (existingResult === false || existingResult === null || existingResult === void 0 || existingResult.length === 0) { results.push(localIdResults[libraryId][localId] = localIdResult); } else { results.push(void 0); } } return results; }; Context.prototype.checkParameters = function(params) { var pDef, pName, pVal; for (pName in params) { pVal = params[pName]; pDef = this.getParameter(pName); if (pVal == null) { return; } if (typeof pDef === "undefined") { return; } else if ((pDef.parameterTypeSpecifier != null) && !this.matchesTypeSpecifier(pVal, pDef.parameterTypeSpecifier)) { throw new Error("Passed in parameter '" + pName + "' is wrong type"); } else if ((pDef['default'] != null) && !this.matchesInstanceType(pVal, pDef['default'])) { throw new Error("Passed in parameter '" + pName + "' is wrong type"); } } return true; }; Context.prototype.matchesTypeSpecifier = function(val, spec) { switch (spec.type) { case "NamedTypeSpecifier": return this.matchesNamedTypeSpecifier(val, spec); case "ListTypeSpecifier": return this.matchesListTypeSpecifier(val, spec); case "TupleTypeSpecifier": return this.matchesTupleTypeSpecifier(val, spec); case "IntervalTypeSpecifier": return this.matchesIntervalTypeSpecifier(val, spec); default: return true; } }; Context.prototype.matchesListTypeSpecifier = function(val, spec) { return typeIsArray(val) && val.every((function(_this) { return function(x) { return _this.matchesTypeSpecifier(x, spec.elementType); }; })(this)); }; Context.prototype.matchesTupleTypeSpecifier = function(val, spec) { return typeof val === "object" && !typeIsArray(val) && spec.element.every((function(_this) { return function(x) { return typeof val[x.name] === "undefined" || _this.matchesTypeSpecifier(val[x.name], x.elementType); }; })(this)); }; Context.prototype.matchesIntervalTypeSpecifier = function(val, spec) { return val.isInterval && ((val.low == null) || this.matchesTypeSpecifier(val.low, spec.pointType)) && ((val.high == null) || this.matchesTypeSpecifier(val.high, spec.pointType)); }; Context.prototype.matchesNamedTypeSpecifier = function(val, spec) { switch (spec.name) { case "{urn:hl7-org:elm-types:r1}Boolean": return typeof val === "boolean"; case "{urn:hl7-org:elm-types:r1}Decimal": return typeof val === "number"; case "{urn:hl7-org:elm-types:r1}Integer": return typeof val === "number" && Math.floor(val) === val; case "{urn:hl7-org:elm-types:r1}String": return typeof val === "string"; case "{urn:hl7-org:elm-types:r1}Concept": return val != null ? val.isConcept : void 0; case "{urn:hl7-org:elm-types:r1}Code": return val != null ? val.isCode : void 0; case "{urn:hl7-org:elm-types:r1}DateTime": return val != null ? val.isDateTime : void 0; case "{urn:hl7-org:elm-types:r1}Date": return val != null ? val.isDate : void 0; case "{urn:hl7-org:elm-types:r1}Quantity": return val != null ? val.isQuantity : void 0; case "{urn:hl7-org:elm-types:r1}Time": return (val != null ? val.isDateTime : void 0) && val.isTime(); default: return true; } }; Context.prototype.matchesInstanceType = function(val, inst) { switch (false) { case !inst.isBooleanLiteral: return typeof val === "boolean"; case !inst.isDecimalLiteral: return typeof val === "number"; case !inst.isIntegerLiteral: return typeof val === "number" && Math.floor(val) === val; case !inst.isStringLiteral: return typeof val === "string"; case !inst.isCode: return val != null ? val.isCode : void 0; case !inst.isConcept: return val != null ? val.isConcept : void 0; case !inst.isDateTime: return val != null ? val.isDateTime : void 0; case !inst.isQuantity: return val != null ? val.isQuantity : void 0; case !inst.isTime: return (val != null ? val.isDateTime : void 0) && val.isTime(); case !inst.isList: return this.matchesListInstanceType(val, inst); case !inst.isTuple: return this.matchesTupleInstanceType(val, inst); case !inst.isInterval: return this.matchesIntervalInstanceType(val, inst); default: return true; } }; Context.prototype.matchesListInstanceType = function(val, list) { return typeIsArray(val) && val.every((function(_this) { return function(x) { return _this.matchesInstanceType(x, list.elements[0]); }; })(this)); }; Context.prototype.matchesTupleInstanceType = function(val, tpl) { return typeof val === "object" && !typeIsArray(val) && tpl.elements.every((function(_this) { return function(x) { return typeof val[x.name] === "undefined" || _this.matchesInstanceType(val[x.name], x.value); }; })(this)); }; Context.prototype.matchesIntervalInstanceType = function(val, ivl) { var pointType, ref; pointType = (ref = ivl.low) != null ? ref : ivl.high; return val.isInterval && ((val.low == null) || this.matchesInstanceType(val.low, pointType)) && ((val.high == null) || this.matchesInstanceType(val.high, pointType)); }; return Context; })(); module.exports.PatientContext = PatientContext = (function(superClass) { extend(PatientContext, superClass); function PatientContext(library1, patient, codeService, parameters, executionDateTime) { this.library = library1; this.patient = patient; this.executionDateTime = executionDateTime != null ? executionDateTime : dt.DateTime.fromJSDate(new Date()); PatientContext.__super__.constructor.call(this, this.library, codeService, parameters); } PatientContext.prototype.rootContext = function() { return this; }; PatientContext.prototype.getLibraryContext = function(library) { var base; return (base = this.library_context)[library] || (base[library] = new PatientContext(this.get(library), this.patient, this.codeService, this.parameters, this.executionDateTime)); }; PatientContext.prototype.getLocalIdContext = function(localId) { var base; return (base = this.localId_context)[localId] || (base[localId] = new PatientContext(this.get(library), this.patient, this.codeService, this.parameters, this.executionDateTime)); }; PatientContext.prototype.findRecords = function(profile) { var ref; return (ref = this.patient) != null ? ref.findRecords(profile) : void 0; }; return PatientContext; })(Context); module.exports.PopulationContext = PopulationContext = (function(superClass) { extend(PopulationContext, superClass); function PopulationContext(library1, results, codeService, parameters, executionDateTime) { this.library = library1; this.results = results; this.executionDateTime = executionDateTime != null ? executionDateTime : dt.DateTime.fromJSDate(new Date()); PopulationContext.__super__.constructor.call(this, this.library, codeService, parameters); } PopulationContext.prototype.rootContext = function() { return this; }; PopulationContext.prototype.findRecords = function(template) { throw new Exception("Retreives are not currently supported in Population Context"); }; PopulationContext.prototype.getLibraryContext = function(library) { throw new Exception("Library expressions are not currently supported in Population Context"); }; PopulationContext.prototype.get = function(identifier) { var pid, ref, ref1, res, results; if (this.context_values[identifier]) { return this.context_values[identifier]; } if (((ref = this.library[identifier]) != null ? ref.context : void 0) === "Population") { return this.library.expressions[identifier]; } ref1 = this.results.patientResults; results = []; for (pid in ref1) { res = ref1[pid]; results.push(res[identifier]); } return results; }; return PopulationContext; })(Context); }).call(this); //# sourceMappingURL=context.js.map