Skip to content

Commit

Permalink
fix(types): refactor metamodel object into class (contributes to acco…
Browse files Browse the repository at this point in the history
…rdproject#402)

Signed-off-by: Simon Stone <[email protected]>
  • Loading branch information
Simon Stone authored and Simon Stone committed Mar 22, 2022
1 parent 75c5b42 commit a183bbf
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 66 deletions.
6 changes: 4 additions & 2 deletions packages/concerto-core/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,11 @@ class Introspector {
+ ClassDeclaration[] getClassDeclarations()
+ ClassDeclaration getClassDeclaration(String) throws Error
}
class MetaModel {
+ void newMetaModelManager()
+ object validateMetaModel()
+ object modelManagerFromMetaModel()
+ object validateMetaModel(object)
+ object modelManagerFromMetaModel(object,boolean?)
}
class ModelFile {
+ void constructor(ModelManager,object,string?,string?) throws IllegalModelException
+ boolean isModelFile()
Expand Down
4 changes: 4 additions & 0 deletions packages/concerto-core/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
# Note that the latest public API is documented using JSDocs and is available in api.txt.
#

Version 2.0.0-alpha.2 {d2a653a4b1e77745acb98e9e31ebd72d} 2021-03-22
- Refactor MetaModel into a class with static functions
- Refactor index.js exports for better TypeScript types

Version 2.0.0-alpha.1 {ee5fa0479fe2720d720ac500b1b3ead8} 2021-11-24
- Remove custom instanceof and add methods to check runtime type
- Remove support for Node 12
Expand Down
99 changes: 51 additions & 48 deletions packages/concerto-core/lib/introspect/metamodel.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,59 +22,62 @@ const Serializer = require('../serializer');
const ModelFile = require('../introspect/modelfile');

/**
* Create a metamodel manager (for validation against the metamodel)
* @return {*} the metamodel manager
* Class containing static helper methods for the metamodel.
*/
function newMetaModelManager() {
const metaModelManager = new ModelManager();
const mf = new ModelFile(
metaModelManager,
MetaModelUtil.metaModelAst,
MetaModelUtil.metaModelCto,
'concerto.metamodel',
true
);
metaModelManager.addModelFile(mf, MetaModelUtil.metaModelCto, 'concerto.metamodel');
return metaModelManager;
}
class MetaModel {

/**
* Validate metamodel instance against the metamodel
* @param {object} input - the metamodel instance in JSON
* @return {object} the validated metamodel instance in JSON
*/
function validateMetaModel(input) {
const metaModelManager = newMetaModelManager();
const factory = new Factory(metaModelManager);
const serializer = new Serializer(factory, metaModelManager);
// First validate the metaModel
const object = serializer.fromJSON(input);
return serializer.toJSON(object);
}
/**
* Create a metamodel manager (for validation against the metamodel)
* @return {*} the metamodel manager
*/
static newMetaModelManager() {
const metaModelManager = new ModelManager();
const mf = new ModelFile(
metaModelManager,
MetaModelUtil.metaModelAst,
MetaModelUtil.metaModelCto,
'concerto.metamodel',
true
);
metaModelManager.addModelFile(mf, MetaModelUtil.metaModelCto, 'concerto.metamodel');
return metaModelManager;
}

/**
* Import metamodel to a model manager
* @param {object} metaModel - the metamodel
* @param {boolean} [validate] - whether to perform validation
* @return {object} the metamodel for this model manager
*/
function modelManagerFromMetaModel(metaModel, validate = true) {
// First, validate the JSON metaModel
const mm = validate ? validateMetaModel(metaModel) : metaModel;
/**
* Validate metamodel instance against the metamodel
* @param {object} input - the metamodel instance in JSON
* @return {object} the validated metamodel instance in JSON
*/
static validateMetaModel(input) {
const metaModelManager = MetaModel.newMetaModelManager();
const factory = new Factory(metaModelManager);
const serializer = new Serializer(factory, metaModelManager);
// First validate the metaModel
const object = serializer.fromJSON(input);
return serializer.toJSON(object);
}

/**
* Import metamodel to a model manager
* @param {object} metaModel - the metamodel
* @param {boolean} [validate] - whether to perform validation
* @return {object} the metamodel for this model manager
*/
static modelManagerFromMetaModel(metaModel, validate = true) {
// First, validate the JSON metaModel
const mm = validate ? MetaModel.validateMetaModel(metaModel) : metaModel;

const modelManager = new ModelManager();

const modelManager = new ModelManager();
mm.models.forEach((mm) => {
const mf = new ModelFile(modelManager, mm, null, null, true);
modelManager.addModelFile(mf, null, null);
});

mm.models.forEach((mm) => {
const mf = new ModelFile(modelManager, mm, null, null, true);
modelManager.addModelFile(mf, null, null);
});
modelManager.validateModelFiles();
return modelManager;
}

modelManager.validateModelFiles();
return modelManager;
}

module.exports = {
newMetaModelManager,
validateMetaModel,
modelManagerFromMetaModel
};
module.exports = MetaModel;
38 changes: 22 additions & 16 deletions packages/concerto-core/types/lib/introspect/metamodel.d.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
export = MetaModel;
/**
* Create a metamodel manager (for validation against the metamodel)
* @return {*} the metamodel manager
* Class containing static helper methods for the metamodel.
*/
export function newMetaModelManager(): any;
/**
* Validate metamodel instance against the metamodel
* @param {object} input - the metamodel instance in JSON
* @return {object} the validated metamodel instance in JSON
*/
export function validateMetaModel(input: object): object;
/**
* Import metamodel to a model manager
* @param {object} metaModel - the metamodel
* @param {boolean} [validate] - whether to perform validation
* @return {object} the metamodel for this model manager
*/
export function modelManagerFromMetaModel(metaModel: object, validate?: boolean): object;
declare class MetaModel {
/**
* Create a metamodel manager (for validation against the metamodel)
* @return {*} the metamodel manager
*/
static newMetaModelManager(): any;
/**
* Validate metamodel instance against the metamodel
* @param {object} input - the metamodel instance in JSON
* @return {object} the validated metamodel instance in JSON
*/
static validateMetaModel(input: object): object;
/**
* Import metamodel to a model manager
* @param {object} metaModel - the metamodel
* @param {boolean} [validate] - whether to perform validation
* @return {object} the metamodel for this model manager
*/
static modelManagerFromMetaModel(metaModel: object, validate?: boolean): object;
}

0 comments on commit a183bbf

Please sign in to comment.