Commit 27b77a2d authored by Nicolas Biri's avatar Nicolas Biri

Documentation effort

parent ebb407a5
Pipeline #866 passed with stage
in 1 minute and 25 seconds
{
"name": "jsmf-core",
"version": "0.9.2",
"version": "0.10.0",
"description": "Javascript Modelling Framework",
"main": "src/index.js",
"directories": {
......
......@@ -24,6 +24,9 @@ const _ = require('lodash')
*/
/** Reference Cardinality
* @constructor
* @param {number} min - The lower bound of a cardinality
* @param {number} max - The upper bound of a cardinality
*/
function Cardinality(min, max) {
/** The minimal cardinality, will be consider as 0 if undefined
......
This diff is collapsed.
'use strict'
const _ = require('lodash')
, uuid = require('uuid')
/**
* @license
* ©2015-2016 Luxembourg Institute of Science and Technology All Rights Reserved
......@@ -20,19 +25,25 @@
* @author A. Vagner
*/
'use strict'
const _ = require('lodash')
, uuid = require('uuid')
/** Provides the conformsTo relationship of any jsmfElement
* @param o - A jsmf Object (usually Class, Enum, Model, or ClassInstance)
*/
function conformsTo(o) {
return _.get(o, ['__jsmf__', 'conformsTo'])
}
/** Returns the jsmfId of any jsmfElement
* @param o - A jsmf Object (usually Class, Enum, Model, or ClassInstance)
*/
function jsmfId(o) {
return _.get(o, ['__jsmf__', 'uuid'])
}
/** Check if an object is a JSMFElement
* By construction a JSMFElement has a jsmfID,
* returns a value on {@link conformsTo}
* this value has a getInheritanceChain method.
*/
function isJSMFElement(o) {
const implement = conformsTo(o)
return implement
......
......@@ -31,30 +31,46 @@ let conformsTo, generateId
generateId = Common.generateId
}).call()
/** Define an Enum
* @constructor
* @param {string} name - The name of the created Enum
* @param values - Either an Array of string or an Object.
* If an Array is provided, the indexes are used as Enum values.
*/
function Enum(name, values) {
function jsmfEnum(x) {return _.includes(jsmfEnum, x)}
Object.defineProperties(jsmfEnum,
/** The generic Enum instance
* @constructor
*/
function EnumInstance(x) {return _.includes(EnumInstance, x)}
Object.defineProperties(EnumInstance,
{ __jsmf__: {value: {uuid: generateId(), conformsTo: Enum}}
, __name: {value: name}
, getName: {value: getName}
, conformsTo: {value: () => conformsTo(jsmfEnum)}
, conformsTo: {value: () => conformsTo(EnumInstance)}
})
if (_.isArray(values)) {
_.forEach(values, (v, k) => jsmfEnum[v] = k)
_.forEach(values, (v, k) => EnumInstance[v] = k)
} else {
_.forEach(values, (v, k) => jsmfEnum[k] = v)
_.forEach(values, (v, k) => EnumInstance[k] = v)
}
return jsmfEnum
return EnumInstance
}
/** The Enum name */
Enum.__name = 'Enum'
Enum.getInheritanceChain = () => [Enum]
/** Given a value, find the associated key in an Enum, if any.
* @memberof Enum~EnumInstance
*/
function getName(o) {
return _.findKey(this, v => v === o)
}
/** Check if an object is an Enum
*/
function isJSMFEnum(o) {
return conformsTo(o) === Enum
}
......
......@@ -41,13 +41,34 @@ let isJSMFClass, refreshElement
const isJSMFEnum = require('./Enum').isJSMFEnum
/** A Model is basically a set of elements
* @constructor
* @param {string} name - The model name
* @param {Model} referenceModel - The reference model (indicative and used for conformance)
* @param {Class~ClassInstance[]} modellingElements - Either an element or an array of elements that
* should be included in the model
* @param {boolean} transitive - If false, only the elements in modellingElements will be in the model,
* otherwise all theelements that can be reach from modellingElements
* references are included in the model
*/
function Model(name, referenceModel, modellingElements, transitive) {
/** @memberOf Model
* @member {string} name - The name of the model
*/
this.__name = name
_.set(this, ['__jsmf__','conformsTo'], Model)
_.set(this, ['__jsmf__','uuid'], generateId())
/** @memberof Model
* @member {Model} referenceModel - The referenceModel (an empty object if undefined)
*/
this.referenceModel = referenceModel || {}
/** @memberof Model
* @member {Object} modellingElements - The objects of the model, classified by their class name
*/
this.modellingElements = {}
this.elemByClassId = new Map()
/** @memberOf Model
* @member {Object} classes - classes and enums of the model, classified by their name
*/
this.classes = {}
if (modellingElements !== undefined) {
modellingElements = _.isArray(modellingElements) ? modellingElements : [modellingElements]
......@@ -58,12 +79,17 @@ function Model(name, referenceModel, modellingElements, transitive) {
}
}
/** A helper to export a model and all its classes in a npm module.
*/
function modelExport(m) {
const result = _.mapValues(m.classes, _.head)
result[m.__name] = m
return result
}
/** Add a modelling element to a model
* @param es - Either an element or an array of elements.
*/
Model.prototype.addModellingElement = function(es) {
es = _.isArray(es) ? es : [es]
_.forEach(es, e => {
......@@ -89,20 +115,43 @@ function addToModellingElements(m, e) {
m.modellingElements[key] = current
}
/** @deprecated
* @method
*/
Model.prototype.Filter = function(cls) {
return this.modellingElements[_.get(cls, '__name')]
}
/** Model inheritanceChain
* @method
*/
Model.getInheritanceChain = _.constant([Model])
Model.prototype.conformsTo = function() { return conformsTo(this) }
/** @deprecated
* @method
* @see Model#addModellingElement
*/
Model.prototype.setModellingElements = Model.prototype.addModellingElement
/** @see Model#addModellingElement
* @method
*/
Model.prototype.add = Model.prototype.addModellingElement
/** Set the reference model of a model
*/
Model.prototype.setReferenceModel = function(rm) {this.referenceModel = rm}
/** List all the elements of a Model
*/
Model.prototype.elements = function() {
return _(this.modellingElements).values().flatten().value()
}
/**
* Remove from the elements of a model all the elements that are not explicitely in the model
*/
Model.prototype.crop = function() {
const elements = this.elements()
_.forEach(elements, e => {
......@@ -159,6 +208,9 @@ function dispatch(elems) {
{})
}
/** Update all the elements of a model: If the class they are conformsTo has changed,
* update the elements to reflect these changes.
*/
function refreshModel(o) {
if (!(o instanceof Model)) {throw new TypeError('Model expected')}
_.forEach(o.elements(), refreshElement)
......
......@@ -44,6 +44,7 @@ module.exports =
, Any: _.constant(true)
}
/** Transform a native JS type in a JSMF type */
module.exports.normalizeType = function normalizeType(t) {
switch (t) {
case Number: return module.exports.Number
......
......@@ -66,6 +66,8 @@ function dryModel(c) {
return _.assign({__jsmf: {uuid: Common.jsmfId(c)}}, _.pick(c, ['__name', 'referenceModel', 'modellingElements']))
}
/** Check structural equality for JSMF elements.
*/
function jsmfIsEqual(obj, other) {
return _.isEqualWith(obj, other, customizer)
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment