-
-
Notifications
You must be signed in to change notification settings - Fork 111
/
modelfile.js
885 lines (799 loc) · 30.6 KB
/
modelfile.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
/*
* 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 { MetaModelNamespace } = require('@accordproject/concerto-metamodel');
const packageJson = require('../../package.json');
const semver = require('semver');
const AssetDeclaration = require('./assetdeclaration');
const EnumDeclaration = require('./enumdeclaration');
const ClassDeclaration = require('./classdeclaration');
const ConceptDeclaration = require('./conceptdeclaration');
const ScalarDeclaration = require('./scalardeclaration');
const ParticipantDeclaration = require('./participantdeclaration');
const TransactionDeclaration = require('./transactiondeclaration');
const EventDeclaration = require('./eventdeclaration');
const IllegalModelException = require('./illegalmodelexception');
const MapDeclaration = require('./mapdeclaration');
const ModelUtil = require('../modelutil');
const Globalize = require('../globalize');
const Decorated = require('./decorated');
// Types needed for TypeScript generation.
/* eslint-disable no-unused-vars */
/* istanbul ignore next */
if (global === undefined) {
const ClassDeclaration = require('./classdeclaration');
const ModelManager = require('../modelmanager');
const Declaration = require('./declaration');
}
/* eslint-enable no-unused-vars */
/**
* Class representing a Model File. A Model File contains a single namespace
* and a set of model elements: assets, transactions etc.
*
* @class
* @memberof module:concerto-core
*/
class ModelFile extends Decorated {
/**
* Create a ModelFile. This should only be called by framework code.
* Use the ModelManager to manage ModelFiles.
* @param {ModelManager} modelManager - the ModelManager that manages this
* ModelFile
* @param {object} ast - The abstract syntax tree of the model as a JSON object.
* @param {string} [definitions] - The optional CTO model as a string.
* @param {string} [fileName] - The optional filename for this modelfile
* @throws {IllegalModelException}
*/
constructor(modelManager, ast, definitions, fileName) {
super(ast);
this.modelManager = modelManager;
this.external = false;
this.declarations = [];
this.localTypes = null;
this.imports = [];
this.importShortNames = new Map();
this.importWildcardNamespaces = [];
this.importUriMap = {};
this.fileName = 'UNKNOWN';
this.concertoVersion = null;
this.version = null;
if(!ast || typeof ast !== 'object') {
throw new Error('ModelFile expects a Concerto model AST as input.');
}
this.ast = ast;
if(definitions && typeof definitions !== 'string') {
throw new Error('ModelFile expects an (optional) Concerto model definition as a string.');
}
this.definitions = definitions;
if(fileName && typeof fileName !== 'string') {
throw new Error('ModelFile expects an (optional) filename as a string.');
}
this.fileName = fileName;
if(fileName) {
this.external = fileName.startsWith('@');
}
// Set up the decorators.
this.process();
// Populate from the AST
this.fromAst(this.ast);
// Check version compatibility
this.isCompatibleVersion();
// Now build local types from Declarations
this.localTypes = new Map();
for(let index in this.declarations) {
let classDeclaration = this.declarations[index];
let localType = this.getNamespace() + '.' + classDeclaration.getName();
this.localTypes.set(localType, this.declarations[index]);
}
}
/**
* Returns the ModelFile that defines this class.
*
* @protected
* @return {ModelFile} the owning ModelFile
*/
getModelFile() {
return this;
}
/**
* Returns true
* @returns {boolean} true
*/
isModelFile() {
return true;
}
/**
* Returns the semantic version
* @returns {string} the semantic version or null if the namespace for the model file is
* unversioned
*/
getVersion() {
return this.version;
}
/**
* Returns true if the ModelFile is a system namespace
* @returns {Boolean} true if this is a system model file
*/
isSystemModelFile() {
return this.namespace.startsWith('concerto@') || this.namespace === 'concerto';
}
/**
* Returns true if this ModelFile was downloaded from an external URI.
* @return {boolean} true iff this ModelFile was downloaded from an external URI
*/
isExternal() {
return this.external;
}
/**
* Returns the URI for an import, or null if the namespace was not associated with a URI.
* @param {string} namespace - the namespace for the import
* @return {string} the URI or null if the namespace was not associated with a URI.
* @private
*/
getImportURI(namespace) {
const result = this.importUriMap[namespace];
if(result) {
return result;
}
else {
return null;
}
}
/**
* Returns an object that maps from the import declarations to the URIs specified
* @return {Object} keys are import declarations, values are URIs
* @private
*/
getExternalImports() {
return this.importUriMap;
}
/**
* Visitor design pattern
* @param {Object} visitor - the visitor
* @param {Object} parameters - the parameter
* @return {Object} the result of visiting or null
*/
accept(visitor,parameters) {
return visitor.visit(this, parameters);
}
/**
* Returns the ModelManager associated with this ModelFile
*
* @return {ModelManager} The ModelManager for this ModelFile
*/
getModelManager() {
return this.modelManager;
}
/**
* Returns the types that have been imported into this ModelFile.
*
* @return {string[]} The array of fully-qualified names for types imported by
* this ModelFile
*/
getImports() {
let result = [];
this.imports.forEach( imp => {
result = result.concat(ModelUtil.importFullyQualifiedNames(imp));
});
return result;
}
/**
* Validates the ModelFile.
*
* @throws {IllegalModelException} if the model is invalid
* @protected
*/
validate() {
super.validate();
// A dictionary of imports to versions to track unique namespaces
const importsMap = new Map();
// Validate all of the imports to check that they reference
// namespaces or types that actually exist.
this.getImports().forEach((importFqn) => {
const importNamespace = ModelUtil.getNamespace(importFqn);
const importShortName = ModelUtil.getShortName(importFqn);
const modelFile = this.getModelManager().getModelFile(importNamespace);
const { name, version: importVersion } = ModelUtil.parseNamespace(importNamespace);
if (!modelFile) {
let formatter = Globalize.messageFormatter('modelmanager-gettype-noregisteredns');
throw new IllegalModelException(formatter({
type: importFqn
}), this);
}
const existingNamespaceVersion = importsMap.get(name);
// undefined means we haven't seen this namespace before,
// null means we have seen it before but it didn't have a version
const unseenNamespace = existingNamespaceVersion === undefined;
// This check is needed because we automatically add both versioned and unversioned versions of
// the root namespace for backwards compatibillity unless we're running in strict mode
const isGlobalModel = name === 'concerto';
const differentVersionsOfSameNamespace = !unseenNamespace && existingNamespaceVersion !== importVersion;
if (!isGlobalModel && differentVersionsOfSameNamespace){
let formatter = Globalize.messageFormatter('modelmanager-gettype-duplicatensimport');
throw new IllegalModelException(formatter({
namespace: importNamespace,
version1: existingNamespaceVersion,
version2: importVersion
}), this);
}
importsMap.set(name, importVersion);
if (importFqn.endsWith('*')) {
// This is a wildcard import, org.acme.*
// Doesn't matter if 0 or 100 types in the namespace.
return;
}
if (!modelFile.isLocalType(importShortName)) {
let formatter = Globalize.messageFormatter('modelmanager-gettype-notypeinns');
throw new IllegalModelException(formatter({
type: importShortName,
namespace: importNamespace
}), this);
}
});
// Validate all of the types in this model file.
// Check if names of the declarations are unique.
const uniqueNames = new Set();
this.declarations.forEach(
d => {
const fqn = d.getFullyQualifiedName();
if (!uniqueNames.has(fqn)) {
uniqueNames.add(fqn);
} else {
throw new IllegalModelException(
`Duplicate class name ${fqn}`
);
}
}
);
// Run validations on class declarations
for(let n=0; n < this.declarations.length; n++) {
let classDeclaration = this.declarations[n];
classDeclaration.validate();
}
}
/**
* Check that the type is valid.
* @param {string} context - error reporting context
* @param {string} type - a short type name
* @param {Object} [fileLocation] - location details of the error within the model file.
* @param {String} fileLocation.start.line - start line of the error location.
* @param {String} fileLocation.start.column - start column of the error location.
* @param {String} fileLocation.end.line - end line of the error location.
* @param {String} fileLocation.end.column - end column of the error location.
* @throws {IllegalModelException} - if the type is not defined
* @private
*/
resolveType(context, type, fileLocation) {
// is the type a primitive?
if(!ModelUtil.isPrimitiveType(type)) {
// is it an imported type?
if(!this.isImportedType(type)) {
// is the type declared locally?
if(!this.isLocalType(type)) {
let formatter = Globalize('en').messageFormatter('modelfile-resolvetype-undecltype');
throw new IllegalModelException(formatter({
'type': type,
'context': context,
}), this, fileLocation);
}
}
else {
// check whether type is defined in another file
this.getModelManager().resolveType(context,this.resolveImport(type));
}
}
}
/**
* Returns true if the type is defined in this namespace.
* @param {string} type - the short name of the type
* @return {boolean} - true if the type is defined in this ModelFile
* @private
*/
isLocalType(type) {
let result = (type && this.getLocalType(type) !== null);
return result;
}
/**
* Returns true if the type is imported from another namespace
* @param {string} type - the short name of the type
* @return {boolean} - true if the type is imported from another namespace
* @private
*/
isImportedType(type) {
if (this.importShortNames.has(type)) {
return true;
} else {
for(let index in this.importWildcardNamespaces) {
let wildcardNamespace = this.importWildcardNamespaces[index];
const modelFile = this.getModelManager().getModelFile(wildcardNamespace);
if (modelFile && modelFile.isLocalType(type)) {
return true;
}
}
return false;
}
}
/**
* Returns the FQN for a type that is imported from another namespace
* @param {string} type - the short name of the type
* @return {string} - the FQN of the resolved import
* @throws {Error} - if the type is not imported
* @private
*/
resolveImport(type) {
if (this.importShortNames.has(type)) {
return this.importShortNames.get(type);
} else {
for(let index in this.importWildcardNamespaces) {
let wildcardNamespace = this.importWildcardNamespaces[index];
const modelFile = this.getModelManager().getModelFile(wildcardNamespace);
if (modelFile && modelFile.isLocalType(type)) {
return wildcardNamespace + '.' + type;
}
}
}
let formatter = Globalize('en').messageFormatter('modelfile-resolveimport-failfindimp');
throw new IllegalModelException(formatter({
'type': type,
'imports': this.imports,
'namespace': this.getNamespace()
}),this);
}
/**
* Returns true if the type is defined in the model file
* @param {string} type the name of the type
* @return {boolean} true if the type (asset or transaction) is defined
*/
isDefined(type) {
return ModelUtil.isPrimitiveType(type) || this.getLocalType(type) !== null;
}
/**
* Returns the FQN of the type or null if the type could not be resolved.
* For primitive types the type name is returned.
* @param {string} type - a FQN or short type name
* @return {string | ClassDeclaration} the class declaration for the type or null.
* @private
*/
getType(type) {
// is the type a primitive?
if(!ModelUtil.isPrimitiveType(type)) {
// is it an imported type?
if(!this.isImportedType(type)) {
// is the type declared locally?
if(!this.isLocalType(type)) {
return null;
}
else {
return this.getLocalType(type);
}
}
else {
// check whether type is defined in another file
const fqn = this.resolveImport(type);
const modelFile = this.getModelManager().getModelFile(ModelUtil.getNamespace(fqn));
if (!modelFile) {
return null;
} else {
return modelFile.getLocalType(fqn);
}
}
}
else {
// for primitive types we just return the name
return type;
}
}
/**
* Returns the FQN of the type or null if the type could not be resolved.
* For primitive types the short type name is returned.
* @param {string} type - a FQN or short type name
* @return {string} the FQN type name or null
* @private
*/
getFullyQualifiedTypeName(type) {
// is the type a primitive?
if(!ModelUtil.isPrimitiveType(type)) {
// is it an imported type?
if(!this.isImportedType(type)) {
// is the type declared locally?
if(!this.isLocalType(type)) {
return null;
}
else {
return this.getLocalType(type).getFullyQualifiedName();
}
}
else {
return this.resolveImport(type);
}
}
else {
// for primitive types we just return the name
return type;
}
}
/**
* Returns the type with the specified name or null
* @param {string} type the short OR FQN name of the type
* @return {ClassDeclaration} the ClassDeclaration, or null if the type does not exist
*/
getLocalType(type) {
if(!this.localTypes) {
throw new Error('Internal error: local types are not yet initialized. Do not try to resolve types inside `process`.');
}
if(!type.startsWith(this.getNamespace())) {
type = this.getNamespace() + '.' + type;
}
if (this.localTypes.has(type)) {
return this.localTypes.get(type);
} else {
return null;
}
}
/**
* Get the AssetDeclarations defined in this ModelFile or null
* @param {string} name the name of the type
* @return {AssetDeclaration} the AssetDeclaration with the given short name
*/
getAssetDeclaration(name) {
let classDeclaration = this.getLocalType(name);
if(classDeclaration && classDeclaration.isAsset()) {
return classDeclaration;
}
return null;
}
/**
* Get the TransactionDeclaration defined in this ModelFile or null
* @param {string} name the name of the type
* @return {TransactionDeclaration} the TransactionDeclaration with the given short name
*/
getTransactionDeclaration(name) {
let classDeclaration = this.getLocalType(name);
if(classDeclaration && classDeclaration.isTransaction()) {
return classDeclaration;
}
return null;
}
/**
* Get the EventDeclaration defined in this ModelFile or null
* @param {string} name the name of the type
* @return {EventDeclaration} the EventDeclaration with the given short name
*/
getEventDeclaration(name) {
let classDeclaration = this.getLocalType(name);
if(classDeclaration && classDeclaration.isEvent()) {
return classDeclaration;
}
return null;
}
/**
* Get the ParticipantDeclaration defined in this ModelFile or null
* @param {string} name the name of the type
* @return {ParticipantDeclaration} the ParticipantDeclaration with the given short name
*/
getParticipantDeclaration(name) {
let classDeclaration = this.getLocalType(name);
if(classDeclaration && classDeclaration.isParticipant()) {
return classDeclaration;
}
return null;
}
/**
* Get the Namespace for this model file.
* @return {string} The Namespace for this model file
*/
getNamespace() {
return this.namespace;
}
/**
* Get the filename for this model file. Note that this may be null.
* @return {string} The filename for this model file
*/
getName() {
return this.fileName;
}
/**
* Get the AssetDeclarations defined in this ModelFile
* @return {AssetDeclaration[]} the AssetDeclarations defined in the model file
*/
getAssetDeclarations() {
return this.getDeclarations(AssetDeclaration);
}
/**
* Get the TransactionDeclarations defined in this ModelFile
* @return {TransactionDeclaration[]} the TransactionDeclarations defined in the model file
*/
getTransactionDeclarations() {
return this.getDeclarations(TransactionDeclaration);
}
/**
* Get the EventDeclarations defined in this ModelFile
* @return {EventDeclaration[]} the EventDeclarations defined in the model file
*/
getEventDeclarations() {
return this.getDeclarations(EventDeclaration);
}
/**
* Get the ParticipantDeclarations defined in this ModelFile
* @return {ParticipantDeclaration[]} the ParticipantDeclaration defined in the model file
*/
getParticipantDeclarations() {
return this.getDeclarations(ParticipantDeclaration);
}
/**
* Get the ClassDeclarations defined in this ModelFile
* @return {ClassDeclaration[]} the ClassDeclarations defined in the model file
*/
getClassDeclarations() {
return this.getDeclarations(ClassDeclaration);
}
/**
* Get the ConceptDeclarations defined in this ModelFile
* @return {ConceptDeclaration[]} the ParticipantDeclaration defined in the model file
*/
getConceptDeclarations() {
return this.getDeclarations(ConceptDeclaration);
}
/**
* Get the EnumDeclarations defined in this ModelFile
* @return {EnumDeclaration[]} the EnumDeclaration defined in the model file
*/
getEnumDeclarations() {
return this.getDeclarations(EnumDeclaration);
}
/**
* Get the MapDeclarations defined in this ModelFile
* @return {MapDeclaration[]} the MapDeclarations defined in the model file
*/
getMapDeclarations() {
return this.getDeclarations(MapDeclaration);
}
/**
* Get the ScalarDeclaration defined in this ModelFile
* @return {ScalarDeclaration[]} the ScalarDeclaration defined in the model file
*/
getScalarDeclarations() {
return this.getDeclarations(ScalarDeclaration);
}
/**
* Get the instances of a given type in this ModelFile
* @param {Function} type - the type of the declaration
* @return {Object[]} the ClassDeclaration defined in the model file
*/
getDeclarations(type) {
let result = [];
for(let n=0; n < this.declarations.length; n++) {
let declaration = this.declarations[n];
if(declaration instanceof type) {
result.push(declaration);
}
}
return result;
}
/**
* Get all declarations in this ModelFile
* @return {ClassDeclaration[]} the ClassDeclarations defined in the model file
*/
getAllDeclarations() {
return this.declarations;
}
/**
* Get the definitions for this model.
* @return {string} The definitions for this model.
*/
getDefinitions() {
return this.definitions;
}
/**
* Get the ast for this model.
* @return {object} The definitions for this model.
*/
getAst() {
return this.ast;
}
/**
* Get the expected concerto version
* @return {string} The semver range for compatible concerto versions
*/
getConcertoVersion() {
return this.concertoVersion;
}
/**
* Check whether this modelfile is compatible with the concerto version
*/
isCompatibleVersion() {
if (this.ast.concertoVersion) {
if (semver.satisfies(packageJson.version, this.ast.concertoVersion, { includePrerelease: true })) {
this.concertoVersion = this.ast.concertoVersion;
} else {
throw new Error(`ModelFile expects Concerto version ${this.ast.concertoVersion} but this is ${packageJson.version}`);
}
}
}
/**
* Verifies that an import is versioned if the strict
* option has been set on the Model Manager
* @param {*} imp - the import to validate
*/
enforceImportVersioning(imp) {
if(this.getModelManager().isStrict()) {
const nsInfo = ModelUtil.parseNamespace(imp.namespace);
if(!nsInfo.version) {
throw new Error(`Cannot use an unversioned import ${imp.namespace} when 'strict' option on Model Manager is set.`);
}
}
}
/**
* Populate from an AST
* @param {object} ast - the AST obtained from the parser
* @private
*/
fromAst(ast) {
const nsInfo = ModelUtil.parseNamespace(ast.namespace);
const namespaceParts = nsInfo.name.split('.');
namespaceParts.forEach(part => {
if (!ModelUtil.isValidIdentifier(part)){
throw new IllegalModelException(`Invalid namespace part '${part}'`, this.modelFile, this.ast.location);
}
});
this.namespace = ast.namespace;
this.version = nsInfo.version;
// Make sure to clone imports since we will add built-in imports
const imports = ast.imports ? ast.imports.concat([]) : [];
if(!this.isSystemModelFile()) {
imports.push(
{
$class: `${MetaModelNamespace}.ImportTypes`,
namespace: '[email protected]',
types: ['Concept', 'Asset', 'Transaction', 'Participant', 'Event']
}
);
}
this.imports = imports;
this.imports.forEach((imp) => {
this.enforceImportVersioning(imp);
switch(imp.$class) {
case `${MetaModelNamespace}.ImportAll`:
if (this.getModelManager().isStrict()){
throw new Error('Wilcard Imports are not permitted in strict mode.');
}
console.warn('DEPRECATED: Wilcard Imports are deprecated in this version of Concerto and will be removed in a future version.');
this.importWildcardNamespaces.push(imp.namespace);
break;
case `${MetaModelNamespace}.ImportTypes`:
imp.types.forEach( type => {
this.importShortNames.set(type, `${imp.namespace}.${type}`);
});
break;
default:
this.importShortNames.set(imp.name, ModelUtil.importFullyQualifiedNames(imp)[0]);
}
if(imp.uri) {
this.importUriMap[ModelUtil.importFullyQualifiedNames(imp)[0]] = imp.uri;
}
});
// declarations is an optional field
if (!ast.declarations) {
return;
}
for(let n=0; n < ast.declarations.length; n++) {
// Make sure to clone since we may add super type
let thing = Object.assign({}, ast.declarations[n]);
if(thing.$class === `${MetaModelNamespace}.AssetDeclaration`) {
// Default super type for asset
if (!thing.superType) {
thing.superType = {
$class: `${MetaModelNamespace}.TypeIdentified`,
name: 'Asset',
};
}
this.declarations.push( new AssetDeclaration(this, thing) );
}
else if(thing.$class === `${MetaModelNamespace}.TransactionDeclaration`) {
// Default super type for transaction
if (!thing.superType) {
thing.superType = {
$class: `${MetaModelNamespace}.TypeIdentified`,
name: 'Transaction',
};
}
this.declarations.push( new TransactionDeclaration(this, thing) );
}
else if(thing.$class === `${MetaModelNamespace}.EventDeclaration`) {
// Default super type for event
if (!thing.superType) {
thing.superType = {
$class: `${MetaModelNamespace}.TypeIdentified`,
name: 'Event',
};
}
this.declarations.push( new EventDeclaration(this, thing) );
}
else if(thing.$class === `${MetaModelNamespace}.ParticipantDeclaration`) {
// Default super type for participant
if (!thing.superType) {
thing.superType = {
$class: `${MetaModelNamespace}.TypeIdentified`,
name: 'Participant',
};
}
this.declarations.push( new ParticipantDeclaration(this, thing) );
}
else if(thing.$class === `${MetaModelNamespace}.EnumDeclaration`) {
this.declarations.push( new EnumDeclaration(this, thing) );
}
else if(thing.$class === `${MetaModelNamespace}.MapDeclaration`) {
this.declarations.push( new MapDeclaration(this, thing) );
}
else if(thing.$class === `${MetaModelNamespace}.ConceptDeclaration`) {
this.declarations.push( new ConceptDeclaration(this, thing) );
}
else if([
`${MetaModelNamespace}.BooleanScalar`,
`${MetaModelNamespace}.IntegerScalar`,
`${MetaModelNamespace}.LongScalar`,
`${MetaModelNamespace}.DoubleScalar`,
`${MetaModelNamespace}.StringScalar`,
`${MetaModelNamespace}.DateTimeScalar`,
].includes(thing.$class)) {
this.declarations.push( new ScalarDeclaration(this, thing) );
}
else {
let formatter = Globalize('en').messageFormatter('modelfile-constructor-unrecmodelelem');
throw new IllegalModelException(formatter({
'type': thing.$class,
}),this);
}
}
}
/**
* A function type definition for use as an argument to the filter function
* @callback FilterFunction
* @param {Declaration} declaration
* @returns {boolean} true, if the declaration satisfies the filter function
*/
/**
* Returns a new ModelFile with only the types for which the
* filter function returns true.
*
* Will return null if the filtered ModelFile doesn't contain any declarations.
*
* @param {FilterFunction} predicate - the filter function over a Declaration object
* @param {ModelManager} modelManager - the target ModelManager for the filtered ModelFile
* @param {string[]} removedDeclarations - an array that will be populated with the FQN of removed declarations
* @returns {ModelFile?} - the filtered ModelFile
* @private
*/
filter(predicate, modelManager, removedDeclarations){
let declarations = []; // ast for all included declarations
this.declarations?.forEach( declaration => {
const included = predicate(declaration);
if(!included) {
removedDeclarations.push(declaration.getFullyQualifiedName());
}
else {
declarations.push(declaration.ast);
}
} );
const ast = {
...this.ast,
declarations: declarations,
};
if (ast.declarations?.length > 0){
return new ModelFile(modelManager, ast, undefined, this.fileName);
}
return null;
}
}
module.exports = ModelFile;