Skip to content
This repository has been archived by the owner on Sep 7, 2023. It is now read-only.

Commit

Permalink
fix(core) Better handling for built-in Concerto models
Browse files Browse the repository at this point in the history
Signed-off-by: Jerome Simeon <[email protected]>
  • Loading branch information
jeromesimeon committed Apr 13, 2021
1 parent 4380d37 commit ca6c1b4
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 37 deletions.
6 changes: 6 additions & 0 deletions packages/ergo-compiler/lib/apmodelmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
const fsPath = require('path');

const ModelManager = require('@accordproject/concerto-core').ModelManager;
const Builtin = require('./builtin');

/**
* Accord Project Model Manager. Bootstraps the ModelManager with system files.
Expand All @@ -31,6 +32,11 @@ class APModelManager extends ModelManager {
*/
constructor() {
super();
this.addModelFile(Builtin.TimeModel, '@org.accordproject.time.cto');
this.addModelFile(Builtin.MoneyModel, '@org.accordproject.money.cto');
this.addModelFile(Builtin.ContractModel, '@org.accordproject.contract.cto');
this.addModelFile(Builtin.RuntimeModel, '@org.accordproject.runtime.cto');
this.validateModelFiles();
}

/**
Expand Down
3 changes: 0 additions & 3 deletions packages/ergo-compiler/lib/ergoloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ async function fromDirectory(path, options) {
}

const logicManager = new LogicManager('es6', options);
logicManager.addErgoBuiltin();

const ctoFiles = await FileLoader.loadFilesContents(path, /model[/\\].*\.cto$/);
ctoFiles.forEach((file) => {
Expand Down Expand Up @@ -78,7 +77,6 @@ async function fromZip(buffer, options) {

const zip = await JSZip.loadAsync(buffer);
const logicManager = new LogicManager('es6', options);
logicManager.addErgoBuiltin();

const ctoFiles = await FileLoader.loadZipFilesContents(zip, /model[/\\].*\.cto$/);
ctoFiles.forEach((file) => {
Expand Down Expand Up @@ -117,7 +115,6 @@ async function fromFiles(files, options) {
}

const logicManager = new LogicManager('es6', options);
logicManager.addErgoBuiltin();

let modelPaths = [];
let logicPaths = [];
Expand Down
30 changes: 14 additions & 16 deletions packages/ergo-compiler/lib/logicmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const APModelManager = require('../lib/apmodelmanager');
const Script = require('./script');
const ScriptManager = require('../lib/scriptmanager');
const ErgoCompiler = require('./compiler');
const Builtin = require('./builtin');
const boxedCollections = require('./boxedCollections');

/**
Expand All @@ -47,6 +46,7 @@ class LogicManager {
this.target = target;
this.contractName = null;
this.modelManager = new APModelManager();
this.builtInNamespaces = this.modelManager.getNamespaces();
this.scriptManager = new ScriptManager(this.target, this.modelManager, options);
this.introspector = new Introspector(this.modelManager);
this.factory = new Factory(this.modelManager);
Expand Down Expand Up @@ -206,12 +206,17 @@ unwrapError(__result);

/**
* Adds a model file (as a string) to the TemplateLogic.
* @param {string} modelFile - The model file as a string
* @param {string} modelFileContent - The model file content as a string
* @param {string} fileName - an optional file name to associate with the model file
*/
addModelFile(modelFile, fileName) {
addModelFile(modelFileContent, fileName) {
this.validated = false;
this.getModelManager().addModelFile(modelFile,slash(fileName),true);
const modelManager = this.getModelManager();
const name = slash(fileName);
const modelFile = new ModelFile(modelManager, modelFileContent, name);
if (!this.builtInNamespaces.includes(modelFile.getNamespace())) {
modelManager.addModelFile(modelFile,name,true);
}
}

/**
Expand All @@ -223,7 +228,11 @@ unwrapError(__result);
*/
addModelFiles(modelFiles, modelFileNames) {
this.validated = false;
this.getModelManager().addModelFiles(modelFiles, modelFileNames.map(name => slash(name)), true);
modelFiles.map((modelFileContent, index) => {
const modelFileName = slash(modelFileNames[index]);
this.addModelFile(modelFileContent, modelFileName);
});
// this.getModelManager().addModelFiles(modelFiles, modelFileNames.map(name => slash(name)), true);
}

/**
Expand Down Expand Up @@ -278,17 +287,6 @@ unwrapError(__result);
}
}

/**
* Add Ergo built-in models
*/
addErgoBuiltin() {
this.addModelFile(Builtin.TimeModel, '@org.accordproject.time.cto');
this.addModelFile(Builtin.MoneyModel, '@org.accordproject.money.cto');
this.addModelFile(Builtin.ContractModel, '@org.accordproject.contract.cto');
this.addModelFile(Builtin.RuntimeModel, '@org.accordproject.runtime.cto');
this.validateModelFiles();
}

/**
* Validate input JSON
* @param {object} input - the input JSON
Expand Down
74 changes: 58 additions & 16 deletions packages/ergo-compiler/test/logicmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,28 @@ describe('LogicManager', () => {
const logicManager = new LogicManager('es6');
logicManager.addModelFile(ctoSample,'test.cto');
const modelManager = logicManager.getModelManager();
modelManager.getModels().map(x => x.name).should.deep.equal(['test.cto']);
modelManager.getModels()[0].content.length.should.equal(175);
modelManager.getModels().map(x => x.name).should.deep.equal([
'@org.accordproject.time.cto',
'@org.accordproject.money.cto',
'@org.accordproject.contract.cto',
'@org.accordproject.runtime.cto',
'test.cto'
]);
modelManager.getModels()[4].content.length.should.equal(175);
});

it('should load a model to the model manager (bulk)', () => {
const logicManager = new LogicManager('es6');
logicManager.addModelFiles([ctoSample],['test.cto']);
const modelManager = logicManager.getModelManager();
modelManager.getModels().map(x => x.name).should.deep.equal(['test.cto']);
modelManager.getModels()[0].content.length.should.equal(175);
modelManager.getModels().map(x => x.name).should.deep.equal([
'@org.accordproject.time.cto',
'@org.accordproject.money.cto',
'@org.accordproject.contract.cto',
'@org.accordproject.runtime.cto',
'test.cto'
]);
modelManager.getModels()[4].content.length.should.equal(175);
});

it('should load a logic file to the script manager', () => {
Expand Down Expand Up @@ -194,7 +206,6 @@ describe('LogicManager', () => {

it('should load a logic file to the script manager (with Ergo builtin)', () => {
const logicManager = new LogicManager('es6');
logicManager.addErgoBuiltin();
logicManager.addLogicFile(ergoSample,'test3.ergo');
logicManager.compileLogicSync(false);
logicManager.getInvokeCall('helloworld').length.should.equal(231);
Expand Down Expand Up @@ -268,8 +279,14 @@ describe('LogicManager', () => {
const logicManager = new LogicManager('es6');
const modelManager = logicManager.getModelManager();
modelManager.addModelFile(ctoSample,null,true);
modelManager.getModels().map(x => x.name).should.deep.equal(['org.accordproject.copyrightlicense.cto']);
modelManager.getModels()[0].content.length.should.equal(175);
modelManager.getModels().map(x => x.name).should.deep.equal([
'@org.accordproject.time.cto',
'@org.accordproject.money.cto',
'@org.accordproject.contract.cto',
'@org.accordproject.runtime.cto',
'org.accordproject.copyrightlicense.cto'
]);
modelManager.getModels()[4].content.length.should.equal(175);
});
});

Expand Down Expand Up @@ -385,22 +402,47 @@ describe('LogicManager', () => {
it('should update a model to the model manager', () => {
const modelManager = logicManager.getModelManager();
logicManager.updateModel(ctoSample,'test.cto');
modelManager.getModels().map(x => x.name).should.deep.equal(['test.cto']);
modelManager.getModels()[0].content.length.should.equal(175);
modelManager.getModels().map(x => x.name).should.deep.equal([
'@org.accordproject.time.cto',
'@org.accordproject.money.cto',
'@org.accordproject.contract.cto',
'@org.accordproject.runtime.cto',
'test.cto'
]);
modelManager.getModels()[4].content.length.should.equal(175);
logicManager.updateModel(ctoSample3,'test.cto');
modelManager.getModels().map(x => x.name).should.deep.equal(['test.cto']);
modelManager.getModels()[0].content.length.should.equal(369);
modelManager.getModels().map(x => x.name).should.deep.equal([
'@org.accordproject.time.cto',
'@org.accordproject.money.cto',
'@org.accordproject.contract.cto',
'@org.accordproject.runtime.cto',
'test.cto'
]);
modelManager.getModels()[4].content.length.should.equal(369);
logicManager.updateModel(ctoSample2,'test.cto');
modelManager.getModels().map(x => x.name).should.deep.equal(['test.cto']);
modelManager.getModels()[0].content.length.should.equal(173);
modelManager.getModels().map(x => x.name).should.deep.equal([
'@org.accordproject.time.cto',
'@org.accordproject.money.cto',
'@org.accordproject.contract.cto',
'@org.accordproject.runtime.cto',
'test.cto'
]);
modelManager.getModels()[4].content.length.should.equal(173);
});

it('should add a model to the model manager', () => {
const modelManager = logicManager.getModelManager();
logicManager.updateModel(ctoSample2,'test2.cto');
modelManager.getModels().map(x => x.name).should.deep.equal(['test.cto','test2.cto']);
modelManager.getModels()[0].content.length.should.equal(175);
modelManager.getModels()[1].content.length.should.equal(173);
modelManager.getModels().map(x => x.name).should.deep.equal([
'@org.accordproject.time.cto',
'@org.accordproject.money.cto',
'@org.accordproject.contract.cto',
'@org.accordproject.runtime.cto',
'test.cto',
'test2.cto'
]);
modelManager.getModels()[4].content.length.should.equal(175);
modelManager.getModels()[5].content.length.should.equal(173);
});

it('should update a logic file in the script manager', () => {
Expand Down
1 change: 0 additions & 1 deletion packages/ergo-engine/test/commonengine.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ function runWorkload(Engine, target) {
beforeEach(async function () {
engine = new Engine();
logicManager = new LogicManager(target, null);
logicManager.addErgoBuiltin();
});

afterEach(() => {});
Expand Down
1 change: 0 additions & 1 deletion packages/ergo-test/lib/steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ Before(function () {
this.utcOffset = 0;
this.logicManager = new LogicManager('es6', null);
this.state = defaultState;
this.logicManager.addErgoBuiltin();
});

Given('the target platform {string}', function (target) {
Expand Down

0 comments on commit ca6c1b4

Please sign in to comment.