@metacell/geppetto-meta-core
Version:
The core functionality of geppetto-meta to build and simulate neuroscience data and models.
150 lines (141 loc) • 3.94 kB
JavaScript
/**
* Client class use to represent top level Geppetto model.
*
* @module model/GeppettoModel
* @author Giovanni Idili
*/
import Resources from '../Resources';
import ObjectWrapper from './ObjectWrapper';
export function GeppettoModel(options) {
var _this$wrappedObj$worl;
ObjectWrapper.prototype.constructor.call(this, options);
this.variables = options.variables !== undefined ? options.variables : [];
this.libraries = options.libraries !== undefined ? options.libraries : [];
this.datasources = options.datasources !== undefined ? options.datasources : [];
this.queries = options.queries !== undefined ? options.queries : [];
this.worlds = options.worlds !== undefined ? options.worlds : [];
this.currentWorldIdx = (_this$wrappedObj$worl = this.wrappedObj.worlds) !== null && _this$wrappedObj$worl !== void 0 && _this$wrappedObj$worl.length ? 0 : -1;
}
GeppettoModel.prototype = Object.create(ObjectWrapper.prototype);
GeppettoModel.prototype.constructor = GeppettoModel;
/**
* Get variables
*
* @command GeppettoModel.getVariables()
*
* @returns {List<Variable>} - List of Variable objects
*
*/
GeppettoModel.prototype.getVariables = function (legacy) {
if (this.currentWorldIdx >= 0 && !legacy) {
return this.getCurrentWorld().getVariables();
}
return this.variables;
};
GeppettoModel.prototype.addToVariables = function (variablesToAdd) {
var variables = this.getVariables();
variables.push.apply(variables, variablesToAdd);
};
GeppettoModel.prototype.setVariables = function (variables) {
if (this.currentWorldIdx >= 0) {
this.getCurrentWorld().setVariables(variables);
} else {
this.variables = variables;
}
};
GeppettoModel.prototype.getAllVariables = function () {
if (this.currentWorldIdx >= 0) {
return this.getCurrentWorld().getVariables().concat(this.variables);
}
return this.variables;
};
/**
* Get the id
*
* @command GeppettoModel.getId()
*
* @returns {String} - The id of the model, a constant
*
*/
GeppettoModel.prototype.getId = function () {
return Resources.MODEL_PREFIX_CLIENT;
};
/**
* Get libraries
*
* @command GeppettoModel.getLibraries()
*
* @returns {List<Library>} - List of library objects
*
*/
GeppettoModel.prototype.getLibraries = function () {
return this.libraries;
};
/**
* Get datasources
*
* @command GeppettoModel.getDatasources()
*
* @returns {List<Datasource>} - List of datasource objects
*
*/
GeppettoModel.prototype.getDatasources = function () {
return this.datasources;
};
/**
* Get top level queries
*
* @command GeppettoModel.getQueries()
*
* @returns {List<Query>} - List of query objects
*
*/
GeppettoModel.prototype.getQueries = function () {
return this.queries;
};
/**
* Get combined list of all children
*
* @command GeppettoModel.getChildren()
*
* @returns {List<Object>} - List of children
*
*/
GeppettoModel.prototype.getChildren = function () {
return this.variables.concat(this.libraries, this.datasources, this.queries, this.worlds);
};
/**
* Get the default selected world
*
*/
GeppettoModel.prototype.getCurrentWorld = function () {
return this.worlds[this.currentWorldIdx];
};
/**
* Get worlds
*
*/
GeppettoModel.prototype.getWorlds = function () {
return this.worlds;
};
/**
* Set the default selected world
*
*/
GeppettoModel.prototype.activateWorld = function (worldOrIndex) {
if (typeof worldOrIndex === 'number') {
this.currentWorldIdx = worldOrIndex;
} else if (typeof worldOrIndex === 'string') {
this.currentWorldIdx = this.worlds.findIndex(function (world) {
return world.id === worldOrIndex;
});
}
this.currentWorldIdx = this.worlds.findIndex(function (world) {
return world.id === worldOrIndex.id;
});
if (this.worlds[this.currentWorldIdx] === undefined) {
console.error(worldOrIndex, "world not found in model");
throw "World not found in model";
}
};
export default GeppettoModel;