This repository has been archived by the owner on Sep 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
apmodelmanager.js
138 lines (127 loc) · 5.08 KB
/
apmodelmanager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const slash = require('slash');
const fsPath = require('path');
const Parser = require('@accordproject/concerto-cto').Parser;
const ModelManager = require('@accordproject/concerto-core').ModelManager;
const ModelFile = require('@accordproject/concerto-core').ModelFile;
const Builtin = require('./builtin');
const processModelFiles = (modelFiles) => {
let models = [];
modelFiles.forEach(function (file) {
// ignore the system namespace when creating an archive
if (file.isSystemModelFile()) {
return;
}
let fileName = file.getName();
if (fileName === 'UNKNOWN' || !fileName) {
fileName = file.getNamespace() + '.cto';
} else {
fileName = fsPath.basename(fileName);
}
models.push({ 'name' : fileName, 'namespace': file.getNamespace(), 'content' : file.definitions });
});
return models;
};
/**
* Accord Project Model Manager. Bootstraps the ModelManager with system files.
* @class
* @public
* @abstract
* @memberof module:ergo-compiler
*/
class APModelManager extends ModelManager {
/**
*/
constructor() {
super();
this.addCTOModel(Builtin.TimeModel, '@[email protected]');
this.addCTOModel(Builtin.MoneyModel, '@[email protected]');
this.addCTOModel(Builtin.ContractModel, '@models.accordproject.org.accordproject.contract.cto');
this.addCTOModel(Builtin.RuntimeModel, '@models.accordproject.org.accordproject.runtime.cto');
this.addCTOModel(Builtin.OptionsModel, '@org.accordproject.ergo.options.cto');
this.validateModelFiles();
this.builtInNamespaces = this.getNamespaces();
}
/**
* Gets all the CTO models
* @return {Array<{name:string, content:string}>} the name and content of each CTO file
*/
getModels() {
return processModelFiles(this.getModelFiles());
}
/**
* Adds a model file (as a string) to the TemplateLogic.
* @param {string} modelFileContent - The model file content as a string
* @param {string} fileName - an optional file name to associate with the model file
*/
addAPModelFile(modelFileContent, fileName) {
const name = slash(fileName);
const ast = Parser.parse(modelFileContent, fileName);
const modelFile = new ModelFile(this, ast, modelFileContent, name);
if (!this.builtInNamespaces.includes(modelFile.getNamespace())) {
this.addModelFile(modelFile,modelFileContent,name,true);
}
}
/**
* Add a set of model files to the TemplateLogic
* @param {string[]} modelFiles - An array of Composer files as
* strings.
* @param {string[]} [modelFileNames] - An optional array of file names to
* associate with the model files
* @param {boolean} offline - do not update external models
*/
async addAPModelFiles(modelFiles, modelFileNames, offline) {
modelFiles.map((modelFileContent, index) => {
const modelFileName = modelFileNames[index];
this.addAPModelFile(modelFileContent, modelFileName);
});
if (offline) {
this.validateModelFiles();
return [];
} else {
const externalModelFiles = await this.updateExternalModels();
return processModelFiles(externalModelFiles);
}
}
/**
* Update of a given model
* @param {string} content - the model content
* @param {string} name - the model name
*/
updateModel(content, name) {
const currentModels = this.getModelFiles();
// Is this a new model?
if (!currentModels.some(x => x.getName() === name)) {
this.addCTOModel(content, name);
} else {
const previousModelFile =
(currentModels.filter(x => x.getName() === name))[0];
const previousContent = previousModelFile.getDefinitions();
if (content !== previousContent) {
const previousNamespace = previousModelFile.getNamespace();
const ast = Parser.parse(content, name);
const newNamespace = new ModelFile(this, ast, content, name).getNamespace();
if (previousNamespace === newNamespace) {
this.updateModelFile(content, name, true);
} else {
this.deleteModelFile(previousNamespace);
this.addCTOModel(content, name, true);
}
}
}
}
}
module.exports = APModelManager;