Skip to content

Commit 93ece98

Browse files
committed
feature(core) Add from model files call to model loader
Signed-off-by: Jerome Simeon <[email protected]>
1 parent 43867e1 commit 93ece98

File tree

6 files changed

+85
-8
lines changed

6 files changed

+85
-8
lines changed

packages/concerto-core/api.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,15 @@ class ValidatedResource extends Resource {
182182
}
183183
class ModelLoader {
184184
+ object loadModelManager(string,string[])
185+
+ object loadModelManagerFromModelFiles(string,object[],undefined)
185186
}
186187
class ModelManager {
187188
+ void constructor()
188189
+ void validateModelFile(string,string) throws IllegalModelException
189190
+ Object addModelFile(string,string,boolean,boolean) throws IllegalModelException
190191
+ Object updateModelFile(string,string,boolean) throws IllegalModelException
191192
+ void deleteModelFile(string)
192-
+ Object[] addModelFiles(string[],undefined,boolean,boolean)
193+
+ Object[] addModelFiles(object[],undefined,boolean,boolean)
193194
+ void validateModelFiles()
194195
+ Promise updateExternalModels(Object,ModelFileDownloader) throws IllegalModelException
195196
+ void writeModelsToFileSystem(String,Object,boolean,boolean)

packages/concerto-core/changelog.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
# Note that the latest public API is documented using JSDocs and is available in api.txt.
2525
#
2626

27+
Version 0.82.6 {03fa6481ffdf0e58cb110c6f24009d18} 2020-02-26
28+
- Update JsDoc for ModelManager.addModelFiles
29+
2730
Version 0.82.5 {d4d49fabba1c6ce465c446e2dccad488} 2019-11-10
2831
- Add instance of alternatives to Factory and Serializer
2932

packages/concerto-core/lib/modelloader.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,29 @@ class ModelLoader {
9494
return modelManager;
9595
}
9696

97+
/**
98+
* Load system and models in a new model manager from model files objects
99+
*
100+
* @param {string} ctoSystemFile - the system model file
101+
* @param {object[]} modelFiles - An array of Concerto files as strings or ModelFile objects.
102+
* @param {string[]} [fileNames] - An optional array of file names to associate with the model files
103+
* @return {object} the model manager
104+
*/
105+
static async loadModelManagerFromModelFiles(ctoSystemFile, modelFiles, fileNames) {
106+
let modelManager = new ModelManager();
107+
const modelFileLoader = new DefaultModelFileLoader(modelManager);
108+
109+
// Load system model
110+
modelManager = await ModelLoader.addModel(modelFileLoader,modelManager,ctoSystemFile,true);
111+
modelManager.addModelFiles(modelFiles, fileNames);
112+
113+
// Load user models
114+
115+
// Validate update models
116+
await modelManager.updateExternalModels();
117+
return modelManager;
118+
}
119+
97120
}
98121

99122
module.exports = ModelLoader;

