Skip to content

Commit

Permalink
feature(core) Add from model files call to model loader
Browse files Browse the repository at this point in the history
Signed-off-by: Jerome Simeon <[email protected]>
  • Loading branch information
jeromesimeon committed Feb 26, 2020
1 parent 43867e1 commit 93ece98
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 8 deletions.
3 changes: 2 additions & 1 deletion packages/concerto-core/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,15 @@ class ValidatedResource extends Resource {
}
class ModelLoader {
+ object loadModelManager(string,string[])
+ object loadModelManagerFromModelFiles(string,object[],undefined)
}
class ModelManager {
+ void constructor()
+ void validateModelFile(string,string) throws IllegalModelException
+ Object addModelFile(string,string,boolean,boolean) throws IllegalModelException
+ Object updateModelFile(string,string,boolean) throws IllegalModelException
+ void deleteModelFile(string)
+ Object[] addModelFiles(string[],undefined,boolean,boolean)
+ Object[] addModelFiles(object[],undefined,boolean,boolean)
+ void validateModelFiles()
+ Promise updateExternalModels(Object,ModelFileDownloader) throws IllegalModelException
+ void writeModelsToFileSystem(String,Object,boolean,boolean)
Expand Down
3 changes: 3 additions & 0 deletions packages/concerto-core/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
# Note that the latest public API is documented using JSDocs and is available in api.txt.
#

Version 0.82.6 {03fa6481ffdf0e58cb110c6f24009d18} 2020-02-26
- Update JsDoc for ModelManager.addModelFiles

Version 0.82.5 {d4d49fabba1c6ce465c446e2dccad488} 2019-11-10
- Add instance of alternatives to Factory and Serializer

Expand Down
23 changes: 23 additions & 0 deletions packages/concerto-core/lib/modelloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,29 @@ class ModelLoader {
return modelManager;
}

/**
* Load system and models in a new model manager from model files objects
*
* @param {string} ctoSystemFile - the system model file
* @param {object[]} modelFiles - An array of Concerto files as strings or ModelFile objects.
* @param {string[]} [fileNames] - An optional array of file names to associate with the model files
* @return {object} the model manager
*/
static async loadModelManagerFromModelFiles(ctoSystemFile, modelFiles, fileNames) {
let modelManager = new ModelManager();
const modelFileLoader = new DefaultModelFileLoader(modelManager);

// Load system model
modelManager = await ModelLoader.addModel(modelFileLoader,modelManager,ctoSystemFile,true);
modelManager.addModelFiles(modelFiles, fileNames);

// Load user models

// Validate update models
await modelManager.updateExternalModels();
return modelManager;
}

}

module.exports = ModelLoader;
8 changes: 3 additions & 5 deletions packages/concerto-core/lib/modelmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,13 @@ class ModelManager {

/**
* Add a set of Concerto files to the model manager.
* @param {string[]} modelFiles - An array of Concerto files as
* strings.
* @param {string[]} [fileNames] - An optional array of file names to
* associate with the model files
* @param {object[]} modelFiles - An array of Concerto files as strings or ModelFile objects.
* @param {string[]} [fileNames] - An optional array of file names to associate with the model files
* @param {boolean} [disableValidation] - If true then the model files are not validated
* @param {boolean} [systemModelTable] - A table that maps classes in the new models to system types
* @returns {Object[]} The newly added model files (internal).
*/
addModelFiles(modelFiles, fileNames, disableValidation,systemModelTable) {
addModelFiles(modelFiles, fileNames, disableValidation, systemModelTable) {
const NAME = 'addModelFiles';
debug(NAME, 'addModelFiles', modelFiles, fileNames);
const originalModelFiles = {};
Expand Down
54 changes: 53 additions & 1 deletion packages/concerto-core/test/modelloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('ModelLoader', () => {
afterEach(() => {
});

describe('#loadModelFromFile', function() {
describe('#loadModelManager', function() {
it('should load models', async function() {
const modelManager = await ModelLoader.loadModelManager(null, [modelBase]);
(function() {
Expand Down Expand Up @@ -71,6 +71,58 @@ describe('ModelLoader', () => {
});
});

describe('#loadModelManagerFromModelFiles', function() {
it('should load models', async function() {
const modelManager = await ModelLoader.loadModelManager(null, [modelBase]);
const files = modelManager.getModelFiles()
.filter(f => !f.isSystemModelFile())
.map(f => f.definitions);
const modelManager2 = await ModelLoader.loadModelManagerFromModelFiles(null, files);
(function() {
modelManager2.getType('String');
}).should.throw(TypeNotFoundException);
});

it('should throw an error for a namespace that does not exist', async function() {
const modelManager = await ModelLoader.loadModelManager(null, [modelBase]);
const files = modelManager.getModelFiles()
.filter(f => !f.isSystemModelFile())
.map(f => f.definitions);
const modelManager2 = await ModelLoader.loadModelManagerFromModelFiles(null, files);
(function() {
modelManager2.getType('org.acme.nosuchns.SimpleAsset');
}).should.throw(TypeNotFoundException, /org.acme.nosuchns/);
});

it('should throw an error for an empty namespace', async function() {
const modelManager = await ModelLoader.loadModelManager(null, [modelBase]);
const files = modelManager.getModelFiles()
.filter(f => !f.isSystemModelFile())
.map(f => f.definitions);
const modelManager2 = await ModelLoader.loadModelManagerFromModelFiles(null, files);
(function() {
modelManager2.getType('NoSuchAsset');
}).should.throw(TypeNotFoundException, /NoSuchAsset/);
});

it('should throw an error for a type that does not exist', async function() {
const modelManager = await ModelLoader.loadModelManager(null, [modelBase]);
const files = modelManager.getModelFiles()
.filter(f => !f.isSystemModelFile())
.map(f => f.definitions);
const modelManager2 = await ModelLoader.loadModelManagerFromModelFiles(null, files);
(function() {
modelManager2.getType('org.acme.base.NoSuchAsset');
}).should.throw(TypeNotFoundException, /NoSuchAsset/);
});

it('should return the class declaration for a valid type', async function() {
const modelManager = await ModelLoader.loadModelManager(null, [modelBase]);
const declaration = modelManager.getType('org.acme.base.AbstractAsset');
declaration.getFullyQualifiedName().should.equal('org.acme.base.AbstractAsset');
});
});

describe('#loadModelFromUrl', function() {
it('should load models', async function() {
const modelManager = await ModelLoader.loadModelManager(null, [modelUrl]);
Expand Down
2 changes: 1 addition & 1 deletion packages/concerto-core/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ declare module '@accordproject/concerto-core' {
private getSystemModelTable(): any;
updateModelFile(modelFile: string, fileName?: string, disableValidation?: boolean): any;
deleteModelFile(namespace: string): void;
addModelFiles(modelFiles: string[], fileNames?: string[], disableValidation?: boolean, systemModelTable?: boolean): any[];
addModelFiles(modelFiles: (string|ModelFile)[], fileNames?: string[], disableValidation?: boolean, systemModelTable?: boolean): any[];
validateModelFiles(): void;
updateExternalModels(options?: any, modelFileDownloader?: ModelFileDownloader): Promise<ModelFile[]>;
writeModelsToFileSystem(path: string, options?: IncludeModelsOptions): void;
Expand Down

0 comments on commit 93ece98

Please sign in to comment.