@phema/cql-execution
Version:
An execution framework for the Clinical Quality Language (CQL)
316 lines (230 loc) • 12 kB
JavaScript
"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 _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
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 _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 _require2 = require('./builder'),
build = _require2.build;
var ExpressionDef = /*#__PURE__*/function (_Expression) {
_inherits(ExpressionDef, _Expression);
var _super = _createSuper(ExpressionDef);
function ExpressionDef(json) {
var _this;
_classCallCheck(this, ExpressionDef);
_this = _super.call(this, json);
_this.name = json.name;
_this.context = json.context;
_this.expression = build(json.expression);
return _this;
}
_createClass(ExpressionDef, [{
key: "exec",
value: function exec(ctx) {
var value = this.expression != null ? this.expression.execute(ctx) : undefined;
ctx.rootContext().set(this.name, value);
return value;
}
}]);
return ExpressionDef;
}(Expression);
var ExpressionRef = /*#__PURE__*/function (_Expression2) {
_inherits(ExpressionRef, _Expression2);
var _super2 = _createSuper(ExpressionRef);
function ExpressionRef(json) {
var _this2;
_classCallCheck(this, ExpressionRef);
_this2 = _super2.call(this, json);
_this2.name = json.name;
_this2.library = json.libraryName;
return _this2;
}
_createClass(ExpressionRef, [{
key: "exec",
value: function exec(ctx) {
ctx = this.library ? ctx.getLibraryContext(this.library) : ctx;
var value = ctx.get(this.name);
if (value instanceof Expression) {
value = value.execute(ctx);
}
return value;
}
}]);
return ExpressionRef;
}(Expression);
var FunctionDef = /*#__PURE__*/function (_Expression3) {
_inherits(FunctionDef, _Expression3);
var _super3 = _createSuper(FunctionDef);
function FunctionDef(json) {
var _this3;
_classCallCheck(this, FunctionDef);
_this3 = _super3.call(this, json);
_this3.name = json.name;
_this3.expression = build(json.expression);
_this3.parameters = json.operand;
return _this3;
}
_createClass(FunctionDef, [{
key: "exec",
value: function exec(ctx) {
return this;
}
}]);
return FunctionDef;
}(Expression);
var FunctionRef = /*#__PURE__*/function (_Expression4) {
_inherits(FunctionRef, _Expression4);
var _super4 = _createSuper(FunctionRef);
function FunctionRef(json) {
var _this4;
_classCallCheck(this, FunctionRef);
_this4 = _super4.call(this, json);
_this4.name = json.name;
_this4.library = json.libraryName;
return _this4;
}
_createClass(FunctionRef, [{
key: "exec",
value: function exec(ctx) {
var functionDefs, child_ctx;
if (this.library) {
var lib = ctx.get(this.library);
functionDefs = lib ? lib.getFunction(this.name) : undefined;
var libCtx = ctx.getLibraryContext(this.library);
child_ctx = libCtx ? libCtx.childContext() : undefined;
} else {
functionDefs = ctx.get(this.name);
child_ctx = ctx.childContext();
}
var args = this.execArgs(ctx); // Filter out functions w/ wrong number of arguments.
functionDefs = functionDefs.filter(function (f) {
return f.parameters.length === args.length;
}); // If there is still > 1 matching function, filter by argument types
if (functionDefs.length > 1) {
functionDefs = functionDefs.filter(function (f) {
var match = true;
for (var i = 0; i < args.length && match; i++) {
if (args[i] !== null) {
var operandTypeSpecifier = f.parameters[i].operandTypeSpecifier;
if (operandTypeSpecifier == null && f.parameters[i].operandType != null) {
// convert it to a NamedTypedSpecifier
operandTypeSpecifier = {
name: f.parameters[i].operandType,
type: 'NamedTypeSpecifier'
};
}
match = ctx.matchesTypeSpecifier(args[i], operandTypeSpecifier);
}
}
return match;
});
} // If there is still > 1 matching function, calculate a score based on quality of matches
if (functionDefs.length > 1) {// TODO
}
if (functionDefs.length === 0) {
throw new Error('no function with matching signature could be found');
} // By this point, we should have only one function, but until implementation is completed,
// use the last one (no matter how many still remain)
var functionDef = functionDefs[functionDefs.length - 1];
for (var i = 0; i < functionDef.parameters.length; i++) {
child_ctx.set(functionDef.parameters[i].name, args[i]);
}
return functionDef.expression.execute(child_ctx);
}
}]);
return FunctionRef;
}(Expression);
var OperandRef = /*#__PURE__*/function (_Expression5) {
_inherits(OperandRef, _Expression5);
var _super5 = _createSuper(OperandRef);
function OperandRef(json) {
var _this5;
_classCallCheck(this, OperandRef);
_this5 = _super5.call(this, json);
_this5.name = json.name;
return _this5;
}
_createClass(OperandRef, [{
key: "exec",
value: function exec(ctx) {
return ctx.get(this.name);
}
}]);
return OperandRef;
}(Expression);
var IdentifierRef = /*#__PURE__*/function (_Expression6) {
_inherits(IdentifierRef, _Expression6);
var _super6 = _createSuper(IdentifierRef);
function IdentifierRef(json) {
var _this6;
_classCallCheck(this, IdentifierRef);
_this6 = _super6.call(this, json);
_this6.name = json.name;
_this6.library = json.libraryName;
return _this6;
}
_createClass(IdentifierRef, [{
key: "exec",
value: function exec(ctx) {
// TODO: Technically, the ELM Translator should never output one of these
// but this code is needed since it does, as a work-around to get queries
// to work properly when sorting by a field in a tuple
var lib = this.library ? ctx.get(this.library) : undefined;
var val = lib ? lib.get(this.name) : ctx.get(this.name);
if (val == null) {
var parts = this.name.split('.');
val = ctx.get(parts[0]);
if (val != null && parts.length > 1) {
var curr_obj = val;
var _iterator = _createForOfIteratorHelper(parts.slice(1)),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var part = _step.value;
// _obj = curr_obj?[part] ? curr_obj?.get?(part)
// curr_obj = if _obj instanceof Function then _obj.call(curr_obj) else _obj
var _obj = void 0;
if (curr_obj != null) {
_obj = curr_obj[part];
if (_obj === undefined && typeof curr_obj.get === 'function') {
_obj = curr_obj.get(part);
}
}
curr_obj = _obj instanceof Function ? _obj.call(curr_obj) : _obj;
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
val = curr_obj;
}
}
if (val instanceof Function) {
return val.call(ctx.context_values);
} else {
return val;
}
}
}]);
return IdentifierRef;
}(Expression);
module.exports = {
ExpressionDef: ExpressionDef,
ExpressionRef: ExpressionRef,
FunctionDef: FunctionDef,
FunctionRef: FunctionRef,
IdentifierRef: IdentifierRef,
OperandRef: OperandRef
};