UNPKG

@metacell/geppetto-meta-core

Version:

The core functionality of geppetto-meta to build and simulate neuroscience data and models.

415 lines (393 loc) 12 kB
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } import { extend } from '../common/Utils'; import ModelFactory from '../ModelFactory'; import Resources from '../Resources'; /** * Client class use to represent an instance object (instantiation of a variable). * * @module model/Instance * @author Giovanni Idili * @author Matteo Cantarelli */ var Instance = /*#__PURE__*/function () { function Instance(options) { _classCallCheck(this, Instance); this.id = options.id; this.name = options.name; this._metaType = options._metaType; this.variable = options.variable; this.parent = options.parent; this.children = options.children !== undefined ? options.children : []; this.capabilities = []; this.connections = []; } /** * Get id * * @command Instance.getId() * * @returns {String} - Id * */ return _createClass(Instance, [{ key: "getId", value: function getId() { return this.id; } /** * Get name * * @command Instance.getName() * * @returns {String} - Name * */ }, { key: "getName", value: function getName() { return this.name; } /** * Get meta type * * @command Instance.getMetaType() * * @returns {String} - meta type * */ }, { key: "getMetaType", value: function getMetaType() { return this._metaType; } /** * Get the type for this instance * * @command Instance.getTypes() * * @returns {List<Type>} - array of types * */ }, { key: "getTypes", value: function getTypes() { return this.getVariable().getTypes(); } }, { key: "getValues", value: function getValues() { return this.getVariable().getValues(); } /** * Get the type of this variable, return a list if it has more than one * * @command Variable.getType() * * @returns List<Type>} - array of types * */ }, { key: "getType", value: function getType() { var types = this.variable.getTypes(); if (types.length == 1) { return types[0]; } else { return types; } } }, { key: "getValue", value: function getValue() { return this.getVariable().getValue(); } /** * * @returns {*|Object} */ }, { key: "getPosition", value: function getPosition() { return this.getVariable().getPosition(); } /** * Checks if this instance has a visual type * * @command Instance.hasVisualType() * * @returns {Boolean} * */ }, { key: "hasVisualType", value: function hasVisualType() { var hasVisual = false; var types = this.getTypes(); // check if any of types is VISUAL_TYPE_NODE or if types HAVE .visualType for (var i = 0; i < types.length; i++) { // could be pointing to an array variable if it's an exploded instance if (types[i].getMetaType() == Resources.ARRAY_TYPE_NODE) { // check it if is a visual type or has a visual type if (types[i].getType().getMetaType() == Resources.VISUAL_TYPE_NODE || types[i].getType().getMetaType() == Resources.COMPOSITE_VISUAL_TYPE_NODE || types[i].getType().getVisualType() != null) { hasVisual = true; break; } } else if (types[i].getMetaType() == Resources.VISUAL_TYPE_NODE || types[i].getMetaType() == Resources.COMPOSITE_VISUAL_TYPE_NODE || types[i].getVisualType() != null) { hasVisual = true; break; } } return hasVisual; } /** * Gets visual types for the instance if any * * @command Instance.getVisualType() * * @returns {*} - Type or list of Types if more than one is found */ }, { key: "getVisualType", value: function getVisualType() { var visualTypes = []; var types = this.getTypes(); // check if any of types is VISUAL_TYPE_NODE or if types HAVE .visualType for (var i = 0; i < types.length; i++) { // could be pointing to an array variable if it's an exploded instance if (types[i].getMetaType() == Resources.ARRAY_TYPE_NODE) { // check it if is a visual type or has a visual type if (types[i].getType().getMetaType() == Resources.VISUAL_TYPE_NODE || types[i].getType().getMetaType() == Resources.COMPOSITE_VISUAL_TYPE_NODE) { visualTypes.push(types[i].getType()); } else if (types[i].getType().getVisualType() != null) { visualTypes.push(types[i].getType().getVisualType()); } } else { // check it if is a visual type or has a visual type if (types[i].getMetaType() == Resources.VISUAL_TYPE_NODE || types[i].getMetaType() == Resources.COMPOSITE_VISUAL_TYPE_NODE) { visualTypes.push(types[i]); } else if (types[i].getVisualType() != null) { visualTypes.push(types[i].getVisualType()); } } } if (visualTypes.length === 0) { return undefined; } if (visualTypes.length === 1) { return visualTypes[0]; } return visualTypes; } /** * Get the variable for this instance * * @command Instance.getVariable() * * @returns {Variable} - Variable object for this instance * */ }, { key: "getVariable", value: function getVariable() { return this.variable; } /** * Get children instances * * @command Instance.getChildren() * * @returns {List<Instance>} - List of instances * */ }, { key: "getChildren", value: function getChildren() { return this.children; } /** * Get instance path * * @command Instance.getInstancePath() * * @returns {String} - Instance path * */ }, { key: "getInstancePath", value: function getInstancePath(useType) { if (useType == undefined) { useType = false; } var parent = this.parent; var parentPath = ""; if (parent != null && parent != undefined) { parentPath = parent.getInstancePath(useType); } var path = parentPath + "." + this.getId(); if (useType) { path += "(" + this.getType().getId() + ")"; } return parentPath != "" ? path : path.replace('.', ''); } /** * Synonym of get instance path * * @command Instance.getPath() * * @returns {String} - Instance path * */ }, { key: "getPath", value: function getPath() { return this.getInstancePath(); } /** * Get raw instance path (without array shortening) * * @command Instance.getRawInstancePath() * * @returns {String} - Instance path * */ }, { key: "getRawInstancePath", value: function getRawInstancePath() { var parent = this.parent; var parentPath = ""; if (parent != null && parent != undefined) { parentPath = parent.getInstancePath(); } return parentPath != "" ? parentPath + "." + this.getId() : this.getId(); } /** * Get parent * * @command Instance.getParent() * * @returns {Instance} - Parent instance * */ }, { key: "getParent", value: function getParent() { return this.parent; } /** * Get children instances * * @command Instance.addChild() */ }, { key: "addChild", value: function addChild(child) { this.children.push(child); } /** * Extends with methods from another object * * @command Instance.extendApi(extensionObj) */ }, { key: "extendApi", value: function extendApi(extensionObj) { extend(this, extensionObj); this.capabilities.push(extensionObj.capabilityId); } /** * Checks if the instance has a given capability * * @command Instance.hasCapability(capabilityId) * * @returns {Boolean} */ }, { key: "hasCapability", value: function hasCapability(capabilityId) { var hasCapability = false; var capabilities = this.capabilities; for (var i = 0; i < capabilities.length; i++) { if (capabilities[i] === capabilityId) { hasCapability = true; } } return hasCapability; } /** * Get instance capabilities * * @returns {Array} */ }, { key: "getCapabilities", value: function getCapabilities() { return this.capabilities; } /** * Return connections, user Resources.INPUT / OUTPUT / INPUT_OUTPUT to filter * * @command Instance.getConnections(direction) * * @returns {List<Instance>} * */ }, { key: "getConnections", value: function getConnections(direction) { ModelFactory.updateConnectionInstances(this); var connections = this.connections; if (direction === Resources.INPUT || direction === Resources.OUTPUT || direction === Resources.INPUT_OUTPUT) { var filteredConnections = []; for (var i = 0; i < connections.length; i++) { // get directionality var connectivity = connections[i].getVariable().getInitialValue().value.connectivity; if (connectivity == Resources.DIRECTIONAL) { var a = connections[i].getA(); var b = connections[i].getB(); // if A is this then it's an output connection if (this.getInstancePath() == a.getPath() && direction === Resources.OUTPUT) { filteredConnections.push(connections[i]); } // if B is this then it's an input connection if (this.getInstancePath() == b.getPath() && direction === Resources.INPUT) { filteredConnections.push(connections[i]); } } else if (connectivity == Resources.BIDIRECTIONAL) { filteredConnections.push(connections[i]); } } // set return variable to filtered list connections = filteredConnections; } return connections; } /** * Get children instances * * @command Instance.addConnection() */ }, { key: "addConnection", value: function addConnection(connection) { this.connections.push(connection); } /** * Deletes instance */ }, { key: "delete", value: function _delete() { var children = [].concat(this.getChildren()); for (var c = 0; c < children.length; c++) { children[c]["delete"](); } ModelFactory.deleteInstance(this); } }]); }(); export default Instance;