Commit 5bb7df01 authored by Nicolas Biri's avatar Nicolas Biri

set classes and enums aside in Model

parent f46d97ee
Pipeline #718 passed with stage
in 1 minute and 27 seconds
......@@ -17,6 +17,8 @@ function Model(name, referenceModel, modellingElements, transitive) {
_.set(this, ['__jsmf__','conformsTo'], Model)
this.referenceModel = referenceModel || {}
this.modellingElements = {}
this.elemByClassId = new Map()
this.classes = {}
if (modellingElements !== undefined) {
modellingElements = modellingElements instanceof Array ? modellingElements : [modellingElements]
if (transitive) {
......@@ -27,9 +29,7 @@ function Model(name, referenceModel, modellingElements, transitive) {
}
function modelExport(m) {
const result = _(m.modellingElements).mapValues(_.head)
.pickBy(x => isJSMFClass(x) || isJSMFEnum(x))
.value()
const result = _.mapValues(m.classes, _.head)
result[m.__name] = m
return result
}
......@@ -38,13 +38,27 @@ 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
addToClass(this, e)
addToModellingElements(this, e)
})
}
function addToClass(m, e) {
if (isJSMFClass(e) || isJSMFEnum(e)) {
const key = e.__name
const current = m.classes[key] || []
current.push(e)
m.classes[key] = current
}
}
function addToModellingElements(m, e) {
const key = conformsTo(e).__name
const current = m.modellingElements[key] || []
current.push(e)
m.modellingElements[key] = current
}
Model.prototype.Filter = function(cls) {
return this.modellingElements[_.get(cls, '__name')]
}
......
......@@ -8,121 +8,122 @@ const should = require('should')
describe('Model', function() {
describe('constructor', function() {
it('creates an empty model', function(done) {
var foo = new Model('Foo')
foo.modellingElements.should.be.empty()
foo.referenceModel.should.be.empty()
done()
})
it('creates a model that is a jsmf element', function(done) {
var foo = new Model('Foo')
JSMF.isJSMFElement(foo).should.be.true()
done()
})
it ('creates a model with a given reference model', function(done) {
var MM = new Model('Foo')
var M = new Model('Bar', MM)
M.referenceModel.should.equal(MM)
done()
})
it ('adds the given classes to the model', function(done) {
var A = new Class('A')
var B = new Class('B')
var M = new Model('Bar', {}, [A, B])
M.modellingElements.should.have.property('A', [A])
M.modellingElements.should.have.property('B', [B])
done()
})
it ('adds the given elements to the model', function(done) {
var A = new Class('A')
var a = new A()
var B = new Class('B')
var b = new B()
var M = new Model('Bar', {}, [a, b])
M.modellingElements.should.have.property('A', [a])
M.modellingElements.should.have.property('B', [b])
done()
})
it ('does not add the given elements transitively if not mentioned', function(done) {
var A = new Class('A')
var B = new Class('B')
A.addReference('b', B)
var a = new A()
var b = new B()
a.b = b
var M = new Model('Bar', {}, [a])
M.modellingElements.should.have.property('A', [a])
M.modellingElements.should.not.have.property('B')
done()
})
it ('adds the given elements transitively if mentioned', function(done) {
var A = new Class('A')
var B = new Class('B')
A.addReference('b', B)
var a = new A()
var b = new B()
a.b = b
var M = new Model('Bar', {}, [a], true)
M.modellingElements.should.have.property('A', [a])
M.modellingElements.should.have.property('B', [b])
done()
})
describe('constructor', function() {
it('creates an empty model', function(done) {
const foo = new Model('Foo')
foo.modellingElements.should.be.empty()
foo.referenceModel.should.be.empty()
done()
})
describe('elements', function() {
it('lists all the elements of the model', function(done) {
var A = new Class('A')
var B = new Class('B')
A.addReference('b', B)
var a = new A()
var b = new B()
a.b = b
var M = new Model('Bar', {}, [a, b], true)
var elements = M.elements()
elements.should.have.length(2)
elements.should.containEql(a)
elements.should.containEql(b)
done()
})
it('creates a model that is a jsmf element', function(done) {
const foo = new Model('Foo')
JSMF.isJSMFElement(foo).should.be.true()
done()
})
describe('crop', function() {
it('remove references to elements that are not in the model', function(done) {
var A = new Class('A')
var B = new Class('B')
A.addReference('b', B)
var a = new A()
var b = new B()
a.b = b
var M = new Model('Bar', {}, [a], true)
M.crop()
a.b.should.be.empty()
done()
})
it('remove references to elements that are not in the model', function(done) {
var A = new Class('A')
var B = new Class('B')
A.addReference('b', B, 1, 'back', 1, A)
var a = new A()
var b = new B()
a.addB(b, a)
var M = new Model('Bar', {}, [a], true)
M.crop()
a.b.should.be.empty()
a.getAssociated().b.should.be.empty()
done()
})
it ('creates a model with a given reference model', function(done) {
const MM = new Model('Foo')
const M = new Model('Bar', MM)
M.referenceModel.should.equal(MM)
done()
})
it ('adds the given classes to the model', function(done) {
const A = new Class('A')
const B = new Class('B')
const M = new Model('Bar', {}, [A, B])
M.modellingElements.should.have.property('Class', [A, B])
M.classes.should.have.property('A', [A])
M.classes.should.have.property('B', [B])
done()
})
it ('adds the given elements to the model', function(done) {
const A = new Class('A')
const a = new A()
const B = new Class('B')
const b = new B()
const M = new Model('Bar', {}, [a, b])
M.modellingElements.should.have.property('A', [a])
M.modellingElements.should.have.property('B', [b])
done()
})
it ('does not add the given elements transitively if not mentioned', function(done) {
const A = new Class('A')
const B = new Class('B')
A.addReference('b', B)
const a = new A()
const b = new B()
a.b = b
const M = new Model('Bar', {}, [a])
M.modellingElements.should.have.property('A', [a])
M.modellingElements.should.not.have.property('B')
done()
})
it ('adds the given elements transitively if mentioned', function(done) {
const A = new Class('A')
const B = new Class('B')
A.addReference('b', B)
const a = new A()
const b = new B()
a.b = b
const M = new Model('Bar', {}, [a], true)
M.modellingElements.should.have.property('A', [a])
M.modellingElements.should.have.property('B', [b])
done()
})
})
describe('elements', function() {
it('lists all the elements of the model', function(done) {
const A = new Class('A')
const B = new Class('B')
A.addReference('b', B)
const a = new A()
const b = new B()
a.b = b
const M = new Model('Bar', {}, [a, b], true)
const elements = M.elements()
elements.should.have.length(2)
elements.should.containEql(a)
elements.should.containEql(b)
done()
})
})
describe('crop', function() {
it('remove references to elements that are not in the model', function(done) {
const A = new Class('A')
const B = new Class('B')
A.addReference('b', B)
const a = new A()
const b = new B()
a.b = b
const M = new Model('Bar', {}, [a], true)
M.crop()
a.b.should.be.empty()
done()
})
it('remove references to elements that are not in the model', function(done) {
const A = new Class('A')
const B = new Class('B')
A.addReference('b', B, 1, 'back', 1, A)
const a = new A()
const b = new B()
a.addB(b, a)
const M = new Model('Bar', {}, [a], true)
M.crop()
a.b.should.be.empty()
a.getAssociated().b.should.be.empty()
done()
})
})
})
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