Commit 8e2c004b authored by Nicolas Biri's avatar Nicolas Biri

Clean code, add build badge

parent cd50b8fd
Pipeline #697 failed with stage
in 23 seconds
# JSMF-Core
[![build status](https://git.list.lu/jsmf/jsmf-core/badges/master/build.svg)](https://git.list.lu/jsmf/jsmf-core/commits/master)
A part of the JSMF project dedicated to model definition.
## Install
Thanks to npm: `npm install git+ssh://git@git.list.lu:model/jsmf.git`
Thanks to npm: `npm install git+ssh://git@git.list.lu:model/jsmf.git#0.7.0`
## License information
......
......@@ -2,117 +2,117 @@
const _ = require('lodash')
let isJSMFElement, conformsTo;
let isJSMFElement, conformsTo
(function() {
var common = require('./Common')
isJSMFElement = common.isJSMFElement
conformsTo = common.conformsTo
}).call();
const common = require('./Common')
isJSMFElement = common.isJSMFElement
conformsTo = common.conformsTo
}).call()
const isJSMFEnum = require('./Enum').isJSMFEnum
const isJSMFClass = require('./Class').isJSMFClass
function Model(name, referenceModel, modellingElements, transitive) {
this.__name = name
_.set(this, ['__jsmf__','conformsTo'], Model)
this.referenceModel = referenceModel || {}
this.modellingElements = {}
if (modellingElements !== undefined) {
modellingElements = modellingElements instanceof Array ? modellingElements : [modellingElements]
if (transitive) {
modellingElements = crawlElements(modellingElements)
}
_.forEach(modellingElements, e => this.addModellingElement(e))
this.__name = name
_.set(this, ['__jsmf__','conformsTo'], Model)
this.referenceModel = referenceModel || {}
this.modellingElements = {}
if (modellingElements !== undefined) {
modellingElements = modellingElements instanceof Array ? modellingElements : [modellingElements]
if (transitive) {
modellingElements = crawlElements(modellingElements)
}
_.forEach(modellingElements, e => this.addModellingElement(e))
}
}
function modelExport(m) {
const result = _(m.modellingElements).mapValues(_.head)
.pickBy(x => isJSMFClass(x) || isJSMFEnum(x))
.value()
result[m.__name] = m
return result
const result = _(m.modellingElements).mapValues(_.head)
.pickBy(x => isJSMFClass(x) || isJSMFEnum(x))
.value()
result[m.__name] = m
return result
}
Model.prototype.addModellingElement = function(es) {
es = es instanceof Array ? es : [es];
_.forEach(es, e => {
if (!isJSMFElement(e)) {throw new TypeError(`can't Add ${e} to model ${this}`)}
const key = (isJSMFClass(e) || isJSMFEnum(e)) ? e.__name : conformsTo(e).__name
const current = this.modellingElements[key] || []
current.push(e)
this.modellingElements[key] = current
});
es = es instanceof Array ? es : [es]
_.forEach(es, e => {
if (!isJSMFElement(e)) {throw new TypeError(`can't Add ${e} to model ${this}`)}
const key = (isJSMFClass(e) || isJSMFEnum(e)) ? e.__name : conformsTo(e).__name
const current = this.modellingElements[key] || []
current.push(e)
this.modellingElements[key] = current
})
}
Model.prototype.Filter = function(cls) {
return this.modellingElements[_.get(cls, '__name')]
return this.modellingElements[_.get(cls, '__name')]
}
Model.getInheritanceChain = _.constant([Model])
Model.prototype.conformsTo = () => conformsTo(this)
Model.prototype.conformsTo = function() { return conformsTo(this) }
Model.prototype.setModellingElements = Model.prototype.addModellingElement
Model.prototype.add = Model.prototype.addModellingElement
Model.prototype.setReferenceModel = function(rm) {this.referenceModel = rm}
Model.prototype.elements = function() {
return _(this.modellingElements).values().flatten().value()
return _(this.modellingElements).values().flatten().value()
}
Model.prototype.crop = function() {
const elements = this.elements()
_.forEach(elements, e => {
const mme = e.conformsTo()
if (mme !== undefined) {
for (var refName in mme.references) {
e.__jsmf__.references[refName] = _.intersection(e.__jsmf__.references, elements)
e.__jsmf__.associated[refName] = _.filter(e.__jsmf__.associated[refName],
x => _.includes(e.__jsmf__.references[refName], x.elem))
}
}
})
const elements = this.elements()
_.forEach(elements, e => {
const mme = e.conformsTo()
if (mme !== undefined) {
for (var refName in mme.references) {
e.__jsmf__.references[refName] = _.intersection(e.__jsmf__.references, elements)
e.__jsmf__.associated[refName] = _.filter(e.__jsmf__.associated[refName],
x => _.includes(e.__jsmf__.references[refName], x.elem))
}
}
})
}
function crawlElements(init) {
const visited = new Set()
let toVisit = init
while (!_.isEmpty(toVisit)) {
var e = toVisit.pop()
if (!visited.has(e)) {
visited.add(e)
let newNodes
if (isJSMFClass(e)) {
const refs = e.getAllReferences()
const refTypes = _.map(refs, 'type')
const attrs = e.getAllAttributes()
const attrsEnum = _(attrs).values().map('type').filter(isJSMFEnum).value()
newNodes = _.flatten([refTypes, attrsEnum, e.getInheritanceChain()])
} else if (isJSMFEnum(e)) {
newNodes = []
} else if (isJSMFElement(e)) {
const refs = conformsTo(e).getAllReferences()
const associated = _(e.getAssociated()).values().flatten().map('associated').value()
newNodes = _(refs).map((v, x) => e[x]).flatten().value()
newNodes = newNodes.concat(associated)
}
toVisit = toVisit.concat(newNodes)
}
const visited = new Set()
let toVisit = init
while (!_.isEmpty(toVisit)) {
var e = toVisit.pop()
if (!visited.has(e)) {
visited.add(e)
let newNodes
if (isJSMFClass(e)) {
const refs = e.getAllReferences()
const refTypes = _.map(refs, 'type')
const attrs = e.getAllAttributes()
const attrsEnum = _(attrs).values().map('type').filter(isJSMFEnum).value()
newNodes = _.flatten([refTypes, attrsEnum, e.getInheritanceChain()])
} else if (isJSMFEnum(e)) {
newNodes = []
} else if (isJSMFElement(e)) {
const refs = conformsTo(e).getAllReferences()
const associated = _(e.getAssociated()).values().flatten().map('associated').value()
newNodes = _(refs).map((v, x) => e[x]).flatten().value()
newNodes = newNodes.concat(associated)
}
toVisit = toVisit.concat(newNodes)
}
return dispatch(Array.from(visited))
}
return dispatch(Array.from(visited))
}
function dispatch(elems) {
return _.reduce(
elems,
(acc, e) => {
const key = (isJSMFClass(e) || isJSMFEnum(e)) ? e.__name : e.conformsTo().__name
const values = _.get(acc, key, [])
values.push(e)
acc[key] = values
return acc
},
{})
return _.reduce(
elems,
(acc, e) => {
const key = (isJSMFClass(e) || isJSMFEnum(e)) ? e.__name : e.conformsTo().__name
const values = _.get(acc, key, [])
values.push(e)
acc[key] = values
return acc
},
{})
}
......
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