Skip to content

Commit

Permalink
make Component as extended Top
Browse files Browse the repository at this point in the history
  • Loading branch information
metelkin committed Sep 13, 2024
1 parent af898c0 commit 08f70f7
Show file tree
Hide file tree
Showing 54 changed files with 796 additions and 934 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Programming platform for Quantitative Systems Pharmacology modeling in NodeJS",
"main": "src/index.js",
"scripts": {
"test:dev": "mocha test/cases/14 --config=./test/.mocharc.json",
"test:dev": "mocha test/time-switcher/bind-error --config=./test/.mocharc.json",
"test": "mocha test --config=./test/.mocharc.json",
"jsdoc": "jsdoc -r -c .jsdoc.json --readme api-references.md -d docs/dev src",
"test:cov": "nyc --reporter=lcov npm run test",
Expand Down
4 changes: 2 additions & 2 deletions src/another-xlsx-export/_size.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ const { _Size } = require('../core/_size');
const legalUnits = require('./legal-units');

// old version
let _toQ = _Size.prototype.toQ;
let oldToQ = _Size.prototype.toQ;
// new version
_Size.prototype.toQ = function(options = {}){
let res = _toQ.call(this, options);
let res = oldToQ.call(this, options);

// unit with transformation to simbio standard
// if (options.useAnotherUnits) res.units2 = this.units;
Expand Down
18 changes: 7 additions & 11 deletions src/container/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Container.prototype.export = function(q = {}, isCore = false) {
*/
Container.prototype.defineUnit = function(q = {}, isCore = false){
// normal flow
let unitDefInstance = new this.classes.UnitDef(q, isCore);
let unitDefInstance = new this.classes.UnitDef(isCore).merge(q);
if (!unitDefInstance.errored) this.unitDefStorage.set(unitDefInstance.id, unitDefInstance);

return unitDefInstance;
Expand All @@ -74,7 +74,7 @@ Container.prototype.defineUnit = function(q = {}, isCore = false){
*/
Container.prototype.defineFunction = function(q = {}, isCore = false){
// normal flow
let functionDefInstance = new this.classes.FunctionDef(q, isCore);
let functionDefInstance = new this.classes.FunctionDef(isCore).merge(q);
if (!functionDefInstance.errored) this.functionDefStorage.set(functionDefInstance.id, functionDefInstance);

return functionDefInstance;
Expand All @@ -90,7 +90,7 @@ Container.prototype.defineFunction = function(q = {}, isCore = false){
*/
Container.prototype.setScenario = function(q = {}, isCore = false){
// normal flow
let scenarioInstance = new this.classes.Scenario(q, isCore);
let scenarioInstance = new this.classes.Scenario(isCore).merge(q);
if (!scenarioInstance.errored) this.scenarioStorage.set(scenarioInstance.id, scenarioInstance);

return scenarioInstance;
Expand Down Expand Up @@ -159,7 +159,7 @@ Container.prototype.forceInsert = function(q = {}, isCore = false){
}

// check if class is in the list
let selectedClass = this._componentClasses[q.class];
let selectedClass = this.classes[q.class];
if (selectedClass === undefined){
this.logger.error(
`"${ind}" Unknown class "${q.class}" for the component.`,
Expand All @@ -179,10 +179,8 @@ Container.prototype.forceInsert = function(q = {}, isCore = false){
}

// normal flow
let component = new selectedClass(isCore);
component._id = q.id;
let component = new selectedClass(isCore).merge(q);
component.namespace = namespace; // set parent space directly to component
component.merge(q);
namespace.set(q.id, component);
return component;
};
Expand Down Expand Up @@ -435,8 +433,7 @@ Container.prototype.importNS = function(_q = {}){
|| [q.prefix, component.id, q.suffix].join(''); // prefix-id-suffix as default value

// cloning and update references
let clone = component.clone();
clone._id = newId;
let clone = component.clone().merge({id: newId});
clone.namespace = namespace;
clone.updateReferences(q);

Expand Down Expand Up @@ -536,8 +533,7 @@ Container.prototype.import = function(_q = {}){
}

// normal flow
let clone = component.clone();
clone._id = q.id;
let clone = component.clone().merge({id: q.id});
clone.namespace = namespace;
clone.updateReferences(q);
namespace.set(q.id, clone);
Expand Down
49 changes: 30 additions & 19 deletions src/container/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const TopoSort = require('@insysbio/topo-sort');
*
* @property {object} classes Map-like storage for all element constructors that can be created inside platform.
* For example the element of the type `UnitsDef` can be created as follows:
* ```let new_unit = new c.classes.UnitDef({id: 'new', units: 'g/litre'})```
* ```let new_unit = new c.classes.UnitDef().merge({id: 'new', units: 'g/litre'})```
* The `new_unit` element will be automatically bound to the container and pushed to `unitDefStorage`.
* @property {Logger} logger object providing transport of errors, warnings and info messages on Heta platform level.
* @property {object[]} defaultLogs Default storage of errors which will be used for diagnostics.
Expand All @@ -43,7 +43,6 @@ const TopoSort = require('@insysbio/topo-sort');
* @property {Map<string,Namespace>} namespaceStorage Storage for `Namespace` instances. Key is a string identifier.
* There is a default namespace with identifier `nameless` which will be used as a default namespace
* for all components where namespace name is not set.
* @property {object} _componentClasses map-like structure for storing all available constructors for `Component`s.
* @property {object} _builder reference to the parent builder object (if exists).
*/
class Container {
Expand All @@ -60,6 +59,35 @@ class Container {
this.classes.FunctionDef.prototype._container = this;
this.classes.Scenario = class extends Scenario {};
this.classes.Scenario.prototype._container = this;
//
this.classes.Component = class extends Component {};
this.classes.Component.prototype._container = this;
this.classes.Record = class extends Record {};
this.classes.Record.prototype._container = this;
this.classes.Compartment = class extends Compartment {};
this.classes.Compartment.prototype._container = this;
this.classes.Species = class extends Species {};
this.classes.Species.prototype._container = this;
this.classes.Reaction = class extends Reaction {};
this.classes.Reaction.prototype._container = this;
this.classes.Process = class extends Process {};
this.classes.Process.prototype._container = this;
this.classes.DSwitcher = class extends DSwitcher {};
this.classes.DSwitcher.prototype._container = this;
this.classes.StopSwitcher = class extends StopSwitcher {};
this.classes.StopSwitcher.prototype._container = this;
this.classes.CSwitcher = class extends CSwitcher {};
this.classes.CSwitcher.prototype._container = this;
this.classes.TimeSwitcher = class extends TimeSwitcher {};
this.classes.TimeSwitcher.prototype._container = this;
this.classes.ReferenceDefinition = class extends ReferenceDefinition {};
this.classes.ReferenceDefinition.prototype._container = this;
this.classes.Page = class extends Page {};
this.classes.Page.prototype._container = this;
this.classes.Const = class extends Const {};
this.classes.Const.prototype._container = this;
this.classes.TimeScale = class extends TimeScale {};
this.classes.TimeScale.prototype._container = this;

// logger
this.logger = new Logger();
Expand Down Expand Up @@ -322,21 +350,4 @@ class Container {

// only component classes are stored

Container.prototype._componentClasses = {
Component,
Record,
Compartment,
Species,
Process,
Reaction,
DSwitcher,
StopSwitcher,
CSwitcher,
TimeSwitcher,
ReferenceDefinition,
Page,
Const,
TimeScale
};

module.exports = Container;
58 changes: 47 additions & 11 deletions src/core/_size.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,43 @@
const { Component } = require('./component');
const { Unit } = require('./unit');
const { ajv } = require('../utils');

const schema = {
type: "object",
properties: {
units: { anyOf: [
{ type: 'number', enum: [1] },
{ $ref: '#/definitions/UnitsExpr' },
{ type: 'array', items: { '$ref': '#/definitions/UnitComponent' } },
{ type: 'null' }
] }
},
definitions:{
UnitsExpr: {
description: 'Unit expression, see qsp-units project.',
type: 'string',
pattern: '^[_a-zA-Z0-9./*^ ()+-]+$',
example: "1/h * ms"
},
UnitComponent: {
type: 'object',
required: ['kind'],
properties: {
kind: { '$ref': '#/definitions/ID' },
multiplier: { type: 'number', exclusiveMinimum: 0 },
exponent: { type: 'number' }
},
example: { kind: 'mole', multiplier: 1e-6, exponent: 1 }
},
ID: {
description: 'First character is letter, others are letter, digit or lodash.',
type: 'string',
minLength: 1,
pattern: '^[_a-zA-Z][_a-zA-Z0-9]*$',
example: 'x_12_'
}
}
};

/*
Abstract class _Size
Expand All @@ -17,7 +55,7 @@ const { Unit } = require('./unit');
class _Size extends Component {
merge(q = {}){
super.merge(q);
let logger = this.namespace?.container?.logger;
let logger = this._container?.logger;
let valid = _Size.isValid(q, logger);
if (valid) {
if (q.units !== undefined) {
Expand Down Expand Up @@ -60,8 +98,8 @@ class _Size extends Component {
/** Additional check of units items */
bind(namespace){
super.bind(namespace);
let logger = this.namespace.container.logger;
let storage = this.namespace.container.unitDefStorage;
let logger = this._container?.logger;
let storage = this._container?.unitDefStorage;

if (this.unitsParsed) {
this.unitsParsed.forEach((x) => {
Expand Down Expand Up @@ -95,7 +133,7 @@ class _Size extends Component {
.rebase(legalUnits)
.toString(usePefix);
} catch(err) {
let logger = this.namespace.container.logger;
let logger = this._container?.logger;
let msg = err.message;
logger.warn(msg);
return undefined;
Expand All @@ -104,8 +142,12 @@ class _Size extends Component {
return undefined;
}
}
static get validate() {
return ajv.compile(schema);
}
toQ(options = {}){
let res = super.toQ(options);

if (this.unitsParsed) {
if (options.noUnitsExpr) {
res.units = this.unitsParsed.toQ(options);
Expand All @@ -116,14 +158,8 @@ class _Size extends Component {

return res;
}
_references(){
let classSpecificRefs = [];

return super._references()
.concat(classSpecificRefs);
}
}

module.exports = {
_Size
};
};
28 changes: 27 additions & 1 deletion src/core/_switcher.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
const { Component } = require('./component');
const { ajv } = require('../utils');

const schema = {
type: 'object',
properties: {
atStart: {oneOf: [
{
description: 'If true than the condition will be checked at start_',
enum: [true, false, 1, 0],
default: false
},
{ type: 'null' }
]},
active: {oneOf: [
{
description: 'if false the event will not run.',
enum: [true, false, 1, 0],
default: true
},
{ type: 'null' }
]}
}
};

/*
_Switcher abstract class
Expand All @@ -14,7 +37,7 @@ class _Switcher extends Component {
}
merge(q = {}){
super.merge(q);
let logger = this.namespace?.container?.logger;
let logger = this._container?.logger;
let valid = _Switcher.isValid(q, logger);

if (valid) {
Expand Down Expand Up @@ -51,6 +74,9 @@ class _Switcher extends Component {

return res;
}
static get validate() {
return ajv.compile(schema);
}
}

_Switcher._requirements = {
Expand Down
28 changes: 25 additions & 3 deletions src/core/c-switcher.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
const { _Switcher } = require('./_switcher');
const { Expression } = require('./expression');
const { ajv } = require('../utils');

const schema = {
type: "object",
properties: {
trigger: {oneOf: [
{ '$ref': '#/definitions/ExprString' },
{ type: "null" }
]}
},
definitions: {
ExprString: {
description: 'Expression as string. Currently pattern does not analyze expressions.',
type: 'string',
minLength: 1,
pattern: '[a-zA-Z0-9. -+/*^()]*$'
}
}
};

/*
CSwitcher class
Expand All @@ -11,7 +30,7 @@ const { Expression } = require('./expression');
class CSwitcher extends _Switcher {
merge(q = {}){
super.merge(q);
let logger = this.namespace?.container?.logger;
let logger = this._container?.logger;
let valid = CSwitcher.isValid(q, logger);

if (valid) {
Expand Down Expand Up @@ -60,7 +79,7 @@ class CSwitcher extends _Switcher {
}
bind(namespace){
super.bind(namespace);
let {logger, functionDefStorage} = this.namespace.container;
let {logger, functionDefStorage} = this._container;

// get list of
this.trigger && this.trigger.dependOnNodes().forEach((node) => {
Expand Down Expand Up @@ -105,7 +124,7 @@ class CSwitcher extends _Switcher {
Works only for bound switchers
*/
checkUnits(){
let logger = this.namespace.container.logger;
let logger = this._container?.logger;

if (typeof this.trigger !== 'undefined') { // skip empty
let rightSideUnit = this.trigger.calcUnit(this);
Expand All @@ -114,6 +133,9 @@ class CSwitcher extends _Switcher {
}
}
}
static get validate() {
return ajv.compile(schema);
}
}

CSwitcher._requirements = {
Expand Down
Loading

0 comments on commit 08f70f7

Please sign in to comment.