UNPKG

@phema/cql-execution

Version:

An execution framework for the Clinical Quality Language (CQL)

504 lines (372 loc) 15.9 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } var _require = require('./expression'), Expression = _require.Expression; var dt = require('../datatypes/datatypes'); var _require2 = require('./builder'), build = _require2.build; var ValueSetDef = /*#__PURE__*/function (_Expression) { _inherits(ValueSetDef, _Expression); var _super = _createSuper(ValueSetDef); function ValueSetDef(json) { var _this; _classCallCheck(this, ValueSetDef); _this = _super.call(this, json); _this.name = json.name; _this.id = json.id; _this.version = json.version; return _this; } //todo: code systems and versions _createClass(ValueSetDef, [{ key: "exec", value: function exec(ctx) { var valueset = ctx.codeService.findValueSet(this.id, this.version) || new dt.ValueSet(this.id, this.version); ctx.rootContext().set(this.name, valueset); return valueset; } }]); return ValueSetDef; }(Expression); var ValueSetRef = /*#__PURE__*/function (_Expression2) { _inherits(ValueSetRef, _Expression2); var _super2 = _createSuper(ValueSetRef); function ValueSetRef(json) { var _this2; _classCallCheck(this, ValueSetRef); _this2 = _super2.call(this, json); _this2.name = json.name; _this2.libraryName = json.libraryName; return _this2; } _createClass(ValueSetRef, [{ key: "exec", value: function exec(ctx) { // TODO: This calls the code service every time-- should be optimized var valueset = ctx.getValueSet(this.name, this.libraryName); if (valueset instanceof Expression) { valueset = valueset.execute(ctx); } return valueset; } }]); return ValueSetRef; }(Expression); var AnyInValueSet = /*#__PURE__*/function (_Expression3) { _inherits(AnyInValueSet, _Expression3); var _super3 = _createSuper(AnyInValueSet); function AnyInValueSet(json) { var _this3; _classCallCheck(this, AnyInValueSet); _this3 = _super3.call(this, json); _this3.codes = build(json.codes); _this3.valueset = new ValueSetRef(json.valueset); return _this3; } _createClass(AnyInValueSet, [{ key: "exec", value: function exec(ctx) { var valueset = this.valueset.execute(ctx); // If the value set reference cannot be resolved, a run-time error is thrown. if (valueset == null || !valueset.isValueSet) { throw new Error('ValueSet must be provided to InValueSet function'); } var codes = this.codes.exec(ctx); return codes != null && codes.some(function (code) { return valueset.hasMatch(code); }); } }]); return AnyInValueSet; }(Expression); var InValueSet = /*#__PURE__*/function (_Expression4) { _inherits(InValueSet, _Expression4); var _super4 = _createSuper(InValueSet); function InValueSet(json) { var _this4; _classCallCheck(this, InValueSet); _this4 = _super4.call(this, json); _this4.code = build(json.code); _this4.valueset = new ValueSetRef(json.valueset); return _this4; } _createClass(InValueSet, [{ key: "exec", value: function exec(ctx) { // If the code argument is null, the result is false if (this.code == null) { return false; } if (this.valueset == null) { throw new Error('ValueSet must be provided to InValueSet function'); } var code = this.code.execute(ctx); // spec indicates to return false if code is null, throw error if value set cannot be resolved if (code == null) { return false; } var valueset = this.valueset.execute(ctx); if (valueset == null || !valueset.isValueSet) { throw new Error('ValueSet must be provided to InValueSet function'); } // If there is a code and valueset return whether or not the valueset has the code return valueset.hasMatch(code); } }]); return InValueSet; }(Expression); var CodeSystemDef = /*#__PURE__*/function (_Expression5) { _inherits(CodeSystemDef, _Expression5); var _super5 = _createSuper(CodeSystemDef); function CodeSystemDef(json) { var _this5; _classCallCheck(this, CodeSystemDef); _this5 = _super5.call(this, json); _this5.name = json.name; _this5.id = json.id; _this5.version = json.version; return _this5; } _createClass(CodeSystemDef, [{ key: "exec", value: function exec(ctx) { return new dt.CodeSystem(this.id, this.version); } }]); return CodeSystemDef; }(Expression); var CodeDef = /*#__PURE__*/function (_Expression6) { _inherits(CodeDef, _Expression6); var _super6 = _createSuper(CodeDef); function CodeDef(json) { var _this6; _classCallCheck(this, CodeDef); _this6 = _super6.call(this, json); _this6.name = json.name; _this6.id = json.id; _this6.systemName = json.codeSystem.name; _this6.display = json.display; return _this6; } _createClass(CodeDef, [{ key: "exec", value: function exec(ctx) { var system = ctx.getCodeSystem(this.systemName).execute(ctx); return new dt.Code(this.id, system.id, system.version, this.display); } }]); return CodeDef; }(Expression); var CodeRef = /*#__PURE__*/function (_Expression7) { _inherits(CodeRef, _Expression7); var _super7 = _createSuper(CodeRef); function CodeRef(json) { var _this7; _classCallCheck(this, CodeRef); _this7 = _super7.call(this, json); _this7.name = json.name; _this7.library = json.libraryName; return _this7; } _createClass(CodeRef, [{ key: "exec", value: function exec(ctx) { ctx = this.library ? ctx.getLibraryContext(this.library) : ctx; var codeDef = ctx.getCode(this.name); return codeDef ? codeDef.execute(ctx) : undefined; } }]); return CodeRef; }(Expression); var Code = /*#__PURE__*/function (_Expression8) { _inherits(Code, _Expression8); var _super8 = _createSuper(Code); function Code(json) { var _this8; _classCallCheck(this, Code); _this8 = _super8.call(this, json); _this8.code = json.code; _this8.systemName = json.system.name; _this8.version = json.version; _this8.display = json.display; return _this8; } // Define a simple getter to allow type-checking of this class without instanceof // and in a way that survives minification (as opposed to checking constructor.name) _createClass(Code, [{ key: "isCode", get: function get() { return true; } }, { key: "exec", value: function exec(ctx) { var system = ctx.getCodeSystem(this.systemName) || {}; return new dt.Code(this.code, system.id, this.version, this.display); } }]); return Code; }(Expression); var ConceptDef = /*#__PURE__*/function (_Expression9) { _inherits(ConceptDef, _Expression9); var _super9 = _createSuper(ConceptDef); function ConceptDef(json) { var _this9; _classCallCheck(this, ConceptDef); _this9 = _super9.call(this, json); _this9.name = json.name; _this9.display = json.display; _this9.codes = json.code; return _this9; } _createClass(ConceptDef, [{ key: "exec", value: function exec(ctx) { var codes = this.codes.map(function (code) { var codeDef = ctx.getCode(code.name); return codeDef ? codeDef.execute(ctx) : undefined; }); return new dt.Concept(codes, this.display); } }]); return ConceptDef; }(Expression); var ConceptRef = /*#__PURE__*/function (_Expression10) { _inherits(ConceptRef, _Expression10); var _super10 = _createSuper(ConceptRef); function ConceptRef(json) { var _this10; _classCallCheck(this, ConceptRef); _this10 = _super10.call(this, json); _this10.name = json.name; return _this10; } _createClass(ConceptRef, [{ key: "exec", value: function exec(ctx) { var conceptDef = ctx.getConcept(this.name); return conceptDef ? conceptDef.execute(ctx) : undefined; } }]); return ConceptRef; }(Expression); var Concept = /*#__PURE__*/function (_Expression11) { _inherits(Concept, _Expression11); var _super11 = _createSuper(Concept); function Concept(json) { var _this11; _classCallCheck(this, Concept); _this11 = _super11.call(this, json); _this11.codes = json.code; _this11.display = json.display; return _this11; } // Define a simple getter to allow type-checking of this class without instanceof // and in a way that survives minification (as opposed to checking constructor.name) _createClass(Concept, [{ key: "isConcept", get: function get() { return true; } }, { key: "toCode", value: function toCode(ctx, code) { var system = ctx.getCodeSystem(code.system.name) || {}; return new dt.Code(code.code, system.id, code.version, code.display); } }, { key: "exec", value: function exec(ctx) { var _this12 = this; var codes = this.codes.map(function (code) { return _this12.toCode(ctx, code); }); return new dt.Concept(codes, this.display); } }]); return Concept; }(Expression); var CalculateAge = /*#__PURE__*/function (_Expression12) { _inherits(CalculateAge, _Expression12); var _super12 = _createSuper(CalculateAge); function CalculateAge(json) { var _this13; _classCallCheck(this, CalculateAge); _this13 = _super12.call(this, json); _this13.precision = json.precision; return _this13; } _createClass(CalculateAge, [{ key: "exec", value: function exec(ctx) { var date1 = this.execArgs(ctx); var date2 = dt.DateTime.fromJSDate(ctx.getExecutionDateTime()); var result = date1 != null ? date1.durationBetween(date2, this.precision.toLowerCase()) : undefined; if (result != null && result.isPoint()) { return result.low; } else { return result; } } }]); return CalculateAge; }(Expression); var CalculateAgeAt = /*#__PURE__*/function (_Expression13) { _inherits(CalculateAgeAt, _Expression13); var _super13 = _createSuper(CalculateAgeAt); function CalculateAgeAt(json) { var _this14; _classCallCheck(this, CalculateAgeAt); _this14 = _super13.call(this, json); _this14.precision = json.precision; return _this14; } _createClass(CalculateAgeAt, [{ key: "exec", value: function exec(ctx) { var _this$execArgs = this.execArgs(ctx), _this$execArgs2 = _slicedToArray(_this$execArgs, 2), date1 = _this$execArgs2[0], date2 = _this$execArgs2[1]; if (date1 != null && date2 != null) { // date1 is the birthdate, convert it to date if date2 is a date (to support ignoring time) if (date2.isDate && date1.isDateTime) { date1 = date1.getDate(); } var result = date1.durationBetween(date2, this.precision.toLowerCase()); if (result != null && result.isPoint()) { return result.low; } else { return result; } } return null; } }]); return CalculateAgeAt; }(Expression); module.exports = { AnyInValueSet: AnyInValueSet, CalculateAge: CalculateAge, CalculateAgeAt: CalculateAgeAt, Code: Code, CodeDef: CodeDef, CodeRef: CodeRef, CodeSystemDef: CodeSystemDef, Concept: Concept, ConceptDef: ConceptDef, ConceptRef: ConceptRef, InValueSet: InValueSet, ValueSetDef: ValueSetDef, ValueSetRef: ValueSetRef };