1 | /*
|
2 | * Licensed under the Apache License, Version 2.0 (the "License");
|
3 | * you may not use this file except in compliance with the License.
|
4 | * You may obtain a copy of the License at
|
5 | *
|
6 | * http://www.apache.org/licenses/LICENSE-2.0
|
7 | *
|
8 | * Unless required by applicable law or agreed to in writing, software
|
9 | * distributed under the License is distributed on an "AS IS" BASIS,
|
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
11 | * See the License for the specific language governing permissions and
|
12 | * limitations under the License.
|
13 | */
|
14 |
|
15 | ;
|
16 |
|
17 | const fs = require('fs');
|
18 |
|
19 | const DefaultModelFileLoader = require('./introspect/loaders/defaultmodelfileloader');
|
20 | const ModelFile = require('./introspect/modelfile');
|
21 | const ModelManager = require('./modelmanager');
|
22 |
|
23 | /**
|
24 | * Create a ModelManager from model files, with an optional system model.
|
25 | *
|
26 | * If a ctoFile is not provided, the Accord Project system model is used.
|
27 | *
|
28 | * @class
|
29 | * @memberof module:concerto-core
|
30 | */
|
31 | class ModelLoader {
|
32 | /**
|
33 | * Add model file
|
34 | *
|
35 | * @param {object} modelFileLoader - the model loader
|
36 | * @param {object} modelManager - the model manager
|
37 | * @param {string} ctoFile - the model file
|
38 | * @return {object} the model manager
|
39 | * @private
|
40 | */
|
41 | static async addModel(modelFileLoader, modelManager, ctoFile) {
|
42 | let modelFile = null;
|
43 | if (modelFileLoader.accepts(ctoFile)) {
|
44 | modelFile = await modelFileLoader.load(ctoFile);
|
45 | } else {
|
46 | const content = fs.readFileSync(ctoFile, 'utf8');
|
47 | modelFile = new ModelFile(modelManager, content, ctoFile);
|
48 | }
|
49 |
|
50 | modelManager.addModelFile(modelFile, modelFile.getName(), true);
|
51 |
|
52 | return modelManager;
|
53 | }
|
54 |
|
55 | /**
|
56 | * Load models in a new model manager
|
57 | *
|
58 | * @param {string[]} ctoFiles - the CTO files (can be local file paths or URLs)
|
59 | * @param {object} options - optional parameters
|
60 | * @param {boolean} [options.offline] - do not resolve external models
|
61 | * @param {number} [options.utcOffset] - UTC Offset for this execution
|
62 | * @return {object} the model manager
|
63 | */
|
64 | static async loadModelManager(ctoFiles, options = { offline: false }) {
|
65 | let modelManager = new ModelManager(options);
|
66 | const modelFileLoader = new DefaultModelFileLoader(modelManager);
|
67 |
|
68 | // Load user models
|
69 | for(let ctoFile of ctoFiles) {
|
70 | modelManager = await ModelLoader.addModel(modelFileLoader,modelManager,ctoFile);
|
71 | }
|
72 |
|
73 | // Validate the models, either offline or with external model resolution
|
74 | if(options && options.offline) {
|
75 | modelManager.validateModelFiles();
|
76 | return modelManager;
|
77 | } else {
|
78 | await modelManager.updateExternalModels();
|
79 | return modelManager;
|
80 | }
|
81 | }
|
82 |
|
83 | /**
|
84 | * Load system and models in a new model manager from model files objects
|
85 | *
|
86 | * @param {object[]} modelFiles - An array of Concerto files as strings or ModelFile objects.
|
87 | * @param {string[]} [fileNames] - An optional array of file names to associate with the model files
|
88 | * @param {object} options - optional parameters
|
89 | * @param {boolean} [options.offline] - do not resolve external models
|
90 | * @param {number} [options.utcOffset] - UTC Offset for this execution
|
91 | * @return {object} the model manager
|
92 | */
|
93 | static async loadModelManagerFromModelFiles(modelFiles, fileNames, options = { offline: false }) {
|
94 | let modelManager = new ModelManager(options);
|
95 |
|
96 | // Load system model
|
97 | modelManager.addModelFiles(modelFiles, fileNames, true);
|
98 |
|
99 | // Validate the models, either offline or with external model resolution
|
100 | if(options && options.offline) {
|
101 | modelManager.validateModelFiles();
|
102 | return modelManager;
|
103 | } else {
|
104 | await modelManager.updateExternalModels();
|
105 | return modelManager;
|
106 | }
|
107 | }
|
108 |
|
109 | }
|
110 |
|
111 | module.exports = ModelLoader;
|