packages/concerto-core/lib/modelmanager.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,13 @@ class ModelManager {
210210

211211
/**
212212
* Add a set of Concerto files to the model manager.
213-
* @param {string[]} modelFiles - An array of Concerto files as
214-
* strings.
215-
* @param {string[]} [fileNames] - An optional array of file names to
216-
* associate with the model files
213+
* @param {object[]} modelFiles - An array of Concerto files as strings or ModelFile objects.
214+
* @param {string[]} [fileNames] - An optional array of file names to associate with the model files
217215
* @param {boolean} [disableValidation] - If true then the model files are not validated
218216
* @param {boolean} [systemModelTable] - A table that maps classes in the new models to system types
219217
* @returns {Object[]} The newly added model files (internal).
220218
*/
221-
addModelFiles(modelFiles, fileNames, disableValidation,systemModelTable) {
219+
addModelFiles(modelFiles, fileNames, disableValidation, systemModelTable) {
222220
const NAME = 'addModelFiles';
223221
debug(NAME, 'addModelFiles', modelFiles, fileNames);
224222
const originalModelFiles = {};

packages/concerto-core/test/modelloader.js

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('ModelLoader', () => {
3535
afterEach(() => {
3636
});
3737

38-
describe('#loadModelFromFile', function() {
38+
describe('#loadModelManager', function() {
3939
it('should load models', async function() {
4040
const modelManager = await ModelLoader.loadModelManager(null, [modelBase]);
4141
(function() {
@@ -71,6 +71,58 @@ describe('ModelLoader', () => {
7171
});
7272
});
7373

74+
describe('#loadModelManagerFromModelFiles', function() {
75+
it('should load models', async function() {
76+
const modelManager = await ModelLoader.loadModelManager(null, [modelBase]);
77+
const files = modelManager.getModelFiles()
78+
.filter(f => !f.isSystemModelFile())
79+
.map(f => f.definitions);
80+
const modelManager2 = await ModelLoader.loadModelManagerFromModelFiles(null, files);
81+
(function() {
82+
modelManager2.getType('String');
83+
}).should.throw(TypeNotFoundException);
84+
});
85+
86+
it('should throw an error for a namespace that does not exist', async function() {
87+
const modelManager = await ModelLoader.loadModelManager(null, [modelBase]);
88+
const files = modelManager.getModelFiles()
89+
.filter(f => !f.isSystemModelFile())
90+
.map(f => f.definitions);
91+
const modelManager2 = await ModelLoader.loadModelManagerFromModelFiles(null, files);
92+
(function() {
93+
modelManager2.getType('org.acme.nosuchns.SimpleAsset');
94+
}).should.throw(TypeNotFoundException, /org.acme.nosuchns/);
95+
});
96+
97+
it('should throw an error for an empty namespace', async function() {
98+
const modelManager = await ModelLoader.loadModelManager(null, [modelBase]);
99+
const files = modelManager.getModelFiles()
100+
.filter(f => !f.isSystemModelFile())
101+
.map(f => f.definitions);
102+
const modelManager2 = await ModelLoader.loadModelManagerFromModelFiles(null, files);
103+
(function() {
104+
modelManager2.getType('NoSuchAsset');
105+
}).should.throw(TypeNotFoundException, /NoSuchAsset/);
106+
});
107+
108+
it('should throw an error for a type that does not exist', async function() {
109+
const modelManager = await ModelLoader.loadModelManager(null, [modelBase]);
110+
const files = modelManager.getModelFiles()
111+
.filter(f => !f.isSystemModelFile())
112+
.map(f => f.definitions);
113+
const modelManager2 = await ModelLoader.loadModelManagerFromModelFiles(null, files);
114+
(function() {
115+
modelManager2.getType('org.acme.base.NoSuchAsset');
116+
}).should.throw(TypeNotFoundException, /NoSuchAsset/);
117+
});
118+
119+
it('should return the class declaration for a valid type', async function() {
120+
const modelManager = await ModelLoader.loadModelManager(null, [modelBase]);
121+
const declaration = modelManager.getType('org.acme.base.AbstractAsset');
122+
declaration.getFullyQualifiedName().should.equal('org.acme.base.AbstractAsset');
123+
});
124+
});
125+
74126
describe('#loadModelFromUrl', function() {
75127
it('should load models', async function() {
76128
const modelManager = await ModelLoader.loadModelManager(null, [modelUrl]);

packages/concerto-core/types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ declare module '@accordproject/concerto-core' {
324324
private getSystemModelTable(): any;
325325
updateModelFile(modelFile: string, fileName?: string, disableValidation?: boolean): any;
326326
deleteModelFile(namespace: string): void;
327-
addModelFiles(modelFiles: string[], fileNames?: string[], disableValidation?: boolean, systemModelTable?: boolean): any[];
327+
addModelFiles(modelFiles: (string|ModelFile)[], fileNames?: string[], disableValidation?: boolean, systemModelTable?: boolean): any[];
328328
validateModelFiles(): void;
329329
updateExternalModels(options?: any, modelFileDownloader?: ModelFileDownloader): Promise<ModelFile[]>;
330330
writeModelsToFileSystem(path: string, options?: IncludeModelsOptions): void;

0 commit comments

Comments
 (0)