Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(decorator): added new methods to extract vocab or non-vocab decorators from model #888

Merged
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/concerto-core/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@ class Concerto {
}
+ object setCurrentTime()
class DecoratorExtractor {
+ void constructor(boolean,string,string,Object)
+ void constructor(boolean,string,string,Object,int)
}
class DecoratorManager {
+ ModelManager validate(decoratorCommandSet,ModelFile[]) throws Error
+ ModelManager decorateModels(ModelManager,decoratorCommandSet,object?,boolean?,boolean?,boolean?,boolean?)
+ ExtractDecoratorsResult extractDecorators(ModelManager,object,boolean,string)
+ ExtractDecoratorsResult extractVocabDecorators(ModelManager,object,boolean,string)
+ ExtractDecoratorsResult extractNonVocabDecorators(ModelManager,object,boolean,string)
+ void validateCommand(ModelManager,command)
+ Boolean falsyOrEqual(string||,string[])
+ void applyDecorator(decorated,string,newDecorator)
Expand Down
5 changes: 5 additions & 0 deletions packages/concerto-core/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
# Note that the latest public API is documented using JSDocs and is available in api.txt.
#

Version 3.17.3 {da7430db25daa6d54cac03c13ef06b01} 2024-07-26
- Added new methods to extract vocab or non-vocab decorators from model
- Fixed some minor bugs related to vocabulary parsing
- Exposed method to validate locale

Version 3.17.2 {eb3903401fdcf7c26cca1f3f8a029171} 2024-07-17
- Added new optimized methods for decorating models with decorator commandsets
- fixed other decorator command set bugs
Expand Down
97 changes: 72 additions & 25 deletions packages/concerto-core/lib/decoratorextractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,49 @@ const { MetaModelNamespace } = require('@accordproject/concerto-metamodel');
* @memberof module:concerto-core
*/
class DecoratorExtractor {
sanketshevkar marked this conversation as resolved.
Show resolved Hide resolved
/**
* The action to be performed to extract all, only vocab or only non-vocab decorators
*/
static Action = {
sanketshevkar marked this conversation as resolved.
Show resolved Hide resolved
EXTRACT_ALL: 0,
EXTRACT_VOCAB: 1,
EXTRACT_NON_VOCAB: 2
};

/**
* Create the DecoratorExtractor.
* @constructor
* @param {boolean} removeDecoratorsFromModel - flag to determine whether to remove decorators from source model
* @param {string} locale - locale for extracted vocabularies
* @param {string} dcs_version - version string
* @param {Object} sourceModelAst - the ast of source models
* @param {int} action - the action to be performed
sanketshevkar marked this conversation as resolved.
Show resolved Hide resolved
*/
constructor(removeDecoratorsFromModel, locale, dcs_version, sourceModelAst) {
constructor(removeDecoratorsFromModel, locale, dcs_version, sourceModelAst, action) {
this.extractionDictionary = {};
this.removeDecoratorsFromModel = removeDecoratorsFromModel;
this.locale = locale;
this.dcs_version = dcs_version;
this.sourceModelAst = sourceModelAst;
this.updatedModelAst = sourceModelAst;
if (action) {
if (Object.values(DecoratorExtractor.Action).includes(action)) {
this.action = action;
} else {
this.action = DecoratorExtractor.Action.EXTRACT_ALL;
}
}
}

/**
* Returns if the decorator is vocab or not
* @param {string} decoractorName - the name of decorator
* @returns {boolean} - returns true if the decorator is a vocabulary decorator else false
* @private
*/
isVocabDecorator(decoractorName) {
const vocabPattern = /^Term_/;
return decoractorName === 'Term' || vocabPattern.test(decoractorName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if you recreate the RegEx object at each call, it would be slower than just using startsWith. Is there a reason you want to put the vocabPattern in the function body?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct, we can use startWith instead of regex. Earlier it was having /^Term_/i as vocabPattern to check it in case insensitive manner.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could have also move the regex to constructor and use the compiled regex. There are trade offs using either

}
/**
* Adds a key-value pair to a dictionary (object) if the key exists,
Expand Down Expand Up @@ -105,18 +133,23 @@ class DecoratorExtractor {
Object.keys(vocabObject).forEach(decl =>{
if (vocabObject[decl].term){
strVoc += ` - ${decl}: ${vocabObject[decl].term}\n`;
const otherProps = Object.keys(vocabObject[decl]).filter((str)=>str !== 'term' && str !== 'propertyVocabs');
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a yaml library that we can use instead of directly parsing the string? Maybe not for this PR but that would help a lot.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can directly create the obj in expected yaml format and use the yaml library to stringify the object or parse the string to yaml obj.

const otherProps = Object.keys(vocabObject[decl]).filter((str)=>str !== 'term' && str !== 'propertyVocabs');
if(otherProps.length > 0){
if (!vocabObject[decl].term){
ekarademir marked this conversation as resolved.
Show resolved Hide resolved
strVoc += ` - ${decl}: ${decl}\n`;
}
otherProps.forEach(key =>{
strVoc += ` ${key}: ${vocabObject[decl][key]}\n`;
});
}
if (vocabObject[decl].propertyVocabs && Object.keys(vocabObject[decl].propertyVocabs).length > 0){
if (!vocabObject[decl].term){
if (!vocabObject[decl].term && otherProps.length === 0){
strVoc += ` - ${decl}: ${decl}\n`;
}
strVoc += ' properties:\n';
Object.keys(vocabObject[decl].propertyVocabs).forEach(prop =>{
strVoc += ` - ${prop}: ${vocabObject[decl].propertyVocabs[prop].term}\n`;
strVoc += ` - ${prop}: ${vocabObject[decl].propertyVocabs[prop].term || ''}\n`;
ekarademir marked this conversation as resolved.
Show resolved Hide resolved
const otherProps = Object.keys(vocabObject[decl].propertyVocabs[prop]).filter((str)=>str !== 'term');
otherProps.forEach(key =>{
strVoc += ` ${key}: ${vocabObject[decl].propertyVocabs[prop][key]}\n`;
Expand Down Expand Up @@ -227,30 +260,54 @@ class DecoratorExtractor {
let vocabData = [];
Object.keys(this.extractionDictionary).forEach(namespace => {
const jsonData = this.extractionDictionary[namespace];
const patternToDetermineVocab = /^Term_/i;
let dcsObjects = [];
let vocabObject = {};
jsonData.forEach(obj =>{
const decos = JSON.parse(obj.dcs);
const target = this.constructTarget(namespace, obj);
decos.forEach(dcs =>{
if (dcs.name !== 'Term' && !patternToDetermineVocab.test(dcs.name)){
const isVocab = this.isVocabDecorator(dcs.name);
if (!isVocab && this.action !== DecoratorExtractor.Action.EXTRACT_VOCAB){
dcsObjects = this.parseNonVocabularyDecorators(dcsObjects, dcs, this.dcs_version, target);
}
else {
if (isVocab && this.action !== DecoratorExtractor.Action.EXTRACT_NON_VOCAB){
vocabObject = this.parseVocabularies(vocabObject, obj, dcs);
}
});
});
decoratorData = this.transformNonVocabularyDecorators(dcsObjects, namespace, decoratorData);
vocabData = this.transformVocabularyDecorators(vocabObject, namespace, vocabData);
if(this.action !== DecoratorExtractor.Action.EXTRACT_VOCAB){
decoratorData = this.transformNonVocabularyDecorators(dcsObjects, namespace, decoratorData);
}
if(this.action !== DecoratorExtractor.Action.EXTRACT_NON_VOCAB){
vocabData = this.transformVocabularyDecorators(vocabObject, namespace, vocabData);
}
});
return {
decoratorCommandSet: decoratorData,
vocabularies: vocabData
};
}

/**
* Filter vocab or non-vocab decorators
* @param {Object} decorators - the collection of decorators
* @returns {Object} - the collection of filtered decorators
* @private
*/
filterDecorators(decorators){
if (this.action === DecoratorExtractor.Action.EXTRACT_ALL){
return undefined;
}
else if(this.action === DecoratorExtractor.Action.EXTRACT_VOCAB){
return decorators.filter((dcs) => {
return !this.isVocabDecorator(dcs.name);
});
}
else if(this.action === DecoratorExtractor.Action.EXTRACT_NON_VOCAB){
return decorators.filter((dcs) => {
return this.isVocabDecorator(dcs.name);
});
}
}
/**
* Process the map declarations to extract the decorators.
*
Expand All @@ -267,9 +324,7 @@ class DecoratorExtractor {
mapElement: 'KEY'
};
this.constructDCSDictionary(namespace, declaration.key.decorators, constructOptions);
if (this.removeDecoratorsFromModel){
declaration.key.decorators = undefined;
}
declaration.key.decorators = this.filterDecorators(declaration.key.decorators);
}
}
if (declaration.value){
Expand All @@ -279,9 +334,7 @@ class DecoratorExtractor {
mapElement: 'VALUE'
};
this.constructDCSDictionary(namespace, declaration.value.decorators, constructOptions);
if (this.removeDecoratorsFromModel){
declaration.value.decorators = undefined;
}
declaration.value.decorators = this.filterDecorators(declaration.value.decorators);
}
}
return declaration;
Expand All @@ -304,9 +357,7 @@ class DecoratorExtractor {
property: property.name
};
this.constructDCSDictionary(namespace, property.decorators, constructOptions );
}
if (this.removeDecoratorsFromModel){
property.decorators = undefined;
property.decorators = this.filterDecorators(property.decorators);
}
return property;
});
Expand All @@ -328,9 +379,7 @@ class DecoratorExtractor {
declaration: decl.name,
};
this.constructDCSDictionary(namespace, decl.decorators, constructOptions);
}
if (this.removeDecoratorsFromModel){
decl.decorators = undefined;
decl.decorators = this.filterDecorators(decl.decorators);
}
if (decl.$class === `${MetaModelNamespace}.MapDeclaration`) {
const processedMapDecl = this.processMapDeclaration(decl, namespace);
Expand All @@ -354,9 +403,7 @@ class DecoratorExtractor {
const processedModels = this.sourceModelAst.models.map(model =>{
if ((model?.decorators.length > 0)){
this.constructDCSDictionary(model.namespace, model.decorators, {});
if (this.removeDecoratorsFromModel){
model.decorators = undefined;
}
model.decorators = this.filterDecorators(model.decorators);
}
const processedDecl = this.processDeclarations(model.declarations, model.namespace);
model.declarations = processedDecl;
Expand Down
55 changes: 47 additions & 8 deletions packages/concerto-core/lib/decoratormanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,15 +424,11 @@ class DecoratorManager {
return newModelManager;
}
/**
* @typedef decoratorCommandSet
* @type {object}
* @typedef vocabularies
* @type {string}
* @typedef ExtractDecoratorsResult
* @type {object}
* @property {ModelManager} modelManager - A model manager containing models stripped without decorators
* @property {decoratorCommandSet} object[] - Stripped out decorators, formed into decorator command sets
* @property {vocabularies} object[] - Stripped out vocabularies, formed into vocabulary files
* @property {object[]} decoratorCommandSet - Stripped out decorators, formed into decorator command sets
sanketshevkar marked this conversation as resolved.
Show resolved Hide resolved
* @property {string[]} vocabularies - Stripped out vocabularies, formed into vocabulary files
*/
/**
* Extracts all the decorator commands from all the models in modelManager
Expand All @@ -449,15 +445,58 @@ class DecoratorManager {
...options
};
const sourceAst = modelManager.getAst(true);
const decoratorExtrator = new DecoratorExtractor(options.removeDecoratorsFromModel, options.locale, DCS_VERSION, sourceAst);
const decoratorExtrator = new DecoratorExtractor(options.removeDecoratorsFromModel, options.locale, DCS_VERSION, sourceAst, DecoratorExtractor.Action.EXTRACT_ALL);
const collectionResp = decoratorExtrator.extract();
return {
modelManager: collectionResp.updatedModelManager,
decoratorCommandSet: collectionResp.decoratorCommandSet,
vocabularies: collectionResp.vocabularies
};
}

/**
* Extracts all the vocab decorator commands from all the models in modelManager
* @param {ModelManager} modelManager the input model manager
* @param {object} options - decorator models options
* @param {boolean} options.removeDecoratorsFromModel - flag to strip out vocab decorators from models
* @param {string} options.locale - locale for extracted vocabulary set
* @returns {ExtractDecoratorsResult} - a new model manager with/without the decorators and vocab yamls
*/
static extractVocabDecorators(modelManager,options) {
options = {
removeDecoratorsFromModel: false,
locale:'en',
...options
};
const sourceAst = modelManager.getAst(true);
const decoratorExtrator = new DecoratorExtractor(options.removeDecoratorsFromModel, options.locale, DCS_VERSION, sourceAst, DecoratorExtractor.Action.EXTRACT_VOCAB);
const collectionResp = decoratorExtrator.extract();
return {
modelManager: collectionResp.updatedModelManager,
vocabularies: collectionResp.vocabularies
sanketshevkar marked this conversation as resolved.
Show resolved Hide resolved
};
}
/**
* Extracts all the non-vocab decorator commands from all the models in modelManager
* @param {ModelManager} modelManager the input model manager
* @param {object} options - decorator models options
* @param {boolean} options.removeDecoratorsFromModel - flag to strip out non-vocab decorators from models
* @param {string} options.locale - locale for extracted vocabulary set
* @returns {ExtractDecoratorsResult} - a new model manager with/without the decorators and a list of extracted decorator jsons
*/
static extractNonVocabDecorators(modelManager,options) {
options = {
removeDecoratorsFromModel: false,
locale:'en',
...options
};
const sourceAst = modelManager.getAst(true);
const decoratorExtrator = new DecoratorExtractor(options.removeDecoratorsFromModel, options.locale, DCS_VERSION, sourceAst, DecoratorExtractor.Action.EXTRACT_NON_VOCAB);
const collectionResp = decoratorExtrator.extract();
return {
modelManager: collectionResp.updatedModelManager,
decoratorCommandSet: collectionResp.decoratorCommandSet
};
}
/**
* Throws an error if the decoractor command is invalid
* @param {ModelManager} validationModelManager the validation model manager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ map mapName {
}

@Term("Person Class")
@Term_desc("Person Class Description")
@Editable
concept Person {
@Term("HI")
Expand All @@ -43,4 +44,4 @@ concept Person {
@Form("inputType", "text")
@New
o String ssn
}
}
26 changes: 25 additions & 1 deletion packages/concerto-core/test/decoratormanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ describe('DecoratorManager', () => {
});

describe('#extractDecorators', function() {
it('should be able to extract decorators and vocabs from a model withoup options', async function() {
it('should be able to extract decorators and vocabs from a model without options', async function() {
const testModelManager = new ModelManager({strict:true,});
const modelText = fs.readFileSync(path.join(__dirname,'/data/decoratorcommands/extract-test.cto'), 'utf-8');
testModelManager.addCTOModel(modelText, 'test.cto');
Expand Down Expand Up @@ -674,6 +674,30 @@ describe('DecoratorManager', () => {
const vocab = resp.vocabularies;
vocab.should.be.deep.equal([]);
});
it('should be able to extract vocabs from a model', async function() {
const testModelManager = new ModelManager({strict:true,});
const modelText = fs.readFileSync(path.join(__dirname,'/data/decoratorcommands/extract-test.cto'), 'utf-8');
testModelManager.addCTOModel(modelText, 'test.cto');
const options = {
removeDecoratorsFromModel:true,
locale:'en'
};
const resp = DecoratorManager.extractVocabDecorators( testModelManager, options);
const vocab = resp.vocabularies;
vocab.should.not.be.null;
sanketshevkar marked this conversation as resolved.
Show resolved Hide resolved
});
it('should be able to extract non-vocab decorators from a model', async function() {
const testModelManager = new ModelManager({strict:true,});
const modelText = fs.readFileSync(path.join(__dirname,'/data/decoratorcommands/extract-test.cto'), 'utf-8');
testModelManager.addCTOModel(modelText, 'test.cto');
const options = {
removeDecoratorsFromModel:true,
locale:'en'
};
const resp = DecoratorManager.extractNonVocabDecorators( testModelManager, options);
const dcs = resp.decoratorCommandSet;
dcs.should.not.be.null;
});
});

describe('#executePropertyCommand', () => {
Expand Down
Loading
Loading