2 * Copyright © 2016-2017 European Support Limited
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 React from 'react';
18 import PropTypes from 'prop-types';
19 import Application from './components/application/Application';
20 import Common from './common/Common';
21 import Options from './common/Options';
22 import Model from './model/Model';
23 import Metamodel from './model/Metamodel';
24 import Metamodels from './model/Metamodels';
25 import Scenarios from './model/demo/scenarios/Scenarios';
26 import '../../../../res/sdc-sequencer.scss';
28 * ASDC Sequencer entry point.
30 class Sequencer extends React.Component {
31 // //////////////////////////////////////////////////////////////////////////////////////////////
33 constructor(props, context) {
34 super(props, context);
36 this.setMetamodel.bind(this);
37 this.setModel.bind(this);
38 this.getModel.bind(this);
39 this.getMetamodel.bind(this);
40 this.getSVG.bind(this);
41 this.getDemoScenarios.bind(this);
42 this.newModel.bind(this);
46 this.options = new Options(props.options);
50 const scenarios = this.getDemoScenarios();
51 this.setMetamodel(scenarios.getMetamodels());
53 // this.setModel(scenarios.getBlank());
54 this.setModel(scenarios.getDimensions());
55 // this.setModel(scenarios.getECOMP());
58 // //////////////////////////////////////////////////////////////////////////////////////////////
61 * Optionally save known metamodels so that subsequent loading and unloading
62 * of models needn't include the corresponding metamodel.
63 * @param metamodels array of conformant metamodel JSON definitions.
66 setMetamodel(metamodels) {
67 Common.assertType(metamodels, 'Array');
68 this.metamodels = new Metamodels(metamodels);
72 // //////////////////////////////////////////////////////////////////////////////////////////////
75 * Set current diagram.
76 * @param modelJSON JSON diagram spec.
77 * @param metamodelIdOrDefinition optional metamodel definition or reference. Defaults to
78 * the model's metadata @ref, or the default (permissive) metamodel.
81 setModel(modelJSON, metamodelIdOrDefinition) {
82 Common.assertType(modelJSON, 'Object');
83 const ref = modelJSON.metadata ? modelJSON.metadata.ref : undefined;
84 const metamodel = this.getMetamodel(metamodelIdOrDefinition || ref);
85 Common.assertInstanceOf(metamodel, Metamodel);
86 this.model = new Model(modelJSON, metamodel);
87 if (this.application) {
88 this.application.setModel(this.model);
93 // //////////////////////////////////////////////////////////////////////////////////////////////
96 * Get current diagram state. At any given instant the diagram might not make *sense*
97 * but it should always be syntactically valid.
98 * @return current Model.
101 if (this.application) {
102 const model = this.application.getModel();
104 return model.unwrap();
111 // //////////////////////////////////////////////////////////////////////////////////////////////
114 * Extract SVG element.
115 * @return stringified SVG element.
118 return this.application.getSVG();
121 // //////////////////////////////////////////////////////////////////////////////////////////////
124 * Get demo scenarios, allowing initialization in demo mode from the outside.
125 * @returns {Scenarios}
128 return new Scenarios();
131 // //////////////////////////////////////////////////////////////////////////////////////////////
135 * @param metamodelIdOrDefinition
136 * @return newly-created model.
138 newModel(metamodelIdOrDefinition) {
139 const metamodel = this.getMetamodel(metamodelIdOrDefinition);
140 Common.assertInstanceOf(metamodel, Metamodel);
141 const model = new Model({}, metamodel);
142 if (this.application) {
143 this.application.setModel(model);
148 // //////////////////////////////////////////////////////////////////////////////////////////////
151 * Get Metamodel instance corresponding to an ID or JSON definition.
152 * @param metamodelIdOrDefinition String ID or JSON definition.
153 * @returns Metamodel instance.
156 getMetamodel(metamodelIdOrDefinition) {
157 const metamodelType = Common.getType(metamodelIdOrDefinition);
158 if (metamodelType === 'Object') {
159 return new Metamodel(metamodelIdOrDefinition);
161 return this.metamodels.getMetamodelOrDefault(metamodelIdOrDefinition);
164 // //////////////////////////////////////////////////////////////////////////////////////////////
167 * Render current diagram state.
170 if (this.props.model) {
171 // If a model was specified as a property, apply it. Otherwise
172 // fall back to the demo model.
174 const scenarios = this.getDemoScenarios();
176 scenarios.getBlankMetamodel(),
177 scenarios.getECOMPMetamodel()
179 if (this.props.metamodel) {
180 metamodel.push(this.props.metamodel);
182 this.setMetamodel(metamodel);
183 this.setModel(this.props.model);
188 options={this.options}
191 this.application = a;
198 Sequencer.propTypes = {
199 options: PropTypes.object.isRequired,
200 model: PropTypes.object,
201 metamodel: PropTypes.object
204 export default Sequencer;