2 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13 * or implied. See the License for the specific language governing
14 * permissions and limitations under the License.
17 import Common from '../common/Common';
18 import Metamodel from './Metamodel';
21 * A simple lookup for schemas by ID.
23 export default class Metamodels {
25 // ///////////////////////////////////////////////////////////////////////////////////////////////
28 * Construct metamodels from provided JSON definitions.
29 * @param metamodels JSON metamodel definitions.
31 constructor(metamodels) {
33 Common.assertType(metamodels, 'Array');
37 // Save each metamodel. It's up to the Metamodel class to make sense of
38 // potentially nonsense metamodel definitions.
40 for (const json of metamodels) {
41 const metamodel = new Metamodel(json);
42 this.lookup[metamodel.getId()] = metamodel;
45 // Set (or override) the default metamodel with the inlined one.
47 this.lookup.$ = Metamodel.getDefault();
48 Common.assertInstanceOf(this.lookup.$, Metamodel);
51 // ///////////////////////////////////////////////////////////////////////////////////////////////
54 * Get Metamodel by its @id.
55 * @param id identifier.
56 * @returns Metamodel, or undefined if no matching metamodel found.
59 return this.lookup[id];
62 // ///////////////////////////////////////////////////////////////////////////////////////////////
65 * Get the default (permissive) metamodel.
66 * @returns default Metamodel.
72 // ///////////////////////////////////////////////////////////////////////////////////////////////
75 * Get metamodel by its @id, falling back to the default.
76 * @param id identifier.
77 * @returns matching metamodel, or default.
79 getMetamodelOrDefault(id) {
80 const metamodel = this.getMetamodel(id);
84 return this.getDefault();